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 ]:
5
2
"""
6
3
Calculate the Relative Strength Index (RSI) for a given list of prices.
7
4
8
5
RSI is a momentum oscillator that measures the speed and change of price movements.
9
6
It is typically used to identify overbought or oversold conditions in a market.
10
7
11
8
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.
13
10
period (int): The number of periods to use in the calculation (default is 14).
14
11
15
12
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.
17
14
18
15
Example:
19
16
>>> 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]:
22
19
Reference:
23
20
https://en.wikipedia.org/wiki/Relative_strength_index
24
21
"""
25
-
26
22
# Validate that there are enough prices to calculate RSI
27
23
if len (prices ) < period :
28
24
raise ValueError ("Not enough price data to calculate RSI." )
29
25
30
- # Initialize lists to store gains and losses
31
26
gains = []
32
27
losses = []
33
28
34
29
# Calculate price changes between consecutive days
35
30
for i in range (1 , len (prices )):
36
31
delta = prices [i ] - prices [i - 1 ]
37
-
38
- # Gain if delta is positive, otherwise it's a loss
39
32
gains .append (max (0 , delta ))
40
33
losses .append (max (0 , - delta ))
41
34
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 )
45
38
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 ] = []
48
41
49
- # First RSI value calculation
50
42
if avg_loss == 0 :
51
- rsi = 100 # No loss means RSI is 100 (overbought)
43
+ rsi = 100.0
52
44
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 ))
55
47
56
48
rsi_values .append (rsi )
57
49
58
- # Calculate the rest of the RSI values using smoothed moving averages
50
+ # Calculate subsequent RSI values
59
51
for i in range (period , len (prices )):
60
52
delta = prices [i ] - prices [i - 1 ]
61
-
62
- # Update gains and losses
63
53
gain = max (0 , delta )
64
54
loss = max (0 , - delta )
65
55
66
- # Calculate smoothed averages
67
56
avg_gain = (avg_gain * (period - 1 ) + gain ) / period
68
57
avg_loss = (avg_loss * (period - 1 ) + loss ) / period
69
58
70
- # Compute RSI
71
59
if avg_loss == 0 :
72
- rsi = 100
60
+ rsi = 100.0
73
61
else :
74
62
rs = avg_gain / avg_loss
75
- rsi = 100 - (100 / (1 + rs ))
63
+ rsi = 100.0 - (100.0 / (1.0 + rs ))
76
64
77
- # Append RSI value to list
78
65
rsi_values .append (rsi )
79
66
80
67
return rsi_values
81
68
82
69
83
- # Example usage:
84
70
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 ]
102
72
rsi = calculate_rsi (prices , 14 )
103
73
print ("RSI Values:" , rsi )
0 commit comments