@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
70 lines (69 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ROC = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const types_1 = require("../types");
const defaultInput = {
period: 9,
source: types_1.OHLCEnum.CLOSE,
};
/**
* The rate of change (ROC) is the speed at which a variable changes
* over a specific period of time. ROC is often used when speaking
* about momentum, and it can generally be expressed as a ratio between
* a change in one variable relative to a corresponding change in another;
* graphically, the rate of change is represented by the slope of a line.
* The ROC is often illustrated by the Greek letter delta.
*/
class ROC {
/**
* @param {OHLC[]} series candles series
* @param {OHLCEnum} source source of values
* @param {number} period period for indicator
*/
constructor(series, source = defaultInput.source, period = defaultInput.period) {
// super();
const values = fp_1.map(source)(series);
this.indicator = new technicalindicators_1.ROC({ values, period });
this.source = source;
}
/**
* Retrieve ROC values for instance
* @return {number[]} values for instance data
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calculate ROC to next tick
* @param {OHLC} candle new candle to add to series
* @return {number | undefined} next ROC value or undefined if period is
* greater than actual series length
*/
next(candle) {
return this.indicator.nextValue(candle[this.source]);
}
/**
* Create instance from data
* @param {RateOfChangeInput} input input data
* @return {ROC} ROC instance
*/
static generator({ series, source, period, }) {
return new ROC(series, source, period);
}
/**
* Get ROC values from input
* @param {RateOfChangeInput} input input data
* @return {number[]} ROC values
*/
static calculate(input) {
const { series, source, period } = Object.assign(Object.assign({}, defaultInput), input);
const values = fp_1.map(source)(series);
return technicalindicators_1.ROC.calculate({
values,
period,
});
}
}
exports.ROC = ROC;