Hi I received this error when updating a StopMarketOrder ticket.

The weird issue is the code works fine with just 1 or 2 symbols but when i add more symbols to the list, it fails.

BacktestingRealTimeHandler.Run(): There was an error in a scheduled event EveryDay: Every 2 min. The error was InvalidOperationException : Sequence contains no elements at System.Linq.Enumerable.Last[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00010] in <92922d9bda2f4e1cba9242d052be6d43>:0 at QuantConnect.Orders.OrderTicket.Update (QuantConnect.Orders.UpdateOrderFields fields) [0x00028] in <325677716eff4fa3b27e747c237dc35e>:0 at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <b0e1ad7573a24fd5a9f2af9595e677e7>:0

Below is the code snippet

#-- Trailing Stop for symbol in self.symbolWithPositionDict: symbolData = self.symbolDict[symbol] symbolPrice = self.Securities[symbol].Close # Trailstop order not yet set if symbolData.trailStopOrderTicket is None: # self.Debug(symbol.Value + " set trailstop") symbolData.trailStopOrderTicket = self.StopMarketOrder(symbol.Value, -self.Portfolio[symbol].Quantity, (1 - self.trailStopPercentage) * symbolPrice) # Trailstop order exist, adjust it if price moves higher else: if symbolPrice > symbolData.lastPeriodMax: symbolData.lastPeriodMax = symbolPrice updateFields = UpdateOrderFields() updateFields.StopPrice = (1 - self.trailStopPercentage) * symbolPrice # self.Debug(symbol.Value + " moving symbolLastPeriodMax up " + str(symbolPrice) + " " + str(updateFields.StopPrice)) symbolData.trailStopOrderTicket.Update(updateFields)

 

Author