The basis of my algorithm is to compare the lows and the highs of previous bars, i created a deque(revolving list) where i saved this data, so that i can reference them later on. When i tried to retrieve them/ compare them by indexing them, i run into 'IndexError : deque index out of range' ,can someone check out and see, what might be the problem, and offer a solution to this error? 

import numpy as np import pandas as pd from collections import deque class PensiveYellowGreenGoat(QCAlgorithm): highestsymbolprice = 0 lowestsymbolprice = 0 def Initialize(self): self.SetStartDate(2019, 1, 1) # Set Start Date self.SetEndDate(2020, 1, 1) self.SetCash(100000) # Set Strategy Cash # add RUBY stock self.symbol = self.AddEquity("RUBY", Resolution.Daily, Market.USA).Symbol self.bbars = 15 def OnData(self, data): # get current high price, close price and low price of Ruby current_price = self.Securities[self.symbol.Value].Close currenthigh_price = self.Securities[self.symbol.Value].High currentlow_price = self.Securities[self.symbol.Value].Low #revolving list of to hold highs self.dtrhigh = deque(maxlen = self.bbars) #revolving list to hold close price self.dtrclose = deque(maxlen = self.bbars) #revolving list to hold low price self.dtrlow = deque(maxlen = self.bbars) #Adding data to the revolving list self.dtrhigh.append(currenthigh_price) self.dtrclose.append(current_price) self.dtrlow.append(currentlow_price) ### Traling Stop loss if self.Portfolio.Invested: if self.isLong: #if condStopProfit: if current_price > self.highestsymbolprice: self.highestsymbolprice = current_price updateFields = UpdateOrderFields() updateFields.StopPrice = self.highestsymbolprice * 0.98 self.trailingstop = updateFields.StopPrice if current_price <= self.trailingstop: self.Liquidate(self.symbol.Value) self.Log(f"{self.Time} Long Position Trailing Stop Profit at {current_price}") else: if current_price < self.lowestsymbolprice: self.lowestsymbolprice = current_price updateFields = UpdateOrderFields() updateFields.StopPrice = self.lowestsymbolprice * 1.02 self.trailingstop = updateFields.StopPrice if current_price >= self.trailingstop: self.Liquidate(self.symbol.Value) self.Log(f"{self.Time} Short Position Trailing Stop Profit at {current_price}") if not self.Portfolio.Invested: # If the high 3 bars ago is less than the current low, and the current low is less than the previous bar high, and the current bar high is # less than the high 2 bars ago and the previous bar high is less than the high 2 bars ago,buy long at the next market open. if self.dtrhigh[11] < self.dtrlow[14] and self.dtrlow[14] < self.dtrhigh[13] and self.dtrhigh[14] < self.dtrhigh[12] and self.dtrhigh[13] < self.dtrhigh[12]: self.SetHoldings(self.symbol.Value, 1) # get buy-in price for trailing stop loss/profit self.buyInPrice = current_price # entered long position self.isLong = True self.Log(f"{self.Time} Entered Long Position at {current_price}") # If the low 3 bars ago is great than the current high, and the current high is greater than the previous bar low, and the current bar low is # greater than the low 2 bars ago and the previous bar low is greater than the low 2 bars ago,sell short at the next market open. if self.dtrlow[11] > self.dtrhigh[14] and self.dtrhigh[14] > self.dtrlow[13] and self.dtrlow[14] > self.dtrlow[12] and self.dtrlow[13] > self.dtrlow[12]: self.SetHoldings(self.symbol.Value, -1) # get sell-in price for trailing stop loss/profit self.sellInPrice = current_price # entered short position self.isLong = False self.Log(f"{self.Time} Entered Short Position at {current_price}")

 

Author