Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Quick Question about EMA Indices

  1. Oct 8, 2012 #1
    Hi All,

    I'm working on a little side-project (not school related) in Java for analyzing Financial Stocks (I understand the stuff I'm doing can be done online but I'm trying to learn some graphing/plotting in Java). Anyways, my question has to do with calculated EMAs (Exponential Moving Averages).

    Example that demonstrates my question:

    12-Day EMA:

    k = (2/(12+1))

    EMA(1) = k * P(13) + (1-k) * (SMA(P1...P12))
    EMA(2) = k * P(14) + (1-k) * EMA(1)
    EMA(N) = k * P(N) + (1-k) * EMA(N-1)

    Where P(n) is the stock value on day n and SMA(P1...P12) is the Simple Moving Average of the first 12 elements (which is just their average). I think I'm calculating the EMA right, I'm just confused as to what to use for the first 12 values (in this case).

    The issue is what values to use before EMA(1). EMA(1) takes twelve values to get the first "real" EMA value. So if I were plotting this against the stock values on a graph do I just use the SMA for the first 12 values? Do I neglect to plot the first 12 Values? I'm just having trouble figuring out how to handle the initial N-values for whatever N-Day EMA I'm calculating.

    The issue seems moot at first, but if I'm calculating the difference between two EMA's (such as in an MACD calculation) the issue becomes more apparent because I have to subtract one EMA from another (both with different N-values) and I also have to plot the 9-Day EMA of the MACD and also plot the difference between those two (the Line and the Trigger line).

    I suppose it's not the biggest deal because obviously you're using large sets of data (100+ data points) and focusing more on the recent data as opposed to the initial data, I just want to make sure I'm doing this right. It would also seem strange (maybe not?) to neglect these initial data points as you'd have to neglect (for a typical MACD calculation) 26+9 = 35 data points.

    Can anyone shed some light? Or correct me if I'm calculating things wrong?

  2. jcsd
  3. Oct 9, 2012 #2


    User Avatar

    I am not an expert on the subject. But I'm not sure how many people here are familiar with this so I'll give an input and someone else can possibly correct me.

    Firstly, your calculations are correct.

    Second, Your EMA starts on day 12. So your EMA plot obviously starts there. For your MACD calculations you do not need to access any information from day 1-11. When you calculate the EMA for the MACD you will be using the first 9 original EMA's. So your MACD plot can only start on day 20. You can't use anything from day 1-11 for the EMA of the MACD because there are no EMA's to base it on there. So you have to use the first 9 actual EMA's to base the first MACD off of.

    The point where you physically start plotting the line is another story. For example, for the EMA, I THINK convention is to start plotting it from day 12. But there is a delay involved, so it may be acceptable to start at the halfway point which would be more accurate for visualisation purposes. You will have to research this and see which is the most correct/acceptable for stocks as I am not sure.
    The same holds true for the MACD EMA. Do you start the plot at day 20 or at day 16(midway). You will have to research what the convention is, or maybe someone here can shed some light.
    Last edited: Oct 9, 2012
  4. Oct 9, 2012 #3


    User Avatar
    Science Advisor
    Homework Helper

    Since your data doesn't extend back indefinitely, you have to guess a starting point. There's no "magic" way to guess correctly.

    At each step, the influence of the previous values are multiplied by (1-k). After k days the effect of the initial guess is scaled down by about 1/e = 1/2.718 = 0.37. After 2k days the factor reduces to about 0.13, after 4k days to about 0.02. That is a measure of how fast the error from a bad guess will wash out.

    You could try making some plots of the same average with different starting points to see the effect.

    If you want to make preditions from only a short sample of data, you probably need a different method, but if you have a data series for say 5 times the length of your average, it doesn't matter much what you choose as the starting value. Just using the first data point in the series is probably good enough.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook