Relative Strength Index
The
(RSI) is a momentum oscillator that measures the speed and change of price movements. It oscillates between zero and one hundred and is typically used to identify overbought or oversold conditions in a market. An RSI reading above 70 suggests that a security may be overbought, while a reading below 30 indicates it may be oversold. The RSI can also signal potential reversals when it diverges from the price trend.
Market Signal
One approach to using the RSI is to identify "divergences" where the security's price hits a new high or low, but the RSI does not, suggesting a potential reversal. A "failure swing" occurs when the RSI fails to exceed a previous high (in an uptrend) or a previous low (in a downtrend) and then crosses its most recent trough. This is often seen as confirmation of an impending trend reversal.
Calculation
The RSI is calculated with the following steps:
- Compute the initial upward and downward movements (
Up_i
andDown_i
) for each period:
if(Close_i > Close_i-1) {
Up_i = Close_i - Close_i-1;
Down_i = 0;
} else {
Up_i = 0;
Down_i = Close_i - Close_i-1;
}
- Calculate the average of these upward and downward movements over a given period using a Simple Moving Average (SMA):
UpAvg = SMA(Up, Period), DownAvg = SMA(Down, Period)
- Compute the RSI:
RSI = 100 * UpAvg / (UpAvg + DownAvg)
RSI values are plotted over time to form a dynamic line oscillating between 0 and 100, helping traders gauge the momentum behind price movements.
import { Bar , InstrumentProcessorBase } from '@botmain/common';
import { RelativeStrengthIndex } from '@botmain/indicators';
export class InstrumentProcessor extends InstrumentProcessorBase {
rsa = new RelativeStrengthIndex (28);
...
onBarClose(bar: Bar ) {
//Add bar close to calculate new simple moving average
this.rsa.addBar(bar);
...
//Get current indicator value
const rsaValue = this.rsa.last;
//Get previous indicator value
if(this.rsa.historyCount >= 2) {
const previousRsiValue = this.rsa.values[1];
}
}
}