Author Message:
RSI with Self-Adjusting Linear Regression Bands (Expo) makes use of RSI and Linear Regression to create an RSI that follows the current trend. The indicator has an upper and lower self-adjusting Linear Regression Band that act as RSI boundaries.
HOW TO USE
The indicator can be used in multiple ways, for instance, to find overbought and oversold areas. Or to identify trends as well as pullbacks in trends.
CODE:
CSS:
#//The information contained in my scripts/indicators/ideas does not constitute financial advice or a solicitation to buy or sell
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Zeiierman
#study("RSI with Self-Adjusting Linear Regression Bands (Expo)", shorttitle="RSI + Linreg Bands (Expo)"
#//Credit to: RafaelZioni
#//Adjustment on the following code
#//https://es.tradingview.com/script/wwWnxGus-Linear-Regression-Trend-bands/
# Converted by Sam4Cok@Samer800 - 05/2023
declare lower;
input rsiSource = close; # "RSI Source"
input rsiLength = 14; # "RSI Length"
input LinearRegressionPeriod = 100; # "Linear Regression Period"
input deviations = 2.5; # "Deviation"
input Overbought = 80; # "> Overbought"
input Oversold = 20; # "> Oversold"
def na = Double.NaN;
DefineGlobalColor("up", CreateColor(33,150,243));
DefineGlobalColor("dn", CreateColor(255,82,82));
#// --- Calc
def aRSI = RSI(Price = rsiSource, Length = rsiLength);
def nRSI = WMA(aRSI, 5);
def bar_index = AbsValue(CompoundValue(1, BarNumber(), 0));
def LinRegPrd = LinearRegressionPeriod;
# closeI = (rsi[i])
def Ex = fold i = 0 to LinRegPrd with p do
p + i;
def Ey = fold i1 = 0 to LinRegPrd with p1 do
p1 + nRSI[i1];
def Ex2 = fold i2 = 0 to LinRegPrd with p2 do
p2 + (i2 * i2);
def Exy = fold i3 = 0 to LinRegPrd with p3 do
p3 + (nRSI[i3] * i3);
def ExEx = Ex * Ex;
#/slope
def slope;# = na
if Ex2!=ExEx {
slope = ((LinRegPrd * Exy) - (Ex * Ey)) / ((LinRegPrd * Ex2) - ExEx);
} else {
slope = slope[1];
}
def ilinearRegression = ((Ey - slope * Ex) / LinRegPrd);
def intercept = ilinearRegression + bar_index * slope;
def deviation = power(nRSI, 2) - (intercept - slope * bar_index);
def deviationNew = deviations * sqrt(deviation / LinRegPrd);
def startingPointY = wma((ilinearRegression + slope / LinRegPrd),5);
#// RSI_Adjust Up and Down
def adjust_up = startingPointY + stdev(nRSI, 20);
def adjust_down = startingPointY - stdev(nRSI, 20);
#// Final Conditions
def a = startingPointY-deviationNew + adjust_down/20;
def c1 = startingPointY;
def b = startingPointY+deviationNew - adjust_up/20;
#//lineColor
def col = startingPointY > startingPointY[1];
#// Rsi Plot
plot prsi= nRSI; # "RSI"
prsi.AssignValueColor(if col then GlobalColor("up") else GlobalColor("dn"));
prsi.SetLineWeight(2);
#// Curve Plot
def Lower_Curve = wma(a,5);
def Upper_Curve = wma(b,5);
plot LowerCurve = Lower_Curve; # "Lower Curve"
plot MidCurve = c1; # "Mid Curve"
plot UpperCurve = Upper_Curve; # "Upper Curve"
LowerCurve.SetDefaultColor(Color.DARK_RED);
MidCurve.SetDefaultColor(Color.GRAY);
UpperCurve.SetDefaultColor(Color.DARK_GREEN);
MidCurve.SetStyle(Curve.SHORT_DASH);
AddCloud(if prsi>=UpperCurve then prsi else na, UpperCurve, Color.GREEN);
AddCloud(if prsi<=LowerCurve then LowerCurve else na, prsi, Color.RED);
#-- END OF CODE