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)
Derek Melchin
Hi Pranava,
Please attach a backtest so we can easily reproduce the issue.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Derek Melchin
Hi Pranava,
Refer to this related thread. In the future, please avoid creating duplicate threads.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Pranava
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!