@showr/indicators
Version:
Technical indicators for Trading made with Showr
42 lines (41 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EMA = void 0;
const core_1 = require("@showr/core");
const __1 = require("..");
class EMA extends core_1.Indicator {
constructor(name = 'EMA', params) {
super(name, function (dataset) {
const { attribute, period = 5 } = params;
const datasetLength = dataset.value.length;
const _smoothing = 2 / (period + 1);
const lastEMA = dataset.at(-2)?.getIndicator(this.name);
if (lastEMA && !isNaN(lastEMA) && datasetLength > period) {
const value = dataset.valueAt(-1, attribute);
return value * _smoothing + lastEMA * (1 - _smoothing);
}
else {
if (datasetLength === period) {
const sma = new __1.SMA('sma', { attribute, period });
return sma.calculate(dataset);
}
else {
if (datasetLength < period) {
return NaN;
}
else {
const dsSliced = new core_1.Dataset(dataset.value.slice(0, period));
const dsRemaining = new core_1.Dataset(dataset.value.slice(period));
this.spread(dsSliced);
dsRemaining.quotes.forEach((q) => dsSliced.add(q));
this.spread(dsSliced);
return dsSliced.at(-1).getIndicator(this.name);
}
}
}
}, {
params,
});
}
}
exports.EMA = EMA;