From 2af0ccfab7800bcffd082c9d14751afa4e67b76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tolga=20G=C3=B6=C4=9Febakan?= <39372186+tlgoa@users.noreply.github.com> Date: Fri, 4 Oct 2024 18:03:31 +0300 Subject: [PATCH 1/2] Create relative_strength_index.py --- financial/relative_strength_index.py | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 financial/relative_strength_index.py diff --git a/financial/relative_strength_index.py b/financial/relative_strength_index.py new file mode 100644 index 000000000000..dff6c2995a05 --- /dev/null +++ b/financial/relative_strength_index.py @@ -0,0 +1,73 @@ +def calculate_rsi(prices: list[float], period: int = 14) -> list[float]: + """ + Calculate the Relative Strength Index (RSI) for a given list of prices. + + RSI is a momentum oscillator that measures the speed and change of price movements. + It is typically used to identify overbought or oversold conditions in a market. + + Args: + prices (list[float]): A list of prices for a financial asset. + period (int): The number of periods to use in the calculation (default is 14). + + Returns: + list[float]: A list of RSI values corresponding to the input price data. + + Example: + >>> calculate_rsi([44.0, 44.15, 44.09, 44.20, 44.30, 44.25, 44.40], 14) + [100.0, 100.0, 100.0, 100.0, 100.0, 100.0] + + Reference: + https://en.wikipedia.org/wiki/Relative_strength_index + """ + # Validate that there are enough prices to calculate RSI + if len(prices) < period: + raise ValueError("Not enough price data to calculate RSI.") + + gains = [] + losses = [] + + # Calculate price changes between consecutive days + for i in range(1, len(prices)): + delta = prices[i] - prices[i - 1] + gains.append(max(0, delta)) + losses.append(max(0, -delta)) + + # Initial averages for gain and loss + avg_gain = float(sum(gains[:period]) / period) + avg_loss = float(sum(losses[:period]) / period) + + # Initialize RSI list and first RSI value + rsi_values: list[float] = [] + + if avg_loss == 0: + rsi = 100.0 + else: + rs = avg_gain / avg_loss + rsi = 100.0 - (100.0 / (1.0 + rs)) + + rsi_values.append(rsi) + + # Calculate subsequent RSI values + for i in range(period, len(prices)): + delta = prices[i] - prices[i - 1] + gain = max(0, delta) + loss = max(0, -delta) + + avg_gain = (avg_gain * (period - 1) + gain) / period + avg_loss = (avg_loss * (period - 1) + loss) / period + + if avg_loss == 0: + rsi = 100.0 + else: + rs = avg_gain / avg_loss + rsi = 100.0 - (100.0 / (1.0 + rs)) + + rsi_values.append(rsi) + + return rsi_values + + +if __name__ == "__main__": + prices = [44.0, 44.15, 44.09, 44.20, 44.30, 44.25, 44.40, 44.35, 44.50, 44.60, 44.55, 44.75, 44.80, 44.70, 44.85] + rsi = calculate_rsi(prices, 14) + print("RSI Values:", rsi) From c4851d62b5b148d17762538ff465bcc0531e3af2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 15:05:08 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/relative_strength_index.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/financial/relative_strength_index.py b/financial/relative_strength_index.py index dff6c2995a05..e57d36e7864d 100644 --- a/financial/relative_strength_index.py +++ b/financial/relative_strength_index.py @@ -68,6 +68,22 @@ def calculate_rsi(prices: list[float], period: int = 14) -> list[float]: if __name__ == "__main__": - prices = [44.0, 44.15, 44.09, 44.20, 44.30, 44.25, 44.40, 44.35, 44.50, 44.60, 44.55, 44.75, 44.80, 44.70, 44.85] + prices = [ + 44.0, + 44.15, + 44.09, + 44.20, + 44.30, + 44.25, + 44.40, + 44.35, + 44.50, + 44.60, + 44.55, + 44.75, + 44.80, + 44.70, + 44.85, + ] rsi = calculate_rsi(prices, 14) print("RSI Values:", rsi)