trading-signals
Version:
Technical indicators to run technical analysis with JavaScript / TypeScript.
39 lines • 1.25 kB
JavaScript
import { IndicatorSeries } from '../../base/Indicator.js';
import { getAverage, pushUpdate } from '../../util/index.js';
export class MAD extends IndicatorSeries {
prices = [];
interval;
constructor(interval) {
super();
this.interval = interval;
}
getRequiredInputs() {
return this.interval;
}
update(price, replace) {
pushUpdate(this.prices, replace, price, this.interval);
if (this.prices.length === this.interval) {
const mean = getAverage(this.prices);
let sum = 0;
for (let i = 0; i < this.interval; i++) {
const deviation = Math.abs(this.prices[i] - mean);
sum += deviation;
}
return this.setResult(sum / this.interval, replace);
}
return null;
}
static getResultFromBatch(prices, average) {
if (prices.length === 0) {
return 0;
}
const mean = average || getAverage(prices);
let sum = 0;
for (let i = 0; i < prices.length; i++) {
const deviation = Math.abs(prices[i] - mean);
sum += deviation;
}
return sum / prices.length;
}
}
//# sourceMappingURL=MAD.js.map