Hello Everyone,
I am trying to create a momentum strategy comparing equal eighted momentum indicators between SPY,EEM and TLT. The plan is to modify the weights based on overall market strategy and create a momentum strategy that becomes aggressive/neutral/defensive based on rate of change of vix.
I do not have any technical background and whatever I have coded is by learning through Udemy/Youtube tutorials. I will deeply appreciate if someone can point out what I am doing wrong in the attached code.
ass MomentumBasedTacticalAllocation(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2007, 1,1)
self.SetEndDate(2020,12,1)
self.SetCash(3000)
self.spy = self.AddEquity("SPY", Resolution.Daily)
self.bnd = self.AddEquity("TLT", Resolution.Daily)
self.eem = self.AddEquity("EEM",Resolution.Daily)
self.spyMomentum30 = self.MOMP("SPY", 30, Resolution.Daily)
self.spyMomentum60 = self.MOMP("SPY", 60, Resolution.Daily)
self.spyMomentum90 = self.MOMP("SPY", 90, Resolution.Daily)
self.spyaverage = (self.spyMomentum30()+self.spyMomentum60()+self.spyMomentum90())/3
self.bondMomentum30 = self.MOMP("BND", 30, Resolution.Daily)
self.bondMomentum60 = self.MOMP("BND", 60, Resolution.Daily)
self.bondMomentum90 = self.MOMP("BND", 90, Resolution.Daily)
self.bondavg = (self.bondMomentum30()+self.bondMomentum60()+self.bondMomentum90())/3
self.eemMomentum30 = self.MOMP("EEM", 30, Resolution.Daily)
self.eemMomentum60 = self.MOMP("EEM", 60, Resolution.Daily)
self.eemMomentum90 = self.MOMP("EEM", 90, Resolution.Daily)
self.eemavg = (self.eemMomentum30()+self.eemMomentum60()+self.eemMomentum90())/3
self.SetBenchmark(self.spy.Symbol)
self.SetWarmUp(90)
def OnData(self, data):
if self.IsWarmingUp:
return
#1. Limit trading to happen once per month
if not self.Time.month() == 1:
return
if self.spyavg.Current.Value > self.eemavg.Current.Value > self.bndavg.Current.Value:
self.Liquidate("TLT")
self.Liquidate("EEM")
self.SetHoldings("SPY", 1)
else:
if self.eemavg.Current.Value > self.spyavg.Current.Value > self.bndavg.Current.Value:
self.Liquidate("TLT")
self.Liquidate("EEM")
self.SetHoldings("SPY", 1)
else:
self.Liquidate("SPY")
self.Liquidate("EEM")
self.SetHoldings("TLT", 1)
Hector Barrio
Hello Jayvardhan,
maybe the attached backtest captures your intent. In general, you have to capture the current values of indicators when they are updated, in the update method, these are class objects that cannot be called with self.eemMomentum30() (the parenthesis basically).
For the timing, the same comment, self.Time.month cannot be called with () it is already a datetime object. The problem is it would only act when the month is 1, Janurary only, you need to check it against the last date the algorithm went through the action script.
I hope this helps you in progressing with coding.
Jayvardhan Sharma
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!