Back

Help appending once a day

I'm trying to append the daily range to a list everyday I can't figure it out. I need this for my custom ATR indicator that analyzes the list and gives an atr value. if someone could help me fix my code you'd save from the final stages of insanity

class CalibratedDynamicInterceptor(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2020, 6, 1)
self.SetCash(100000)
self.atrLength = 14
self.trList = []

self.AddEquity("SPY", Resolution.Minute)
history = self.History(["SPY"], 2, Resolution.Daily)

self.currentSymbolData = history

self.yesterdayhigh = (self.currentSymbolData["high"][0])
self.yesterdaylow = (self.currentSymbolData["low"][0])
self.tr = abs(self.yesterdayhigh - self.yesterdaylow)

self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(18, 1), self.newTR)

def newTR(self):
self.trList.append(self.tr)

def GetATRW(self):
self.trLen = 0
self.atrV = 5
self.old = 0
self.newer = 1
self.new = 2
self.adjList = []
self.WTRS = 0
for i in self.trList:
self.trLen += 1
if self.trLen > 3:
for num in self.trList:
self.WTRS += num
self.atrV = self.WTRS / self.trLen
elif self.trLen <= 3 and self.trLen < self.atrLength:
while self.new < self.trLen:
self.newNum = (self.trList[old]*.2) + (self.trList[newer]*.3) + (self.trList[new]*.5)
self.adjList.append(newNum)
self.old += 1
self.newer += 1
self.new += 1
for numbers in self.adjList:
self.WTRS += numbers
self.atrV = self.WTRS / (self.trLen - 2)
elif self.trLen < self.atrLength or self.trLen == self.atrLength:
while self.trLen > self.atrLength:
self.trList.pop(0)
self.trLen -= 1
while self.new < self.atrLength:
self.newNum = (self.trList[old]*.2) + (self.trList[newer]*.3) + (self.trList[new]*.5)
self.adjList.append(newNum)
self.old += 1
self.newer += 1
self.new += 1
for numbers in self.adjList:
self.WTRS += numbers
self.atrV = self.WTRS / (self.atrLength - 2)
return self.atrV

# self.SetWarmUp(timedelta(days=self.atrLength))


def OnData(self, data):
# if self.IsWarmingUp: return
self.atr = self.GetATRW()
self.Debug("wtrs " + str(self.WTRS) + " " + "trLen " + str(self.trLen))
self.Debug("ATR: " + str(self.atr))
self.Debug("trList: " + str(self.trList))
self.Debug("yesHigh: " + str(self.yesterdayhigh))
self.Debug("yesHigh: " + str(self.yesterdaylow))

 

Update Backtest







0

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.


Hi Michael,

I suggest using our built-in AverageTrueRange class and updating it with the current bar with self.CurrentSlice[symbol]. I’ve shown how to do this in the attached backtest.

Best,
Shile Wen

0

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.


Thank you Shile Wen for your respose. The reason I made a custom ATR indicator vs using the built in one is, I need it to use a weighted average. And when I try and use that weighted I get the error saying thats not an availible type. Is there a way to set the built in ATR to Weighted?

0

Hi Michael,

I am unsure what kind of weighted moving average you’d like, but I’ve reimplemented the strategy with EMA in the attached backtest. For a full list of moving average types, please see these docs.

Best,
Shile Wen

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed