예제 #1
0
    def test_StopLimit_order_handler(self):
        handler = StopLimitOrderHandler(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=StopLimit,
                                                      qty=1000,
                                                      limit_price=18,
                                                      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(None, fill_info)

        fill_info = handler.process_w_price_qty(order1, 18, 1000)
        self.assertTrue(handler.stop_limit_ready(order1.cl_id, order1.cl_ord_id))
        self.assertEquals(18, 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=StopLimit,
                                                      qty=1000,
                                                      limit_price=18,
                                                      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(None, fill_info)

        fill_info = handler.process(order2, bar2)
        self.assertTrue(handler.stop_limit_ready(order2.cl_id, order2.cl_ord_id))
        self.assertEquals(18, 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=StopLimit,
                                                      qty=1000,
                                                      limit_price=20,
                                                      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(None, fill_info)

        fill_info = handler.process_w_price_qty(order3, 20, 1000)
        self.assertTrue(handler.stop_limit_ready(order3.cl_id, order3.cl_ord_id))
        self.assertEquals(20, 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=StopLimit,
                                                      qty=1000,
                                                      limit_price=20,
                                                      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(None, fill_info)

        fill_info = handler.process(order4, bar1)
        self.assertTrue(handler.stop_limit_ready(order4.cl_id, order4.cl_ord_id))
        self.assertEquals(20, fill_info.fill_price)
        self.assertEquals(1000, fill_info.fill_qty)
예제 #2
0
    def test_stop_limit_order_handler(self):
        handler = StopLimitOrderHandler(self.config)

        bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5, vol=1000)
        bar2 = Bar(inst_id=1, open=16, high=18, low=15, close=17, vol=1000)

        # BUY
        order1 = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.STOP_LIMIT,
                                 qty=1000,
                                 limit_price=18,
                                 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(None, fill_info)

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

        # BUY with bar
        order2 = NewOrderRequest(cl_id='test', cl_ord_id=2, inst_id=1, action=OrdAction.BUY, type=OrdType.STOP_LIMIT,
                                 qty=1000,
                                 limit_price=18,
                                 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(None, fill_info)

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

        # SELL
        order3 = NewOrderRequest(cl_id='test', cl_ord_id=3, inst_id=1, action=OrdAction.SELL, type=OrdType.STOP_LIMIT,
                                 qty=1000,
                                 limit_price=20,
                                 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(None, fill_info)

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

        # SELL with bar
        order4 = NewOrderRequest(cl_id='test', cl_ord_id=4, inst_id=1, action=OrdAction.SELL, type=OrdType.STOP_LIMIT,
                                 qty=1000,
                                 limit_price=20,
                                 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(None, fill_info)

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