What is the exact formula for EMA

Today I compared the performance and completed trade dates for my MAC model of the S&P500 that runs in an excel spreadsheet and also as a P123 sim. The MAC model and its simple rules are described here http://advisorperspectives.com/newsletters12/Beyond_the_Ultimate_Death_Cross.php

The performance was more or less equal for both, the excel and P123 sim, and there were 11 completed trades from 1999 to 2014 for both. However there was in some instances a difference of 3 weeks between the trade dates. There should be no more than 1 week difference, because the P123 model re-balances weekly.

I suspect the difference may be due to how P123 calculates an exponential moving average. Does anybody know the exact formula which P123 uses.

The formula I used for calculating the EMA is:

EMAtoday = EMAyesterday + α × (VALUEtoday - EMAyesterday)

The coefficient α represents the degree of weighting decrease, a constant smoothing factor between 0 and 1. A higher α discounts older observations faster. Alternatively, α may be expressed in terms of N time periods, where α = 2/(N+1).

So N should be the number of bars used for the EMA. Is this also how P123 calculates the EMA?

Georg

I think the EMA always represents a bit of a challenge as it theoretically requires an infinite sum, a numerical approximation requires a fixed cutoff to make the calculation possible.

The wikipedia article on ema suggests a 86% accuracy by cutting off after N sampling periods, however it does then go on to note:

There are other variations as well, like using a SMA for the starting value, and so on. I would also be interested to know how p123 calculates this.

Oliver, that is why we should know how P123 handles this calculation. The problem is that we cannot check the actual values at P123 because we cannot download the series. So I am waiting for a response from the P123-team. If you specify EMA(40) then it would be important to know if they start the calculation 40 bars ago or if they start the calculation from the earliest date that data is available on P123, i.e. 1/2/1999, or perhaps earlier. S&P500 daily data is available from 1/2/1957 at FRED, so one could start the calculation of the EMA in Jan-1957 for higher accuracy.
Georg

Best illustrated with an example: for EMA(10) we seed the EMA with the SMA(10,110) or the 10 bar average 100+10 bars ago. In other words the seeding is 100 bars, which seemed like a bood balance between speed and precision.

Thanks Marco for the clarification. This makes sense. So for EMA(40) the EMA is seeded with the SMA(40) calculated from 140 to 100 bars ago, and the EMA is then always calculated over 100 bars.

Georg,

The way I interpret Marco’s explanation is that your EMA(40) would be mathematically equal to; EMA(41) where the value for the 41st bar is SMA(40,140). I’m not sure why he doesn’t seed the EMA(40) with EMA(100,40) instead.

Denny :sunglasses:

Denny, I don’t think you are interpreting this correctly.

The seeding value to start the EMA calculation with is the SMA(40) 100 bars ago, which would then be the first EMAyesterday in the formula
EMAtoday = EMAyesterday + α × (VALUEtoday - EMAyesterday), which then gets repeated for 100 bars to arrive at the EMA corresponding to the last bar.

For EMA(40) the coefficient α represents the degree of weighting decrease, α = 2/(N+1) = 2/(40+1)= 0.0488 and has actually nothing to do with days or bars - it is just a convenient way of getting the coefficient. So if you set N=1, then α= 1, and you get no smoothing at all.
Georg

Ah! I remember now. Marco needed a seed to determine the current value to start the calculation. I was thinking about a value for the oldest bar in EMA(41) to add into EMA(40) to represent the final value instead of a value to start the calculation.

Denny :sunglasses: