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?

  • $\begingroup$ I did this calculation for RSI, if you want to calculate a 100 period RSI in a scenario where you will remove the oldest candle and add one extra candle, to ensure the indicator output doesnt change you need 1873 candles per asset at minimum, obviously the number would be much lower for a 14 period RSI $\endgroup$
    – PirateApp
    Jun 14, 2018 at 7:30

2 Answers 2


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'.

  • $\begingroup$ I am using TA-LIB Java library $\endgroup$ Apr 5, 2012 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.

  • $\begingroup$ They are asking how many data points to start with. 1 bar? 10 bars? 100 bars? $\endgroup$
    – Chloe
    Apr 5, 2012 at 3:12
  • $\begingroup$ "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 $\endgroup$
    – LazyCat
    Apr 5, 2012 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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