augment_rsi

augment_rsi(data, date_column, close_column, periods=14, reduce_memory=False, engine='pandas')

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.

Parameters

Name Type Description Default
data Union[pd.DataFrame, pd.core.groupby.generic.DataFrameGroupBy] 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
  1. 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 pd
import pytimetk as tk

df = tk.load_dataset('stocks_daily', parse_dates=['date'])
df

# Example 1 - Calculate RSI for a single column
rsi_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 groups
rsi_df = (
    df
        .groupby('symbol')
        .augment_rsi(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28]
        )
)
rsi_df.groupby('symbol').tail(1)
symbol date open high low close volume adjusted ['adjusted']_rsi_14 ['adjusted']_rsi_28
2698 META 2023-09-21 295.700012 300.260010 293.269989 295.730011 21300500 295.730011 49.861252 47.648225
5397 AMZN 2023-09-21 131.940002 132.240005 129.309998 129.330002 70234800 129.330002 36.274533 41.651344
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 28.474387 46.929686
10795 NFLX 2023-09-21 386.500000 395.899994 383.420013 384.149994 5547900 384.149994 21.937687 40.284900
13494 NVDA 2023-09-21 415.829987 421.000000 409.799988 410.170013 44893000 410.170013 8.187872 50.335641
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 33.258816 51.458691
# Example 3 - Calculate RSI for polars engine
rsi_df = (
    df
        .query("symbol == 'AAPL'")
        .augment_rsi(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28],
            engine='polars'
        )
)
rsi_df
symbol date open high low close volume adjusted adjusted_rsi_14 adjusted_rsi_28
0 AAPL 2013-01-02 19.779285 19.821428 19.343929 19.608213 560518000 16.791180 NaN NaN
1 AAPL 2013-01-03 19.567142 19.631071 19.321428 19.360714 352965200 16.579241 NaN NaN
2 AAPL 2013-01-04 19.177500 19.236786 18.779642 18.821428 594333600 16.117437 NaN NaN
3 AAPL 2013-01-07 18.642857 18.903570 18.400000 18.710714 484156400 16.022623 NaN NaN
4 AAPL 2013-01-08 18.900356 18.996071 18.616072 18.761070 458707200 16.065746 NaN NaN
... ... ... ... ... ... ... ... ... ... ...
2694 AAPL 2023-09-15 176.479996 176.500000 173.820007 175.009995 109205100 175.009995 44.451286 46.814996
2695 AAPL 2023-09-18 176.479996 179.380005 176.169998 177.970001 67257600 177.970001 46.717917 48.643316
2696 AAPL 2023-09-19 177.520004 179.630005 177.130005 179.070007 51826900 179.070007 41.852236 50.965781
2697 AAPL 2023-09-20 179.259995 179.699997 175.399994 175.490005 58436200 175.490005 30.412405 48.174715
2698 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 28.474387 46.929686

2699 rows ร— 10 columns

# Example 4 - Calculate RSI for polars engine and groups
rsi_df = (
    df
        .groupby('symbol')
        .augment_rsi(
            date_column='date',
            close_column='adjusted',
            periods=[14, 28],
            engine='polars'
        )
)
rsi_df.groupby('symbol').tail(1)
symbol date open high low close volume adjusted adjusted_rsi_14 adjusted_rsi_28
2698 META 2023-09-21 295.700012 300.260010 293.269989 295.730011 21300500 295.730011 49.861252 47.648225
5397 AMZN 2023-09-21 131.940002 132.240005 129.309998 129.330002 70234800 129.330002 36.274533 41.651344
8096 AAPL 2023-09-21 174.550003 176.300003 173.860001 173.929993 63047900 173.929993 28.474387 46.929686
10795 NFLX 2023-09-21 386.500000 395.899994 383.420013 384.149994 5547900 384.149994 21.937687 40.284900
13494 NVDA 2023-09-21 415.829987 421.000000 409.799988 410.170013 44893000 410.170013 8.187872 50.335641
16193 GOOG 2023-09-21 132.389999 133.190002 131.089996 131.360001 22042700 131.360001 33.258816 51.458691