@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
88 lines (87 loc) • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ADX = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const types_1 = require("../types");
const defaultInput = {
period: 14,
};
/**
* The average directional index (ADX) is a technical analysis indicator used
* by some traders to determine the strength of a trend. The trend can be either
* up or down, and this is shown by two accompanying indicators, the negative
* directional indicator (-DI) and the positive directional indicator (+DI).
* Therefore, the ADX commonly includes three separate lines. These are used to
* help assess whether a trade should be taken long or short, or if a trade
* should be taken at all.
*/
class ADX {
/**
* @param {OHLC[]} series candles series
* @param {number} period period for indicator
*/
constructor(series, period = defaultInput.period) {
// super();
const high = fp_1.map(types_1.OHLCEnum.HIGH)(series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series);
this.indicator = new technicalindicators_1.ADX({ high, low, close, period });
this.series = series;
this.period = period;
}
/**
* Retrieve ADX values for instance
* @return {ADXOutput[]} values for instance data
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calculate ADX to next tick
* @param {OHLC} candle new candle to add to series
* @return {ADXOutput | undefined} next ADX value or undefined if period is
* greater than actual series length
*/
next(candle) {
var _a;
// INFO: workaround to calculate next
this.series = [...this.series, candle];
const high = fp_1.map(types_1.OHLCEnum.HIGH)(this.series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(this.series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(this.series);
this.indicator = new technicalindicators_1.ADX({
high,
low,
close,
period: this.period,
});
return (_a = this.getResults()) === null || _a === void 0 ? void 0 : _a.pop();
}
/**
* Create instance from data
* @param {AverageDirectionalIndexInput} input input data
* @return {ADX} ADX instance
*/
static generator({ series, period, }) {
return new ADX(series, period);
}
/**
* Get ADX values from input
* @param {AverageDirectionalIndexInput} input input data
* @return {ADXOutput[]} ADX values
*/
static calculate(input) {
const { series, period } = Object.assign(Object.assign({}, defaultInput), input);
const high = fp_1.map(types_1.OHLCEnum.HIGH)(series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series);
return technicalindicators_1.ADX.calculate({
high,
low,
close,
period,
});
}
}
exports.ADX = ADX;