I am trying to create 50-day and 200-day Simple Moving Average indicators for multiple stocks. Does this setup work?
### In Initialize Function
self.tickers = ["TSLA", "FB", "SPY"]
self.sma_slow = {}
self.sma_fast = {}
for ticker in self.tickers:
self.AddEquity(ticker, Resolution.Daily)
self.sma_slow[ticker] = self.SMA(ticker, 200, Resolution.Daily)
self.sma_fast[ticker] = self.SMA(ticker, 50, Resolution.Daily)
My goal is to call the SMA values later in the following form:
### In other function
for ticker in self.tickers:
if self.sma_slow[ticker].Current.Value < self.sma_fast[ticker].Current.Value:
### Do something
Is this method doing what I think it is? The goal is to be able to call the 50-day and 200-day SMA values separately for each ticker. I have seen other examples where people don't index the fast and slow values by ticker, and it still seems to work, but that method is rather confusing.
Another question: How do I add history to my SMA indicators without having to wait 200 days for a warm-up period to finish? I would prefer to be able to run my code and have it decide things without waiting 200 days for the slow SMA to be initialized.
Shile Wen
Hi Chase,
My first suggestion is to use the Symbol objects, which you can get from AddEquity(...).Symbol. this would also mean you would need to create a new list other than tickers for the Symbols. This isn't 100% necessary, however, it is helpful when transitioning the code to a dynamic universe. Furthermore, I'd suggest calling self.SetWarmUp(200) or self.AutomaticIndicatorWarmUp=True to have the indicators ready by the start date. However, please note that you would need to check self.IsWarminUp to make sure you don't have trading during the warm up period. Please see the docs for more information.
Best,
Shile Wen
John J
Did you ever figure this one out? I have the same question
Varad Kabade
Hi John J,
Shile has resolved the original query; please elaborate more on them for further assistance if you have further questions.
Best,
Varad Kabade
Chase
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!