diff --git a/src/indicator/momentum/rsi.test.ts b/src/indicator/momentum/rsi.test.ts new file mode 100644 index 0000000..6b0aa3d --- /dev/null +++ b/src/indicator/momentum/rsi.test.ts @@ -0,0 +1,22 @@ +// Copyright (c) 2022 Onur Cinar. All Rights Reserved. +// https://github.com/cinar/indicatorts + +import { deepStrictEqual } from 'assert'; +import { roundDigitsAll } from '../../helper/numArray'; +import { customRsi } from './rsi'; + +describe('Relative Strength Index (RSI)', () => { + it('should be able to compute the custom RSI', () => { + const closings = [ + 10, 12, 11, 11, 14, 16, 18, 17, 18, 19, 16, 14, 14, 15, 16, 19, 20, 22, + ]; + + const expected = [ + 0, 100, 66.67, 66.67, 83.33, 87.5, 90, 81.82, 83.33, 84.62, 68.75, 61.11, + 61.11, 63.16, 65.13, 70.28, 71.78, 74.54, + ]; + + const actual = customRsi(14, closings); + deepStrictEqual(roundDigitsAll(2, actual), expected); + }); +}); diff --git a/src/indicator/momentum/rsi.ts b/src/indicator/momentum/rsi.ts index e4251b7..64a5dee 100644 --- a/src/indicator/momentum/rsi.ts +++ b/src/indicator/momentum/rsi.ts @@ -19,6 +19,8 @@ export function customRsi(period: number, closings: number[]): number[] { const gains = new Array(closings.length); const losses = new Array(closings.length); + gains[0] = losses[0] = 0; + for (let i = 1; i < closings.length; i++) { const difference = closings[i] - closings[i - 1]; @@ -37,7 +39,9 @@ export function customRsi(period: number, closings: number[]): number[] { const r = new Array(closings.length); const rs = new Array(closings.length); - for (let i = 0; i < closings.length; i++) { + r[0] = rs[0] = 0; + + for (let i = 1; i < closings.length; i++) { rs[i] = meanGains[i] / meanLosses[i]; r[i] = 100 - 100 / (1 + rs[i]); } diff --git a/src/indicator/volatility/bollingerBandsWidth.ts b/src/indicator/volatility/bollingerBandsWidth.ts index ea149fd..8b82640 100644 --- a/src/indicator/volatility/bollingerBandsWidth.ts +++ b/src/indicator/volatility/bollingerBandsWidth.ts @@ -29,10 +29,7 @@ export interface BollingerBandsWidth { * @return bollinger bands width result. */ export function bollingerBandsWidth(bb: BollingerBands): BollingerBandsWidth { - const bandWidth = divide( - subtract(bb.upperBand, bb.lowerBand), - bb.middleBand - ); + const bandWidth = divide(subtract(bb.upperBand, bb.lowerBand), bb.middleBand); const bandWidthEma90 = ema(PERIOD, bandWidth); diff --git a/src/indicator/volatility/projectionOscillator.ts b/src/indicator/volatility/projectionOscillator.ts index 183e1fa..8a01751 100644 --- a/src/indicator/volatility/projectionOscillator.ts +++ b/src/indicator/volatility/projectionOscillator.ts @@ -58,10 +58,7 @@ export function projectionOscillator( const pu = mmax(period, vHighs); const pl = mmin(period, vLows); - const po = divide( - multiplyBy(100, subtract(closings, pl)), - subtract(pu, pl) - ); + const po = divide(multiplyBy(100, subtract(closings, pl)), subtract(pu, pl)); const spo = ema(smooth, po); return {