self.model:Model
self.instrument:Instrument
cb = mock.MagicMock()
tm:TradeManager = self.model.tradeManager()
tm.eventAutoTradePlaced.connect(cb)
order = tm.placeOrder(OrderAction.BUY,self.instrument)
self.assertTrue(len(order.childOrders) == 2)
trailing:Order
profit:Order
trailing,profit = order.childOrders
self.assertGreater(order.limitPrice,trailing.limitPrice)
self.assertGreater(profit.limitPrice,order.limitPrice)
status = OrderStatus()
status.orderId = order.orderId
status.status = OrderStatusType.FILLED
status.filled = order.totalQuantity
status.remaining = 0
status.avgFillPrice = order.limitPrice
order.status = status
order.account = order.account
self.assertEqual(self.account.positions[self.instrument].quantity,0)
self.assertEqual(self.account.positions[self.instrument].price,0)
self.model._broker.eventOrderUpdated(order)
self.assertEqual(self.account.positions[self.instrument].quantity,order.totalQuantity)
self.assertEqual(self.account.positions[self.instrument].price,order.limitPrice)
initialQuantity = self.account.positions[self.instrument].quantity
# simulate take profit fill
status = OrderStatus()
status.orderId = profit.orderId
status.status = OrderStatusType.FILLED
status.filled = profit.totalQuantity
status.remaining = 0
status.avgFillPrice = profit.limitPrice
profit.status = status
profit.orderId = order.orderId + 1
profit.account = order.account
self.model._broker.eventOrderUpdated(profit)
self.assertEqual(self.account.positions[self.instrument].quantity,order.totalQuantity/2)
self.assertTrue(cb.called)
breakevenStop:Order
breakevenStop, = cb.call_args_list[0][0]
self.assertEqual(breakevenStop.action,OrderAction.SELL)
self.assertEqual(breakevenStop.limitPrice,self.account.positions[self.instrument].price)
self.assertEqual(breakevenStop.totalQuantity,profit.totalQuantity