Example #1
0
 def __init__(self, app_context=None, sim_config=None, slippage=None):
     self.app_context = app_context
     self.__sim_config = sim_config if sim_config else SimConfig()
     self.__slippage = slippage if slippage else NoSlippage()
     self.__market_ord_handler = MarketOrderHandler(self.__sim_config, self.__slippage)
     self.__limit_ord_handler = LimitOrderHandler(self.__sim_config)
     self.__stop_limit_ord_handler = StopLimitOrderHandler(self.__sim_config)
     self.__stop_ord_handler = StopOrderHandler(self.__sim_config, self.__slippage)
     self.__trailing_stop_ord_handler = TrailingStopOrderHandler(self.__sim_config, self.__slippage)
Example #2
0
 def __init__(self, app_context=None, sim_config=None, slippage=None):
     self.app_context = app_context
     self.__sim_config = sim_config if sim_config else SimConfig()
     self.__slippage = slippage if slippage else NoSlippage()
     self.__market_ord_handler = MarketOrderHandler(self.__sim_config, self.__slippage)
     self.__limit_ord_handler = LimitOrderHandler(self.__sim_config)
     self.__stop_limit_ord_handler = StopLimitOrderHandler(self.__sim_config)
     self.__stop_ord_handler = StopOrderHandler(self.__sim_config, self.__slippage)
     self.__trailing_stop_ord_handler = TrailingStopOrderHandler(self.__sim_config, self.__slippage)
    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)
Example #4
0
class DefaultFillStrategy(FillStrategy):
    def __init__(self, app_context=None, sim_config=None, slippage=None):
        self.app_context = app_context
        self.__sim_config = sim_config if sim_config else SimConfig()
        self.__slippage = slippage if slippage else NoSlippage()
        self.__market_ord_handler = MarketOrderHandler(self.__sim_config,
                                                       self.__slippage)
        self.__limit_ord_handler = LimitOrderHandler(self.__sim_config)
        self.__stop_limit_ord_handler = StopLimitOrderHandler(
            self.__sim_config)
        self.__stop_ord_handler = StopOrderHandler(self.__sim_config,
                                                   self.__slippage)
        self.__trailing_stop_ord_handler = TrailingStopOrderHandler(
            self.__sim_config, self.__slippage)

    def process_new_order(self, new_ord_req):
        fill_info = None
        config = self.__sim_config

        quote = self.app_context.inst_data_mgr.get_quote(new_ord_req.inst_id)
        trade = self.app_context.inst_data_mgr.get_trade(new_ord_req.inst_id)
        bar = self.app_context.inst_data_mgr.get_bar(new_ord_req.inst_id)

        if not fill_info and config.fill_on_quote and config.fill_on_bar_mode == SimConfig.FillMode.LAST and quote:
            fill_info = self.process_w_market_data(new_ord_req, quote, True)
        elif not fill_info and config.fill_on_trade and config.fill_on_trade_mode == SimConfig.FillMode.LAST and trade:
            fill_info = self.process_w_market_data(new_ord_req, trade, True)
        elif not fill_info and config.fill_on_bar and config.fill_on_bar_mode == SimConfig.FillMode.LAST and bar:
            fill_info = self.process_w_market_data(new_ord_req, bar, True)

        return fill_info

    def process_w_market_data(self, new_ord_req, event, new_order=False):

        config = self.__sim_config

        if not event \
                or (isinstance(event, Bar) and not config.fill_on_bar) \
                or (isinstance(event, Trade) and not config.fill_on_trade) \
                or (isinstance(event, Quote) and not config.fill_on_quote):
            return None

        if new_ord_req.type == OrdType.MARKET:
            return self.__market_ord_handler.process(new_ord_req, event,
                                                     new_order)
        elif new_ord_req.type == OrdType.LIMIT:
            return self.__limit_ord_handler.process(new_ord_req, event,
                                                    new_order)
        elif new_ord_req.type == OrdType.STOP_LIMIT:
            return self.__stop_limit_ord_handler.process(
                new_ord_req, event, new_order)
        elif new_ord_req.type == OrdType.STOP:
            return self.__stop_ord_handler.process(new_ord_req, event,
                                                   new_order)
        elif new_ord_req.type == OrdType.TRAILING_STOP:
            return self.__trailing_stop_ord_handler.process(
                new_ord_req, event, new_order)
        assert False

    def process_w_price_qty(self, new_ord_req, price, qty):
        if new_ord_req.type == OrdType.MARKET:
            return self.__market_ord_handler.process_w_price_qty(
                new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.LIMIT:
            return self.__limit_ord_handler.process_w_price_qty(
                new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.STOP_LIMIT:
            return self.__stop_limit_ord_handler.process_w_price_qty(
                new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.STOP:
            return self.__stop_ord_handler.process_w_price_qty(
                new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.TRAILING_STOP:
            return self.__trailing_stop_ord_handler.process_w_price_qty(
                new_ord_req, price, qty)
        return None
    def test_trailing_stop_order_handler(self):
        handler = TrailingStopOrderHandler(self.config)

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

        # BUY with bar
        order1 = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.TRAILING_STOP,
                                 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 = NewOrderRequest(cl_id='test', cl_ord_id=2, inst_id=1, action=OrdAction.BUY, type=OrdType.TRAILING_STOP,
                                 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 = NewOrderRequest(cl_id='test', cl_ord_id=3, inst_id=1, action=OrdAction.SELL,
                                 type=OrdType.TRAILING_STOP, 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 = NewOrderRequest(cl_id='test', cl_ord_id=4, inst_id=1, action=OrdAction.SELL,
                                 type=OrdType.TRAILING_STOP, 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)
Example #6
0
class DefaultFillStrategy(FillStrategy):
    def __init__(self, app_context=None, sim_config=None, slippage=None):
        self.app_context = app_context
        self.__sim_config = sim_config if sim_config else SimConfig()
        self.__slippage = slippage if slippage else NoSlippage()
        self.__market_ord_handler = MarketOrderHandler(self.__sim_config, self.__slippage)
        self.__limit_ord_handler = LimitOrderHandler(self.__sim_config)
        self.__stop_limit_ord_handler = StopLimitOrderHandler(self.__sim_config)
        self.__stop_ord_handler = StopOrderHandler(self.__sim_config, self.__slippage)
        self.__trailing_stop_ord_handler = TrailingStopOrderHandler(self.__sim_config, self.__slippage)

    def process_new_order(self, new_ord_req):
        fill_info = None
        config = self.__sim_config

        quote = self.app_context.inst_data_mgr.get_quote(new_ord_req.inst_id)
        trade = self.app_context.inst_data_mgr.get_trade(new_ord_req.inst_id)
        bar = self.app_context.inst_data_mgr.get_bar(new_ord_req.inst_id)

        if not fill_info and config.fill_on_quote and config.fill_on_bar_mode == SimConfig.FillMode.LAST and quote:
            fill_info = self.process_w_market_data(new_ord_req, quote, True)
        elif not fill_info and config.fill_on_trade and config.fill_on_trade_mode == SimConfig.FillMode.LAST and trade:
            fill_info = self.process_w_market_data(new_ord_req, trade, True)
        elif not fill_info and config.fill_on_bar and config.fill_on_bar_mode == SimConfig.FillMode.LAST and bar:
            fill_info = self.process_w_market_data(new_ord_req, bar, True)

        return fill_info

    def process_w_market_data(self, new_ord_req, event, new_order=False):

        config = self.__sim_config

        if not event \
                or (isinstance(event, Bar) and not config.fill_on_bar) \
                or (isinstance(event, Trade) and not config.fill_on_trade) \
                or (isinstance(event, Quote) and not config.fill_on_quote):
            return None

        if new_ord_req.type == OrdType.MARKET:
            return self.__market_ord_handler.process(new_ord_req, event, new_order)
        elif new_ord_req.type == OrdType.LIMIT:
            return self.__limit_ord_handler.process(new_ord_req, event, new_order)
        elif new_ord_req.type == OrdType.STOP_LIMIT:
            return self.__stop_limit_ord_handler.process(new_ord_req, event, new_order)
        elif new_ord_req.type == OrdType.STOP:
            return self.__stop_ord_handler.process(new_ord_req, event, new_order)
        elif new_ord_req.type == OrdType.TRAILING_STOP:
            return self.__trailing_stop_ord_handler.process(new_ord_req, event, new_order)
        assert False

    def process_w_price_qty(self, new_ord_req, price, qty):
        if new_ord_req.type == OrdType.MARKET:
            return self.__market_ord_handler.process_w_price_qty(new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.LIMIT:
            return self.__limit_ord_handler.process_w_price_qty(new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.STOP_LIMIT:
            return self.__stop_limit_ord_handler.process_w_price_qty(new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.STOP:
            return self.__stop_ord_handler.process_w_price_qty(new_ord_req, price, qty)
        elif new_ord_req.type == OrdType.TRAILING_STOP:
            return self.__trailing_stop_ord_handler.process_w_price_qty(new_ord_req, price, qty)
        return None