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()
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_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))
def test_bar_processor(self): config = SimConfig() processor = BarProcessor() order = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5) bar = Bar(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))
def __init__(self, config): self._config = config self._bar_processor = BarProcessor() self._trade_processor = TradeProcessor() self._quote_processor = QuoteProcessor()