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/SMA.js
T
2021-06-06 20:25:14 +02:00

22 lines
548 B
JavaScript

// required indicators
// Simple Moving Average - O(1) implementation
var Indicator = function(windowLength) {
this.input = 'price';
this.windowLength = windowLength;
this.prices = [];
this.result = 0;
this.age = 0;
this.sum = 0;
}
Indicator.prototype.update = function(price) {
var tail = this.prices[this.age] || 0; // oldest price in window
this.prices[this.age] = price;
this.sum += price - tail;
this.result = this.sum / this.prices.length;
this.age = (this.age + 1) % this.windowLength
}
module.exports = Indicator;