Can EMA be calculated using JavaScript?
I am attempting to apply the following formula for EMA:
EMA = array[i] * K + EMA(previous) * (1 – K)
Where K represents the smooth factor:
K = 2/(N + 1)
And N is the range of values that I want to consider
If I have an array of values like this, where the values increase over time:
var data = [15,18,12,14,16,11,6,18,15,16];
The objective is to create a function that can return the array of EMAs. Each value in the array, except the very first "range" value, will have a corresponding EMA. By having the related EMA value for each item in the data array, I can either use all of them or just the last one to predict the next value.
function calculateEMA(array, range) {
var k = 2/(range + 1);
...
}
I'm struggling to implement this function and would appreciate any assistance.