Ejemplo n.º 1
0
    def test_time_bar_from_ask(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id="1", input=self.input,
                            input_type=BarAggregationRequest.Ask)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", ask=30, ask_size=100)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", ask=70, ask_size=300)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(1, agg.count())
        self.assertEqual(
            ModelFactory.build_bar(inst_id="1", begin_time=9000000000, timestamp=9000059999, type=Bar.Time, size=60, open=30, high=30, low=30,
                close=30, vol=100, adj_close=0), items[0])

        self.event_bus.reset()

        self.time += 49999
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", ask=20, ask_size=100)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            ModelFactory.build_bar(inst_id="1", begin_time=9000060000, timestamp=9000119999, type=Bar.Time, size=60, open=70, high=70, low=20,
                close=20, vol=400, adj_close=0), items[0])
Ejemplo n.º 2
0
    def test_stop_order_handler(self):
        handler = StopOrderHandler(self.config)

        bar1 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=16, high=18, low=15, close=17, vol=1000)

        # BUY
        order1 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="1", inst_id="1", action=Buy,
                                                      type=Stop,
                                                      qty=1000, stop_price=18.5)
        fill_info = handler.process_w_price_qty(order1, 18, 1000)
        self.assertFalse(handler.stop_limit_ready(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order1, 19, 1000)
        self.assertTrue(handler.stop_limit_ready(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(19, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # BUY with bar
        order2 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="2", inst_id="1", action=Buy,
                                                      type=Stop,
                                                      qty=1000, stop_price=18.5)
        fill_info = handler.process(order2, bar2)
        self.assertFalse(handler.stop_limit_ready(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order2, bar1)
        self.assertTrue(handler.stop_limit_ready(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(18.5, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # SELL
        order3 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="3", inst_id="1",
                                                      action=Sell, type=Stop,
                                                      qty=1000, stop_price=18.5)
        fill_info = handler.process_w_price_qty(order3, 19, 1000)
        self.assertFalse(handler.stop_limit_ready(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order3, 18, 1000)
        self.assertTrue(handler.stop_limit_ready(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(18, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # SELL with bar
        order4 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="4", inst_id="1",
                                                      action=Sell, type=Stop,
                                                      qty=1000, stop_price=18.5)
        fill_info = handler.process(order4, bar1)
        self.assertFalse(handler.stop_limit_ready(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order4, bar2)
        self.assertTrue(handler.stop_limit_ready(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(18.5, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)
Ejemplo n.º 3
0
    def test_on_limit_order_fill_with_new_data(self):
        orders = self.simulator._get_orders()
        self.assertEqual(0, len(orders))

        nos = ModelFactory.build_new_order_request(timestamp=0,
                                                   cl_id='TestClient',
                                                   cl_ord_id="TestClientOrder",
                                                   portf_id="TestPortf",
                                                   broker_id="TestBroker",
                                                   inst_id="HSI@SEHK",
                                                   action=Buy,
                                                   type=Limit,
                                                   qty=1000,
                                                   limit_price=18.5)
        self.simulator.on_new_ord_req(nos)
        cl_ord_id = ModelFactory.build_cl_ord_id(nos.cl_id, nos.cl_ord_id)
        orders = self.simulator._get_orders()
        self.assertEqual(1, len(orders))
        self.assertEqual(1, len(orders[nos.inst_id]))
        self.assertIsNotNone(orders[nos.inst_id][cl_ord_id])
        self.assertEqual(1, len(self.exec_handler.exec_reports))

        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, nos.cl_id, nos.cl_ord_id, 0, 0,
                                Submitted)

        bar1 = ModelFactory.build_bar(timestamp=0,
                                      inst_id="HSI@SEHK",
                                      open=20,
                                      high=21,
                                      low=19,
                                      close=20.5,
                                      vol=1000)
        bar2 = ModelFactory.build_bar(timestamp=1,
                                      inst_id="HSI@SEHK",
                                      open=16,
                                      high=18,
                                      low=15,
                                      close=17,
                                      vol=1000)

        self.exec_handler.reset()
        self.simulator.on_bar(bar1)
        self.assertEqual(0, len(self.exec_handler.exec_reports))

        self.exec_handler.reset()
        self.simulator.on_bar(bar2)
        self.assertEqual(1, len(self.exec_handler.exec_reports))
        exec_report = self.exec_handler.exec_reports[0]
        self.assert_exec_report(exec_report, nos.cl_id, nos.cl_ord_id, 1000,
                                18.5, Filled)
Ejemplo n.º 4
0
    def test_save_and_load(self):
        inputs = []
        for x in range(0, 10):
            data = sorted([random.randint(0, 100) for i in range(0, 4)])
            bar = ModelFactory.build_bar(timestamp=x,
                                         inst_id="3",
                                         open=data[1],
                                         high=data[3],
                                         low=data[0],
                                         close=data[2],
                                         vol=random.randint(100, 1000))
            inputs.append(bar)
            self.db.save_bar(bar)

        self.db.stop()

        self.db = InMemoryDataStore()
        self.db.start(self.app_context)

        bars = self.db.load_all('bars')
        bars = sorted(bars, key=lambda x: x.timestamp, reverse=False)
        self.assertEquals(10, len(bars))

        for x in range(0, 10):
            self.assertEquals(inputs[x], bars[x])
Ejemplo n.º 5
0
    def test_tick_bar_from_trade(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id="1", input=self.input,
                            output_bar_type=Bar.Tick, output_size=3)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 60000
        t = ModelFactory.build_trade(timestamp=self.time, inst_id="1", price=20, size=200)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = ModelFactory.build_trade(timestamp=self.time, inst_id="1", price=80, size=100)
        self.update(self.input, t)
        self.assertEqual(2, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = ModelFactory.build_trade(timestamp=self.time, inst_id="1", price=10, size=200)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            ModelFactory.build_bar(inst_id="1", begin_time=9000060000, timestamp=9000180000, type=Bar.Tick, size=3, open=20, high=80,
                low=10,
                close=10, vol=500, adj_close=0), items[0])
Ejemplo n.º 6
0
    def test_limit_order_handler(self):
        handler = LimitOrderHandler(self.config)

        bar1 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=16, high=18, low=15, close=17, vol=1000)

        # BUY
        order = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="1", inst_id="1", action=Buy,
                                                     type=Limit,
                                                     qty=1000, limit_price=18.5)
        fill_info = handler.process_w_price_qty(order, 20, 1000)
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order, 18, 1000)
        self.assertEquals(18.5, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # BUY with bar
        fill_info = handler.process(order, bar1)
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order, bar2)
        self.assertEquals(18.5, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # SELL
        order2 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="2", inst_id="1",
                                                      action=Sell, type=Limit,
                                                      qty=1000,
                                                      limit_price=18.5)
        fill_info = handler.process_w_price_qty(order2, 18, 1000)
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order2, 20, 1000)
        self.assertEquals(18.5, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # SELL with bar
        fill_info = handler.process(order2, bar2)
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order2, bar1)
        self.assertEquals(18.5, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)
Ejemplo n.º 7
0
 def _build_bar(self, row, timestamp) -> Bar:
     return ModelFactory.build_bar(
         inst_id=row['InstId'],
         type=Bar.Time,
         provider_id=row['ProviderId'],
         timestamp=timestamp,
         open=row['Open'],
         high=row['High'],
         low=row['Low'],
         close=row['Close'],
         vol=row['Volume'],
         adj_close=row['Adj Close'] if 'Adj Close' in row else None,
         size=row['BarSize'])
    def test_bar_processor(self):
        config = SimConfig()
        processor = BarProcessor()

        order = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="1", inst_id="1", action=Buy,
                                                     type=Limit,
                                                     qty=1000, limit_price=18.5)
        bar = ModelFactory.build_bar(timestamp=0, inst_id="1", open=18, high=19, low=17, close=17.5, vol=1000)

        self.assertEqual(17.5, processor.get_price(order, bar, config))
        self.assertEqual(1000, processor.get_qty(order, bar, config))

        config2 = SimConfig(fill_on_bar_mode=SimConfig.FillMode.NEXT_OPEN)
        self.assertEqual(18, processor.get_price(order, bar, config2))
        self.assertEqual(1000, processor.get_qty(order, bar, config2))
Ejemplo n.º 9
0
    def test_time_bar_from_mid(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id="1", input=self.input,
                            input_type=BarAggregationRequest.Middle)
        agg.start(None)
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 59999
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", ask=30, ask_size=100, bid=18, bid_size=200)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            ModelFactory.build_bar(inst_id="1", begin_time=9000000000, timestamp=9000059999, type=Bar.Time, size=60, open=24, high=24, low=24,
                close=24, vol=150, adj_close=0), items[0])
Ejemplo n.º 10
0
    def sample_bar(self):
        bar = ModelFactory.build_bar("HSI@SEHK",
                                     Bar.Time,
                                     86400,
                                     provider_id='IB',
                                     timestamp=12312,
                                     utc_time=12312,
                                     begin_time=12300,
                                     open=123,
                                     high=300,
                                     low=30,
                                     close=156,
                                     vol=500,
                                     adj_close=324,
                                     open_interest=123)

        return bar
Ejemplo n.º 11
0
    def test_time_bar_from_trade(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id="1", input=self.input)
        agg.start(None)
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = ModelFactory.build_trade(timestamp=self.time, inst_id="1", price=20, size=200)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        # expect get a aggregated bar at 9000059999
        self.time += 49999
        t = ModelFactory.build_trade(timestamp=self.time, inst_id="1", price=10, size=200)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            ModelFactory.build_bar(inst_id="1", begin_time=9000000000, timestamp=9000059999, type=Bar.Time, size=60, open=20, high=20, low=10,
                close=10, vol=400), items[0])
Ejemplo n.º 12
0
    def test_time_bar_from_bid(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id="1", input=self.input,
                            input_type=BarAggregationRequest.Bid)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", bid=30, bid_size=100)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 10000
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", bid=10, bid_size=200)
        self.update(self.input, t)
        self.assertEqual(2, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 10000
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", bid=70, bid_size=300)
        self.update(self.input, t)
        self.assertEqual(3, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 29998
        t = ModelFactory.build_quote(timestamp=self.time, inst_id="1", bid=50, bid_size=400)
        self.update(self.input, t)
        self.assertEqual(4, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 3
        self.simluation_clock.update_time(self.time)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            ModelFactory.build_bar(inst_id="1", begin_time=9000000000, timestamp=9000059999, type=Bar.Time, size=60, open=30, high=70, low=10,
                close=50, vol=1000, adj_close=0), items[0])
Ejemplo n.º 13
0
 def test_simulation_clock_current_date_time_with_bar(self):
     timestamp = ClockTest.ts + 10
     bar = ModelFactory.build_bar(inst_id="test", timestamp=timestamp)
     self.simluation_clock.on_bar(bar)
     self.assertEquals(timestamp, self.simluation_clock.now())
Ejemplo n.º 14
0
    def test_trailing_stop_order_handler(self):
        handler = TrailingStopOrderHandler(self.config)

        bar1 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=16, high=18, low=15, close=17, vol=1000)
        bar2 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=17, high=19, low=16, close=18, vol=1000)
        bar3 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=18, high=20, low=17, close=19, vol=1000)
        bar4 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=19, high=21, low=18, close=20, vol=1000)
        bar5 = ModelFactory.build_bar(timestamp=0, inst_id="1", open=20, high=22, low=19, close=21, vol=1000)

        # BUY with bar
        order1 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="1", inst_id="1", action=Buy,
                                                      type=TrailingStop,
                                                      qty=1000,
                                                      stop_price=5)

        self.assertEquals(0, handler.trailing_stop_exec_price(order1.cl_id, order1.cl_ord_id))

        fill_info = handler.process(order1, bar2)
        self.assertEquals(21, handler.trailing_stop_exec_price(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order1, bar3)
        self.assertEquals(21, handler.trailing_stop_exec_price(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order1, bar1)
        self.assertEquals(20, handler.trailing_stop_exec_price(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order1, bar3)
        self.assertEquals(20, handler.trailing_stop_exec_price(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(20, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # BUY
        order2 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="2", inst_id="1", action=Buy,
                                                      type=TrailingStop,
                                                      qty=1000,
                                                      stop_price=5)

        self.assertEquals(0, handler.trailing_stop_exec_price(order2.cl_id, order2.cl_ord_id))

        fill_info = handler.process_w_price_qty(order2, 16, 1000)
        self.assertEquals(21, handler.trailing_stop_exec_price(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order2, 17, 1000)
        self.assertEquals(21, handler.trailing_stop_exec_price(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order2, 15, 1000)
        self.assertEquals(20, handler.trailing_stop_exec_price(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order2, 19, 1000)
        self.assertEquals(20, handler.trailing_stop_exec_price(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order2, 20, 1000)
        self.assertEquals(20, handler.trailing_stop_exec_price(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(20, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # SELL with bar
        order3 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="3", inst_id="1",
                                                      action=Sell,
                                                      type=TrailingStop, qty=1000,
                                                      stop_price=5)

        self.assertEquals(0, handler.trailing_stop_exec_price(order3.cl_id, order3.cl_ord_id))

        fill_info = handler.process(order3, bar4)
        self.assertEquals(16, handler.trailing_stop_exec_price(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order3, bar3)
        self.assertEquals(16, handler.trailing_stop_exec_price(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order3, bar5)
        self.assertEquals(17, handler.trailing_stop_exec_price(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process(order3, bar3)
        self.assertEquals(17, handler.trailing_stop_exec_price(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(17, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)

        # SELL
        order4 = ModelFactory.build_new_order_request(timestamp=0, cl_id='test', cl_ord_id="4", inst_id="1",
                                                      action=Sell,
                                                      type=TrailingStop, qty=1000,
                                                      stop_price=5)

        self.assertEquals(0, handler.trailing_stop_exec_price(order4.cl_id, order4.cl_ord_id))

        fill_info = handler.process_w_price_qty(order4, 21, 1000)
        self.assertEquals(16, handler.trailing_stop_exec_price(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order4, 20, 1000)
        self.assertEquals(16, handler.trailing_stop_exec_price(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order4, 22, 1000)
        self.assertEquals(17, handler.trailing_stop_exec_price(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order4, 18, 1000)
        self.assertEquals(17, handler.trailing_stop_exec_price(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(None, fill_info)

        fill_info = handler.process_w_price_qty(order4, 17, 1000)
        self.assertEquals(17, handler.trailing_stop_exec_price(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(17, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)