@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
75 lines (74 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ATR = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const types_1 = require("../types");
const defaultInput = {
period: 14,
};
/**
* The average true range (ATR) is a technical analysis indicator,
* introduced by market technician J. Welles Wilder Jr.
* in his book New Concepts in Technical Trading Systems, that measures
* market volatility by decomposing the entire range of an asset price
* for that period. The true range indicator is taken as the greatest
* of the following: current high less the current low; the absolute
* value of the current high less the previous close; and the absolute
* value of the current low less the previous close. The ATR is then a
* moving average, generally using 14 days, of the true ranges.
*/
class ATR {
/**
* @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.ATR({ high, low, close, period });
}
/**
* Retrieve ATR values for instance
* @return {number[]} values for instance data
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calculate ATR to next tick
* @param {OHLC} candle new candle to add to series
* @return {number | undefined} next ATR value or undefined if period is
* greater than actual series length
*/
next(candle) {
return this.indicator.nextValue(candle);
}
/**
* Create instance from data
* @param {AverageTrueRangeInput} input input data
* @return {ATR} ATR instance
*/
static generator({ series, period }) {
return new ATR(series, period);
}
/**
* Get ATR values from input
* @param {AverageTrueRangeInput} input input data
* @return {number[]} ATR values
*/
static calculate({ series, period, }) {
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.ATR.calculate({
high,
low,
close,
period: period !== null && period !== void 0 ? period : defaultInput.period,
});
}
}
exports.ATR = ATR;