Technical Analysis

## Calculating Exponential Moving Average - a Tutorial

Exponetial Moving Average (EMA for short) is one of the most used indicators in technical analysis today. But how do you calculate it for yourself, using a paper and a pen or – preferred – a spreadsheet program of your choice. Let’s find out in this explanation of EMA calculation.

Calculating Exponential Moving Average (EMA) is of course done automatically by most trading and technical analysis software out there today.

Here is how to calculate it manually which also adds to the understanding on how it works.

In this example we shall calculate EMA for a the price of a stock. We want a 22 day EMA which is a common enough time frame for a long EMA.

The formula for calculating EMA is as follows:

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

Use the following steps to calculate a 22 day EMA:

1) Start by calculating k for the given timeframe. 2 / (22 + 1) = 0,0869

2) Add the closing prices for the first 22 days together and divide them by 22.

3) You’re now ready to start getting the first EMA day by taking the following day’s (day 23) closing price multiplied by k, then multiply the previous day’s moving average by (1-k) and add the two.

4) Do step 3 over and over for each day that follows to get the full range of EMA.

This can of course be put into Excel or some other spreadsheet software to make the process of calculating EMA semi-automatic.

To give you an algorithmic view on how this can be accomplished, see below.

public float CalculateEMA(float todaysPrice, float numberOfDays, float EMAYesterday){
float k = 2 / (numberOfDays + 1);
}

This method would typically be called from a loop through your data, looking something like this:

foreach (DailyRecord sdr in DataRecords){
//call the EMA calculation
ema = CalculateEMA(sdr.Close, numberOfDays, yesterdayEMA);
//put the calculated ema in an array