UNPKG

@4ex/indicators

Version:

Technical indicators for ohlc charts written in TypeScript

75 lines (74 loc) 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RSI = void 0; const fp_1 = require("lodash/fp"); const technicalindicators_1 = require("technicalindicators"); const types_1 = require("../types"); const defaultInput = { period: 14, source: types_1.OHLCEnum.CLOSE, }; /** * The relative strength index (RSI) is a momentum indicator used in technical * analysis that measures the magnitude of recent price changes to evaluate * overbought or oversold conditions in the price of a stock or other asset. * The RSI is displayed as an oscillator (a line graph that moves between two * extremes) and can have a reading from 0 to 100. The indicator was originally * developed by J. Welles Wilder Jr. and introduced in his seminal 1978 book, * "New Concepts in Technical Trading Systems". Traditional interpretation and * usage of the RSI are that values of 70 or above indicate that a security is * becoming overbought or overvalued and may be primed for a trend reversal or * corrective pullback in price. An RSI reading of 30 or below indicates an * oversold or undervalued condition. */ class RSI { /** * @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.RSI({ values, period }); this.source = source; } /** * Retrieve RSI values for instance * @return {number[]} values for instance data */ getResults() { return this.indicator.getResult(); } /** * Calculate RSI to next tick * @param {OHLC} candle new candle to add to series * @return {number | undefined} next RSI 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 {RelativeStrengthIndexInput} input input data * @return {RSI} RSI instance */ static generator({ series, source, period, }) { return new RSI(series, source, period); } /** * Get RSI values from input * @param {RelativeStrengthIndexInput} input input data * @return {number[]} RSI values */ static calculate(input) { const { series, source, period } = Object.assign(Object.assign({}, defaultInput), input); const values = fp_1.map(source)(series); return technicalindicators_1.RSI.calculate({ values, period, }); } } exports.RSI = RSI;