I have been practicing making alpha model with shared project in this community.
I would like to add alpha model but I keep having error message.
"Runtime Error: TypeError : iteration over non-sequence TypeError : iteration over non-sequence"
This is "main.py" file.
import pandas as pd
from SpyTrendAlphaModel import SpyTrendAlphaModel
class MultidimensionalTransdimensionalPrism(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 2, 1) # Earliest start date for all ETFs in universe 2/1/10
self.SetEndDate(2020, 5, 6)
self.SetCash(10000)
symbols = [ Symbol.Create("UST", SecurityType.Equity, Market.USA), Symbol.Create("TQQQ", SecurityType.Equity, Market.USA), Symbol.Create("UBT", SecurityType.Equity, Market.USA)]
self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
self.SetAlpha(SpyTrendAlphaModel())
self.SetRiskManagement(CompositeRiskManagementModel(
MaximumUnrealizedProfitPercentPerSecurity(0.2),
MaximumDrawdownPercentPerSecurity(0.3)
))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(NullExecutionModel())
and this is "SpyTrendAlphaModel.py" file.
class SpyTrendAlphaModel(AlphaModel):
def __init__(self):
pass
def OnSecuritiesChanged(self, algorithm, changes):
self.symbols = [x.Symbol for x in changes.AddedSecurities]
def Update(self, algorithm, data):
for x in self.symbols:
history = algorithm.History(x, 7, Resolution.Daily)
price = history["close"]
TF_3 = price.pct_change(3)[-1]
if TF_3 > 0:
return Insight.Price(price.index[0][0], timedelta(1), InsightDirection.Up)
else :
return Insight.Price(price.index[0][0], timedelta(1), InsightDirection.Down)
because of the error I could not attach my backtest.
I would appreciate somebody help me to figure out why I keep getting error msg.
Thanks!
Rahul Chowdhury
Hi Juhwan,
The Update method in the AlphaModel must return a list of insights. The portfolio construction model iterates through this list of insights to create portfolio targets. Let's define an empty list and append the insights we create to that list.
def Update(self, algorithm, data): insights = [] for x in self.symbols: history = algorithm.History(x, 7, Resolution.Daily) price = history["close"] TF_3 = price.pct_change(3)[-1] if TF_3 > 0: insights.append(Insight.Price(price.index[0][0], timedelta(1), InsightDirection.Up)) else : insights.append(Insight.Price(price.index[0][0], timedelta(1), InsightDirection.Down)) return insights
Learn more in the documentation on Alpha Creation and make sure to also check out the boot camps on the framework!
Best
Rahul
Juhwan Kim
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!