Hi, I'm pretty new to all this so here's my code. I'm trying to get the top 20 stocks with highest volatility. I get volatility by ATR/price. I keep getting this error:
Runtime Error: AttributeError : 'SelectionData' object has no attribute 'ATR' at __init__ self.atr = self.ATR(self.symbol in main.py: line 48
My code's below, I was wondering about how I would go about correcting this?
"""
This algorithm will find 20 stocks with highest momentum so far at 9:00am to give market time to move.
I will buy these stocks if the SPY is positive for the day. It will sell stocks at 11:45am because
this is when its lunch time and trading dies down.
"""
class CalmMagentaBat(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1)
self.SetStartDate(2021, 1, 1)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Minute
self.UniverseSettings.Leverage = 2
self.stateData = {}
self.AddUniverse(self.MyCoarseFilterFunction)
def MyCoarseFilterFunction(self, coarse):
for c in coarse:
if c.Symbol not in self.stateData:
self.stateData[c.Symbol] = SelectionData(c.Symbol, 30)
values = [x for x in self.stateData.values()]
values.sort(key=lambda x: x.volatility, reverse=True)
return [x.symbol for x in values[:20]]
# this event fires whenever we have changes to our universe
def OnSecuritiesChanged(self, changes):
# liquidate removed securities
for security in changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
# we want 10% allocation in each security in our universe
for security in changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.05)
def OnData(self, data):
pass
class SelectionData(object):
def __init__(self, symbol, period):
self.symbol = symbol
self.atr = self.ATR(self.symbol, period, Resolution.Minute)
self.volatility = (self.atr.Current.Value)/(self.Securities[self.symbol].Price)
def update(self, time, price):
pass
Varad Kabade
Hi Arsal Khan,
The indicator helper methods are part of the QCAlgorithm class; to use them in our SelectionData class, we need to pass the algorithm instance inside while initializing the SelectionData object and modify the Selection Data class:
Note that as we are creating the indicators before the underlying security is added to the universe so the indicator will not be ready. We recommend using the constructor to initialize the indicator and updating it with historical data.
Best,
Varad Kabade
Arsal Khan
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!