Hi there,
I've been trying to create a universe based on bollinger bands and keltner channels. I've followed the example here, with no success. My code is returning the error ‘object not set to an instance of a class.’ I have no idea why the algorithm is breaking at the history request. Can anyone help?
class TheSqueezeUniverseSelection(QCAlgorithm):
def __init__(self, algorithm, period = 20):
self.algorithm = algorithm
self.period = period
self.indicators = {}
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.indicators:
# 1. Call history to get an array of 200 days of history data
history = self.History(symbol, 200, Resolution.Daily)
#2. Adjust SelectionData to pass in the history result
self.indicators[symbol] = SelectionData(history, self.period)
self.indicators[symbol].update(self.Time, coarse.AdjustedPrice)
if self.indicators[symbol].is_ready() and \
indicators.BollingerUpper < indicators.KelterUpper and \
indicators.BollingerLower > indicators.KelterLower:
selected.append(symbol)
return selected[:10]
class SelectionData():
#3. Update the constructor to accept a history array
def __init__(self, history, period):
self.bollinger = BollingerBands(period, 2, MovingAverageType.Simple)
self.keltner = KeltnerChannels(period, 1.5, MovingAverageType.Simple)
#4. Loop over the history data and update the indicatorsc
for bar in history.itertuples():
tradeBar = TradeBar(bar.Index[1], bar.Index[0], bar.open, bar.high, bar.low, bar.close, bar.volume, timedelta(1))
self.bollinger.Update(bar.Index[1], bar.close)
self.keltner.Update(tradeBar)
@property
def BollingerUpper(self):
return float(self.bollinger.UpperBand.Current.Value)
@property
def BollingerLower(self):
return float(self.bollinger.LowerBand.Current.Value)
@property
def KeltnerUpper(self):
return float(self.keltner.UpperBand.Current.Value)
@property
def KeltnerLower(self):
return float(self.keltner.LowerBand.Current.Value)
def is_ready(self):
return self.bollinger.IsReady and self.keltner.IsReady
def update(self, time, value):
return self.bollinger.Update(time, value)
Nicholas Fitzgerald
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!