Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently, I have history data for 10 years.

Most of the time, I only interested in getting EMA/ MACD for the last history point (means yesterday point). Instead of using entire 10 years history, I would only like to provide N data points, for fast calculation, yet get a reasonable accurate EMA. Currently, here is the way for me to determine how many N data.

9 days SMA lookback = 8
9 days EMA lookback = 8
9 days 12/26 MACD lookback = 33

final int scale = 2;
startIndex = historyDataSize - ((lookback + 1) * scale);
for (int i = startIndex; i < historyDataSize; i++) {
    // ...

I know I can further increase scale to get a reasonable good EMA/ MACD outcome. The question is, how large I should increase, for fast calculation purpose, yet reasonable accurate?

share|improve this question
up vote 3 down vote accepted

I implemented these algorithms just a few months ago! I would just double the number of data points that you need. If you need a 10 day EMA, take 20 data points. For the MACD, you need the EMA, so add them together then double it. Here is the algorithm including an Excel spreadsheet you can compare with:


They claim on their charts to use 200+ days for accuracy, but remember that stock prices only have 2 decimals, and usually 4 significant digits, with a maximum of 4 decimals for penny stocks. So you wouldn't need anything more accurate than 2 decimals most of the time. Slippage would matter more!

If you want to be super accurate, and have a dynamic 'scale' or 'lookback', you can calculate the indicator using increasing 'scale' or exponentially greater history samples (x2, x4, x8, ...) until the different between the final results is within 4 significant digits of each other. Then you would need no more accuracy. You could either do this every time or 'calibrate' it using maybe a year of data for the whole market, and take the average of your 'scale'.

share|improve this answer
I am using TA-LIB Java library – Cheok Yan Cheng Apr 5 '12 at 9:46

You shouldn't recompute your EMA - just keep its old values, and apply your EMA formula to the last value to update.

share|improve this answer
They are asking how many data points to start with. 1 bar? 10 bars? 100 bars? – Chloe Apr 5 '12 at 3:12
"I would only like to provide N data points, for fast calculation.." - my point is that the number of points shouldn't affect the speed: use all the data you have, run EMA on it once (go have a drink while the data is being processed), and once it's done, you only have to update the last value – LazyCat Apr 5 '12 at 15:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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