Hi! Could someone please help me figure out what in my code is causing this error? The purpose of this algorithm is to help me get acquanted with universes and how to use/implement them. I have attached my code so that you can see it. Thanks in advance!
class LiquidUniverseSelection(QCAlgorithm):
filteredByPrice = None
def Initialize(self):
self.SetStartDate(2019, 1, 11)
self.SetEndDate(2019, 1, 20)
self.SetCash(100000)
self.stateData = { }
self.AddUniverse(self.CoarseSelectionFilter)
# Ignore this for now, we'll cover it in the next task.
self.UniverseSettings.Resolution = Resolution.Daily
def CoarseSelectionFilter(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
filteredByPrice = [x for x in sortedByDollarVolume if x.Price > 1 and x.Price < 100]
for c in filteredByPrice:
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)
self.values = [sd for sd in self.stateData.values() if sd.sma.Current.Value > 3500000]
return self.values[:8]
def OnSecuritiesChanged(self, changes):
self.changes = changes
self.Log(f"OnSecuritiesChanged({self.UtcTime}):: {changes}")
#1. Liquidate removed securities
for security in changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
#2. We want 10% allocation in each security in our universe
for security in changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.1)
class SelectionData(object):
def __init__(self, symbol, period):
self.symbol = symbol
self.volume = 0
self.sma = SimpleMovingAverage(period)
#self.vol_greater = False
def update(self, time, price, volume):
self.volume = volume
self.sma.Update(time, volume)
#self.vol_greater = self.sma > 3500000
Derek Melchin
Hi Cole,
The error is caused because the universe selection method is returning a list of SelectionData objects instead of Symbols. To resolve this, replace
self.values = [sd for sd in self.stateData.values() if sd.sma.Current.Value > 3500000]
with
self.values = [symbol for symbol, sd in self.stateData.items() if sd.sma.Current.Value > 3500000]
See the attached backtest for reference.
Best,
Derek Melchin
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.
Cole Kuster
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!