I am trying to calculate the Exponential Moving Average (EMA) of stock. I found this formula:
=AVERAGE(INDEX(GoogleFinance("GOOG","all",WORKDAY(TODAY(),-200),TODAY()),,3))
But it is a function just to calculate Simple Moving Average. I researched more and found a script from this
Solution:
Therefore, the reduce
function should take the first value in the series as the initial value:
function EMA(range, n) {
if (!range.reduce) {
return range;
}
n = Math.min(n, range.length);
const a = 2 / (n 1);
const first = range.shift();
return range.reduce((accumulator, currentValue) => {
return currentValue != "" ? (currentValue * a accumulator * (1-a)) : accumulator;
}, first);
}