Hello,
after hours of searching through google, the forums etc., i'm still unable to figure out how to get historical values for indicators. Could you help me out?
What I am trying to do is to access the ATR values of the past 20 days for each symbol in the universe and calculate the 95% percentile of it to compare it with the actual value.
I think i managed to save data in the rolling window object but when i try to access it, it throws an error for an unrecognized method.
Could you help me to figure out what I am doing wrong?
Thank you for your effort.
Sadly, I am unable to attach a backtest, as the backtest always fails in the first second with the error below.
class ATRMomentumUniverse(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 1, 7)
self.SetEndDate(2020, 4, 1)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
self.atrscan = { }
def CoarseSelectionFunction(self, universe):
selected = []
universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)
universe = [c for c in universe if c.Price > 10][:100]
for coarse in universe:
symbol = coarse.Symbol
if symbol not in self.atrscan:
history = self.History(symbol, 20, Resolution.Daily)
self.atrscan[symbol] = SelectionData(symbol, history)
atr_quantile = self.atrscan[symbol].atr_container[0:15].quantile(0.25)
if self.atrscan[symbol].is_ready() and self.atrscan[symbol].atr_container[0] < atr_quantile:
selected.append(symbol)
return selected[:10]
def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.1)
def OnData(self, data):
for symbol, selectionData in self.atrscan.items():
if data.Bars.ContainsKey(symbol):
selectionData.update(data.Bars[symbol])
if self.Portfolio[symbol].Invested and selectionData.atr.Current.Value > 100:
self.Liquidate(symbol)
class SelectionData():
def __init__(self, symbol, history):
self.symbol = symbol
self.atr = AverageTrueRange(1)
self.atr.Updated += self.container_update
self.atr_container = RollingWindow[IndicatorDataPoint](20)
for bar in history.itertuples():
tbar = TradeBar(bar.Index[1], self.symbol, bar.open, bar.high, bar.low, bar.close, bar.volume)
self.atr.Update(tbar)
def is_ready(self):
return self.atr_container.IsReady
def update(self, bar):
self.atr.Update(bar)
def container_update(self, sender, updated):
self.atr_container.Add(updated)
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the arguments method. Please checkout the API documentation.
at CoarseSelectionFunction in main.py:line 23
:: atr_quantile = self.atrscan[symbol].atr_container[0:15].quantile(0.25)
TypeError : No method matches given arguments (Open Stacktrace)