Ejemplo n.º 1
0
    def send_order(self, new_ord_req):
        if new_ord_req.id() in self.order_dict:
            raise Exception(
                "ClientOrderId has been used!! cl_id = %s, cl_ord_id = %s" %
                (new_ord_req.cl_id, new_ord_req.cl_ord_id))

        # persist
        if hasattr(
                self, "store"
        ) and self.store and self.persist_mode != PersistenceMode.RealTime:
            self.store.save_new_order_req(new_ord_req)

        order = Order(new_ord_req)
        self.order_dict[order.id()] = order

        if order.broker_id:
            broker = self.app_context.provider_mgr.get(order.broker_id)
            if broker:
                broker.on_new_ord_req(new_ord_req)
            else:
                logger.warn(
                    "broker [%s] not found for order cl_id [%s] cl_ord_id [%s]"
                    % (order.broker_id, order.cl_id, order.cl_ord_id))

        # persist
        self._save_order(order)

        return order
Ejemplo n.º 2
0
    def test_update_status_with_diff_cl_ord_id(self):
        order = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="3",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        self.assertEquals(New, order.status())

        status_update = ModelFactory.build_order_status_update(
            timestamp=0,
            broker_id="TestBroker",
            broker_event_id="3",
            broker_ord_id="3",
            cl_id='TestClient',
            cl_ord_id="ERROR",
            status=Submitted)

        with self.assertRaises(Exception) as ex:
            order.on_ord_upd(status_update)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    def test_exec_report_with_diff_ord_id(self):
        order = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="1",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        er1 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="1",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="ERROR",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=800,
                                                  last_price=18.4,
                                                  status=PartiallyFilled)

        with self.assertRaises(Exception) as ex:
            order.on_exec_report(er1)
Ejemplo n.º 5
0
    def test_update_status_with_diff_cl_ord_id(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.assertEquals(OrdStatus.NEW, order.status)

        status_update = OrderStatusUpdate(cl_id='test', cl_ord_id=2, ord_id=2, inst_id=1, status=OrdStatus.SUBMITTED)

        with self.assertRaises(Exception) as ex:
            order.on_ord_upd(status_update)
Ejemplo n.º 6
0
 def test_is_active(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.assertTrue(order.is_active())
Ejemplo n.º 7
0
    def test_exec_report_with_diff_ord_id(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=2, ord_id=2, er_id=1, inst_id=1, last_qty=800, last_price=18.4,
                              status=OrdStatus.PARTIALLY_FILLED)

        with self.assertRaises(Exception) as ex:
            order.on_exec_report(er1)
Ejemplo n.º 8
0
    def test_on_ord_upd(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.assertEquals(OrdStatus.NEW, order.status)

        status_update = OrderStatusUpdate(cl_id='test', cl_ord_id=1, ord_id=1, inst_id=1, status=OrdStatus.SUBMITTED)
        order.on_ord_upd(status_update)

        self.assertEqual(status_update.ord_id, order.broker_ord_id)
        self.assertEquals(OrdStatus.SUBMITTED, order.status)
Ejemplo n.º 9
0
    def test_exec_report_with_exceed_qty(self):
        order = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="1",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        self.assertEqual(1000, order.leave_qty())
        self.assertEqual(0, len(order.get_events(ExecutionReport)))

        er1 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="1",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=800,
                                                  last_price=18.4,
                                                  status=PartiallyFilled)
        order.on_exec_report(er1)

        er2 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="2",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=200,
                                                  last_price=18.4,
                                                  status=Filled)
        order.on_exec_report(er2)

        er3 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="3",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=100,
                                                  last_price=18.4,
                                                  status=Filled)
        with self.assertRaises(Exception) as ex:
            order.on_exec_report(er3)
Ejemplo n.º 10
0
 def test_is_buy(self):
     nos = ModelFactory.build_new_order_request(timestamp=0,
                                                cl_id='TestClient',
                                                cl_ord_id="TestClientOrder",
                                                portf_id="TestPortf",
                                                broker_id="TestBroker",
                                                inst_id="HSI@SEHK",
                                                action=Buy,
                                                type=Limit,
                                                qty=1000,
                                                limit_price=18.5)
     order = Order(ModelFactory.build_order_state_from_nos(nos))
     self.assertTrue(order.is_buy())
     self.assertFalse(order.is_sell())
Ejemplo n.º 11
0
    def test_is_active(self):
        order1 = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="1",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        self.assertTrue(order1.is_active())
Ejemplo n.º 12
0
    def test_order(self, name, serializer):
        nos = NewOrderRequest(timestamp=None, cl_id=None, cl_ord_id=None, portf_id=None, broker_id=None, inst_id=None,
                              action=None, type=None,
                              qty=0, limit_price=0,
                              stop_price=0, tif=TIF.DAY, oca_tag=None, params=None)

        item = Order(nos=nos)
        SerializerTest.ser_deser(name, serializer, item)
Ejemplo n.º 13
0
    def test_add_order(self):
        position = Position(1)
        self.assertEquals(0, position.filled_qty())
        self.assertEquals(0, len(position.all_orders()))

        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(1000, position.ordered_qty())
        self.assertEquals(1, len(position.all_orders()))

        order2 = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=2,
                            inst_id=1,
                            action=OrdAction.BUY,
                            type=OrdType.LIMIT,
                            qty=1000,
                            limit_price=18.5))
        position.add_order(order2)

        self.assertEquals(2000, position.ordered_qty())
        self.assertEquals(2, len(position.all_orders()))

        order3 = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=3,
                            inst_id=1,
                            action=OrdAction.SELL,
                            type=OrdType.LIMIT,
                            qty=1200,
                            limit_price=18.5))
        position.add_order(order3)

        self.assertEquals(800, position.ordered_qty())
        self.assertEquals(3, len(position.all_orders()))
Ejemplo n.º 14
0
    def test_update_status_with_diff_cl_ord_id(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.assertEquals(OrdStatus.NEW, order.status)

        status_update = OrderStatusUpdate(cl_id='test',
                                          cl_ord_id=2,
                                          ord_id=2,
                                          inst_id=1,
                                          status=OrdStatus.SUBMITTED)

        with self.assertRaises(Exception) as ex:
            order.on_ord_upd(status_update)
Ejemplo n.º 15
0
    def test_on_ord_upd(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.assertEquals(OrdStatus.NEW, order.status)

        status_update = OrderStatusUpdate(cl_id='test',
                                          cl_ord_id=1,
                                          ord_id=1,
                                          inst_id=1,
                                          status=OrdStatus.SUBMITTED)
        order.on_ord_upd(status_update)

        self.assertEqual(status_update.ord_id, order.broker_ord_id)
        self.assertEquals(OrdStatus.SUBMITTED, order.status)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    def test_exec_report_with_diff_ord_id(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=2,
                              ord_id=2,
                              er_id=1,
                              inst_id=1,
                              last_qty=800,
                              last_price=18.4,
                              status=OrdStatus.PARTIALLY_FILLED)

        with self.assertRaises(Exception) as ex:
            order.on_exec_report(er1)
Ejemplo n.º 18
0
    def test_on_ord_upd(self):
        order = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="3",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        status_update = ModelFactory.build_order_status_update(
            timestamp=0,
            broker_id="TestBroker",
            broker_event_id="3",
            broker_ord_id="3",
            cl_id='TestClient',
            cl_ord_id="3",
            status=Submitted)

        order.on_ord_upd(status_update)
        self.assertEqual(status_update.broker_ord_id, order.broker_ord_id())
        self.assertEquals(Submitted, order.status())
Ejemplo n.º 19
0
    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())
Ejemplo n.º 20
0
    def test_add_order_with_diff_inst(self):
        position = Position(1)
        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))
        order2 = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=2,
                            inst_id=2,
                            action=OrdAction.BUY,
                            type=OrdType.LIMIT,
                            qty=1000,
                            limit_price=18.5))

        position.add_order(order1)

        with self.assertRaises(RuntimeError) as ex:
            position.add_order(order2)
Ejemplo n.º 21
0
    def send_order(self, new_ord_req):
        if new_ord_req.id() in self.order_dict:
            raise Exception(
                "ClientOrderId has been used!! cl_id = %s, cl_ord_id = %s" % (new_ord_req.cl_id, new_ord_req.cl_ord_id))

        # persist
        if hasattr(self, "store") and self.store and self.persist_mode != PersistenceMode.RealTime:
            self.store.save_new_order_req(new_ord_req)

        order = Order(new_ord_req)
        self.order_dict[order.id()] = order

        if order.broker_id:
            broker = self.app_context.provider_mgr.get(order.broker_id)
            if broker:
                broker.on_new_ord_req(new_ord_req)
            else:
                logger.warn("broker [%s] not found for order cl_id [%s] cl_ord_id [%s]" % (
                    order.broker_id, order.cl_id, order.cl_ord_id))

        # persist
        self._save_order(order)

        return order
Ejemplo n.º 22
0
    def test_is_sell(self):
        order = Order(
            NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.SELL, type=OrdType.LIMIT, qty=1000,
                            limit_price=18.5))
        self.assertFalse(order.is_buy())
        self.assertTrue(order.is_sell())

        order = Order(
            NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.SSHORT, type=OrdType.LIMIT, qty=1000,
                            limit_price=18.5))
        self.assertFalse(order.is_buy())
        self.assertTrue(order.is_sell())
Ejemplo n.º 23
0
 def test_order(self, name, datastore):
     nos = NewOrderRequest(timestamp=None,
                           cl_id=None,
                           cl_ord_id=None,
                           portf_id=None,
                           broker_id=None,
                           inst_id=None,
                           action=None,
                           type=None,
                           qty=0,
                           limit_price=0,
                           stop_price=0,
                           tif=TIF.DAY,
                           oca_tag=None,
                           params=None)
     persistable = Order(nos=nos)
     DataStoreTest.save_load(name, persistable, datastore,
                             datastore.save_order, 'orders')
Ejemplo n.º 24
0
    def test_is_sell(self):
        order = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=1,
                            inst_id=1,
                            action=OrdAction.SELL,
                            type=OrdType.LIMIT,
                            qty=1000,
                            limit_price=18.5))
        self.assertFalse(order.is_buy())
        self.assertTrue(order.is_sell())

        order = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=1,
                            inst_id=1,
                            action=OrdAction.SSHORT,
                            type=OrdType.LIMIT,
                            qty=1000,
                            limit_price=18.5))
        self.assertFalse(order.is_buy())
        self.assertTrue(order.is_sell())
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
    def test_is_done(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.assertFalse(order.is_done())

        status_update1 = OrderStatusUpdate(cl_id='test', cl_ord_id=1, ord_id=1, inst_id=1, status=OrdStatus.REJECTED)
        order.on_ord_upd(status_update1)
        self.assertTrue(order.is_done())

        order2 = Order(
            NewOrderRequest(cl_id='test', cl_ord_id=2, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000,
                            limit_price=18.5))
        status_update2 = OrderStatusUpdate(cl_id='test', cl_ord_id=2, ord_id=2, inst_id=1, status=OrdStatus.CANCELLED)
        order2.on_ord_upd(status_update2)
        self.assertTrue(order2.is_done())

        order3 = Order(
            NewOrderRequest(cl_id='test', cl_ord_id=3, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000,
                            limit_price=18.5))
        status_update3 = OrderStatusUpdate(cl_id='test', cl_ord_id=3, ord_id=3, inst_id=1, status=OrdStatus.FILLED)
        order3.on_ord_upd(status_update3)
        self.assertTrue(order3.is_done())
Ejemplo n.º 27
0
 def test_is_active(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.assertTrue(order.is_active())
Ejemplo n.º 28
0
    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)
Ejemplo n.º 29
0
    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())
Ejemplo n.º 30
0
    def test_is_done(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.assertFalse(order.is_done())

        status_update1 = OrderStatusUpdate(cl_id='test',
                                           cl_ord_id=1,
                                           ord_id=1,
                                           inst_id=1,
                                           status=OrdStatus.REJECTED)
        order.on_ord_upd(status_update1)
        self.assertTrue(order.is_done())

        order2 = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=2,
                            inst_id=1,
                            action=OrdAction.BUY,
                            type=OrdType.LIMIT,
                            qty=1000,
                            limit_price=18.5))
        status_update2 = OrderStatusUpdate(cl_id='test',
                                           cl_ord_id=2,
                                           ord_id=2,
                                           inst_id=1,
                                           status=OrdStatus.CANCELLED)
        order2.on_ord_upd(status_update2)
        self.assertTrue(order2.is_done())

        order3 = Order(
            NewOrderRequest(cl_id='test',
                            cl_ord_id=3,
                            inst_id=1,
                            action=OrdAction.BUY,
                            type=OrdType.LIMIT,
                            qty=1000,
                            limit_price=18.5))
        status_update3 = OrderStatusUpdate(cl_id='test',
                                           cl_ord_id=3,
                                           ord_id=3,
                                           inst_id=1,
                                           status=OrdStatus.FILLED)
        order3.on_ord_upd(status_update3)
        self.assertTrue(order3.is_done())
Ejemplo n.º 31
0
    def test_leave_qty(self):
        order = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="1",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))
        self.assertEqual(1000, order.leave_qty())

        er1 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="1",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=800,
                                                  last_price=18.4,
                                                  status=PartiallyFilled)
        order.on_exec_report(er1)
        self.assertEqual(200, order.leave_qty())

        er2 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="2",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=200,
                                                  last_price=18.4,
                                                  status=Filled)
        order.on_exec_report(er2)
        self.assertEqual(0, order.leave_qty())
        self.assertTrue(order.is_done())
        self.assertEquals(Filled, order.status())
Ejemplo n.º 32
0
    def test_exec_report(self):
        order = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="1",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        self.assertEqual(1000, order.leave_qty())
        self.assertEqual(0, len(order.get_events(ExecutionReport)))

        er1 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="1",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=800,
                                                  last_price=18.4,
                                                  status=PartiallyFilled)
        order.on_exec_report(er1)
        self.assertEqual(1, len(order.get_events(ExecutionReport)))
        self.assertTrue(er1 in order.get_events(ExecutionReport))

        self.assertEqual(er1.broker_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(PartiallyFilled, order.status())

        er2 = ModelFactory.build_execution_report(timestamp=0,
                                                  broker_id="TestBroker",
                                                  broker_event_id="1",
                                                  broker_ord_id="1",
                                                  cl_id='TestClient',
                                                  cl_ord_id="1",
                                                  inst_id="HSI@SEHK",
                                                  last_qty=200,
                                                  last_price=18.4,
                                                  status=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.broker_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(Filled, order.status())
Ejemplo n.º 33
0
    def test_is_done(self):
        order1 = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="1",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        status_update1 = ModelFactory.build_order_status_update(
            timestamp=0,
            broker_id="TestBroker",
            broker_event_id="1",
            broker_ord_id="1",
            cl_id='TestClient',
            cl_ord_id="1",
            status=Rejected)

        order1.on_ord_upd(status_update1)
        self.assertTrue(order1.is_done())

        order2 = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="2",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        status_update2 = ModelFactory.build_order_status_update(
            timestamp=0,
            broker_id="TestBroker",
            broker_event_id="2",
            broker_ord_id="2",
            cl_id='TestClient',
            cl_ord_id="2",
            status=Cancelled)

        order2.on_ord_upd(status_update2)
        self.assertTrue(order1.is_done())

        order3 = Order(
            ModelFactory.build_order_state_from_nos(
                ModelFactory.build_new_order_request(timestamp=0,
                                                     cl_id='TestClient',
                                                     cl_ord_id="3",
                                                     portf_id="TestPortf",
                                                     broker_id="TestBroker",
                                                     inst_id="HSI@SEHK",
                                                     action=Buy,
                                                     type=Limit,
                                                     qty=1000,
                                                     limit_price=18.5)))

        status_update3 = ModelFactory.build_order_status_update(
            timestamp=0,
            broker_id="TestBroker",
            broker_event_id="3",
            broker_ord_id="3",
            cl_id='TestClient',
            cl_ord_id="3",
            status=Filled)

        order3.on_ord_upd(status_update3)
        self.assertTrue(order3.is_done())