I’m having some trouble understanding how to initialise, warmup and update a manual indicator. I made it work with the help of the documents, but I’m not entirely sure I understand how it works.
So, I have a dynamic universe and inside my AlphaModel in the OnSecuritiesChanged event handler I create a dictionary to save the security’s symbol and a reference to the SymbolData class. (Like the docs specify)
https://www.quantconnect.com/docs/v2/writing-algorithms/universes/key-concepts
Inside the SymbolData (nicely copied from the docs) it initialises an SMA indicator, it creates a consolidator which is used to Update the indicator and it warms up the indicator.
https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/key-concepts
class SymbolData:
def __init__(self, algorithm, symbol):
self.algorithm = algorithm
self.symbol = symbol
# Create an indicator
self.sma = SimpleMovingAverage(20)
# Create a consolidator to update the indicator
self.consolidator = TradeBarConsolidator(1)
self.consolidator.DataConsolidated += self.OnDataConsolidated
# Register the consolidator to update the indicator
algorithm.SubscriptionManager.AddConsolidator(symbol, self.consolidator)
# Warm up the indicator
algorithm.WarmUpIndicator(symbol, self.sma)
def OnDataConsolidated(self, sender: object, consolidated_bar: TradeBar) -> None:
self.sma.Update(consolidated_bar.EndTime, consolidated_bar.Close)
# If you have a dynamic universe, remove consolidators for the securities removed from the universe
def dispose(self) -> None:
self.algorithm.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
Question #1: is this consolidator added for someone that wants to use a custom time to feed their indicator? Now the value of the TradeBarConsolidator is set to 1, so basically it is just created a consolidator which is redundant?
I removed the code of the consolidator and updated my SMA indicator after the initialisation line with CurrentSlice and the value I got in the backtest were the same. So, I assume I inferred the above correctly?
Question#2: the SymbolData class returns a SymbolData object so to access the indicator’s value I added a function GetIndicatorValue to the SymbolData class which I call from inside my Update() method in my AlphaModel class.
Is this the correct (or most efficient way) of accessing the indicator values?
Thanks to anyone that can bring some clarity!
Stany
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!