Hello, I am creating a new algorithm that uses the MACD indicator, but I would like to use Energy ETF Basket Universe Module from the algorithm creator through "self.SetUniverseSelection(VolatilityETFUniverse())" in the Initialize method.
How do I get the indicator to get the MACD values on each individual stock in the Universe, and then how do I reference that in OnData? Here is a sample:
def Initialize(self):
self.SetStartDate(2021, 1, 1)
self.SetCash(1000000)
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.05))
self.UniverseSettings.Resolution = Resolution.Minute
self.SetUniverseSelection(VolatilityETFUniverse())
self.mac = self.MACD(*symbols*, 12, 26, 9, MovingAverageType.Exponential, Resolution.Minute)
def OnData(self, data):
if self.mac.Current.Value < 1:
self.SetHoldings(*symbols*, .1)
What exactly do I have to put in place of *symbols* ? Do I have to do something like create a variable and set it equal to the Universe, then reference it with variable.Symbol?
Thanks for the help
Derek Melchin
Hi Ethan and Jackson,
To setup an indicator on each security in a dynamic universe, we should:
Refer to this Strategy Library tutorial for an example.
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.
Ethan Wertlieb
Derek,
Thank you for the links and tutorials! I am sure they will be handy. Athough, of the examples you gave me seem to be specified for custom indicators that need to be updated to work. For indicators such as self.MACD, it says they update automatically. How exactly could I get the self.MACD or any automatically updating indicator to accept a universe of symbols instead of just one and perform it's calculations on every symbol on that collection/universe, making trades accordingly? Do I just follow all the steps from the examples you've given but ignore anything that has to do with updating? Or maybe for something like that will I have to manually create the MACD indicator so I can copy the format of the examples?
Derek Melchin
Hi Ethan,
We need to apply a new instance of an indicator for each symbol in a universe. It's not possible to apply one indicator to multiple symbols.
With a dynamic universe, we can't use the shortcut methods (self.MACD) when creating indicators for the securities. Instead, we must use the full class name (MovingAverageConvergenceDivergene) and keep it updated with a consolidator. This is because we can't unsubscribe the indicator created with the shortcut method when the security it's applied to is removed from the universe. As a result, we recommend following the format of the examples listed above.
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.
Ethan Wertlieb
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!