This repository has been archived on 2025-10-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
gekko/strategies/indicators/EMA.js
T
2021-06-06 20:25:14 +02:00

37 lines
880 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @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;