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

  • Context: Java 
  • Thread starter Thread starter DougD720
  • Start date Start date
  • Tags Tags
    Indices
Click For Summary
SUMMARY

The discussion focuses on calculating Exponential Moving Averages (EMAs) in Java for financial stock analysis. The participant correctly identifies that the first EMA value (EMA(1)) requires twelve initial stock values, leading to confusion about how to handle these initial values. It is established that the EMA calculation begins on day 12, and for MACD calculations, the first EMA values are used to derive subsequent EMAs. The convention for plotting these values may vary, with suggestions to start from day 12 or a midpoint for better visualization.

PREREQUISITES
  • Understanding of Exponential Moving Averages (EMAs)
  • Familiarity with Simple Moving Averages (SMAs)
  • Knowledge of MACD (Moving Average Convergence Divergence) calculations
  • Proficiency in Java programming for financial data analysis
NEXT STEPS
  • Research conventions for plotting EMAs and MACDs in financial analysis
  • Explore Java libraries for financial data visualization, such as JFreeChart
  • Learn about different methods for estimating initial values in time series analysis
  • Investigate the impact of different starting points on EMA calculations through simulation
USEFUL FOR

Java developers, financial analysts, and anyone interested in implementing technical analysis for stock market data using EMAs and MACDs.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K