#// Indicator for TOS
#// © BigBeluga
#indicator("Regression Indicator [BigBeluga]", overlay = false, max_bars_back = 500)
# Converted by Sam4Cok@Samer800 - 03/2025
Declare lower;
input colorBars = yes;
input showLabel = yes;
input showSignalLine = yes;
input RegressionLength = 400; #, "Regression Length")
input RegressionNormalization = 100; #, "Regression Normalization")
input SignalLineLength = 50; #, "Signal Line")
def na = Double.NaN;
def last = isNaN(close);
#// @function regression_indicator is a Normalized Ratio of Regression Lines with close
script regression_indicator {
input src = close;
input length = 400;
input length1 = 100;
def sum_y = Sum(src, length);
def sum_x = fold i = 0 to length with p do
p + i + 1;
def sum_xy = fold j = 0 to length with q do
q + (j + 1) * src[j];
def sum_x_sq = fold k = 0 to length with r do
r + Sqr(k + 1);
def slpNor = (length * sum_xy) - (sum_x * sum_y);
def slpDor = (length * sum_x_sq - Sqr(sum_x));
def slope = slpNor / slpDor;
def intercept = (sum_y - slope * sum_x) / length;
def y1 = intercept + slope;
def distance = (close - y1);
def diff = distance - Average(distance, length1);
def difStDt = diff / StDev(distance, length1);
def distance_n = Average(difStDt, 10);
plot reg = if isNaN(distance_n) then 0 else distance_n;
}
##// ~~ Gradient Coloring {
Script gradient_color {
input src = close;
input minVal = 10;
input maxVal = 400;
input loR = 173;
input loG = 216;
input loB = 230;
input hiR = 41;
input hiG = 98;
input hiB = 255;
def value = if isNaN(src) then 0 else src;
def clamped_value = max(min(value, maxVal), minVal);
def normalized_value = (clamped_value - minVal) / (maxVal - minVal);
def re = floor(loR + (hiR - loR) * normalized_value);
def gr = floor(loG + (hiG - loG) * normalized_value);
def bl = floor(loB + (hiB - loB) * normalized_value);
plot r = re;
plot g = gr;
plot b = bl;
}
def y = regression_indicator(close, RegressionLength, RegressionNormalization);
#// Signal Lines
def mean = Average(y, SignalLineLength);
def mean1 = ExpAverage(y, 5);
#// Conditions
def cond3 = (y > 0) and (y[1] <= 0);
def cond4 = (y < 0) and (y[1] >= 0);
#// Colors
def upR = gradient_color(Y, 0, 1, 0, 20, 20, 0, 255, 255).R;
def upG = gradient_color(Y, 0, 1, 0, 20, 20, 0, 255, 255).G;
def upB = gradient_color(Y, 0, 1, 0, 20, 20, 0, 255, 255).B;
def dnR = gradient_color(Y,-1, 0, 255, 0, 255, 27, 0, 27).R;
def dnG = gradient_color(y,-1, 0, 255, 0, 255, 27, 0, 27).G;
def dnB = gradient_color(y,-1, 0, 255, 0, 255, 27, 0, 27).B;
def meR = gradient_color(mean,-1, 1, 255, 0, 0, 0, 255, 0).R;
def meG = gradient_color(mean,-1, 1, 255, 0, 0, 0, 255, 0).G;
def meB = gradient_color(mean,-1, 1, 255, 0, 0, 0, 255, 0).B;
def coR = gradient_color(Y,-1, 1, 255, 0, 0, 0, 255, 0).R;
def coG = gradient_color(Y,-1, 1, 255, 0, 0, 0, 255, 0).G;
def coB = gradient_color(Y,-1, 1, 255, 0, 0, 0, 255, 0).B;
#-- Signals
plot trendUp1 = if cond3 then 0 else na;
plot trendDn1 = if cond4 then 0 else na;
plot trendUp = if cond3 then 0 else na;
plot trendDn = if cond4 then 0 else na;
trendUp.SetLineWeight(4);
trendDn.SetLineWeight(4);
trendUp1.SetLineWeight(2);
trendDn1.SetLineWeight(2);
trendUp.SetDefaultColor(Color.CYAN);
trendDn.SetDefaultColor(Color.MAGENTA);
trendUp1.SetDefaultColor(Color.DARK_GRAY);
trendDn1.SetDefaultColor(Color.DARK_GRAY);
trendUp.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
trendDn.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
trendUp1.SetPaintingStrategy(PaintingStrategy.POINTS);
trendDn1.SetPaintingStrategy(PaintingStrategy.POINTS);
#-- Reg Line
plot regLine = if !last then y else na;
regLine.SetLineWeight(2);
regLine.AssignValueColor(if y>0 then CreateColor(upR, upG, upB) else CreateColor(dnR, dnG, dnB));
#// Signal Line
plot sigLine = if showSignalLine and !last then mean else na;
plot zero = if last then na else 0;
zero.AssignValueColor(if y>0 then CreateColor(upR, upG, upB) else CreateColor(dnR, dnG, dnB));
sigLine.AssignValueColor(CreateColor(meR, meG, meB));
#-- Clouds
AddCloud(y, 1, CreateColor(0, 127, 127), color.CURRENT);
AddCloud(-1, y, Color.PLUM, color.CURRENT);
AddCloud(y, 2, CreateColor(0, 127, 127), color.CURRENT);
AddCloud(-2, y, Color.PLUM, color.CURRENT);
#-- trend
def dir = if !last[-1] then if cond3 then 1 else if cond4 then -1 else dir[1] else dir[1];
plot dirUp = if last[0] and !last[1] then if dir[1]>0 then y[1]+0.45 else na else na;
plot dirDn = if last[0] and !last[1] then if dir[1]<0 then y[1]-0.45 else na else na;
dirUp.AssignValueColor(CreateColor(coR[1], coG[1], coB[1]));
dirDn.AssignValueColor(CreateColor(coR[1], coG[1], coB[1]));
dirUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
dirDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#-- Label
AddLabel(showLabel and dir>0, "Trend UP (" + AsText(y) + ")", CreateColor(upR, upG, upB));
AddLabel(showLabel and dir<0, "Trend Dn (" + AsText(y) + ")", CreateColor(dnR, dnG, dnB));
#-- Bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else CreateColor(coR, coG, coB));
#-- END of CODE