Skip to content

Commit

Permalink
RSI returns NaN array (#103)
Browse files Browse the repository at this point in the history
* RSI returns `NaN` array
Fixes #102

* Removing logging.
  • Loading branch information
cinar authored Jun 23, 2022
1 parent 044eb7e commit 1313764
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/indicator/momentum/rsi.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 5 additions & 1 deletion src/indicator/momentum/rsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function customRsi(period: number, closings: number[]): number[] {
const gains = new Array<number>(closings.length);
const losses = new Array<number>(closings.length);

gains[0] = losses[0] = 0;

for (let i = 1; i < closings.length; i++) {
const difference = closings[i] - closings[i - 1];

Expand All @@ -37,7 +39,9 @@ export function customRsi(period: number, closings: number[]): number[] {
const r = new Array<number>(closings.length);
const rs = new Array<number>(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]);
}
Expand Down
5 changes: 1 addition & 4 deletions src/indicator/volatility/bollingerBandsWidth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 1 addition & 4 deletions src/indicator/volatility/projectionOscillator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1313764

Please sign in to comment.