This should be the right way to do it ..
delta = df['close'].diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
# Calculate the first AvgGain and AvgLoss
avg_gain = gain.rolling(window=window_rsi).mean().dropna()
avg_loss = loss.rolling(window=window_rsi).mean().dropna()
# Calculate the remaining AvgGain and AvgLoss
for i in range(len(avg_gain), len(gain)):
avg_gain.loc[gain.index[i]] = (avg_gain.loc[gain.index[i-1]]*13 + gain.loc[gain.index[i]]) / 14
avg_loss.loc[loss.index[i]] = (avg_loss.loc[loss.index[i-1]]*13 + loss.loc[loss.index[i]]) / 14
rs = avg_gain / avg_loss
df['rsi'] = 100 - (100 / (1 + rs))
Its still difference
i tested on XAUUSD M1 exness, on chart at 21:43 is 62.40 but calculate python is 79.12 bro.
This should be the right way to do it ..
delta = df['close'].diff()
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)
# Calculate the first AvgGain and AvgLoss
avg_gain = gain.rolling(window=window_rsi).mean().dropna()
avg_loss = loss.rolling(window=window_rsi).mean().dropna()
# Calculate the remaining AvgGain and AvgLoss
for i in range(len(avg_gain), len(gain)):
avg_gain.loc[gain.index[i]] = (avg_gain.loc[gain.index[i-1]]*13 + gain.loc[gain.index[i]]) / 14
avg_loss.loc[loss.index[i]] = (avg_loss.loc[loss.index[i-1]]*13 + loss.loc[loss.index[i]]) / 14
rs = avg_gain / avg_loss
df['rsi'] = 100 - (100 / (1 + rs))
To closely match the RSI values you see on most charts, you might want to use an exponential moving average (EMA) instead of SMA for the average gain and loss. Here's how you can adjust your code to use EMA, which might reduce the discrepancies:
import pandas as pd # Assuming df is your DataFrame and it contains a 'close' column window_rsi = 14 delta = df['close'].diff() gain = delta.where(delta > 0, 0) loss = -delta.where(delta < 0, 0) # Use EMA instead of SMA gain_ema = gain.ewm(alpha=1/window_rsi, adjust=False).mean() loss_ema = loss.ewm(alpha=1/window_rsi, adjust=False).mean() rs = gain_ema / loss_ema df['rsi'] = 100 - (100 / (1 + rs))
Please note, the exact calculation might still differ slightly from what you see on some charts due to variations in how initial values are handled or how data boundaries are treated. Additionally, ensure your data (specifically the 'close' prices) is clean and matches the data used by the chart you're comparing against, as discrepancies in data can lead to different RSI values.
To closely match the RSI values you see on most charts, you might want to use an exponential moving average (EMA) instead of SMA for the average gain and loss. Here's how you can adjust your code to use EMA, which might reduce the discrepancies:
Please note, the exact calculation might still differ slightly from what you see on some charts due to variations in how initial values are handled or how data boundaries are treated. Additionally, ensure your data (specifically the 'close' prices) is clean and matches the data used by the chart you're comparing against, as discrepancies in data can lead to different RSI values.
Thank you so much, its exact what i need.
Love u 3000 <3
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
As tittle, im trying calculate rsi using python follow this code:
But the result is difference to Chart. I tried the other library like ta, ta-lib, still difference to Chart
Can some one help me how can i calculate rsi exact chart.
Thank you so much.