I would like to know how to actually weight a portfolio with the symbols returned by a coarse universe selection.
I am able to perform a coarse universe selection and can see that the securities are visible in self.ActiveSecurities. However when I attempt to use the self.SetHoldings function to weight the portfolio using the securities which are within self.ActiveSecurities I receive this error:
"Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the SetHoldings method. Please checkout the API documentation.
at OnData in main.py:line 34
TypeError : No method matches given arguments for SetHoldings (Open Stacktrace)"
My code is attached..
class MultidimensionalTachyonChamber(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2010,1,1)
self.SetEndDate(2010,2,2)
self.SetCash(100000)
self.SetUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction))
self.UniverseSettings.Resolution = Resolution.Daily
self.NumSymbols = 5
def CoarseSelectionFunction(self, coarse):
sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)
return [ x.Symbol for x in sortedByDollarVolume[:self.NumSymbols] ]
def OnSecuritiesChanged(self, changes):
self.changes = changes
def OnData(self, data):
for s in self.changes.RemovedSecurities:
if s.Invested:
self.Liquidate(s.Symbol)
for s in self.ActiveSecurities:
self.SetHoldings(s, 1/self.NumSymbols)
Alethea Lin
Hi Kieran,
self.ActiveSecurities.Keys returns a list of the ticker string for selected securities, while self.ActiveSecurities is a dictionary with the ticker string as keys and corresponding Symbol objects as values. Change your code to below should get rid of the error.
for s in self.ActiveSecurities.Keys: self.SetHoldings(s, 1/self.NumSymbols)
Hope this helps!
Kieran Flynn
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!