The simple moving average (SMA)
We might as well plot the SMA, just so we can compare the two after. Add this line to your script:
This plots the average of the previous ten days. Tweak the number in the brackets to see how the curve changes when taking into account different lengths.
The SMA, based on the previous ten days.
The exponential moving average (EMA)
The EMA will be a bit trickier to understand, but not to worry. Let’s break down the formula first:
EMA = (Close - Previous Day’s EMA) * Multiplier - Previous Day’s EMA
So, what’s this telling us? Well, for each day, we calculate a new moving average based on the previous day’s one. The multiplier is what “weighs” the most recent period, and is calculated with the following formula:
Multiplier = 2 / (Length of EMA + 1)
As with simple moving averages, we need to specify how long the EMA will be. Syntactically, the function to plot EMA is similar to the SMA one. Plot it alongside the SMA so you can compare the two:
//@version=4 study("My Script", overlay=true) plot(sma(close, 10)) plot(ema(close,10))
You can see a slight difference in the two types of MA.