HI,
I am new to MQL4 and have a question.
On my visual chart, I have a MACD with two moving average indicators applied to it (Exponential using First Indicator's Data). How do I replicate this in mql4 code so I can create a EA?
I know how to create a MA and MACD seperately in code, but I dont know how to create the MAs based on the MACD values and not the price data.
You will have to find out which is the indicator data that it's grabbing.
I tested this out using MT4's MACD and the indicator data is the Histogram. You can verify this yourself by dragging the Moving Average indicator to MT4's MACD subwindow and selecting MA Method (Simple), Period (1), Applied To (First Indicator's Data).
So now you know what the data is that it returns, you just have to write your own EMA function that reads from MACD's histogram buffer (you can get this by calling iMACD or, if you have your own MACD, by using iCustom).
Edit: I'm going to leave you the link to MT4/5's explanation of the EMA. It appears to be a bit different than the one I linked above.
Exponential Moving Average (EMA) #
Exponentially smoothed moving average is calculated by adding of a certain share of the current closing price to the previous value of the moving average. With exponentially smoothed moving averages, the latest close prices are of more value. P-percent exponential moving average will look like:
EMA = (CLOSE (i) * P) + (EMA (i - 1) * (1 - P))
Where:
CLOSE (i) — current period close price;
EMA (i - 1) — value of the Moving Average of a preceding period;
P — the percentage of using the price value.
How they calculate EMA in MT5's Adaptive Moving Average indicator is identical to the first link. You'll have to do some testing to see which one matches the result that you want.
EMA(i) = Price(i) * SC + EMA(i-1) * (1 - SC)
Where:
SC = 2/(n+1) — EMA smoothing constant, n — period of the exponential moving;
EMA(i-1) — previous value of EMA.
The smoothing ratio for the fast market must be as for EMA with period 2 (fast SC = 2/(2+1) = 0.6667), and for the period of no trend EMA period must be equal to 30 (slow SC = 2/(30+1) = 0.06452). Thus the new changing smoothing constant is introduced (scaled smoothing constant) SSC:
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
HI,
I am new to MQL4 and have a question.
On my visual chart, I have a MACD with two moving average indicators applied to it (Exponential using First Indicator's Data). How do I replicate this in mql4 code so I can create a EA?
I know how to create a MA and MACD seperately in code, but I dont know how to create the MAs based on the MACD values and not the price data.