Skip to content

Commit ad554ad

Browse files
authored
Merge pull request #1 from tlgoa/tlgoa-patch-1
Update and rename Relative Strength Index (RSI).py to relative_streng…
2 parents 09fa901 + 2c3a193 commit ad554ad

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
from typing import List
2-
3-
4-
def calculate_rsi(prices: List[float], period: int = 14) -> List[float]:
1+
def calculate_rsi(prices: list[float], period: int = 14) -> list[float]:
52
"""
63
Calculate the Relative Strength Index (RSI) for a given list of prices.
74
85
RSI is a momentum oscillator that measures the speed and change of price movements.
96
It is typically used to identify overbought or oversold conditions in a market.
107
118
Args:
12-
prices (List[float]): A list of prices for a financial asset.
9+
prices (list[float]): A list of prices for a financial asset.
1310
period (int): The number of periods to use in the calculation (default is 14).
1411
1512
Returns:
16-
List[float]: A list of RSI values corresponding to the input price data.
13+
list[float]: A list of RSI values corresponding to the input price data.
1714
1815
Example:
1916
>>> calculate_rsi([44.0, 44.15, 44.09, 44.20, 44.30, 44.25, 44.40], 14)
@@ -22,82 +19,55 @@ def calculate_rsi(prices: List[float], period: int = 14) -> List[float]:
2219
Reference:
2320
https://en.wikipedia.org/wiki/Relative_strength_index
2421
"""
25-
2622
# Validate that there are enough prices to calculate RSI
2723
if len(prices) < period:
2824
raise ValueError("Not enough price data to calculate RSI.")
2925

30-
# Initialize lists to store gains and losses
3126
gains = []
3227
losses = []
3328

3429
# Calculate price changes between consecutive days
3530
for i in range(1, len(prices)):
3631
delta = prices[i] - prices[i - 1]
37-
38-
# Gain if delta is positive, otherwise it's a loss
3932
gains.append(max(0, delta))
4033
losses.append(max(0, -delta))
4134

42-
# Calculate the initial average gain and average loss (Simple Moving Average)
43-
avg_gain = sum(gains[:period]) / period
44-
avg_loss = sum(losses[:period]) / period
35+
# Initial averages for gain and loss
36+
avg_gain = float(sum(gains[:period]) / period)
37+
avg_loss = float(sum(losses[:period]) / period)
4538

46-
# Initialize the RSI list with the first calculated RSI value
47-
rsi_values = []
39+
# Initialize RSI list and first RSI value
40+
rsi_values: list[float] = []
4841

49-
# First RSI value calculation
5042
if avg_loss == 0:
51-
rsi = 100 # No loss means RSI is 100 (overbought)
43+
rsi = 100.0
5244
else:
53-
rs = avg_gain / avg_loss # Relative Strength
54-
rsi = 100 - (100 / (1 + rs)) # RSI formula
45+
rs = avg_gain / avg_loss
46+
rsi = 100.0 - (100.0 / (1.0 + rs))
5547

5648
rsi_values.append(rsi)
5749

58-
# Calculate the rest of the RSI values using smoothed moving averages
50+
# Calculate subsequent RSI values
5951
for i in range(period, len(prices)):
6052
delta = prices[i] - prices[i - 1]
61-
62-
# Update gains and losses
6353
gain = max(0, delta)
6454
loss = max(0, -delta)
6555

66-
# Calculate smoothed averages
6756
avg_gain = (avg_gain * (period - 1) + gain) / period
6857
avg_loss = (avg_loss * (period - 1) + loss) / period
6958

70-
# Compute RSI
7159
if avg_loss == 0:
72-
rsi = 100
60+
rsi = 100.0
7361
else:
7462
rs = avg_gain / avg_loss
75-
rsi = 100 - (100 / (1 + rs))
63+
rsi = 100.0 - (100.0 / (1.0 + rs))
7664

77-
# Append RSI value to list
7865
rsi_values.append(rsi)
7966

8067
return rsi_values
8168

8269

83-
# Example usage:
8470
if __name__ == "__main__":
85-
prices = [
86-
44.0,
87-
44.15,
88-
44.09,
89-
44.20,
90-
44.30,
91-
44.25,
92-
44.40,
93-
44.35,
94-
44.50,
95-
44.60,
96-
44.55,
97-
44.75,
98-
44.80,
99-
44.70,
100-
44.85,
101-
]
71+
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]
10272
rsi = calculate_rsi(prices, 14)
10373
print("RSI Values:", rsi)

0 commit comments

Comments
 (0)