My algorithm responds to an Insight by entering a trade with a MarketOrder, and also setting target and stop prices with StopMarkerOrder and LimitOrder like so:

def onInsightEmmitted(self, insight, currentPrice, targetPrice, stopPrice): stopSize = abs(currentPrice - stopPrice) positionSize = self.CalculatePositionSize(stopSize) if not self.Portfolio[insight.Symbol].Invested: if insight.Direction == InsightDirection.Up: self.MarketOrder(insight.Symbol, positionSize) self.StopMarketOrder(insight.Symbol, -positionSize, stopPrice) self.LimitOrder(insight.Symbol, -positionSize, targetPrice) if insight.Direction == InsightDirection.Down: self.MarketOrder(insight.Symbol, -positionSize) self.LimitOrder(insight.Symbol, positionSize, stopPrice) self.StopMarketOrder(insight.Symbol, positionSize, targetPrice) def OnOrderEvent(self, orderEvent): order = self.Transactions.GetOrderById(orderEvent.OrderId) if orderEvent.Status == OrderStatus.Filled: if order.Type == 2 or order.Type == 1: self.Transactions.CancelOpenOrders(order.Symbol)

This seems to work well in most cases, but it looks like I had a case in my last backtest where the MarketOrder was turned into a MarketOnOpen order, there was a price gap on market open, and the MarketOrder, LimitOrder and StopMarketOrder for each security were filled all at the same time. How can I prevent something like this from happening? Can I use some logic to say that if multiple orders for the same security are going to be filled at the same time, then they should all just be cancelled?