I have been trying to add one of the universe filters from the documentation, but I am having an issue. The error code is that a number is not divisible by zero when creating the volume ratio. I tried changing the values to slightly above zero, but that has not helped. Any advice?
class SelectionData(object):
def __init__(self, symbol, period):
self.symbol = symbol
self.volume = 0.00000001
self.volume_ratio = 0.0000000001
self.sma = SimpleMovingAverage(period)
def update(self, time, price, volume):
self.volume = volume
if self.sma.Update(time, volume):
# get ratio of this volume bar vs previous 10 before it.
self.volume_ratio = volume / self.sma.Current.Value ## this line is the problem
class AddUniverse(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 5, 1)
self.SetEndDate(2022, 5, 1)
self.SetWarmUp(timedelta(days = 20))
self.UniverseSettings.Resolution = Resolution.Minute
self.AddUniverse(self.CoarseFilterFunction)
self.SetCash(100000)
self.SetTimeZone("America/New_York")
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
self.timeCheck = False
self.symbolData = {}
self.stateData = {}
for ticker in self.ActiveSecurities:
symbol = ticker.Symbol
self.symbolData[symbol] = SymbolData(self, symbol)
everyday = self.DateRules.EveryDay()
self.Schedule.On(everyday, self.TimeRules.At(10,00), self.SetEntryPrices)
self.Schedule.On(everyday, self.TimeRules.At(15, 55), self.Liquidate)
self.Schedule.On(everyday, self.TimeRules.At(15, 30), self.timeCheckNight)
self.Schedule.On(everyday, self.TimeRules.At(10,00), self.timeCheckMorning)
self.Schedule.On(everyday, self.TimeRules.Midnight, self.Reset)
def CoarseFilterFunction(self, coarse):
for c in coarse:
if c.Symbol not in self.stateData:
self.stateData[c.Symbol] = SelectionData(c.Symbol, 10)
avg = self.stateData[c.Symbol]
avg.update(c.EndTime, c.AdjustedPrice, c.DollarVolume)
# filter the values of selectionData(sd) above SMA
values = [sd for sd in self.stateData.values() if sd.volume > sd.sma.Current.Value and sd.volume_ratio > 0]
# sort sd by the largest % jump in volume.
values.sort(key=lambda sd: sd.volume_ratio, reverse=True)
# return the top 10 symbol objects
return [ sd.symbol for sd in values[:10] ]
Vladimir
Victoria Butler
Congratulations on your election as QuantConnect Princess.
Here is your algorithm with some modifications and additions. Hope it helps solve the problem.
Victoria Butler
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!