Hello,
I am building an alpha model (Andreas Clenow momentum) that requires a condition using the indicator SPY 200 SMA to generate insights -> in other words, use SPY with 200 SMA as a benchmark to determine the bull or bear state of the market.
Since SPY is not in my Universe Selection, is there a way to manually register for SPY data and warmup the indicator in my alpha ?
Another solution would be to implement this condition directly in the UniverseSelection, but it would defy the purpose of the framework.
Etienne RAINAUT
After a bit of research, this is working for me
class ClenowMomentumAlphaModel(AlphaModel): def __init__(self, resolution = Resolution.Daily): self.referenceSMAperiod = 200 def Update(self, algorithm, data): insights = [] # Weekly update of insights every Wednesday if algorithm.Time.isoweekday() != 3: return insights else: SPYSymbol = algorithm.AddEquity(self.referenceTicker, Resolution.Daily) referenceHistory = algorithm.History([self.referenceTicker], self.referenceSMAperiod, Resolution.Daily) referenceSMA = algorithm.SMA(self.referenceTicker, self.referenceSMAperiod, Resolution.Daily) referencePrice = None if not referenceHistory.empty: for tuple in referenceHistory.loc[self.referenceTicker].itertuples(): referenceSMA.Update(tuple.Index, tuple.close) referencePrice = tuple.close if referencePrice < referenceSMA.Current.Value: return []
Shile Wen
Hi Etienne,
I suggest moving the AddEquity logic to the init function so that we only need to call it once. Furthermore, if you use AddEquity in init, you should also have self.spySMA = self.SMA("SPY", 200, Resolution.Daily) in init as well and use self.spySMA in SelectFine.
Best,
Shile Wen
Etienne RAINAUT
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!