Exemple #1
0
    def test_can_flatten_all_positions(self):
        # Arrange
        strategy = TradingStrategy(order_id_tag='001')
        self.exec_engine.register_strategy(strategy)

        order1 = strategy.order_factory.market(USDJPY_FXCM, OrderSide.BUY,
                                               Quantity(100000))

        order2 = strategy.order_factory.market(USDJPY_FXCM, OrderSide.BUY,
                                               Quantity(100000))

        position_id1 = strategy.position_id_generator.generate()
        position_id2 = strategy.position_id_generator.generate()

        strategy.submit_order(order1, position_id1)
        strategy.submit_order(order2, position_id2)

        # Act
        strategy.flatten_all_positions()

        # Assert
        self.assertEqual(order1, strategy.orders()[order1.id])
        self.assertEqual(order2, strategy.orders()[order2.id])
        self.assertEqual(OrderState.FILLED, strategy.orders()[order1.id].state)
        self.assertEqual(OrderState.FILLED, strategy.orders()[order2.id].state)
        self.assertEqual(MarketPosition.FLAT,
                         strategy.positions()[position_id1].market_position)
        self.assertEqual(MarketPosition.FLAT,
                         strategy.positions()[position_id2].market_position)
        self.assertTrue(strategy.positions()[position_id1].is_closed)
        self.assertTrue(strategy.positions()[position_id2].is_closed)
        self.assertTrue(position_id1 in strategy.positions_closed())
        self.assertTrue(position_id2 in strategy.positions_closed())
        self.assertTrue(strategy.is_flat())
Exemple #2
0
    def test_can_cancel_all_orders(self):
        # Arrange
        strategy = TradingStrategy(order_id_tag='001')
        self.exec_engine.register_strategy(strategy)

        order1 = strategy.order_factory.stop(USDJPY_FXCM, OrderSide.BUY,
                                             Quantity(100000),
                                             Price(90.003, 3))

        order2 = strategy.order_factory.stop(USDJPY_FXCM, OrderSide.BUY,
                                             Quantity(100000),
                                             Price(90.005, 3))

        position_id = strategy.position_id_generator.generate()

        strategy.submit_order(order1, position_id)
        strategy.submit_order(order2, position_id)

        # Act
        strategy.cancel_all_orders()

        # Assert
        self.assertEqual(order1, strategy.orders()[order1.id])
        self.assertEqual(order2, strategy.orders()[order2.id])
        self.assertEqual(OrderState.CANCELLED,
                         strategy.orders()[order1.id].state)
        self.assertEqual(OrderState.CANCELLED,
                         strategy.orders()[order2.id].state)
        self.assertTrue(order1.id in strategy.orders_completed())
        self.assertTrue(order2.id in strategy.orders_completed())
Exemple #3
0
    def test_strategy_can_submit_order(self):
        # Arrange
        strategy = TradingStrategy(order_id_tag='001')
        self.exec_engine.register_strategy(strategy)

        order = strategy.order_factory.market(USDJPY_FXCM, OrderSide.BUY,
                                              Quantity(100000))

        # Act
        strategy.submit_order(order, strategy.position_id_generator.generate())

        # Assert
        self.assertEqual(order, strategy.orders()[order.id])
        self.assertEqual(OrderState.FILLED, strategy.orders()[order.id].state)
        self.assertTrue(order.id not in strategy.orders_working())
        self.assertTrue(order.id in strategy.orders_completed())
        self.assertTrue(strategy.order_exists(order.id))
        self.assertFalse(strategy.is_order_working(order.id))
        self.assertTrue(strategy.is_order_completed(order.id))
Exemple #4
0
    def test_can_modify_order(self):
        # Arrange
        strategy = TradingStrategy(order_id_tag='001')
        self.exec_engine.register_strategy(strategy)

        order = strategy.order_factory.limit(USDJPY_FXCM, OrderSide.BUY,
                                             Quantity(100000),
                                             Price(90.001, 3))

        strategy.submit_order(order, strategy.position_id_generator.generate())

        # Act
        strategy.modify_order(order, Quantity(110000), Price(90.002, 3))

        # Assert
        self.assertEqual(order, strategy.orders()[order.id])
        self.assertEqual(OrderState.WORKING, strategy.orders()[order.id].state)
        self.assertEqual(Quantity(110000),
                         strategy.orders()[order.id].quantity)
        self.assertEqual(Price(90.002, 3), strategy.orders()[order.id].price)
        self.assertTrue(strategy.is_flat())
        self.assertTrue(strategy.order_exists(order.id))
        self.assertTrue(strategy.is_order_working(order.id))
        self.assertFalse(strategy.is_order_completed(order.id))