4 Replies Latest reply: Jun 14, 2014 5:30 PM by Leslie Blumenfeld

# Calculate Extended Moving Average  (EMA)

Does anyone have an example of a QlikView expressio to calculate an "x" period exponential moving average (EMA)? I know how to do a simple (rolling) average using RangeAvg() and Above(), but an EMA is more complex, because it uses the prior record's EMA.

"x" can be any number you'd like (e.g. 50, 200, etc.)

Thanks.

• ###### Re: Calculate Extended Moving Average  (EMA)

EMA is a recursive calculation. That can't be done in an expression afaik since an expression can't reference itself. You can calculate it in the script using the peek function. But that's not very dynamic since you can't vary x. Maybe you can use R.

edit: fixed some typos, i.e can to can't

• ###### Re: Calculate Extended Moving Average  (EMA)

You're saying that it "can" reference itself? Do you have an example of this?

• ###### Re: Calculate Extended Moving Average  (EMA)

I know this thread is a bit old but for the sake of posterity I'd thought I'd point out that a chart expression CAN reference itself.  For example, I've had a quick go at doing an exponential moving average with a period of 9 and a weighting of 0.2 where the expression has the Label "EMA9":

if(RowNo() = 1, Data, (Data - Above(EMA9)) * .2 + Above(EMA9))

Technically I guess I shouldn't be starting the EMA from row 1 the way I have, but I hope it gives you the idea.

Cheers,

Rod

• ###### Re: Calculate Extended Moving Average  (EMA)

Rod, That turns out to be exactly what I did. I simply named the column and then referred to the column by name in the row above. I just never got to post the result.