-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEMA.js
36 lines (28 loc) · 880 Bytes
/
EMA.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// @link http://en.wikipedia.org/wiki/Exponential_moving_average#Exponential_moving_average
var Indicator = function(weight) {
this.input = 'price';
this.weight = weight;
this.result = false;
this.age = 0;
}
Indicator.prototype.update = function(price) {
// The first time we can't calculate based on previous
// ema, because we haven't calculated any yet.
if(this.result === false)
this.result = price;
this.age++;
this.calculate(price);
return this.result;
}
// calculation (based on tick/day):
// EMA = Price(t) * k + EMA(y) * (1 – k)
// t = today, y = yesterday, N = number of days in EMA, k = 2 / (N+1)
Indicator.prototype.calculate = function(price) {
// weight factor
var k = 2 / (this.weight + 1);
// yesterday
var y = this.result;
// calculation
this.result = price * k + y * (1 - k);
}
module.exports = Indicator;