The augment_rsi function calculates the Relative Strength Index (RSI) for a given financial instrument using either pandas or polars engine, and returns the augmented DataFrame.
The data parameter is the input data that can be either a pandas DataFrame or a pandas DataFrameGroupBy object. It contains the data on which the RSI will be calculated.
required
date_column
str
The name of the column in the data that contains the dates or timestamps.
required
close_column
str
The close_column parameter is used to specify the column(s) in the input data that contain the values on which the RSI will be calculated. It can be either a single column name (string) or a list of column names (if you want to calculate RSI on multiple columns).
required
periods
Union[int, Tuple[int, int], List[int]]
The periods parameter in the augment_rsi function specifies the number of rolling periods over which the RSI is calculated. It can be provided as an integer, a tuple of two integers (start and end periods), or a list of integers.
14
reduce_memory
bool
The reduce_memory parameter is a boolean flag that indicates whether or not to reduce the memory usage of the data before performing the RSI calculation. If set to True, the function will attempt to reduce the memory usage of the input data. If set to False, the function will not attempt to reduce the memory usage of the input data.
False
engine
str
The engine parameter specifies the computation engine to use for calculating the RSI. It can take two values: โpandasโ or โpolarsโ.
'pandas'
Returns
Type
Description
pd.DataFrame
The function augment_rsi returns a pandas DataFrame that contains the augmented data with the Relative Strength Index (RSI) values added.
Notes
The Relative Strength Index (RSI) is a momentum oscillator that measures the speed and change of price movements. Developed by J. Welles Wilder Jr. and introduced in his 1978 book โNew Concepts in Technical Trading Systemsโ, the RSI is one of the most well-known and widely used technical analysis indicators.
Range: The RSI oscillates between 0 and 100.
Overbought and Oversold Levels: Traditionally, the RSI is considered overbought when above 70 and oversold when below
These thresholds can indicate potential reversal points where a security is overvalued or undervalued.
Divergence: RSI can also be used to identify potential reversals by looking for bearish and bullish divergences.
Examples
import pandas as pdimport pytimetk as tkdf = tk.load_dataset('stocks_daily', parse_dates=['date'])df# Example 1 - Calculate RSI for a single columnrsi_df = ( df .query("symbol == 'AAPL'") .augment_rsi( date_column='date', close_column='adjusted', periods=[14, 28] ))rsi_df
symbol
date
open
high
low
close
volume
adjusted
['adjusted']_rsi_14
['adjusted']_rsi_28
5398
AAPL
2013-01-02
19.779285
19.821428
19.343929
19.608213
560518000
16.791180
NaN
NaN
5399
AAPL
2013-01-03
19.567142
19.631071
19.321428
19.360714
352965200
16.579241
NaN
NaN
5400
AAPL
2013-01-04
19.177500
19.236786
18.779642
18.821428
594333600
16.117437
NaN
NaN
5401
AAPL
2013-01-07
18.642857
18.903570
18.400000
18.710714
484156400
16.022623
NaN
NaN
5402
AAPL
2013-01-08
18.900356
18.996071
18.616072
18.761070
458707200
16.065746
NaN
NaN
...
...
...
...
...
...
...
...
...
...
...
8092
AAPL
2023-09-15
176.479996
176.500000
173.820007
175.009995
109205100
175.009995
44.451286
46.814996
8093
AAPL
2023-09-18
176.479996
179.380005
176.169998
177.970001
67257600
177.970001
46.717917
48.643316
8094
AAPL
2023-09-19
177.520004
179.630005
177.130005
179.070007
51826900
179.070007
41.852236
50.965781
8095
AAPL
2023-09-20
179.259995
179.699997
175.399994
175.490005
58436200
175.490005
30.412405
48.174715
8096
AAPL
2023-09-21
174.550003
176.300003
173.860001
173.929993
63047900
173.929993
28.474387
46.929686
2699 rows ร 10 columns
# Example 2 - Calculate RSI for multiple groupsrsi_df = ( df .groupby('symbol') .augment_rsi( date_column='date', close_column='adjusted', periods=[14, 28] ))rsi_df.groupby('symbol').tail(1)