ATR indicator (python)


I am trying to get the ATR value before the open for each ETF in the symbols list.

but the line below is not working. Any idea ?

self.atr = self.ATR([i], 14)


class BasicTemplateAlgorithm(QCAlgorithm):
def __init__(self):
self.symbols = ['XLY','XLP','XLE','XLF','XLV','XLI','XLB','XLK','XLU'] # all sector

def Initialize(self):
self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
# Before the open
self.Schedule.On(self.DateRules.EveryDay(self.spy), \
self.TimeRules.AfterMarketOpen(self.spy, -5), \

for i in range(len(self.symbols)):
symbol = self.AddEquity(self.symbols[i], Resolution.Minute).Symbol
self.symbols[i] = symbol

def beforeTheOpen(self):
for i in self.symbols:
self.atr = self.ATR([i], 14)
self.Log("ATR: {0}".format(self.atr))

def OnData(self, slice):

Thank you.


Update Backtest

You should use self.atr.Current.Value to get the current updated indicator value


I tried but I am still getting this error:

Runtime Error: Python.Runtime.PythonException: TypeError : No method matches given arguments

self.atr = self.ATR([i], 14) should be self.atr = self.ATR(i, 14).

You need to put self.ATR in the initialize() method and use  self.atr.Current.Value to get value. You don't need __init__ here, all initialization steps can be realized in Initialize(self). Please search other related threads on the forum for common questions, they are very helpful.


Thank you very much Jingw.


Update Backtest


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.


This discussion is closed