示例#1
0
    def test_historical_bar_market_order(self):
        listeners = AsyncListeners()

        with IQFeedHistoryProvider() as provider:
            f = BarsFilter(ticker=["GOOG", "AAPL", "IBM"], interval_len=60, interval_type='s', max_bars=20)
            data = provider.request_data(f, sync_timestamps=False).swaplevel(0, 1).sort_index()
            dre = DataReplayEvents(listeners=listeners,
                                   data_replay=DataReplay().add_source([data], 'data', historical_depth=5),
                                   event_name='bar')

            order_request_events = SyncListeners()

            me = MockExchange(listeners=listeners,
                              order_requests_event_stream=order_request_events,
                              bar_event_stream=dre.event_filter_by_source('data'),
                              order_processor=StaticSlippageLoss(0.1),
                              commission_loss=PerShareCommissionLoss(0.1))

            fulfilled_orders = me.fulfilled_orders_stream()

            e1 = threading.Event()
            fulfilled_orders += lambda x: e1.set() if x.symbol == 'GOOG' else None

            e2 = threading.Event()
            fulfilled_orders += lambda x: e2.set() if x.symbol == 'AAPL' else None

            e3 = threading.Event()
            fulfilled_orders += lambda x: e3.set() if x.symbol == 'IBM' else None

            o1 = MarketOrder(Type.BUY, 'GOOG', 1)
            order_request_events(o1)

            o2 = MarketOrder(Type.BUY, 'AAPL', 3)
            order_request_events(o2)

            o3 = MarketOrder(Type.SELL, 'IBM', 1)
            order_request_events(o3)

            dre.start()

            e1.wait()
            e2.wait()
            e3.wait()

        self.assertEqual(o1.obtained_quantity, 1)
        self.assertGreater(o1.cost, 0)
        self.assertIsNotNone(o1.fulfill_time)

        self.assertEqual(o2.obtained_quantity, 3)
        self.assertGreater(o2.cost, 0)
        self.assertIsNotNone(o2.fulfill_time)

        self.assertEqual(o3.obtained_quantity, 1)
        self.assertGreater(o3.cost, 0)
        self.assertIsNotNone(o3.fulfill_time)
示例#2
0
    def test_bar_market_order(self):
        listeners = AsyncListeners()

        with IQFeedBarDataListener(interval_len=300, mkt_snapshot_depth=10, listeners=listeners) as bars:
            order_request_events = SyncListeners()

            me = MockExchange(listeners=listeners,
                              order_requests_event_stream=order_request_events,
                              bar_event_stream=bars.all_full_bars_event_stream(),
                              order_processor=StaticSlippageLoss(0.1),
                              commission_loss=PerShareCommissionLoss(0.1))

            fulfilled_orders = me.fulfilled_orders_stream()

            e1 = threading.Event()
            fulfilled_orders += lambda x: e1.set() if x.symbol == 'GOOG' else None

            e2 = threading.Event()
            fulfilled_orders += lambda x: e2.set() if x.symbol == 'AAPL' else None

            e3 = threading.Event()
            fulfilled_orders += lambda x: e3.set() if x.symbol == 'IBM' else None

            bars.watch_bars('GOOG')
            bars.watch_bars('AAPL')
            bars.watch_bars('IBM')

            o1 = MarketOrder(Type.BUY, 'GOOG', 1)
            order_request_events(o1)

            o2 = MarketOrder(Type.BUY, 'AAPL', 3)
            order_request_events(o2)

            o3 = MarketOrder(Type.SELL, 'IBM', 1)
            order_request_events(o3)

            e1.wait()
            e2.wait()
            e3.wait()

        self.assertEqual(o1.obtained_quantity, 1)
        self.assertGreater(o1.cost, 0)
        self.assertIsNotNone(o1.fulfill_time)

        self.assertEqual(o2.obtained_quantity, 3)
        self.assertGreater(o2.cost, 0)
        self.assertIsNotNone(o2.fulfill_time)

        self.assertEqual(o3.obtained_quantity, 1)
        self.assertGreater(o3.cost, 0)
        self.assertIsNotNone(o3.fulfill_time)
示例#3
0
    def test_stop_limit_order(self):
        listeners = AsyncListeners()

        with IQFeedLevel1Listener(listeners=listeners) as level_1:
            order_request_events = SyncListeners()

            me = MockExchange(listeners=listeners,
                              order_requests_event_stream=order_request_events,
                              tick_event_stream=level_1.all_level_1_filter(),
                              order_processor=StaticSlippageLoss(0.1),
                              commission_loss=PerShareCommissionLoss(0.1))

            fulfilled_orders = me.fulfilled_orders_stream()

            e1 = threading.Event()
            fulfilled_orders += lambda x: e1.set() if x.symbol == 'GOOG' else None

            e2 = threading.Event()
            fulfilled_orders += lambda x: e2.set() if x.symbol == 'AAPL' else None

            e3 = threading.Event()
            fulfilled_orders += lambda x: e3.set() if x.symbol == 'IBM' else None

            level_1.watch('GOOG')
            level_1.watch('AAPL')
            level_1.watch('IBM')

            o1 = StopLimitOrder(Type.BUY, 'GOOG', 1, 99999, 1)
            order_request_events(o1)

            o2 = StopLimitOrder(Type.BUY, 'AAPL', 3, 99999, 1)
            order_request_events(o2)

            o3 = StopLimitOrder(Type.SELL, 'IBM', 1, 1, 99999)
            order_request_events(o3)

            e1.wait()
            e2.wait()
            e3.wait()

        self.assertEqual(o1.obtained_quantity, 1)
        self.assertGreater(o1.cost, 0)
        self.assertIsNotNone(o1.fulfill_time)

        self.assertEqual(o2.obtained_quantity, 3)
        self.assertGreater(o2.cost, 0)
        self.assertIsNotNone(o2.fulfill_time)

        self.assertEqual(o3.obtained_quantity, 1)
        self.assertGreater(o3.cost, 0)
        self.assertIsNotNone(o3.fulfill_time)
示例#4
0
def add_mock_exchange(listeners,
                      order_requests_stream,
                      bar_event_stream,
                      slippage_loss_ratio: float = None,
                      commission_per_share: float = None):
    """
    Append mock exchange
    :param listeners: listeners environment
    :param order_requests_stream: event stream for order requests
    :param bar_event_stream: event stream for bar data
    :param slippage_loss_ratio: ratio for slippage loss
    :param commission_per_share: broker tax per share
    """

    return MockExchange(
        listeners=listeners,
        order_requests_event_stream=order_requests_stream,
        bar_event_stream=bar_event_stream,
        order_processor=StaticSlippageLoss(slippage_loss_ratio)
        if slippage_loss_ratio is not None else None,
        commission_loss=PerShareCommissionLoss(commission_per_share)
        if commission_per_share is not None else None)
示例#5
0
    def test_historical_bar_mock_orders(self):
        with IQFeedHistoryProvider() as provider:
            listeners = AsyncListeners()

            f = BarsFilter(ticker=["GOOG", "AAPL", "IBM"],
                           interval_len=60,
                           interval_type='s',
                           max_bars=5)
            data = provider.request_data(f, sync_timestamps=False).swaplevel(
                0, 1).sort_index()
            dre = DataReplayEvents(listeners=listeners,
                                   data_replay=DataReplay().add_source(
                                       [data], 'data', historical_depth=5),
                                   event_name='bar')

            bars = dre.event_filter_by_source('data')

            order_request_events = SyncListeners()

            me = MockExchange(listeners=listeners,
                              order_requests_event_stream=order_request_events,
                              bar_event_stream=bars,
                              order_processor=StaticSlippageLoss(0.1),
                              commission_loss=PerShareCommissionLoss(0.1))

            pm = PortfolioManager(
                listeners=listeners,
                initial_capital=10000,
                bar_event_stream=bars,
                fulfilled_orders_event_stream=me.fulfilled_orders_stream())

            portfolio_updates = pm.portfolio_updates_stream()

            o1 = StopLimitOrder(Type.BUY, 'GOOG', 1, 99999, 1)
            o2 = StopLimitOrder(Type.BUY, 'AAPL', 3, 99999, 1)
            o3 = StopLimitOrder(Type.BUY, 'IBM', 1, 99999, 1)
            o4 = StopLimitOrder(Type.SELL, 'AAPL', 1, 1, 99999)

            order_request_events(o1)
            order_request_events(o2)
            order_request_events(o3)
            order_request_events(o4)

            e1 = threading.Event()
            portfolio_updates += lambda x: e1.set(
            ) if 'GOOG' in x.symbols else None

            e2 = threading.Event()
            portfolio_updates += lambda x: e2.set(
            ) if 'AAPL' in x.symbols else None

            e3 = threading.Event()
            portfolio_updates += lambda x: e3.set(
            ) if 'IBM' in x.symbols else None

            dre.start()

            e1.wait()
            e2.wait()
            e3.wait()

        self.assertLess(pm.capital, pm.initial_capital)
        self.assertTrue('GOOG' in pm.symbols)
        self.assertTrue('AAPL' in pm.symbols)
        self.assertTrue('IBM' in pm.symbols)

        self.assertEqual(pm.quantity('GOOG'), 1)
        self.assertEqual(pm.quantity('AAPL'), 2)
        self.assertEqual(pm.quantity('IBM'), 1)

        self.assertGreater(pm.value('GOOG'), 0)
        self.assertGreater(pm.value('AAPL'), 0)
        self.assertGreater(pm.value('IBM'), 0)
示例#6
0
    def test_mock_orders(self):
        listeners = SyncListeners()

        with IQFeedLevel1Listener(listeners=listeners) as level1:
            order_request_events = SyncListeners()

            me = MockExchange(listeners=listeners,
                              order_requests_event_stream=order_request_events,
                              tick_event_stream=level1.all_level_1_filter(),
                              order_processor=StaticSlippageLoss(0.1),
                              commission_loss=PerShareCommissionLoss(0.1))

            pm = PortfolioManager(
                listeners=listeners,
                initial_capital=10000,
                tick_event_stream=level1.all_level_1_filter(),
                fulfilled_orders_event_stream=me.fulfilled_orders_stream())

            e1 = threading.Event()

            portfolio_updates = pm.portfolio_updates_stream()
            portfolio_updates += lambda x: e1.set(
            ) if 'GOOG' in x.symbols else None

            e2 = threading.Event()
            portfolio_updates += lambda x: e2.set(
            ) if 'AAPL' in x.symbols else None

            e3 = threading.Event()
            portfolio_updates += lambda x: e3.set(
            ) if 'IBM' in x.symbols else None

            o1 = StopLimitOrder(Type.BUY, 'GOOG', 1, 99999, 1)
            order_request_events(o1)

            o2 = StopLimitOrder(Type.BUY, 'AAPL', 3, 99999, 1)
            order_request_events(o2)

            o3 = StopLimitOrder(Type.BUY, 'IBM', 1, 99999, 1)
            order_request_events(o3)

            o4 = StopLimitOrder(Type.SELL, 'AAPL', 1, 1, 99999)
            order_request_events(o4)

            level1.watch('GOOG')
            level1.watch('AAPL')
            level1.watch('IBM')

            e1.wait()
            e2.wait()
            e3.wait()

        self.assertLess(pm.capital, pm.initial_capital)
        self.assertTrue('GOOG' in pm.symbols)
        self.assertTrue('AAPL' in pm.symbols)
        self.assertTrue('IBM' in pm.symbols)

        self.assertEqual(pm.quantity('GOOG'), 1)
        self.assertEqual(pm.quantity('AAPL'), 2)
        self.assertEqual(pm.quantity('IBM'), 1)

        self.assertGreater(pm.value('GOOG'), 0)
        self.assertGreater(pm.value('AAPL'), 0)
        self.assertGreater(pm.value('IBM'), 0)