Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Relative Strength Index (RSI) #3

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Provide a TypeScript implementation for common technical indicators with arbitra
## Supported Indicators

- Exponential Moving Average (EMA)
- Relative Strength Index (RSI)
- Simple Moving Average (SMA)
- Smoothed Moving Average (SMMA)

Expand Down
3 changes: 2 additions & 1 deletion nyc.config.coverage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const defaultConfig = require('./nyc.config');

module.exports = Object.assign({}, defaultConfig, {
'check-coverage': true,
'skip-full': true,
all: true,
branches: 100,
'check-coverage': true,
functions: 100,
lines: 100,
reporter: ['html'],
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
"homepage": "https://documentup.com/bennyn/trading-signals",
"keywords": [
"ema",
"smma",
"trading",
"trading-signals",
"exponential-moving-average",
"moving-average",
"rsi",
"simple-moving-average",
"sma",
"smma",
"technical-analysis",
"trading",
"trading-signals",
"trading-signals"
],
"license": "UNLICENSED",
Expand Down
47 changes: 47 additions & 0 deletions src/RSI/RSI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Big as BigNumber} from 'big.js';
import {RSI} from './RSI';

import prices from '../test/fixtures/prices.json';
import results from '../test/fixtures/RSI/results.json';

const rsi2results = results.interval_2;
const rsi12results = results.interval_12;
const rsi26results = results.interval_26;

describe('RSI', () => {
describe('getResult', () => {
it('calculates RSI with interval 2', () => {
const rsi = new RSI(2);
prices.forEach((price, index) => {
rsi.update(price);
const res = new BigNumber(rsi2results[index]);
expect(rsi.getResult().toPrecision(12)).toEqual(res.toPrecision(12));
});
});

it('calculates RSI with interval 12', () => {
const rsi = new RSI(12);
prices.forEach((price, index) => {
rsi.update(price);
const res = new BigNumber(rsi12results[index]);
expect(rsi.getResult().toPrecision(12)).toEqual(res.toPrecision(12));
});
});

it('calculates RSI with interval 26', () => {
const rsi = new RSI(26);
prices.forEach((price, index) => {
rsi.update(price);
const res = new BigNumber(rsi26results[index]);
expect(rsi.getResult().toPrecision(12)).toEqual(res.toPrecision(12));
});
});

it('prevents division by zero errors when the average gain and average loss equal 0', () => {
const rsi = new RSI(1);
rsi.update(0);
rsi.update(0);
expect(rsi.getResult().toFixed(0)).toBe('99');
});
});
});
60 changes: 60 additions & 0 deletions src/RSI/RSI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Big, {BigSource} from 'big.js';
import {SMMA} from '..';

export class RSI {
private readonly interval: number;
private readonly prices: Big[] = [];
private result: Big;

private readonly avgGain: SMMA;
private readonly avgLoss: SMMA;

constructor(interval: number) {
this.interval = interval;

this.avgGain = new SMMA(this.interval);
this.avgLoss = new SMMA(this.interval);

this.result = new Big(0);
}

update(price: BigSource): void {
const currentClose = new Big(price);
this.prices.push(currentClose);

// at least 2 prices are required to do a calculation
if (this.prices.length === 1) {
return;
}

const lastClose = this.prices[this.prices.length - 2];

// Update average gain/loss
if (currentClose.gt(lastClose)) {
this.avgLoss.update(new Big(0)); // price went up, therefore no loss
this.avgGain.update(currentClose.sub(lastClose));
} else {
this.avgLoss.update(lastClose.sub(currentClose));
this.avgGain.update(new Big(0)); // price went down, therefore no gain
}

// as long as there are not enough values as the required interval, the result should always be 0
if (this.prices.length <= this.interval) {
this.result = new Big(0);
return;
}

const relativeStrength = this.avgLoss.getResult().eq(new Big(0))
? new Big(100)
: this.avgGain.getResult().div(this.avgLoss.getResult());

const max = new Big(100);
this.result = max.minus(max.div(relativeStrength.add(1)));

this.prices.shift();
}

getResult(): Big {
return this.result;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './EMA/EMA';
export * from './RSI/RSI';
export * from './SMA/SMA';
export * from './SMMA/SMMA';
125 changes: 125 additions & 0 deletions src/test/fixtures/RSI/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"interval_2": [
0,
0,
47.22222222222223,
23.611111111111114,
38.43283582089553,
30.294117647058826,
78.2967032967033,
86.31639722863741,
89.12844036697248,
13.311866264730071,
64.95013850415512,
59.85653017461453,
54.19016363132106,
12.8454188854557,
68.56748255340673,
57.41553886370404,
26.512530826658676,
59.75774345724092,
36.04149739852744,
17.51008036340795,
26.905774699598737,
58.00044863973779,
85.82728624443239,
38.371227493966536,
74.96090570884536,
61.21019495608749,
19.438903652597787,
76.51344601664712,
72.30324117451528,
69.74473974944168,
84.24228523539779,
10.429850167468388,
59.707866801087974,
27.99023519523047,
48.57675474701421,
34.80106793288934,
81.96579567492182,
57.78953245436237,
23.585658801479056
],
"interval_12": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
49.44320712694878,
42.432667245873155,
51.2017782300719,
49.94116787991835,
45.55663590860508,
49.284333951746184,
46.74501674557244,
43.4860123510052,
44.01823674189631,
45.82704797004994,
49.90209564896179,
46.351969981868436,
51.34208640997234,
50.37501845071388,
44.96351306736635,
54.464714123844956,
54.04642006918241,
53.895901697369816,
55.6476477612053,
42.60631369413207,
51.2964732419777,
43.83906729252931,
47.00600661743475,
45.08586375053323,
54.44633399991266,
51.6681674437389,
45.44886082103851
],
"interval_26": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
46.44444444444444,
50.61070579555701,
50.44298978067838,
50.385107396174675,
51.143071170125296,
45.77270174725033,
49.6130641968312,
46.115177339098764,
47.56394036014283,
46.660676933508505,
51.11283316502063,
49.861270146262456,
46.92369439491447
]
}