Hi everyone,
I've completed the bootcamp excercises. I'm striving to follow the recommended practices from the very beginning hence using the framework.
The code below is supposed to simply pick top N stocks by descending ROIC.
However at Runtime I receive a TypeError : OnSecuritiesChanged() missing 1 required positional argument: 'changes' TypeError: OnSecuritiesChanged() missing 1 required positional argument: 'changes'. As if OnSecuritiesChanged was called without 'changes' at some point.
What am I doing wrong?
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
from QuantConnect.Data import Fundamental
from datetime import timedelta
class VentralTachyonGearbox(QCAlgorithm):
def Initialize(self):
self.nAssetsInPortfolio = 10
self.SetStartDate(2020, 11, 1)
self.SetCash(100000)
self.SetExecution(ImmediateExecutionModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(
self.CoarseSelectionFilter,
self.FineFundamentalSorter
)
self.AddAlpha(AllUp)
def CoarseSelectionFilter(self, coarse):
filtered = [x.Symbol for x in coarse if x.HasFundamentalData]
return filtered
def FineFundamentalSorter(self, fine):
sortedByROIC = sorted(fine, key=lambda x: x.OperationRatios.ROIC.ThreeMonths, reverse=True)
return [s.Symbol for s in sortedByROIC[:self.nAssetsInPortfolio]]
class AllUp(AlphaModel):
def __init__(self):
self.securities = []
def OnSecuritiesChanged(self, algorithm, changes):
if not changes:
return
for security in changes.AddedSecurities:
self.securities.append(security)
for security in changes.RemovedSecurities:
if symbol in self.securities:
self.secs.remove(security)
def Update(self, algorithm, data):
return Insight.Group(
[Insight.Price(x.Symbol, timedelta(1), InsightDirection.Up) for x in self.securities]
)
Arthur Asenheimer
Hi,
the error message is misleading since this has nothing to do with the arguments in OnSecuritiesChanged().
This error occurs because of the missing paranthesis when you add your AlphaModel.
self.AddAlpha(AllUp) # wrong
self.AddAlpha(AllUp()) # correct
And one more remark: You don't need
if not changes:
return
since OnSecuritiesChanged() is by default only called when there were changes in Universe.
Nanofon
Ah, so it takes an instance of object, not class itself. Thank you very much.
Nanofon
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!