I am Zheng Tian. I am a beginner on this platform. I am trying to merge both the EMA and MACD together. However, I keep on facing this issue called During the algorithm initialization, the following exception has occurred: NotImplementedException: Please override the Initialize() method.
Hence, I would like to know how to ratify this issue. Thanks for your help.
Jared Broad
Please post your attempt to be respectful of other people's time:
https://www.quantconnect.com/codeofconduct#soliciting-assistanceThe 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.
Vladimir
Financial Freedom with Zheng Tian,
It is not clear from your question that you need to create an EMA using the exponential moving average type inside the EMA indicator, or you want to create an EMA of MACD, but both of them
are available in QuantConnect.
Create an EMA using the Exponential MovingAverage Type:
self.macd=self.MACD("SPY",MA_FAST,MA_SLOW,MA_SIGNAL, MovingAverageType.Exponential, Resolution.Daily)
Create an EMA of MACD:
self.macd_ema = IndicatorExtensions.EMA(self.macd, MA_SIGNAL)
BTW QC MACD is multioutput indicator and it already has macd_ema called Signal
macd_signal = self.macd.Signal.Current.Value
and you can see from the chart it's absolutely the same value as
macd_signal_2 = self.macd_ema.Current.Value
Hope this will help.
Vladimir
Financial Freedom with Zheng Tian,
There was a typo in my previous post .
Here is a correct one.
It is not clear from your question that you need to create an EMA using the exponential moving
average type inside the MACD indicator, or you want to create an EMA of MACD, but both of them are available in QuantConnect.
Create an MACD using the Exponential MovingAverage Type:
self.macd = self.MACD("SPY", MA_FAST, MA_SLOW, MA_SIGNAL, MovingAverageType.Exponential, Resolution.Daily)
Create an EMA of MACD:
self.macd_ema = IndicatorExtensions.EMA(self.macd, MA_SIGNAL)
BTW QC MACD is multioutput indicator and it already has macd_ema called Signal
macd_signal = self.macd.Signal.Current.Value
and you can see from the chart it's absolutely the same value as
macd_signal_2 = self.macd_ema.Current.Value
Hope this will help.
Financial Freedom with Zheng Tian
I am sorry that I did not make myself very clear as I am extremely new to this product. I will like to know how to scan the stock market by using EMA before purchasing stocks in the companies based on MACD.
Runtime Error: AttributeError : 'WellDressedYellowGreenFish' object has no attribute '_WellDressedYellowGreenFish__macd' at OnSecuritiesChanged if not self.__macd.IsReady:from Alphas.MacdAlphaModel import MacdAlphaModel class WellDressedYellowGreenFish(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 1, 1) # Set Start Date self.SetCash(10000) # Set Strategy Cash self.AddAlpha(MacdAlphaModel(12, 26, 9, MovingAverageType.Simple, Resolution.Daily)) self.AddUniverse(self.CoarseSelectionFunction) self.UniverseSettings.Resolution = Resolution.Daily self.averages = { } def CoarseSelectionFunction(self, universe): selected = [] universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True) universe = [c for c in universe if c.Price > 5][:100] for coarse in universe: symbol = coarse.Symbol if symbol not in self.averages: # 1. Call history to get an array of 200 days of history data history = self.History(symbol, 200, Resolution.Daily) #2. Adjust SelectionData to pass in the history result self.averages[symbol] = SelectionData(history) self.averages[symbol].update(self.Time, coarse.AdjustedPrice) if self.averages[symbol].is_ready() and self.averages[symbol].fast > self.averages[symbol].slow: selected.append(symbol) return selected[:5] self.__macd = self.MACD([symbol], 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily) self.__previous = datetime.min self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal) self.PlotIndicator([symbol], self.__macd.Fast, self.__macd.Slow) def OnSecuritiesChanged(self, changes): if not self.__macd.IsReady: return if self.__previous.date() == self.Time.date(): return tolerance = 0.0025 holdings = self.Portfolio[symbol].Quantity signalDeltaPercent = (self.__macd.Current.Value - self.__macd.Signal.Current.Value)/self.__macd.Fast.Current.Value if holdings <= 0 and signalDeltaPercent > tolerance: # 0.01% # longterm says buy as well self.SetHoldings([symbol], 0.2) elif holdings >= 0 and signalDeltaPercent < -tolerance: self.Liquidate([symbol]) self.__previous = self.Time class SelectionData(): #3. Update the constructor to accept a history array def __init__(self, history): self.slow = ExponentialMovingAverage(100) self.fast = ExponentialMovingAverage(20) #4. Loop over the history data and update the indicators for bar in history.itertuples(): self.fast.Update(bar.Index[1], bar.close) self.slow.Update(bar.Index[1], bar.close) def is_ready(self): return self.slow.IsReady and self.fast.IsReady def update(self, time, price): self.fast.Update(time, price) self.slow.Update(time, price)
Derek Melchin
Hi Zheng Tian,
If we want to manually place orders with the SetHoldings and Liquidate methods, we should avoid using the built-in alpha models. The attached backtest shows how we can select a universe of securities based on their EMA but make trading decisions based on a security's MACD indicator. Note how we have to add a MACD indicator for each security in the universe.
Continuing the development of this algorithm, consider removing the size restriction on the universe.
universe = [c for c in universe if c.Price > 5][:100]
If we restrict the size, it's possible for some SelectionData objects in the `averages` dictionary to miss an update during the `CoarseSelectionFunction`. See the EmaCrossUniverseSelectionAlgorithm for reference.
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.
Financial Freedom with Zheng Tian
Hi there. this is zheng tian again. I will like to know how to ask the bot do to purchase and sell shares. Given how new I am with this product, if you won't mind, you could explain to me the steps that need to be added to the bot.
Gabriel Guerrero
Hi all, What would this code look like if we were to add a stop loss or take profit function?
Thanks
Derek Melchin
Hi Gabriel,
To add a stop loss, we can add a risk management model like the MaximumDrawdownPercentPerSecurity. However, because of the Stop Loss Orders Workaround, we may need to define a custom Alpha model.
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.
Financial Freedom with Zheng Tian
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!