Skip to content

RSI Indicator

The Relative Strength Index (RSI) is a technical analysis indicator that measures the speed and change of price movements. It was developed by J. Welles Wilder Jr. and introduced in his 1978 book "New Concepts in Technical Trading Systems". The RSI is a momentum oscillator that ranges between 0 and 100 and is primarily used to identify overbought or oversold conditions in a security.

Overview

The RSI calculates the ratio of the average price gains to the average price losses over a specified period, usually 14 days. The RSI oscillates between 0 and 100, with readings above 70 generally considered overbought and readings below 30 considered oversold.


graph LR

  A[Price Gains and Losses] --> B[Compute Average Gain and Loss]

  B --> C[Compute RS]

  C --> D[Compute RSI]

Implementation

Our implementation uses PyTorch to compute the RSI indicator. The code defines a class called RSI that inherits from the nn.Module class. The RSI class has a forward method that takes two arguments: window_size and prices.

Main Steps

  1. Compute the individual gains and losses for each price change.
  2. Calculate the average gain and loss using a moving average with the specified window size.
  3. Compute the Relative Strength (RS) as the ratio of the average gain to the average loss.
  4. Calculate the RSI using the RS value.

Code Structure

The RSI class has the following methods:

  • __init__: Initializes the RSI class and sets the window_size attribute to None.
  • forward: Takes prices and window_size as input arguments, sets the window_size attribute, and computes the RSI.
  • compute_individual_gains_losses: Takes gains_losses as input and returns the individual gains and losses.
  • compute_avg_gain_loss: Takes gains and losses as input and returns the average gain and loss.
  • compute_rs: Takes avg_gain and avg_loss as input and returns the Relative Strength (RS).
  • compute_rsi: Takes rs as input and returns the RSI value.

Example Usage

import torch from torch.jit import script
from torchtrader.ta.rsi import RSI

# Set the seed
torch.manual_seed(42)
# Generate random test prices
prices = torch.randn(100)
# Prepare the RSI class for TorchScript
scripted_rsi_calculator = script(RSI())
window_size = 14
scripted_rsi = scripted_rsi_calculator(prices, window_size)
print(scripted_rsi)

This example demonstrates how to use the RSI class with TorchScript to compute the RSI indicator for a sequence of 100 random prices.

RSI

Bases: nn.Module

The RSI class is a PyTorch implementation of the Relative Strength Index (RSI) indicator.

graph LR
  A[Price Gains and Losses] --> B[Compute Average Gain and Loss]
  B --> C[Compute RS]
  C --> D[Compute RSI]

__init__(window_size=14)

Initializes the RSI class.

Parameters:

Name Type Description Default
window_size int

The size of the moving window to calculate

14

compute_avg_gain_loss(gains, losses)

Computes the average gain and loss using a moving window.

Parameters:

Name Type Description Default
gains torch.Tensor

The individual gains of price changes.

required
losses torch.Tensor

The individual losses of price changes.

required

Returns:

Type Description
torch.Tensor

Tuple[torch.Tensor, torch.Tensor]: A tuple containing the average

torch.Tensor

gain and loss.

compute_individual_gains_losses(gains_losses) staticmethod

Computes individual gains and losses from price changes.

Parameters:

Name Type Description Default
gains_losses torch.Tensor

The differences between consecutive

required

Returns:

Type Description
torch.Tensor

Tuple[torch.Tensor, torch.Tensor]: A tuple containing individual

torch.Tensor

gains and losses.

compute_rs(avg_gain, avg_loss) staticmethod

Computes the Relative Strength (RS) from the average gain and loss.

Parameters:

Name Type Description Default
avg_gain torch.Tensor

The average gain of price changes.

required
avg_loss torch.Tensor

The average loss of price changes.

required

Returns:

Type Description
torch.Tensor

torch.Tensor: The Relative Strength (RS) values.

compute_rsi(rs) staticmethod

Computes the RSI values from the Relative Strength (RS) values.

Parameters:

Name Type Description Default
rs torch.Tensor

The Relative Strength (RS) values.

required

Returns:

Type Description
torch.Tensor

torch.Tensor: The RSI values.

forward(prices, window_size)

Computes the RSI values for the given price series and window size.

Parameters:

Name Type Description Default
prices torch.Tensor

The price series for which RSI values are

required
window_size int

The size of the moving window to calculate

required

Returns:

Type Description
torch.Tensor

torch.Tensor: The RSI values for the given price series and window

torch.Tensor

size.