So, my algorithm default resolution is minute.
And it has a warm up for suppose 240 minutes
Now, I want to introduce a custom indicator like RSI with Daily resolution but in a custom class like SymbolData.
How can I do this with RSI populated from first day, so I have warm up of 14 days specially for the indicator (algo warm is different duration)
I used rolling window to store the information but not sure how exactly to warm the RSI indicator.
Thank you
Ray
Derek Melchin
Hi Ray,
To warm up the RSI indicator in the SymbolData class, we can make History call to gather the trailing 14 days of pricing data then manually warm up the indicator through its Update method. We can then setup a consolidator to pass our algorithm data at the daily resolution. This is all done in the SymbolData constructor.
def __init__(self, symbol, algorithm, rsi_length): self.symbol = symbol self.algorithm = algorithm self.rsi = RelativeStrengthIndex(rsi_length, MovingAverageType.Simple) # Warm up RSI history = algorithm.History(symbol, rsi_length, Resolution.Daily).loc[symbol] for idx, row in history.iterrows(): self.rsi.Update(idx, row.close) # Setup daily indicator consolidator self.consolidator = TradeBarConsolidator(timedelta(1)) self.consolidator.DataConsolidated += self.CustomDailyHandler algorithm.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)
Here is what the CustomDailyHandler method looks like:
def CustomDailyHandler(self, sender, consolidated): self.rsi.Update(consolidated.Time, consolidated.Close)
To remove the consolidator, we add a helper method, `dispose`.
def dispose(self): self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
The last step is to ensure we only create SymbolData objects and consolidators for securities in our universe. To do so, we define the OnSecuritiesChanged method in our alpha model as
def OnSecuritiesChanged(self, algorithm, changes): for added in changes.AddedSecurities: self.symbol_data_by_symbol[added.Symbol] = SymbolData(added.Symbol, algorithm, self.rsi_length) for removed in changes.RemovedSecurities: symbol_data = self.symbol_data_by_symbol.pop(removed.Symbol, None) if symbol_data: symbol_data.dispose()
See the attached backtest for a full example algorithm. To learn more, I recommend reviewing our documentation on consolidating data.
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.
Ray Trader
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!