Home > Net >  How to Calculate Exponential Moving Average (EMA) of last 200 days in Google sheets?
How to Calculate Exponential Moving Average (EMA) of last 200 days in Google sheets?

Time:08-25

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 enter image description here

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);
}

enter image description here

  • Related