@showr/core
Version:
Core library for Showr
56 lines (55 loc) • 1.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Indicator = void 0;
const _1 = require("./");
/**
* Creates a indicator that can be calculated over a dataset.
*/
class Indicator {
_name;
_calculate;
_options;
/**
* Creates an indicator with definition and configuration.
* @param name - Name of the indicator.
* @param calculate - Indicator definition function that accepts the `Dataset` and returns a number.
* @param options - Indicator configuration object.
*/
constructor(name, calculate, options) {
this._name = name;
this._calculate = calculate;
this._options = options;
}
get name() {
return this._name;
}
get options() {
return this._options;
}
get params() {
return this._options?.params;
}
calculate(dataset) {
if (this.options?.beforeCalculate) {
this.options.beforeCalculate(dataset);
}
return this._calculate(dataset);
}
/**
* Mutates each quote of the given dataset with a calculated indicator value.
* @param dataset - `Dataset`.
* @returns Mutated `Dataset`.
*/
spread(dataset) {
if (this.options && this.options.beforeCalculate) {
this.options.beforeCalculate(dataset);
}
const emptyDataset = new _1.Dataset();
dataset.quotes.forEach(quote => {
const quoteWithIndicator = quote.setIndicator(this.name, this.calculate(emptyDataset.add(quote)));
emptyDataset.update(quoteWithIndicator);
});
return dataset;
}
}
exports.Indicator = Indicator;