My current understanding of the EMA calculation with daily periods is to use as quote the closing price of each day:
- Day 0 - Take the closing price as Q0. EMA = Q0
- Day 1 - Take the closing price as Q1. EMA = Q1 * k + Q0 * (1-k)
- Day 2 - Take the closing price as Q2. EMA = Q2 * k + Q1 * (1-k)
- Day n - Take the closing price as Qn. EMA = Qn * k + Qn-1 * (1-k)
I executed my algorithm over SPY daily quotes, and I stopped it arbitrarily at August the 5th 2019, to display the EMA9 and EMA21 values. These are my results:
- EMA9: 294.77
- EMA21: 296.24
To validate my algorithm, I compared with the IBKR Trading Workstation, at the same date. Here is a screenshot with the SPY quotes around 5-Aug-2019 in daily candles, and the corresponding EMA9 and EMA21:
- Closing price: 283.82
- EMA9: 294.8
- EMA21: 296.3
So far so good (except some reading error).
Now, using the same application, I choose weekly candles. As expected, the result is quite different because, although the EMA computation is identical, they're not using the same values as input.
- Closing Price: 291.62
- EMA9: 294.0
- EMA21: 289.6
I am not able to reach the same results in my algorithm. Based on my previous success, I believe my EMA computation is correct, but I'm probably not using the correct quotes Q[0..n].
I've tried several options:
- The mean of closing prices over the week (simple average of 5 closing prices, of Monday to Friday).
- The closing price of the last day of the week (one single closing price, the Friday afternoon).
- The mean of mid points over the week (simple average of (open + close)/2, Monday to Friday).
But none give me the same results as the Trading Workstation application. I've checked other sources, and I've got the impression that they all do it in the same way.
So, this is my question:
- When using periods that are not days, what is the usual quote that is used as input into the EMA calculation?