@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
87 lines (86 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WR = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const types_1 = require("../types");
const defaultInput = {
period: 14,
};
/**
* Williams %R, also known as the Williams Percent Range, is a type of
* momentum indicator that moves between 0 and -100 and measures overbought
* and oversold levels. The Williams %R may be used to find entry and exit
* points in the market. The indicator is very similar to the Stochastic
* oscillator and is used in the same way. It was developed by Larry Williams
* and it compares a stock’s closing price to the high-low range over a
* specific period, typically 14 days or periods.
*/
class WR {
/**
* @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 close = fp_1.map(types_1.OHLCEnum.CLOSE)(series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(series);
this.indicator = new technicalindicators_1.WilliamsR({ high, low, close, period });
this.series = series;
this.period = period;
}
/**
* Retrieve WR values for instance
* @return {number[]} values for instance data
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calculate WR to next tick
* @param {OHLC} candle new candle to add to series
* @return {number | undefined} next WR value or undefined if period is
* greater than actual series length
*/
next(candle) {
var _a;
// INFO: workaround to enable next
this.series = [...this.series, candle];
const high = fp_1.map(types_1.OHLCEnum.HIGH)(this.series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(this.series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(this.series);
this.indicator = new technicalindicators_1.WilliamsR({
high,
low,
close,
period: this.period,
});
return (_a = this.getResults()) === null || _a === void 0 ? void 0 : _a.pop();
}
/**
* Create instance from data
* @param {WilliamsPercentRangeInput} input input data
* @return {WR} WR instance
*/
static generator({ series, period }) {
return new WR(series, period);
}
/**
* Get WR values from input
* @param {WilliamsPercentRangeInput} input input data
* @return {number[]} WR values
*/
static calculate({ series, period, }) {
const high = fp_1.map(types_1.OHLCEnum.HIGH)(series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(series);
return technicalindicators_1.WilliamsR.calculate({
high,
low,
close,
period: period !== null && period !== void 0 ? period : defaultInput.period,
});
}
}
exports.WR = WR;