I am trying to create a universe which filters by relative volume, and I'm using a class - SelectionData - to hold the SMA's for daily volume. However, I run into the error, "no constructor matches given arguments" on the line which instatiates the class -> self.universeAvgVol[symbol] = SelectionData(symbol, 60).
class RelativeVolumeUniverseSelector(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2021, 1, 1)
self.SetCash(100000)
# relative volume universe selection.
self.UniverseSettings.Resolution = Resolution.Minute
self.AddUniverse(self.CoarseSelectionFunction)
self.universeAvgVol = { }
self.symbols = list()
def CoarseSelectionFunction(self, universe):
# calculate the 60-day average volume, then compare with premarket volume to get Relative Volume
selected = []
for coarse in universe:
symbol = coarse.Symbol
# create list of all symbols with 60 day volume sma
if symbol not in self.universeAvgVol:
self.universeAvgVol[symbol] = SelectionData(symbol, 60)
history = self.History(symbol, 60, Resolution.Daily)
self.universeAvgVol[symbol].warmUp(history)
# update symbol volume sma daily
sd = self.univerAvgVol[symbol]
sd.update(self.Time, coarse.Volume)
if sd.is_ready():
selected.append(symbol)
selected.sort(key=lambda x: x.relativeVolume, reverse=True)
self.symbols = selected[:10]
return self.symbols
class SelectionData(object):
def __init__(self, symbol, period):
self.symbol = symbol
self.sma = SimpleMovingAverage(period, Resolution.Daily)
self.premarketVolume = 0
self.relativeVolume = 1
def is_ready(self):
return self.sma.IsReady
def update(self, time, volume):
self.sma.Update(time, volume)
if self.sma.Current.Value != 0:
self.relativeVolume = self.premarketVolume / self.sma.Current.Value
def warmUp(self, history):
for bar in history.itertuples():
self.sma.Update(bar.Index[1], bar.volume)
Shile Wen
Hi Michael,
When creating a Custom Indicator, we shouldn't specify the Resolution, thus, SimpleMovingAverage(period, Resolution.Daily) should be SimpleMovingAverage(period) instead. Furthermore, the self.universeAvgVol is misspelled on line 30. Furthermore, on line 36, we try to access .relativeVolume on a Symbol object, however, we should access this value from the self.universeAvgVol dict. I've shown these fixes in the attached backtest.
Best,
Shile Wen
MichaelRoberts137
Thanks, that fixes the problems. I didn't catch that I was referencing .relativeVolume on a Symbol object.
MichaelRoberts137
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!