-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrelativeStrengthIndex.ts
68 lines (55 loc) · 1.68 KB
/
relativeStrengthIndex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import { rma } from '../trend/rollingMovingAverage';
/**
* Optional configuration of RSI parameters.
*/
export interface RSIConfig {
period?: number;
}
/**
* The default configuration of RSI.
*/
export const RSIDefaultConfig: Required<RSIConfig> = {
period: 14,
};
/**
* Relative Strength Index (RSI). It is a momentum indicator that measures the magnitude of
* recent price changes to evaluate overbought and oversold conditions
* using the given window period.
*
* RS = Average Gain / Average Loss
* RSI = 100 - (100 / (1 + RS))
*
* @param closings closing values.
* @param config configuration.
* @return rsi values.
*/
export function rsi(closings: number[], config: RSIConfig = {}): number[] {
const { period } = { ...RSIDefaultConfig, ...config };
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];
if (difference > 0) {
gains[i] = difference;
losses[i] = 0;
} else {
losses[i] = -difference;
gains[i] = 0;
}
}
const meanGains = rma(gains, { period });
const meanLosses = rma(losses, { period });
const rValue = new Array<number>(closings.length);
const rsValue = new Array<number>(closings.length);
rValue[0] = rsValue[0] = 0;
for (let i = 1; i < closings.length; i++) {
rsValue[i] = meanGains[i] / meanLosses[i];
rValue[i] = 100 - 100 / (1 + rsValue[i]);
}
return rValue;
}
// Export full name
export { rsi as relativeStrengthIndex };