10

Hi Is possible to calculate EMA in javascript?

The formula for EMA that I'm trying to apply is this

EMA = array[i] * K + EMA(previous) * (1 – K)

Where K is the smooth factor:

K = 2/(N + 1)

And N is the Range of value that I wanna consider

So if I've an array of value like this, and this value grow during the times:

var data = [15,18,12,14,16,11,6,18,15,16];

the goal is to have a function, that return the array of the EMA, because any of this value, expect the very fist "Range" value, have this EMA, for each item on data, I've the related EMA value. In that way I can use all or use only the last one to "predict" the next one.

function EMACalc(Array,Range) {
var k = 2/(Range + 1);
...
}

I can't figure out how to achieve this, any help would be apreciated

3
  • You shouldn't be using Array and Range as parameter names, as you are overwriting native JS objects Oct 15, 2016 at 9:11
  • Question: by EMA = array[i] * K + EMA(previous) * (1 – K) you mean EMA[i] = array[i] * K + EMA[i - 1] * (1 – K)? Oct 15, 2016 at 9:19
  • Ok @DanielG.Reina, got it, and yes, the EMA[i] sound better. Because to calc the EMA for the i items you should know the EMA for the i-1 item! For the very 1st element can be used the value of the same element in it Oct 15, 2016 at 9:26

3 Answers 3

25

I don't know if I completely understood what you need, but I will give you the code for a function that returns an array with the EMA computed for each index > 0 (the first index doesn't have any previous EMA computed, and will return the first value of the input).

function EMACalc(mArray,mRange) {
  var k = 2/(mRange + 1);
  // first item is just the same as the first item in the input
  emaArray = [mArray[0]];
  // for the rest of the items, they are computed with the previous one
  for (var i = 1; i < mArray.length; i++) {
    emaArray.push(mArray[i] * k + emaArray[i - 1] * (1 - k));
  }
  return emaArray;
}

This should do it.

6
  • Tnx @Daniel, I tried with the value in this article docs.oracle.com/cd/E12032_01/doc/epm.921/html_ir_studio/… but give NaN except for the 1st item. Oct 15, 2016 at 9:43
  • Ops, sorry found the error, your function say mRange but k have Range :D Oct 15, 2016 at 9:44
  • 1
    I forgot to change Range to mRange when computing k. Let me know if there is any other problem. If you copy the info as text, remember to parse the values to numbers (integer or float) before calling the function, as it expects an array of numbers Oct 15, 2016 at 9:48
  • great :) could you validate my answer then? Thanks! Oct 15, 2016 at 10:06
  • 1
    tried that as well... I can never get the EMA numbers that binance is outputting. Every price is different. Tried it in 20 different ways. Finally tried yours - same, Every price is different. I tried to supply 21,51,101 period cnadles. Tried also 20,50 and 100. Nothing is working... I have no idea how these guys calculate EMA. 2 days I lost on this... about to give up... Jun 16, 2021 at 1:50
6

The following can be another way of implementing the EMA.

var getEMA = (a,r) => a.reduce((p,n,i) => i ? p.concat(2*n/(r+1) + p[p.length-1]*(r-1)/(r+1)) : p, [a[0]]),
      data = [15,18,12,14,16,11,6,18,15,16],
     range = 3;

console.log(getEMA(data,range));

4
  • 1
    Warning: I believe there is a subtle error here producing wrong results. The value of a[i-1] in the lambda to map is not the previously calculated ema, it is the previous input value. The array is not mutated by the map operator. The error can be clearly spotted in the following codepen: codepen.io/corolla/pen/QpwRmz I believe the answer by @dgrcode is correct.
    – corolla
    Feb 26, 2017 at 10:56
  • @corolla Yes you are right. I have corrected my code accordingly. Thanks for the heads up.
    – Redu
    Feb 26, 2017 at 14:45
  • 1
    I copied in the values from github.com/jonschlinkert/exponential-moving-average and this calculation doesn't match the output there. Any idea on which one is correct? Aug 11, 2019 at 6:52
  • @Blair Zajac If you check above code returns same length array as the input. It attempts to calculate EMA for the first range (r) many items too. The code that you refer starts after index r-1. Their results converge but i believe this is better.
    – Redu
    Aug 11, 2019 at 17:05
2

I like recursion, so here's an example of an EMA function that uses it. No need to maintain arrays.

function weightMultiplier(N) { return 2 / (N + 1) }

function ema(tIndex, N, array) {
    if (!array[tIndex-1] || (tIndex) - (N) < 0) return undefined;
    const k = weightMultiplier(N);
    const price = array[tIndex];
    const yEMA = ema(tIndex-1, N, array) || array[tIndex-1]
    return (price - yEMA) * k + yEMA
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.