ExponentialMA() Question?

 

Hello all.

‌‌I have been searching forums and reading for weeks to find my answer and am getting no where so I figured I would take a chance and post it  :<)

‌‌I am trying to write an indicator that uses the ExponentialMA() function on MT5 but I cannot get it to work because I am not sure how to calculate the parameter for double prev_value.

I‌ know that it is the EMA value from the previous candle, so should I be using the same function within itself, and the the closing price within that function?

I‌ gave up and tried using iMA instead, but I cannot get that working either and MQL5 examples for this must be hard to come by. I am basically just trying to

‌duplicate Dmitriy Skub's EMA fan example using EMA instead of SMA, but the extra parameter is stumping me.

https://www.mql5.com/en/articles/136

MATrendDetector.mq5‌

‌‌

double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
 

What about this:‌

#define ema(nw,pr,c) (c >= 0.99 ? nw : (((nw)-(pr))*(c) + pr))
double coeff = 2.0/(double(periods+1))
...
emaValue = ema(newPrice,emaValue,coeff);

 
Carl Schreiber:

What about this:‌

#define ema(nw,pr,c) (c >= 0.99 ? nw : (((nw)-(pr))*(c) + pr))
double coeff = 2.0/(double(periods+1))
...
emaValue = ema(newPrice,emaValue,coeff);


Ah, so your manually calculating the value of the previous EMA value, nice.

‌Would I just replace the periods with the time period I am using for that EMA? Also, what value would be used for newPrice as that is not a constant that I know of.

‌I tried replacing "double prev_value" with emaValue. Thanks for the ideas.

double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])

 

In contrary to the sma periods does not really make sense for the ema.

But if you compare sma and ema a coefficient of 2/(period+1) gives you a quite similar ema to an sma with the length of period. That's from where this formula comes from.

‌If you want to have an ema of the closes of the bars for the bar[n] the arguments of the ema[n] are (for MT4!!): ema[n]= ema(Close[n],ema[n+1],coeff)

(This is how in MT4 the prices are presented; [0]=actua‌l, [1]=previous, ... In Mt5 it't the other way round: [0] = oldest, [1] = 1 bar 'younger'!)

Reason: