@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
101 lines (100 loc) • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StochRSI = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const types_1 = require("../types");
const defaultInput = {
source: types_1.OHLCEnum.CLOSE,
kPeriod: 3,
dPeriod: 3,
rsiPeriod: 14,
stochasticPeriod: 14,
};
/**
* The Stochastic RSI (StochRSI) is an indicator used in technical analysis
* that ranges between zero and one (or zero and 100 on some charting
* platforms) and is created by applying the Stochastic oscillator formula
* to a set of relative strength index (RSI) values rather than to standard
* price data. Using RSI values within the Stochastic formula gives traders an
* idea of whether the current RSI value is overbought or oversold.
* The StochRSI oscillator was developed to take advantage of both momentum
* indicators in order to create a more sensitive indicator that is attuned to
* a specific security's historical performance rather than a generalized
* analysis of price change.
*/
class StochRSI {
/**
*
* @param {OHLC[]} series candles series
* @param {OHLCEnum} source source of values
* @param {number} kPeriod
* @param {number} dPeriod
* @param {number} rsiPeriod
* @param {number} stochasticPeriod
*/
constructor(series, source = defaultInput.source, kPeriod = defaultInput.kPeriod, dPeriod = defaultInput.dPeriod, rsiPeriod = defaultInput.rsiPeriod, stochasticPeriod = defaultInput.stochasticPeriod) {
// super();
const values = fp_1.map(source)(series);
this.indicator = new technicalindicators_1.StochasticRSI({
values,
kPeriod,
dPeriod,
rsiPeriod,
stochasticPeriod,
});
this.source = source;
this.dPeriod = dPeriod;
this.kPeriod = kPeriod;
this.rsiPeriod = rsiPeriod;
this.stochasticPeriod = stochasticPeriod;
}
/**
* Retrieve StochRSI values for instance
* @return {StochasticRSIOutput[]} values for instance data
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calculate StochRSI to next tick
* @param {OHLC} candle new candle to add to series
* @return {StochasticRSIOutput | undefined} next StochRSI value or
* undefined if period is greater than actual series length
*/
next(candle) {
const values = [candle[this.source]];
return this.indicator.nextValue({
values,
kPeriod: this.kPeriod,
dPeriod: this.dPeriod,
rsiPeriod: this.rsiPeriod,
stochasticPeriod: this.stochasticPeriod,
});
}
/**
* Create instance from data
* @param {StochasticRSIInput} input input data
* @return {StochRSI} StochRSI instance
*/
static generator({ series, source, kPeriod, dPeriod, rsiPeriod, stochasticPeriod, }) {
return new StochRSI(series, source, kPeriod, dPeriod, rsiPeriod, stochasticPeriod);
}
/**
* Get StochRSI values from input
* @param {StochasticRSIInput} input input data
* @return {StochasticRSIOutput[]} StochRSI values
*/
static calculate(input) {
const { series, source, kPeriod, dPeriod, rsiPeriod, stochasticPeriod, } = Object.assign(Object.assign({}, defaultInput), input);
const values = fp_1.map(source)(series);
return technicalindicators_1.StochasticRSI.calculate({
values,
kPeriod,
dPeriod,
rsiPeriod,
stochasticPeriod,
});
}
}
exports.StochRSI = StochRSI;