#// Indicator for TOS
#// © LuxAlgo
#indicator("Linear Regression Histogram [LuxAlgo]",overlay=true,
# Converted by Sam4Cok@Samer800 - 08/2024
input length = 100; #,maxval=500)
input BinsNumber = 10; #,'Bins Number')
input mult = 2.0;
input src = close;
input ShowHistogram = yes; #(true,'Show Histogram',group='Style')
def na = Double.NaN;
def last = isNaN(close);
def hist = if ShowHistogram then last else no;
def bins = Min(Max(BinsNumber, 2), 10);
def bar = BarNumber();
def n = if !last then bar else if(n[1], n[1], 1);
def v = Sqr(StDev(src, length));
def r = Correlation(src, bar, length );
def alpha = r * (sqrt(v)/ stdev(bar,length));
def beta = Average(src,length) - alpha * Average(bar,length);
def mad = sqrt(v - v * Sqr(r)) * mult;
def wmad0; def wmad1; def wmad2; def wmad3; def wmad4; def a;
def wmad5; def wmad6; def wmad7; def wmad8; def wmad9; def b;
def sum0; def sum1; def sum2; def sum3; def sum4;
def sum5; def sum6; def sum7; def sum8; def sum9;
def col0; def col1; def col2; def col3; def col4;
def col5; def col6; def col7; def col8; def col9;
if last[-1] {
a = alpha *(n - length + 1) + beta - mad;
b = alpha * n + beta - mad;
col0 = 0/(bins-1);
col1 = 1/(bins-1);
col2 = if bins < 3 then na else 2/(bins-1);
col3 = if bins < 4 then na else 3/(bins-1);
col4 = if bins < 5 then na else 4/(bins-1);
col5 = if bins < 6 then na else 5/(bins-1);
col6 = if bins < 7 then na else 6/(bins-1);
col7 = if bins < 8 then na else 7/(bins-1);
col8 = if bins < 9 then na else 8/(bins-1);
col9 = if bins < 10 then na else 9/(bins-1);
wmad0 = col0 * mad * 2;
wmad1 = col1 * mad * 2;
wmad2 = col2 * mad * 2;
wmad3 = col3 * mad * 2;
wmad4 = col4 * mad * 2;
wmad5 = col5 * mad * 2;
wmad6 = col6 * mad * 2;
wmad7 = col7 * mad * 2;
wmad8 = col8 * mad * 2;
wmad9 = col9 * mad * 2;
sum0 = fold j0 = 0 to length with p0 do
p0 + (if (src[j0] > (alpha*(n-j0) + beta - mad + wmad0) and
src[j0] < (alpha*(n-j0) + beta - mad + (((0+1)/(bins-1)) * mad*2))) then 1 else 0);
sum1 = fold j1 = 0 to length with p1 do
p1 + (if (src[j1] > (alpha*(n-j1) + beta - mad + wmad1) and
src[j1] < (alpha*(n-j1) + beta - mad + (((1+1)/(bins-1)) * mad*2))) then 1 else 0);
sum2 = fold j2 = 0 to length with p2 do
p2 + (if (src[j2] > (alpha*(n-j2) + beta - mad + wmad2) and
src[j2] < (alpha*(n-j2) + beta - mad + (((2+1)/(bins-1)) * mad*2))) then 1 else 0);
sum3 = fold j3 = 0 to length with p3 do
p3 + (if (src[j3] > (alpha*(n-j3) + beta - mad + wmad3) and
src[j3] < (alpha*(n-j3) + beta - mad + (((3+1)/(bins-1)) * mad*2))) then 1 else 0);
sum4 = fold j4 = 0 to length with p4 do
p4 + (if (src[j4] > (alpha*(n-j4) + beta - mad + wmad4) and
src[j4] < (alpha*(n-j4) + beta - mad + (((4+1)/(bins-1)) * mad*2))) then 1 else 0);
sum5 = fold j5 = 0 to length with p5 do
p5 + (if (src[j5] > (alpha*(n-j5) + beta - mad + wmad5) and
src[j5] < (alpha*(n-j5) + beta - mad + (((5+1)/(bins-1)) * mad*2))) then 1 else 0);
sum6 = fold j6 = 0 to length with p6 do
p6 + (if (src[j6] > (alpha*(n-j6) + beta - mad + wmad6) and
src[j6] < (alpha*(n-j6) + beta - mad + (((6+1)/(bins-1)) * mad*2))) then 1 else 0);
sum7 = fold j7 = 0 to length with p7 do
p7 + (if (src[j7] > (alpha*(n-j7) + beta - mad + wmad7) and
src[j7] < (alpha*(n-j7) + beta - mad + (((7+1)/(bins-1)) * mad*2))) then 1 else 0);
sum8 = fold j8 = 0 to length with p8 do
p8 + (if (src[j8] > (alpha*(n-j8) + beta - mad + wmad8) and
src[j8] < (alpha*(n-j8) + beta - mad + (((8+1)/(bins-1)) * mad*2))) then 1 else 0);
sum9 = fold j9 = 0 to length with p9 do
p9 + (if (src[j9] > (alpha*(n-j9) + beta - mad + wmad9) and
src[j9] < (alpha*(n-j9) + beta - mad + (((9+1)/(bins-1)) * mad*2))) then 1 else 0);
} else {
a = a[1];
b = b[1];
col0 = col0[1];
col1 = col1[1];
col2 = col2[1];
col3 = col3[1];
col4 = col4[1];
col5 = col5[1];
col6 = col6[1];
col7 = col7[1];
col8 = col8[1];
col9 = col9[1];
wmad0 = wmad0[1];
wmad1 = wmad1[1];
wmad2 = wmad2[1];
wmad3 = wmad3[1];
wmad4 = wmad4[1];
wmad5 = wmad5[1];
wmad6 = wmad6[1];
wmad7 = wmad7[1];
wmad8 = wmad8[1];
wmad9 = wmad9[1];
sum0 = sum0[1];
sum1 = sum1[1];
sum2 = sum2[1];
sum3 = sum3[1];
sum4 = sum4[1];
sum5 = sum5[1];
sum6 = sum6[1];
sum7 = sum7[1];
sum8 = sum8[1];
sum9 = sum9[1];
}
Script drawLine {
input priceStart = close;
input priceEnd = close;
input length = 100;
input sum = 0;
def last = isNaN(close);
def b = if last[-1] then highestAll(sum) else b[1];
def x = x[1] + 1;
def x1 = if !last then x else x1[1];
def x2 = if x1 == (highestAll(x1)- length+1) then x1 else x2[1];
def y1 = if last[-1] then priceEnd else y1[1];
def y2 = if x1 == HighestAll(x2) then highestall(priceStart) else y2[1];
def x22= highestall(x2);
def y11= highestall(y1);
def slope = (y11 - y2)/ (length +1);
def base = y2 + ((x1 - x22) * slope);
plot line= if x1 >= x22 and base and GetValue(!last, b) then base else Double.NaN;
}
def l0 = drawLine(a + wmad0, b + wmad0, length, sum0);
def l1 = drawLine(a + wmad1, b + wmad1, length, sum1);
def l2 = drawLine(a + wmad2, b + wmad2, length, sum2);
def l3 = drawLine(a + wmad3, b + wmad3, length, sum3);
def l4 = drawLine(a + wmad4, b + wmad4, length, sum4);
def l5 = drawLine(a + wmad5, b + wmad5, length, sum5);
def l6 = drawLine(a + wmad6, b + wmad6, length, sum6);
def l7 = drawLine(a + wmad7, b + wmad7, length, sum7);
def l8 = drawLine(a + wmad8, b + wmad8, length, sum8);
def l9 = drawLine(a + wmad9, b + wmad9, length, sum9);
plot linReg0 = if last then na else l0;
plot linReg1 = if last then na else l1;
plot linReg2 = if last then na else l2;
plot linReg3 = if last then na else l3;
plot linReg4 = if last then na else l4;
plot linReg5 = if last then na else l5;
plot linReg6 = if last then na else l6;
plot linReg7 = if last then na else l7;
plot linReg8 = if last then na else l8;
plot linReg9 = if last then na else l9;
plot linRegH0 = if !hist then na else l0;
plot linRegH1 = if !hist then na else l1;
plot linRegH2 = if !hist then na else l2;
plot linRegH3 = if !hist then na else l3;
plot linRegH4 = if !hist then na else l4;
plot linRegH5 = if !hist then na else l5;
plot linRegH6 = if !hist then na else l6;
plot linRegH7 = if !hist then na else l7;
plot linRegH8 = if !hist then na else l8;
plot linRegH9 = if !hist then na else l9;
def col11 = if last[-length] then highestAll(col1) else col11[1];
def col22 = if last[-length] then highestAll(col2) else col22[1];
def col33 = if last[-length] then highestAll(col3) else col33[1];
def col44 = if last[-length] then highestAll(col4) else col44[1];
def col55 = if last[-length] then highestAll(col5) else col55[1];
def col66 = if last[-length] then highestAll(col6) else col66[1];
def col77 = if last[-length] then highestAll(col7) else col77[1];
def col88 = if last[-length] then highestAll(col8) else col88[1];
linReg0.SetDefaultColor(Color.RED);
linReg1.AssignValueColor(CreateColor(255-col11*255, col11*255, col11*255));
linReg2.AssignValueColor(CreateColor(255-col22*255, col22*255, col22*255));
linReg3.AssignValueColor(CreateColor(255-col33*255, col33*255, col33*255));
linReg4.AssignValueColor(CreateColor(255-col44*255, col44*255, col44*255));
linReg5.AssignValueColor(CreateColor(255-col55*255, col55*255, col55*255));
linReg6.AssignValueColor(CreateColor(255-col66*255, col66*255, col66*255));
linReg7.AssignValueColor(CreateColor(255-col77*255, col77*255, col77*255));
linReg8.AssignValueColor(CreateColor(255-col88*255, col88*255, col88*255));
linReg9.AssignValueColor(Color.CYAN);
linRegH0.setDefaultColor(GetColor(7));
linRegH1.setDefaultColor(GetColor(7));
linRegH2.setDefaultColor(GetColor(7));
linRegH3.setDefaultColor(GetColor(7));
linRegH4.setDefaultColor(GetColor(7));
linRegH5.setDefaultColor(GetColor(7));
linRegH6.setDefaultColor(GetColor(7));
linRegH7.setDefaultColor(GetColor(7));
linRegH8.setDefaultColor(GetColor(7));
linRegH9.setDefaultColor(GetColor(7));
linRegH0.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH1.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH2.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH3.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH4.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH5.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH6.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH7.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH8.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
linRegH9.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
# End of CODE