|
| 1 | +def calculate_rsi(prices, period=14): |
| 2 | + """ |
| 3 | + Calculate the Relative Strength Index (RSI) for a given list of prices. |
| 4 | + |
| 5 | + RSI is a momentum oscillator that measures the speed and change of price movements. |
| 6 | + It is typically used to identify overbought or oversold conditions in a market. |
| 7 | + |
| 8 | + Args: |
| 9 | + prices (list of float): A list of prices for a financial asset. |
| 10 | + period (int): The number of periods to use in the calculation (default is 14). |
| 11 | + |
| 12 | + Returns: |
| 13 | + list of float: A list of RSI values corresponding to the input price data. |
| 14 | + """ |
| 15 | + |
| 16 | + # Initialize lists to store gains and losses |
| 17 | + gains = [] |
| 18 | + losses = [] |
| 19 | + |
| 20 | + # Loop through the prices starting from the second one (since we compare prices[i] with prices[i-1]) |
| 21 | + for i in range(1, len(prices)): |
| 22 | + # Calculate the price difference from the previous day |
| 23 | + delta = prices[i] - prices[i - 1] |
| 24 | + |
| 25 | + # If delta is positive, it's a gain. Otherwise, it's a loss. |
| 26 | + if delta > 0: |
| 27 | + gains.append(delta) # Positive change is a gain |
| 28 | + losses.append(0) # No loss on this day |
| 29 | + else: |
| 30 | + gains.append(0) # No gain on this day |
| 31 | + losses.append(abs(delta)) # Negative change becomes a loss |
| 32 | + |
| 33 | + # Calculate the initial average gain and loss for the first 'period' number of data points |
| 34 | + # This is the simple moving average (SMA) for the first 'period' values |
| 35 | + avg_gain = sum(gains[:period]) / period |
| 36 | + avg_loss = sum(losses[:period]) / period |
| 37 | + |
| 38 | + # Initialize a list to store the RSI values that we will calculate |
| 39 | + rsi_values = [] |
| 40 | + |
| 41 | + # Calculate the first RSI value: |
| 42 | + # RSI is 100 if there's no loss (i.e., avg_loss == 0), otherwise, it's based on RS (Relative Strength) |
| 43 | + if avg_loss == 0: |
| 44 | + rsi = 100 # If there's no loss, RSI is set to 100 (overbought condition) |
| 45 | + else: |
| 46 | + rs = avg_gain / avg_loss # Relative Strength (RS) |
| 47 | + rsi = 100 - (100 / (1 + rs)) # RSI formula |
| 48 | + |
| 49 | + # Add the first RSI value to the list |
| 50 | + rsi_values.append(rsi) |
| 51 | + |
| 52 | + # Now, we calculate RSI for the rest of the data using the smoothed moving average technique |
| 53 | + for i in range(period, len(prices) - 1): |
| 54 | + # Calculate the price change from the previous day |
| 55 | + delta = prices[i] - prices[i - 1] |
| 56 | + |
| 57 | + # Calculate the gain and loss for this day |
| 58 | + gain = max(0, delta) # Gain is positive changes only |
| 59 | + loss = max(0, -delta) # Loss is the absolute value of negative changes |
| 60 | + |
| 61 | + # Smooth the average gain and average loss over time (using previous averages) |
| 62 | + avg_gain = (avg_gain * (period - 1) + gain) / period |
| 63 | + avg_loss = (avg_loss * (period - 1) + loss) / period |
| 64 | + |
| 65 | + # Calculate the RSI based on the updated averages |
| 66 | + if avg_loss == 0: |
| 67 | + rsi = 100 # If avg_loss is zero, RSI is 100 (overbought) |
| 68 | + else: |
| 69 | + rs = avg_gain / avg_loss # Calculate the new Relative Strength |
| 70 | + rsi = 100 - (100 / (1 + rs)) # Calculate the new RSI value |
| 71 | + |
| 72 | + # Append the RSI value to our list of results |
| 73 | + rsi_values.append(rsi) |
| 74 | + |
| 75 | + return rsi_values |
| 76 | + |
| 77 | + |
| 78 | +# Example usage: |
| 79 | +if __name__ == "__main__": |
| 80 | + # Example list of daily closing prices for a financial asset (e.g., stock or currency pair) |
| 81 | + prices = [44, 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] |
| 82 | + |
| 83 | + # Call the RSI calculation function with the list of prices |
| 84 | + rsi = calculate_rsi(prices) |
| 85 | + |
| 86 | + # Print the calculated RSI values |
| 87 | + print("RSI Values:", rsi) |
0 commit comments