Weights of an EMA

 

So in mql4 you can use the function iMA or iMAOnArray if you want to create an exponential (MODE_EMA) or other type of moving average. Now an EMA is usually created by iteration:                                  EMA = (CLOSE(i)*P)+(EMA(i-1)*(1-P)). But you could also calculate it in a different way: using a vector with weights, that you multiply to the close prices over the window and then you sum those products to give you the value of the EMA at the right side of the window. Is this the right semi-code (not mql4) to find those weights that returns the same type of EMA as mql4 uses?

ema.vector<-function(n)
                   {
                     alpha<-2/(n+1)
                     i<-1:n
                     sm<-sum((alpha*(1-alpha)^(1-i)))
                     return(((alpha*(1-alpha)^(1-i)))/sm)  
                   }

 That returns you the vector of weights of an EMA, which looks like this for a 10-period EMA:

weights of an EMA 

I appreciate the help on this slightly technical topic!! 

 
MrH:

So in mql4 you can use the function iMA or iMAOnArray if you want to create an exponential (MODE_EMA) or other type of moving average. Now an EMA is usually created by iteration:                                  EMA = (CLOSE(i)*P)+(EMA(i-1)*(1-P)). But you could also calculate it in a different way: using a vector with weights, that you multiply to the close prices over the window and then you sum those products to give you the value of the EMA at the right side of the window. Is this the right semi-code (not mql4) to find those weights that returns the same type of EMA as mql4 uses?

  1. EMA = (CLOSE(i)*P)+(EMA(i-1)*(1-P)). You should NEVER calculate it that way because it amplifies round off errors. ALWAYS use EMA = EMA(i-1) + P*[Close(i)-Ema(i-1)]. Where P = alpha = 2/(N+1)
  2. You CAN NOT use a weight vector because EMA uses infinite number of weights. You need 3.45*(N+1) entries to get 99.9% of the true weights.
  3. https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
 
WHRoeder:
  1. EMA = (CLOSE(i)*P)+(EMA(i-1)*(1-P)). You should NEVER calculate it that way because it amplifies round off errors. ALWAYS use EMA = EMA(i-1) + P*[Close(i)-Ema(i-1)]. Where P = alpha = 2/(N+1)
  2. You CAN NOT use a weight vector because EMA uses infinite number of weights. You need 3.45*(N+1) entries to get 99.9% of the true weights.
  3. https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average


Thanks WHRoeder, but I ask myself, if you can't use weights for EMA, then why does it say:

"for any suitable k = 0, 1, 2, ... The weight of the general datum point  is ." ?

 
MrH:


Thanks WHRoeder, but I ask myself, if you can't use weights for EMA, then why does it say:

"for any suitable k = 0, 1, 2, ... The weight of the general datum point  is ." ?


I meant: "for any suitable k = 0, 1, 2, ... The weight of the general datum point  is Yt-i=alpha(1-alpha)^i-1" ?


 

That's the weight of a specific data point. But what part of "EMA uses infinite number of weights" was not clear?

For a 10 period EMA you need at least 38 weights and data points, thats 38 multiplication, additions and lookups (114+ operations). EMA(i-1) + P*[Close(i)-Ema(i-1)]. requires 1 lookup, 1 multiplication, 1 subtraction, 1 addition (4 operations.)

Reason: