0
$\begingroup$

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

IBKR Trading Workstation - EMA9 and EMA21 over daily candles around August the 5th 2019

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

IBKR Trading Workstation - EMA9 and EMA21 over weekly candles around August the 5th 2019

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?
$\endgroup$
8
  • $\begingroup$ I don't know. For what it is worth 2019-AUG-5 was a Monday. Could they be using Monday closing prices? $\endgroup$
    – noob2
    Apr 12 '20 at 9:44
  • $\begingroup$ When placing the cursor over the candle corresponding to August the 5th, it shows a closing price of 291.62, which corresponds to the closing price of the next Friday - August the 9th. I'm going to test this, anyway. $\endgroup$
    – jmgonet
    Apr 12 '20 at 10:22
  • $\begingroup$ @noob2 - I've just tested as you suggested, and it isn't. Based on your experience, would you say that there is no standard approach to this question? $\endgroup$
    – jmgonet
    Apr 12 '20 at 10:28
  • $\begingroup$ Hi: In your "I've tried several options section", you say 5 times the whatever. Don't you mean one fifth and not 5 ? If you used 5 by mistake, change it to one fifth because usually some kind of average price would be used. $\endgroup$
    – mark leeds
    Apr 13 '20 at 8:18
  • 1
    $\begingroup$ Note that there is no "standard" for the EWMA. You could take the last day of the week's price or the week's average. It's pretty much upto you but if you want to match what you're comparing to, I guess you'd have to ask them what they do. I hope this helps. $\endgroup$
    – mark leeds
    Apr 13 '20 at 15:54
1
$\begingroup$

There are several points to answer this question:

Most usual quote is the closing price

That was my original question. Initially I thought that there is a standard. In his comments, @markleeds pointed me out that there is no such standard. This explains why I couldn't find any satisfactory information in Google.

As I'm using TWS, from Interactive Brokers, I opened a ticket to their support platform, but, as usual, I didn't get any response from them (not even an acknowledgement to the reception of my question).

Then I went to www.investing.com, and checked out one of their interactive charts. There you can set the period length to 1d, 1w or 1M, and add any number of indicators. One of the indicators is the EMA. Now, what's so cool with their chart is that you can configure the indicator and select different input values as quotes (open, close, high, low, typical, median...) and, by comparison, I could establish that Trading Workstation is using the closing price.

My conclusions in that respect are:

  1. When periods are days, the most usual quote to use as input in EMA calculations (and MACD, and others), is the closing price.
  2. Consistently, when the period is a WEEK, the usual quote is the closing price the last trading day of the week, most commonly Friday.
  3. And, when the period is a MONTH, the usual quote is the closing price of the last trading day of the month.

When I say usual I mean that, although there is no standard, two mainstream platform are using the same default configuration, so I believe others are doing the same.

Beware of the number of samples

Once I found out what quote to use as input, I could fix my algorithm and match exactly the charts in TWS. But only for daily periods. So, back to case one. I didn't fix anything.

By reading about approximating the EMA with a limited number of terms in the article Moving average from Wikipedia, I realized that, for keeping a precision better than 99.9% you need k samples or more, where:

  • k = 3.45 * (N + 1)

As I'm using EMA9 and EMA21, my worst case is N = 21, so I need 72.45 periods. When periods are days, and having 5 trading days per week, this means going back from August the 5th, 2019 to:

  • Sd = 5-Aug-2019 - (73 / 5) * 7 = 23-Apr-2019

And this was OK when using days, because I including the whole 2019 year from January the 1st. But, when using week periods, I need to include at least 73 weeks in the calculation:

  • Sw = 5-Aug-2019 - 73 * 7 = 11-Mar-2018

So, my second error was not getting enough samples.

Beware of the 'circuit breakers'

I felt really good having fixed out those two problems, the input quote and the number of samples. I could see now my charts being completely identical to both TWS and investing.com in daily, weekly and monthly periods. As a last test, I compared with the current days, around February, March and April 2020, to see if my charts were correct also during the Coronavirus crisis.

And they weren't.

I couldn't believe this. EMA's mathematic is not that complex, it is not prone to accumulating rounding errors, and I fully tested it with data from 2017, so what on Earth?

By checking sample by sample, I found out that there are 4 trading days missing from the data I downloaded from finance.yahoo.com, all in 2020. Namely:

  • March the 28th
  • March the 21st
  • March the 14th
  • and February the 17th

Which are days when circuit breakers kicked in Wall Street. To verify this, I downloaded the same SPY historic data from investing.com and comparing both sources, yahoo and investing, I could see that:

  • While yahoo simply removed those days from downloaded data,
  • investing.com used the same values from the previous day.

So I replaced the missing data, and then my charts matched completely.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.