# Relative Strength ## Relative Strength (RS) The Relative Strength (RS) is a component of the RSI calculation and it represents the average gain of the asset's price over a specific period divided by the average loss over the same period. Here's how it works step by step: 1. **Choose the Period**: - Typically, a 14-day period is used, but this can be adjusted based on the trader's preference. 2. **Calculate the Daily Changes**: - For each day, calculate the change in price from the previous day. This can be a gain (if the price went up) or a loss (if the price went down). 3. **Separate Gains and Losses**: - List out all the positive changes (gains) and all the negative changes (losses). 4. **Calculate the Averages**: - **Average Gain**: Sum of all gains over the period divided by the number of periods. - **Average Loss**: Sum of all losses over the period divided by the number of periods (absolute value, as losses are negative). 5. **Calculate RS**: - RS = Average Gain / Average Loss ## Example Calculation Let's go through a simplified example using a 14-day period. ### Step 1: Calculate Daily Changes Assume we have the closing prices for 14 days as follows: - Day 1: 44 - Day 2: 45 - Day 3: 46 - Day 4: 45 - Day 5: 44 - Day 6: 43 - Day 7: 44 - Day 8: 45 - Day 9: 46 - Day 10: 47 - Day 11: 48 - Day 12: 47 - Day 13: 46 - Day 14: 45 Calculate the changes: - Day 2: 45 - 44 = +1 - Day 3: 46 - 45 = +1 - Day 4: 45 - 46 = -1 - Day 5: 44 - 45 = -1 - Day 6: 43 - 44 = -1 - Day 7: 44 - 43 = +1 - Day 8: 45 - 44 = +1 - Day 9: 46 - 45 = +1 - Day 10: 47 - 46 = +1 - Day 11: 48 - 47 = +1 - Day 12: 47 - 48 = -1 - Day 13: 46 - 47 = -1 - Day 14: 45 - 46 = -1 ### Step 2: Separate Gains and Losses - Gains: 1, 1, 1, 1, 1, 1, 1 - Losses: -1, -1, -1, -1, -1, -1, -1 ### Step 3: Calculate the Averages - Average Gain = (1 + 1 + 1 + 1 + 1 + 1 + 1) / 14 = 7 / 14 = 0.5 - Average Loss = (1 + 1 + 1 + 1 + 1 + 1 + 1) / 14 = 7 / 14 = 0.5 ### Step 4: Calculate RS - RS = Average Gain / Average Loss = 0.5 / 0.5 = 1 ## Putting It All Together Once we have RS, we can use it to calculate RSI: \[ RSI = 100 - \frac{100}{1 + RS} \] For our example: \[ RSI = 100 - \frac{100}{1 + 1} = 100 - \frac{100}{2} = 100 - 50 = 50 \] This simplified example shows how you calculate the RS from average gains and losses over a period (typically 14 days), which is then used to derive the RSI. ## In Python ```python import pandas as pd # Sample closing prices closing_prices = [44, 45, 46, 45, 44, 43, 44, 45, 46, 47, 48, 47, 46, 45] # Convert closing prices to a pandas DataFrame data = pd.DataFrame(closing_prices, columns=['Close']) # Calculate the daily price changes data['Change'] = data['Close'].diff() # Separate gains and losses data['Gain'] = data['Change'].apply(lambda x: x if x > 0 else 0) data['Loss'] = data['Change'].apply(lambda x: -x if x < 0 else 0) # Calculate the average gain and loss over the period (14 days) period = 14 data['Avg_Gain'] = data['Gain'].rolling(window=period, min_periods=1).mean() data['Avg_Loss'] = data['Loss'].rolling(window=period, min_periods=1).mean() # Calculate the RS (Relative Strength) data['RS'] = data['Avg_Gain'] / data['Avg_Loss'] # Calculate the RSI data['RSI'] = 100 - (100 / (1 + data['RS'])) # Print the resulting DataFrame print(data) # Get the latest RSI value latest_rsi = data['RSI'].iloc[-1] print(f"The latest RSI value is: {latest_rsi:.2f}") ``` ``` Close Change Gain Loss Avg_Gain Avg_Loss RS RSI 0 44 NaN 0.0 0.0 0.000000 0.000000 NaN NaN 1 45 1.0 1.0 0.0 0.500000 0.000000 inf 100.000000 2 46 1.0 1.0 0.0 0.666667 0.000000 inf 100.000000 3 45 -1.0 0.0 1.0 0.500000 0.250000 2.000000 66.666667 4 44 -1.0 0.0 1.0 0.400000 0.400000 1.000000 50.000000 5 43 -1.0 0.0 1.0 0.333333 0.500000 0.666667 40.000000 6 44 1.0 1.0 0.0 0.428571 0.428571 1.000000 50.000000 7 45 1.0 1.0 0.0 0.500000 0.375000 1.333333 57.142857 8 46 1.0 1.0 0.0 0.555556 0.333333 1.666667 62.500000 9 47 1.0 1.0 0.0 0.600000 0.300000 2.000000 66.666667 10 48 1.0 1.0 0.0 0.636364 0.272727 2.333333 70.000000 11 47 -1.0 0.0 1.0 0.583333 0.333333 1.750000 63.636364 12 46 -1.0 0.0 1.0 0.538462 0.384615 1.400000 58.333333 13 45 -1.0 0.0 1.0 0.500000 0.428571 1.166667 53.846154 The latest RSI value is: 53.85 ``` ## Relationship Between RS and RSI ```python import numpy as np import matplotlib.pyplot as plt # Generate a range of RS values rs_values = np.linspace(0, 10, 100) # Calculate corresponding RSI values rsi_values = 100 - (100 / (1 + rs_values)) # Plotting RS vs RSI plt.figure(figsize=(10, 6)) plt.plot(rs_values, rsi_values, color='b', marker='o', linestyle='-', label='RSI') plt.title('RS vs RSI') plt.xlabel('Relative Strength (RS)') plt.ylabel('Relative Strength Index (RSI)') plt.grid(True, linestyle='--', alpha=0.6) plt.axhline(70, color='gray', linestyle='--', alpha=0.7, label='Overbought (70)') plt.axhline(30, color='gray', linestyle='--', alpha=0.7, label='Oversold (30)') plt.legend() plt.show() ``` ![[rs-vs-rsi.png]]