@showr/indicators
Version:
Technical indicators for Trading made with Showr
35 lines (34 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SMA = void 0;
const core_1 = require("@showr/core");
class SMA extends core_1.Indicator {
constructor(name = 'SMA', params) {
super(name, function (dataset) {
const { attribute, period = 5 } = params;
const datasetLength = dataset.value.length;
const lastSMA = dataset.at(-2)?.getIndicator(this.name);
if (lastSMA !== undefined && datasetLength > period) {
const firstAttributeValue = dataset.valueAt(-1 - period, attribute);
const lastAttributeValue = dataset.valueAt(-1, attribute);
const change = lastAttributeValue - firstAttributeValue;
return (lastSMA * period + change) / period;
}
else {
if (datasetLength < period) {
return dataset.valueAt(-1, attribute);
}
else {
let total = 0;
for (let i = datasetLength - period; i < datasetLength; i++) {
total += dataset.valueAt(i, attribute);
}
return total / period;
}
}
}, {
params,
});
}
}
exports.SMA = SMA;