Hi,

I am new to quant and I am trying to do the basics that are doing a trade with a stop loss and a take profit.

In my algorithm to set the orders I do:

self.marketOrder=self.MarketOrder( self.symbol,-50000) self.limitOrder=self.LimitOrder(self.symbol,50000,price-0.20) self.stopOrder=self.StopMarketOrder(self.symbol,50000,price+0.10)

And when the stop or the limit order is filled I cancel the other one to avoid filling both leading to a living trade that should not exist:

def OnOrderEvent(self,orderEvent): self.Log(orderEvent) order = self.Transactions.GetOrderById(orderEvent.OrderId) if self.limitOrder is not None and self.stopOrder is not None and orderEvent.Status==OrderStatus.Filled and (order.Id== self.limitOrder.OrderId or order.Id==self.stopOrder.OrderId): if order.Id==self.limitOrder.OrderId: #self.Log("Stop order cancelled: %d" %self.limitOrder.OrderId) self.stopOrder.Cancel() else: #self.Log("limit order cancelled: %d" %self.stopOrder.OrderId) self.limitOrder.Cancel()

This works for 99% of the trades in the attached backtest.

But it does not work for one. And unless I am missing something obvious, it seems to me that it is a bug.

Indeed, in the log we can see:

2020-03-12 14:00:00 : Warning: To meet brokerage precision requirements, order StopPrice was rounded to 76.706 from 76.7055 2020-03-12 14:00:00 : Time: 03/12/2020 17:10:00 OrderID: 13 EventID: 1 Symbol: CADJPY Status: Submitted Quantity: -50000 2020-03-12 14:00:00 : Time: 03/12/2020 17:10:00 OrderID: 13 EventID: 2 Symbol: CADJPY Status: Filled Quantity: -50000 FillQuantity: -50000 FillPrice: 76.589 JPY 2020-03-12 14:00:00 : Time: 03/12/2020 17:10:00 OrderID: 14 EventID: 1 Symbol: CADJPY Status: Submitted Quantity: 50000 LimitPrice: 76.406 2020-03-12 14:00:00 : Time: 03/12/2020 17:10:00 OrderID: 15 EventID: 1 Symbol: CADJPY Status: Submitted Quantity: 50000 StopPrice: 76.706 2020-03-12 14:00:00 : Time: 03/12/2020 18:00:00 OrderID: 14 EventID: 2 Symbol: CADJPY Status: Filled Quantity: 50000 FillQuantity: 50000 FillPrice: 76.406 JPY LimitPrice: 76.406 2020-03-12 14:00:00 : Time: 03/12/2020 18:00:00 OrderID: 15 EventID: 2 Symbol: CADJPY Status: CancelPending Quantity: 50000 StopPrice: 76.706 2020-03-12 14:00:00 : Time: 03/12/2020 18:00:00 OrderID: 15 EventID: 3 Symbol: CADJPY Status: Filled Quantity: 50000 FillQuantity: 50000 FillPrice: 76.706 JPY StopPrice: 76.706

So the stop loss is filled, then the cancel request is sent and then the limit order is filled instead of being canceled.
There are 30 pipsĀ  between the limit and the stop loss so unless the 30 pips are reached in a second at that time in live, this should not happen from my understanding right?

Thank you for any hint of what I am missing or confirming it is a bug.

Regards