Overview
The relative strength indicator is an oscillator that moves between 0 and 100 and is thought to show overbought or oversold conditions. The indicator requires a period to do its calculation. (The period is typically set to 14 days.)
The calculation is performed in 2 steps.
Calculation
The first step calculates a number called the relative strength. The relative strength is defined as
{% RS = \frac{average \; gain}{average \; loss} %}
Where the average is calculated over the defined period. That is, if the period is defined to be
14 days, then the average is taken over the last 14 days.
Then the index is defined as
{% 100 - 100/[1 + RS] %}
Average Calculation
The RSI calculation depends on calculating the average gain and average loss. The simplest way to do this is to use a simple average. In order to smooth out the calculation, other calculations can be used, such as an exponentally weighted moving average.
API
The davinci library hosts a technical analysis module that can be used to calculate rsi.
let tc = await import('/lib/finance/technical/v1.0.0/technical.mjs');
let data = [{price:100},{price:101},{price:101},{price:100},{price:102},{price:103},{price:102},{price:103},{price:100},{price:102},
{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},
{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},{price:100},];
let test = tc.rsi(data.map(p=>p.price), 14);
Try it!
Alternative Uses
The rsi calculation can be used on data other than prices. For instance, when applied to price * volume, you get an indicator sometimes referred to the money flow index.