I am receiving the following error upon running my algo (below). The algo is a modified RSI crossover with a trailing stop. Any ideas why this error is coming up? The error refers to the first instance of             self.stopMarketTicket.Update(updateFields). Thanks.
 

Runtime Error: InvalidOperationException : Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
at QuantConnect.Orders.OrderTicket.Update(UpdateOrderFields fields) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Orders/OrderTicket.cs:line 279 
at OnData
self.stopMarketTicket.Update(updateFields)
===
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 64 (Open Stacktrace)

  1. import numpy as np
  2. class BasicTemplateAlgorithm(QCAlgorithm):
  3. def __init__(self):
  4. self.stopMarketTicket = None
  5. self.stopMarketOrderFillTime = datetime.min
  6. self.stopPrice = 0
  7. self.risk = 0.95
  8. def Initialize(self):
  9. self.cash = 100000
  10. self.stock = "TQQQ"
  11. self.SetStartDate(2021,1, 1)
  12. self.SetEndDate(2021,11, 12)
  13. self.SetCash(self.cash)
  14. self.AddEquity(self.stock, Resolution.Hour)
  15. #We add our RSI 14 period indicator
  16. self.rsi = self.RSI(self.stock, 14)
  17. self.lastrsi = None
  18. self.SetWarmUp(14)
  19. def OnData(self, data):
  20. if not self.rsi.IsReady:
  21. return
  22. if self.lastrsi is None:
  23. self.lastrsi = self.rsi.Current.Value
  24. return
  25. #If we cross oversold threshold from below
  26. if self.lastrsi < 30 and self.rsi.Current.Value > 30:
  27. #if we are not currently in a trade
  28. if not self.Portfolio[self.stock].Invested:
  29. #we place a long market order
  30. self.SetHoldings(self.stock, 1.0)
  31. self.stopMarketTicket = self.StopMarketOrder(self.stock, -self.Portfolio[self.stock].Quantity, self.risk * self.Securities[self.stock].Close)
  32. #If RSI is oversold while we are short
  33. elif self.rsi.Current.Value < 30 and self.Portfolio[self.stock].IsShort:
  34. # if we are already in a short trade we liquidate
  35. self.Liquidate()
  36. #if RSI signals overbought
  37. if self.lastrsi > 70 and self.rsi.Current.Value < 70:
  38. if not self.Portfolio[self.stock].Invested:
  39. #enter short position
  40. self.SetHoldings(self.stock, -1)
  41. self.stopMarketTicket = self.StopMarketOrder(self.stock, self.Portfolio[self.stock].Quantity, self.risk * self.Securities[self.stock].Close)
  42. #if RSI is overbought while we are long
  43. elif self.rsi.Current.Value > 70 and self.Portfolio[self.stock].IsLong:
  44. #if we already in a long trade we liquidate
  45. self.Liquidate()
  46. if self.Portfolio[self.stock].IsLong and self.Securities[self.stock].Close > self.stopPrice:
  47. self.stopPrice = self.Securities[self.stock].Close
  48. updateFields = UpdateOrderFields()
  49. updateFields.StopPrice = self.stopPrice * self.risk
  50. self.stopMarketTicket.Update(updateFields)
  51. elif self.Portfolio[self.stock].IsShort and self.Securities[self.stock].Close < self.stopPrice:
  52. self.stopPrice = self.Securities[self.stock].Close
  53. updateFields = UpdateOrderFields()
  54. updateFields.StopPrice = self.stopPrice * self.risk
  55. self.stopMarketTicket.Update(updateFields)
  56. #store current RSI value to use later
  57. self.lastrsi = self.rsi.Current.Value
  58. def OnOrderEvent(self, orderEvent):
  59. if orderEvent.Status != OrderStatus.Filled: return
  60. if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == orderEvent.OrderId:
  61. self.stopMarketOrderFillTime = self.Time
+ Expand

 

 

Author

James Hawkins

November 2021