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}")
Derek Melchin
Hi Samwel,
In the algorithm above, the deques are created in OnData. Since we only ever append 1 element to each deque in OnData, they never grow past the size of 1. To resolve the error, we should initialize the deques in the `Initialize` method. We can then manually warm them up with history to ensure we can start trading immediately. See the attached backtest for reference.
Continuing with the development of this algorithm, consider adding consolidators to update the deques instead of manually updating them during OnData.
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.
Vladimir
Samwel Kibet, Derek Melchin,
I backtested the algorithm above on SPY from 2008, 1, 1 to 2021, 3, 18.
On April 29, 2008, only one trade was created.
Does the algorithm work as intended?
Samwel Kibet
Derek Melchin, Vladimir Thank you for your help.
I saw that, it only took one trade within one year. I would love your input on how i can modify the algorithm, without compromising too much on the strategy, to create many trades. I'm not sure whether my trailing stop works as intended. Or needs some modifications.
Thank you for taking the time, to offer your valuable input.
Vladimir
Samwel Kibet
It looks like your main triggers are firing very rarely.
I would start with the simplest version of the basic idea, and then try
all these complexities in turn and add them only if they improve the results.
Samwel Kibet
Thanks Vladimir, you may have just open up a new perspective for me, that i didn't think was possible, with regards to the algorirthm. I'll keep modifying it and see how i can improve it.
Thanks!
Samwel Kibet
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!