def test_leave_qty(self): order = Order( NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)) self.assertEqual(1000, order.leave_qty()) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=800, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) order.on_exec_report(er1) self.assertEqual(200, order.leave_qty()) er2 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=2, inst_id=1, last_qty=200, last_price=18.4, status=OrdStatus.FILLED) order.on_exec_report(er2) self.assertEqual(0, order.leave_qty()) self.assertTrue(order.is_done()) self.assertEquals(OrdStatus.FILLED, order.status)
def test_exec_report(self): order = Order( NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)) self.assertEqual(1000, order.leave_qty()) self.assertEqual(0, len(order.get_events(ExecutionReport))) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=800, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) order.on_exec_report(er1) self.assertEqual(1, len(order.get_events(ExecutionReport))) self.assertTrue(er1 in order.get_events(ExecutionReport)) self.assertEqual(er1.ord_id, order.broker_ord_id) self.assertEqual(800, order.last_qty) self.assertAlmostEqual(18.4, order.last_price) self.assertEqual(800, order.filled_qty) self.assertAlmostEqual(18.4, order.avg_price) self.assertEqual(OrdStatus.PARTIALLY_FILLED, order.status) er2 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=2, inst_id=1, last_qty=200, last_price=18.4, status=OrdStatus.FILLED) order.on_exec_report(er2) self.assertEqual(2, len(order.get_events(ExecutionReport))) self.assertTrue(er1 in order.get_events(ExecutionReport)) self.assertTrue(er2 in order.get_events(ExecutionReport)) self.assertEqual(er2.ord_id, order.broker_ord_id) self.assertEqual(200, order.last_qty) self.assertAlmostEqual(18.4, order.last_price) self.assertEqual(1000, order.filled_qty) self.assertAlmostEqual(18.4, order.avg_price) self.assertEqual(OrdStatus.FILLED, order.status)
def test_position(self): ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5) order1 = self.portfolio.send_order(ord_req) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) self.app_context.order_mgr.on_exec_report(er1) self.assertEqual(500, order1.last_qty) self.assertEqual(18.4, order1.last_price) self.assertEqual(500, order1.filled_qty) self.assertEqual(18.4, order1.avg_price) self.assertEqual(OrdStatus.PARTIALLY_FILLED, order1.status) self.assertEquals(True, self.portfolio.has_position(cl_id='test', inst_id=1)) self.assertEquals(False, self.portfolio.has_position(cl_id='test', inst_id=2)) self.assertEquals( False, self.portfolio.has_position(cl_id='dummy', inst_id=2))
def test_execution_report(self, name, serializer): item = ExecutionReport(broker_id="IB", ord_id=1, cl_id=1, cl_ord_id=1, inst_id=3, timestamp=datetime.datetime.now(), er_id=None, last_qty=10, last_price=10.8, filled_qty=140, avg_price=21.2, commission=18, status=OrdStatus.NEW) SerializerTest.ser_deser(name, serializer, item)
def test_on_exec_report(self): ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5) order1 = self.portfolio.send_order(ord_req) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) self.app_context.order_mgr.on_exec_report(er1) self.assertEqual(500, order1.last_qty) self.assertEqual(18.4, order1.last_price) self.assertEqual(500, order1.filled_qty) self.assertEqual(18.4, order1.avg_price) self.assertEqual(OrdStatus.PARTIALLY_FILLED, order1.status) self.check_order(self.portfolio, [order1], {1: (1000, 500)}) expected_cash = 100000 - 500 * 18.4 expected_stock_value = 500 * 18.4 expected_total_equity = expected_cash + expected_stock_value self.assertEqual(expected_cash, self.portfolio.cash) self.assertEqual(expected_stock_value, self.portfolio.performance_series.now('stock_value')) self.assertEqual(expected_total_equity, self.portfolio.performance_series.now('total_equity')) er2 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=2, inst_id=1, last_qty=500, last_price=18.2, status=OrdStatus.FILLED) self.app_context.order_mgr.on_exec_report(er2) self.assertEqual(500, order1.last_qty) self.assertEqual(18.2, order1.last_price) self.assertEqual(1000, order1.filled_qty) self.assertEqual(18.3, order1.avg_price) self.assertEqual(OrdStatus.FILLED, order1.status) self.check_order(self.portfolio, [order1], {1: (1000, 1000)}) expected_cash = 100000 - 500 * 18.4 - 500 * 18.2 expected_stock_value = 1000 * 18.2 expected_total_equity = expected_cash + expected_stock_value self.assertEqual(expected_cash, self.portfolio.cash) self.assertEqual(expected_stock_value, self.portfolio.performance_series.now('stock_value')) self.assertEqual(expected_total_equity, self.portfolio.performance_series.now('total_equity'))
def test_exec_report_with_exceed_qty(self): order = Order( NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=800, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) order.on_exec_report(er1) er2 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=2, inst_id=1, last_qty=200, last_price=18.4, status=OrdStatus.FILLED) order.on_exec_report(er2) er3 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=3, inst_id=1, last_qty=100, last_price=18.4, status=OrdStatus.FILLED) with self.assertRaises(Exception) as ex: order.on_exec_report(er3)
def test_execution_report(self, name, datastore): persistable = ExecutionReport(broker_id="IB", ord_id=1, cl_id=1, cl_ord_id=1, inst_id=3, timestamp=clock.now(), er_id=None, last_qty=10, last_price=10.8, filled_qty=140, avg_price=21.2, commission=18, status=OrdStatus.NEW) DataStoreTest.save_load(name, persistable, datastore, datastore.save_exec_report, 'exec_reports')
def __send_exec_report(self, new_ord_req, last_price, last_qty, ord_status): commission = self.commission.calc(new_ord_req, last_price, last_qty) ord_id = self.clordid_ordid_map[new_ord_req.cl_id][new_ord_req.cl_ord_id] exec_report = ExecutionReport(broker_id=Broker.Simulator, ord_id=ord_id, cl_id=new_ord_req.cl_id, cl_ord_id=new_ord_req.cl_ord_id, inst_id=new_ord_req.inst_id, timestamp=self.clock.now(), er_id=self.next_exec_id(), last_qty=last_qty, last_price=last_price, status=ord_status, commission=commission) self.exec_handler.on_exec_report(exec_report)
def execDetails(self, reqId, contract, execution): """ int reqId, Contract contract, Execution execution """ new_ord_req = self.ord_req_reg.get_ord_req(ord_id=execution.orderId) if new_ord_req: self.execution_event_bus.on_next(ExecutionReport( broker_id=self.ID, ord_id=execution.orderId, cl_id=new_ord_req.cl_id, cl_ord_id=new_ord_req.cl_ord_id, er_id=execution.execId, timestamp=self.model_factory.convert_ib_datetime(execution.time), inst_id=new_ord_req.inst_id, last_qty=execution.shares, last_price=execution.price, filled_qty=execution.cumQty, avg_price=execution.avgPrice ))
def test_on_market_date_update(self): ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5, timestamp=0) order1 = self.portfolio.on_new_ord_req(ord_req) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED, timestamp=1) self.app_context.order_mgr.on_exec_report(er1) expected_cash = 100000 - 500 * 18.4 expected_stock_value = 500 * 18.4 expected_total_equity = expected_cash + expected_stock_value self.assertEqual(expected_cash, self.portfolio.cash) self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(0, 'stock_value')) self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(0, 'total_equity')) self.portfolio.on_trade(Trade(inst_id=1, price=20, size=1000, timestamp=2)) expected_cash = 100000 - 500 * 18.4 expected_stock_value = 500 * 20 expected_total_equity = expected_cash + expected_stock_value self.assertEqual(expected_cash, self.portfolio.cash) self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(1, 'stock_value')) self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(1, 'total_equity')) self.portfolio.on_bar(Bar(inst_id=1, close=16, adj_close=16, vol=1000, timestamp=3)) expected_cash = 100000 - 500 * 18.4 expected_stock_value = 500 * 16 expected_total_equity = expected_cash + expected_stock_value self.assertEqual(expected_cash, self.portfolio.cash) self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(2, 'stock_value')) self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(2, 'total_equity')) self.portfolio.on_quote(Quote(inst_id=1, bid=16, ask=18, timestamp=4)) expected_cash = 100000 - 500 * 18.4 expected_stock_value = 500 * 17 expected_total_equity = expected_cash + expected_stock_value self.assertEqual(expected_cash, self.portfolio.cash) self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(3, 'stock_value')) self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(3, 'total_equity'))
def test_fill_qty(self): position = Position(1) self.assertEquals(0, position.filled_qty()) order1 = Order( NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5)) position.add_order(order1) self.assertEquals(0, position.filled_qty()) er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) order1.on_exec_report(er1) position.add_position(er1.cl_id, er1.cl_ord_id, er1.last_qty) self.assertEquals(500, position.filled_qty()) er2 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=2, inst_id=1, last_qty=500, last_price=18.4, status=OrdStatus.FILLED) order1.on_exec_report(er2) position.add_position(er2.cl_id, er2.cl_ord_id, er2.last_qty) self.assertEquals(1000, position.filled_qty()) order2 = Order( NewOrderRequest(cl_id='test', cl_ord_id=2, inst_id=1, action=OrdAction.SELL, type=OrdType.LIMIT, qty=1200, limit_price=18.5)) position.add_order(order2) self.assertEquals(1000, position.filled_qty()) er3 = ExecutionReport(cl_id='test', cl_ord_id=2, ord_id=2, er_id=3, inst_id=1, last_qty=800, last_price=18.4, status=OrdStatus.PARTIALLY_FILLED) order2.on_exec_report(er3) position.add_position(er3.cl_id, er3.cl_ord_id, er3.last_qty * -1) self.assertEquals(200, position.filled_qty()) er4 = ExecutionReport(cl_id='test', cl_ord_id=2, ord_id=2, er_id=4, inst_id=1, last_qty=400, last_price=18.4, status=OrdStatus.FILLED) order2.on_exec_report(er4) position.add_position(er4.cl_id, er4.cl_ord_id, er4.last_qty * -1) self.assertEquals(-200, position.filled_qty())