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)
import numpy as np
class BasicTemplateAlgorithm(QCAlgorithm):
def __init__(self):
self.stopMarketTicket = None
self.stopMarketOrderFillTime = datetime.min
self.stopPrice = 0
self.risk = 0.95
def Initialize(self):
self.cash = 100000
self.stock = "TQQQ"
self.SetStartDate(2021,1, 1)
self.SetEndDate(2021,11, 12)
self.SetCash(self.cash)
self.AddEquity(self.stock, Resolution.Hour)
#We add our RSI 14 period indicator
self.rsi = self.RSI(self.stock, 14)
self.lastrsi = None
self.SetWarmUp(14)
def OnData(self, data):
if not self.rsi.IsReady:
return
if self.lastrsi is None:
self.lastrsi = self.rsi.Current.Value
return
#If we cross oversold threshold from below
if self.lastrsi < 30 and self.rsi.Current.Value > 30:
#if we are not currently in a trade
if not self.Portfolio[self.stock].Invested:
#we place a long market order
self.SetHoldings(self.stock, 1.0)
self.stopMarketTicket = self.StopMarketOrder(self.stock, -self.Portfolio[self.stock].Quantity, self.risk * self.Securities[self.stock].Close)
#If RSI is oversold while we are short
elif self.rsi.Current.Value < 30 and self.Portfolio[self.stock].IsShort:
# if we are already in a short trade we liquidate
self.Liquidate()
#if RSI signals overbought
if self.lastrsi > 70 and self.rsi.Current.Value < 70:
if not self.Portfolio[self.stock].Invested:
#enter short position
self.SetHoldings(self.stock, -1)
self.stopMarketTicket = self.StopMarketOrder(self.stock, self.Portfolio[self.stock].Quantity, self.risk * self.Securities[self.stock].Close)
#if RSI is overbought while we are long
elif self.rsi.Current.Value > 70 and self.Portfolio[self.stock].IsLong:
#if we already in a long trade we liquidate
self.Liquidate()
if self.Portfolio[self.stock].IsLong and self.Securities[self.stock].Close > self.stopPrice:
self.stopPrice = self.Securities[self.stock].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.stopPrice * self.risk
self.stopMarketTicket.Update(updateFields)
elif self.Portfolio[self.stock].IsShort and self.Securities[self.stock].Close < self.stopPrice:
self.stopPrice = self.Securities[self.stock].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.stopPrice * self.risk
self.stopMarketTicket.Update(updateFields)
#store current RSI value to use later
self.lastrsi = self.rsi.Current.Value
def OnOrderEvent(self, orderEvent):
if orderEvent.Status != OrderStatus.Filled: return
if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == orderEvent.OrderId:
self.stopMarketOrderFillTime = self.Time
Alexandre Catarino
Hi James Hawkins ,
We have debugged your algorithm, and we have found that it's trying to update stop market orders that are invalid. If you add the following if-conditions, the issue is solved:
It would be interesting to investigate further why these orders are invalid.
Best regards,
Alex
James Hawkins
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!