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_trader_processor(self):
        config = SimConfig()
        processor = TradeProcessor()

        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)
        trade = ModelFactory.build_trade(timestamp=0, inst_id="1", price=20, size=200)

        self.assertEqual(20, processor.get_price(order, trade, config))
        self.assertEqual(200, processor.get_qty(order, trade, config))
Exemple #3
0
    def test_trader_processor(self):
        config = SimConfig()
        processor = TradeProcessor()

        order = NewOrderRequest(cl_id='test',
                                cl_ord_id=1,
                                inst_id=1,
                                action=OrdAction.BUY,
                                type=OrdType.LIMIT,
                                qty=1000,
                                limit_price=18.5)
        trade = Trade(price=20, size=200)

        self.assertEqual(20, processor.get_price(order, trade, config))
        self.assertEqual(200, processor.get_qty(order, trade, config))
Exemple #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()