I am writing code to calculate and plot the Exponential Moving Average(EMA) for different chart timeframes(tick/1M/5M/1H etc). I found the formula for calculating EMA:

EMA = Price(t) * k + EMA(y) * (1 – k)


t = today, y = yesterday, N = number of days in EMA, k = 2/(N+1)

But I can't figure out how to factor in the timeframe into this formula. I know that the EMA plot looks different for different chart timeframes but I am unable to figure out why it is different.

To summarize with an example:

How would I go about calculating the plot points for the 5-day EMA for a candlestick chart timeframe of 5M, 15M, 1H etc?

Thank you in advance.


First, let's assume (hypothetically) you want to compute 5-day EMA from data sampled daily. Then $k_D=2/(N+1)$, here $N=5$, so $k_D=0.333$

Now assume you want to use data sampled every 5 minutes. There are 288 5-minutes intervals in a day (I am assuming a market that trades around the clock). There are 288*5 = 1440 intervals in a 5 day period. So $k_{5m} =2/(288*5+1)=0.000693963$

And similarly you can find the appropriate $k$ to use for other sampling intervals. The general formula is $k=\frac{2}{mN+1}$ where m= number of sampling intervals in a day, N= desired decay period for the moving average in days.

  • $\begingroup$ Thank you. That makes a lot of sense. I will try this out :) $\endgroup$ Nov 9 '17 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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