Пример #1
0
    def test_quote_processor(self):
        config = SimConfig()
        processor = QuoteProcessor()

        order = NewOrderRequest(cl_id='test',
                                cl_ord_id=1,
                                inst_id=1,
                                action=OrdAction.BUY,
                                type=OrdType.LIMIT,
                                qty=1000,
                                limit_price=18.5)
        quote = Quote(bid=18, ask=19, bid_size=200, ask_size=500)

        self.assertEqual(19, processor.get_price(order, quote, config))
        self.assertEqual(500, processor.get_qty(order, quote, config))

        order2 = NewOrderRequest(cl_id='test',
                                 cl_ord_id=2,
                                 inst_id=1,
                                 action=OrdAction.SELL,
                                 type=OrdType.LIMIT,
                                 qty=1000,
                                 limit_price=18.5)
        self.assertEqual(18, processor.get_price(order2, quote, config))
        self.assertEqual(200, processor.get_qty(order2, quote, config))
Пример #2
0
class SimOrderHandler(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, config):
        self._config = config
        self._bar_processor = BarProcessor()
        self._trade_processor = TradeProcessor()
        self._quote_processor = QuoteProcessor()

    def process(self, new_ord_req, event, new_order=False):
        if event:
            if isinstance(event, Bar):
                fill_qty = self._bar_processor.get_qty(new_ord_req, event, self._config)
                return self.process_w_bar(new_ord_req, event, fill_qty, new_order)
            elif isinstance(event, Quote):
                fill_price = self._quote_processor.get_price(new_ord_req, event, self._config, new_order)
                fill_qty = self._quote_processor.get_qty(new_ord_req, event, self._config)
                if fill_price <= 0.0 or fill_qty <= 0:
                    return None
                return self.process_w_price_qty(new_ord_req, fill_price, fill_qty)
            elif isinstance(event, Trade):
                fill_price = self._trade_processor.get_price(new_ord_req, event, self._config, new_order)
                fill_qty = self._trade_processor.get_qty(new_ord_req, event, self._config)
                if fill_price <= 0.0 or fill_qty <= 0:
                    return None
                return self.process_w_price_qty(new_ord_req, fill_price, fill_qty)
        return None

    @abc.abstractmethod
    def process_w_bar(self, new_ord_req, bar, new_order=False):
        raise NotImplementedError()

    @abc.abstractmethod
    def process_w_price_qty(self, new_ord_req, price, qty):
        raise NotImplementedError()
    def test_quote_processor(self):
        config = SimConfig()
        processor = QuoteProcessor()

        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)
        quote = ModelFactory.build_quote(timestamp=0, inst_id="1", bid=18, ask=19, bid_size=200, ask_size=500)

        self.assertEqual(19, processor.get_price(order, quote, config))
        self.assertEqual(500, processor.get_qty(order, quote, config))

        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)
        self.assertEqual(18, processor.get_price(order2, quote, config))
        self.assertEqual(200, processor.get_qty(order2, quote, config))
Пример #4
0
class SimOrderHandler(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, config):
        self._config = config
        self._bar_processor = BarProcessor()
        self._trade_processor = TradeProcessor()
        self._quote_processor = QuoteProcessor()

    def process(self, new_ord_req, event, new_order=False):
        if event:
            if isinstance(event, Bar):
                fill_qty = self._bar_processor.get_qty(new_ord_req, event,
                                                       self._config)
                return self.process_w_bar(new_ord_req, event, fill_qty,
                                          new_order)
            elif isinstance(event, Quote):
                fill_price = self._quote_processor.get_price(
                    new_ord_req, event, self._config, new_order)
                fill_qty = self._quote_processor.get_qty(
                    new_ord_req, event, self._config)
                if fill_price <= 0.0 or fill_qty <= 0:
                    return None
                return self.process_w_price_qty(new_ord_req, fill_price,
                                                fill_qty)
            elif isinstance(event, Trade):
                fill_price = self._trade_processor.get_price(
                    new_ord_req, event, self._config, new_order)
                fill_qty = self._trade_processor.get_qty(
                    new_ord_req, event, self._config)
                if fill_price <= 0.0 or fill_qty <= 0:
                    return None
                return self.process_w_price_qty(new_ord_req, fill_price,
                                                fill_qty)
        return None

    @abc.abstractmethod
    def process_w_bar(self, new_ord_req, bar, new_order=False):
        raise NotImplementedError()

    @abc.abstractmethod
    def process_w_price_qty(self, new_ord_req, price, qty):
        raise NotImplementedError()