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?

  1. """
  2. This algorithm will find 20 stocks with highest momentum so far at 9:00am to give market time to move.
  3. I will buy these stocks if the SPY is positive for the day. It will sell stocks at 11:45am because
  4. this is when its lunch time and trading dies down.
  5. """
  6. class CalmMagentaBat(QCAlgorithm):
  7. def Initialize(self):
  8. self.SetStartDate(2018, 1, 1)
  9. self.SetStartDate(2021, 1, 1)
  10. self.SetCash(100000)
  11. self.UniverseSettings.Resolution = Resolution.Minute
  12. self.UniverseSettings.Leverage = 2
  13. self.stateData = {}
  14. self.AddUniverse(self.MyCoarseFilterFunction)
  15. def MyCoarseFilterFunction(self, coarse):
  16. for c in coarse:
  17. if c.Symbol not in self.stateData:
  18. self.stateData[c.Symbol] = SelectionData(c.Symbol, 30)
  19. values = [x for x in self.stateData.values()]
  20. values.sort(key=lambda x: x.volatility, reverse=True)
  21. return [x.symbol for x in values[:20]]
  22. # this event fires whenever we have changes to our universe
  23. def OnSecuritiesChanged(self, changes):
  24. # liquidate removed securities
  25. for security in changes.RemovedSecurities:
  26. if security.Invested:
  27. self.Liquidate(security.Symbol)
  28. # we want 10% allocation in each security in our universe
  29. for security in changes.AddedSecurities:
  30. self.SetHoldings(security.Symbol, 0.05)
  31. def OnData(self, data):
  32. pass
  33. class SelectionData(object):
  34. def __init__(self, symbol, period):
  35. self.symbol = symbol
  36. self.atr = self.ATR(self.symbol, period, Resolution.Minute)
  37. self.volatility = (self.atr.Current.Value)/(self.Securities[self.symbol].Price)
  38. def update(self, time, price):
  39. pass
+ Expand

Author

Arsal Khan

October 2021