Author Message:
Background information
The Relative Strength Index (RSI) and the Exponential Moving Average (EMA) are two popular indicators. Traders use these indicators to understand market trends and predict future price changes. However, traders often wonder which indicator is better: RSI or EMA.
- What if these indicators give similar results? To find out, we wanted to study the relationship between RSI and EMA. We focused on a hypothesis: when the RSI goes above 50, it might be similar to the price crossing above a certain length of EMA. Similarly, when the RSI goes below 50, it might be similar to the price crossing below a certain length of EMA.
- Our goal was simple: to figure out if there is any connection between RSI and EMA.
- Conclusion: Yes, it seems that there is a correlation between RSI and EMA, and this indicator clearly displays that relationship.
The RSI Exponential Smoothing indicator displays RSI levels with clear overbought and oversold zones, shown as easy-to-understand moving averages, and the RSI 50 line as an EMA. Another excellent feature is the added FIB levels. To activate, open the settings and click on "FIB Bands." These levels act as short-term support and resistance levels which can be used for scalping.
Benefits of using this indicator instead of regular RSI
The findings about the Relative Strength Index (RSI) and the Exponential Moving Average (EMA) highlight that both indicators are equally accurate (when it comes to crossings), meaning traders can choose either one without compromising accuracy. This empowers traders to pick the indicator that suits their personal preferences and trading style.
CODE:
CSS:
#// https://www.tradingview.com/v/OhFeqKQX/
#This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © Zeiierman
#indicator("RSI Exponential Smoothing (Expo)", overlay=true)
# Converted by Sam4Cok@Samer800 - 05/2023
#// ~~ Inputs {
input showLabel = yes; # "Show Table"
input ShowCloud = yes; # "Show highlight"
input rsiPeriod = 14; # "RSI Period"
input Overbought = 70; # "Overbought"
input Oversold = 30; # "Oversold"
input obosBands = yes; # "OBOS Band"
input fibBands = no; # "FIB Band"
input ShowBubbles = no; # "RSI Bubbles"
input validity_check = no; # "Show Validity check"
def na = Double.NaN;
def last = isNaN(close[-1]);
#// ~~ Exponential Smoothing Coefficient Calculation {
#emaresult(level)=>
script emaresult {
input level = 50;
input rsiperiod = 14;
def exponential_period = 2 * rsiperiod - 1;
def smoothing_coefficient = 2 / (exponential_period + 1);
def averageUp = if close > close[1] then
smoothing_coefficient * (close - close[1]) + (1 - smoothing_coefficient) * (If(averageUp[1], averageUp[1], 1)) else
(1 - smoothing_coefficient) * (If(averageUp[1], averageUp[1], 1));
def averageDown = if close > close[1] then
(1 - smoothing_coefficient) * (If(averageDown[1], averageDown[1], 1)) else
smoothing_coefficient * (close[1] - close) + (1 - smoothing_coefficient) * (If(averageDown[1], averageDown[1], 1));
def netValue = (rsiperiod - 1) * (averageDown * level / (100 - level) - averageUp);
def result = if netValue >= 0 then close + netValue else close + netValue * (100 - level) / level;
plot out = result;
}
#// ~~ Calculate RSI Standard Deviation, in order to calculate the Deviation {
#StandardDeviation(src, period)=>
Script StandardDeviation {
input src = close;
input period = 14;
def mean = Average(src, period);
def squared_diff = ((src - mean) * (src - mean));
def sum_squared_diff = sum(squared_diff, period);
def std_dev = sqrt(sum_squared_diff/period);
plot out = std_dev;
}
#dev(obos)=>
script dev {
input obos = 50;
input ema_result = close;
input std_dev = close;
def deviating = (obos-ema_result)/std_dev;
plot out = deviating;
}
#// ~~ Hull Moving Average Calculation, in order to smooth the Upper OB and Lower OS lines {
#hma(src,len)=>
Script hma {
input src = close;
input len = 14;
def halfLen = Round(len/2, 0);
def wma1 = wma(2 * wma(src, halfLen) - wma(src, len), round(sqrt(0.5),0));
def wma2 = wma(2 * wma(wma1, halfLen) - wma(wma1, len), round(sqrt(0.5),0));
plot out = wma2;
}
#// ~~ Calculate fib 50% from OB to 50 level and OS to 50 level {
#fib(src)=>
Script fib {
input src = close;
input ema_result = close;
def fib = ema_result + (src-ema_result)*0.5;
plot out = fib;
}
#/ ~~ Return EMA {
def ema_result = emaresult(50, rsiperiod);
#// Plot the Optimal EMA based on the RSI value
plot OptimalEMA = ema_result; # "Optimal EMA"
OptimalEMA.SetDefaultColor(CreateColor(33,150,243));
#// ~~ Return RSI Standard Deviation {
def rsi = rsi(Price=close,Length=rsiperiod);
def std_dev = StandardDeviation(rsi, rsiperiod);
def ob_dev = dev(emaresult(Overbought, rsiperiod),ema_result, std_dev);
def os_dev = dev(emaresult(Oversold, rsiperiod),ema_result, std_dev);
def upper = hma(WildersAverage(ema_result + (ob_dev*std_dev),3),10);# // Returns the upper OB line on the chart.
def lower = hma(WildersAverage(ema_result + (os_dev*std_dev),3),10);# // Returns the upper OS line on the chart.
#/ ~~ Plot Upper OB and Lower OS lines on the chart {
plot UpperOB = if obosbands then upper else na;#, "Upper OB"
plot LowerOS = if obosbands then lower else na;#, "Lower OS"
plot UpperDev = if obosbands then upper - stdev(upper,2) else na;#, "Upper OB"
plot LowerDev = if obosbands then lower + stdev(lower,2) else na;#, "Lower OS"
UpperOB.SetDefaultColor(Color.GREEN);
LowerOS.SetDefaultColor(Color.RED);
UpperDev.SetDefaultColor(Color.GREEN);
LowerDev.SetDefaultColor(Color.RED);
#// ~~ Plot the Fib lines on the chart {
def fibUp = fib(upper, ema_result);
def fibDn = fib(lower, ema_result);
plot UpperFib = if fibbands then fibUp else na; # "Upper Fib 50%"
plot LowerFib = if fibbands then fibDn else na; # "Lower Fib 50%"
UpperFib.SetDefaultColor(Color.DARK_GREEN);
LowerFib.SetDefaultColor(Color.DARK_RED);
#-- Cloud
AddCloud(if ShowCloud then upper else na, fibUp, Color.DARK_GREEN);
AddCloud(if ShowCloud then fibDn else na, lower, Color.DARK_RED);
#// ~~ Validity Checks {
#// ~~ Store crossover values to check Validity {
#// ~~ Optimal EMA Length {
def emavalue = Round(2*rsiperiod-1, 0);
def ema = ExpAverage(close,emavalue);
def occurrence;# = 0
def non_occurrence;# = 0
if ((rsi Crosses Above 50) and (close Crosses Above ema)) or ((rsi Crosses below 50) and (close Crosses below ema)) {
occurrence = occurrence[1] + 1;
non_occurrence = non_occurrence[1];
} else
if ((rsi Crosses Above 50) and !(close Crosses Above ema)) or ((rsi Crosses below 50) and !(close Crosses below ema)) {
occurrence = occurrence[1];
non_occurrence = non_occurrence[1] + 1;
} else {
occurrence = occurrence[1];
non_occurrence = non_occurrence[1];
}
#// ~~ Precentage Check {
def percent = ((AbsValue(occurrence)/(occurrence + non_occurrence) * 100));
#// ~~ Trend Direction {
def dir = rsi>50;
#// ~~ Overbought & Oversold {
def ob_ = rsi>Overbought;
def os_ = rsi<Oversold;
#// ~~ Labels
AddLabel(showLabel, "RSI Value: " + rsiperiod, CreateColor(33,150,243));
AddLabel(showLabel, "EMA Value: " + emavalue, CreateColor(33,150,243));
AddLabel(showLabel , "RSI" + if dir then " > 50 " else " < 50", CreateColor(33,150,243));
AddLabel(showLabel , "Trend: " + if dir then "Bullish" else "Bearish", if dir then Color.GREEN else Color.RED);
AddLabel(showLabel and (ob_ or os_), if ob_ then " > " + Overbought + "(Overbought)" else " < " + Oversold + " Oversold",
if ob_ then Color.LIGHT_GREEN else Color.PINK);
AddLabel(showLabel and validity_check, "Validity Check: " + Round(percent,2) + "%", Color.WHITE);
AddLabel(showLabel and validity_check, "RSI Stdev: " + Round(std_dev,2) + "%", Color.WHITE);
AddChartBubble(ShowBubbles and last, ema_result, "RSI 50", CreateColor(33,150,243), yes);
AddChartBubble(ShowBubbles and last, upper, "RSI " + Overbought, Color.GREEN, yes);
AddChartBubble(ShowBubbles and last, lower, "RSI " + Oversold ,Color.RED, no);
AddChartBubble(ShowBubbles and fibbands and last, UpperFib, "FIB 50%", Color.DARK_GREEN, yes);
AddChartBubble(ShowBubbles and fibbands and last, LowerFib, "FIB 50%", Color.DARK_RED, no);
#--- END of CODE