Java How Should Initial Values Be Handled When Calculating EMAs in Java?

  • Thread starter Thread starter DougD720
  • Start date Start date
  • Tags Tags
    Indices
AI Thread Summary
When calculating Exponential Moving Averages (EMAs) in Java, the initial values for the first EMA can be derived from the Simple Moving Average (SMA) of the first 12 data points. The EMA calculation begins on day 12, and for MACD calculations, the first EMA values are used, starting from day 20. It's acceptable to plot the EMA from day 12, but some suggest starting at a midpoint for better visualization. The influence of initial values diminishes quickly, becoming negligible after a few periods. Ultimately, the choice of starting point can vary, but using the first data point is often sufficient when working with larger datasets.
DougD720
Messages
47
Reaction score
0
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?

Thanks!
 
Technology news on Phys.org
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:
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
1
Views
1K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
29
Views
3K
Replies
15
Views
3K
Replies
3
Views
3K
Replies
2
Views
3K
Replies
2
Views
2K
Back
Top