# Calculate Exponential Moving Average for a specific time frame

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)

Where,

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.

## 1 Answer

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.

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