Hey Guys!
I have been stuck on this for a couple days now and if anyone has any insight it would be hugely appreciated. Long story short I want to check the status of the 200day EMA vs the 50day EMA in order to determine if I want to enter a trade intraday. I currently have the add equity set to Resolution.Minute but want to set the EMA's to Resolution.Daily and then have the SetWarmup to 200days. Here is my current code
class WarmupAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
# Select ticker and amount of contracts
self.ticker = "SPY"
self.contracts = 100
self.SetStartDate(2019,1,1) #Set Start Date
self.SetEndDate(2019,1,30) #Set End Date
self.SetCash(100000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
spy = self.AddEquity(self.ticker, Resolution.Minute)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
# MA Periods
fast_period = 50
slow_period = 200
self.fast = self.EMA(self.ticker, fast_period, Resolution.Daily)
self.slow = self.EMA(self.ticker, slow_period, Resolution.Daily)
# Set the warm up period to the length of the slow period MA
self.SetWarmup(timedelta(slow_period))
def OnData(self, data):
# Plot the values of the various indicators
self.Plot("EMAfast", "Value", self.fast.Current.Value)
self.Plot("EMAslow", "Value", self.slow.Current.Value)
# Warmup starts as True and once Warmup is complete goes to false which lets the algo run
if self.IsWarmingUp:
return
if self.fast.Current.Value > self.slow.Current.Value:
self.SetHoldings(self.ticker, 1)
else:
self.SetHoldings(self.ticker, -1)
If I remove the Resolution.Daily from the EMA's:
self.fast = self.EMA(self.ticker, fast_period)
Then everything works fine but the EMA will be based on a Minute period as opposed to the Daily period that I am looking for.
I have looked into self.History(symbol, 200, Resolution.Daily) to try add data prior to my intended start date but again couldn't get it to work. How do people typically pull in the required data for the indicators so that they are ready to run by the desired start date?
Any help is hugely appreciate and I apologize for the super simple question.
Seb
Sebastian Bunney
I forgot to add the error message I get when I try to debug the issue and ask for the value of self.slow I get "*** AttributeError: 'WarmupAlgorithm' object has no attirbute 'slow'" and then when I ask for the value of self.fast it just says 0. If I remove the Resolution.Daily from the EMA's then everything works great and I have no issues.
Any help at all means a lot.
Derek Melchin
Hi Sebastian,
The issue here is that the algorithm wasn't warming up the indicators properly. In this situation, instead of calling
self.SetWarmup(timedelta(slow_period))
we should use
self.SetWarmup(slow_period, Resolution.Daily)
Refer to our SetWarmup method documentation to understand the difference.
Additionally, to get our EMAfast plot to line up with the equity curve along the x-axis, we should move the plotting code in OnData below the `IsWarmingUp` guard. See the attached algorithm for a working solution.
Best,
Derek Melchin
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.
Sebastian Bunney
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!