Author Message:
This indicator is designed to perform multiple non-linear regression analysis using four independent variables: close, open, high, and low prices.
Read more here : https://www.tradingview.com/v/s73CMR2W/
Upper Study:
CSS:
#// Indicator for TOS
#// © ChartPrime -- Upper Study --
#indicator("Multiple Non-Linear Regression [ChartPrime]", shorttitle = "NonLR [ChartPrime]")
# Converted by Sam4Cok@Samer800 - 07/2024
input colorBars = no;
input showLabel = yes;
input showPredictedValue = no;
input gapBetweenSignals = 0;
input NormalizationDataLength = 50; # "Normalization Data Length"
input LearningRate = 0.1; # "Learning Rate"
input useSmoothing = yes; # "Smooth?"
input smoothingType = AverageType.SIMPLE;
input smoothingLength = 5; # "Smooth Length"
input signalThresholdPercentage = 50;
input CoefficientClose = 0.5; # "Coefficient b1 (close)", maxval = 1, step = 0.01, minval = 0.25
input CoefficientOpen = 0.4; # "Coefficient b2 (open)", maxval = 1, step = 0.01, minval = 0.25
input CoefficientHigh = 0.35; # "Coefficient b3 (high)", maxval = 1, step = 0.01, minval = 0.25
input CoefficientLow = 0.25; # "Coefficient b4 (low)", maxval = 1, step = 0.01, minval = 0.25
def na = Double.NaN;
def last = IsNaN(close);
#-- Colors
DefineGlobalColor("x1", CreateColor(4, 58, 13));
DefineGlobalColor("x2", CreateColor(70, 5, 5));
DefineGlobalColor("x3", CreateColor(5, 57, 57));
DefineGlobalColor("x4", CreateColor(70, 44, 44));
#// Define your input variables
def th = Max(Min(signalThresholdPercentage/100,1), 0.01);
def b_1 = Max(Min(CoefficientClose,1), 0.25);
def b_2 = Max(Min(CoefficientOpen, 1), 0.25);
def b_3 = Max(Min(CoefficientHigh, 1), 0.25);
def b_4 = Max(Min(CoefficientLow, 1), 0.25);
def rate = Max(LearningRate + 1, 1.01);
def x1 = close;
def x2 = open;
def x3 = high;
def x4 = low;
#/ 𝙈𝙐𝙇𝙏𝙄𝙋𝙇𝙀 𝙉𝙊𝙉-𝙇𝙄𝙉𝙀𝘼𝙍 𝙍𝙀𝙂𝙍𝙀𝙎𝙎𝙄𝙊𝙉
#// Data Normalization Function (0 - 1)
script normaliztion_data {
input x = close;
input len = 50;
def hh = Highest(x, len);
def ll = Lowest(x, len);
def norm = (x - ll) / (hh - ll) ;
plot out = norm;
}
#// Multiple Non-linear Regression with 4 independent variables
script nonLinearRegression {
input x1 = close;
input x2 = open;
input x3 = high;
input x4 = low;
input b1 = 0.5;
input b2 = 0.4;
input b3 = 0.35;
input b4 = 0.25;
def b_1;# = b1
def b_2;# = b2
def b_3;# = b3
def b_4;# = b4
def sumB = b1 + b2 + b3 + b4;
if sumB > 1 {
b_1 = b1 / sumB;
b_2 = b2 / sumB;
b_3 = b3 / sumB;
b_4 = b4 / sumB;
} else {
b_1 = b1;
b_2 = b2;
b_3 = b3;
b_4 = b4;
}
def poly = b_1 * x1 + b_2 * x2 + b_3 * x3 + b_4 * x4;
plot out = poly;
}
def x_1 = normaliztion_data(x1, NormalizationDataLength);
def x_2 = normaliztion_data(x2, NormalizationDataLength);
def x_3 = normaliztion_data(x3, NormalizationDataLength);
def x_4 = normaliztion_data(x4, NormalizationDataLength);
def initial_val = x_1;
#// Compute the predicted values using the non-linear regression function
def predictedValues = nonLinearRegression(x_1, x_2, x_3, x_4, b_1, b_2, b_3, b_4);
#// Compute the error
def error = initial_val - predictedValues;
#// Update the coefficients using gradient descent
def b1 = b_1 - (rate * (error * x_1));
def b2 = b_2 - (rate * (error * x_2));
def b3 = b_3 - (rate * (error * x_3));
def b4 = b_4 - (rate * (error * x_4));
def non_linReg = nonLinearRegression(x_1, x_2, x_3, x_4, b1, b2, b3, b4) + error;
def non_linRegAvg = MovingAverage(smoothingType, non_linReg, smoothingLength);
def non_linerreg = if useSmoothing then non_linRegAvg else non_linReg;
def col = if isNaN(non_linerreg) then 0 else
if non_linerreg > 1 then 255 else
if non_linerreg < 0 then 0 else non_linerreg * 255;
# // 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
#// Plot Regression
plot NonLinRegUp = if showPredictedValue then if non_linerreg > 0.5 then Round(non_linerreg,2) else na else na;
plot NonLinRegDn = if showPredictedValue then if non_linerreg > 0.5 then na else Round(non_linerreg,2) else na;
NonLinRegUp.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
NonLinRegUp.AssignValueColor(CreateColor(col, col, col));
NonLinRegDn.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
NonLinRegDn.AssignValueColor(CreateColor(255 - col,col,255 - col));
#-- Signal
def active;
def thUp = th;
def thDn = 1 - th;
def crossUp = active[1] >= gapBetweenSignals and (non_linerreg > thUp) and (non_linerreg[1] <= thUp);
def crossDn = active[1] >= gapBetweenSignals and (non_linerreg < thDn) and (non_linerreg[1] >= thDn);
active = if crossUp then 0 else if crossDn then 0 else active[1] + 1;
plot SigUp = if crossUp then low else na;
plot SigDn = if crossDn then high else na;
SigUp.SetLineWeight(2);
SigDn.SetLineWeight(2);
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);
SigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
SigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
#-- bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else CreateColor(255 - col, col, 0));
#-- Label
def rReg = Round(non_linerreg, 3);
def rx1 = Round(x_1, 3);
def rx2 = Round(x_2, 3);
def rx3 = Round(x_3, 3);
def rx4 = Round(x_4, 3);
AddLabel(showLabel, "Predicted Value(" + rReg + ")", CreateColor(255 - col, col, 0));
AddLabel(showLabel, "Close(" + rx1 + ")", CreateColor(38, 240, 72));
AddLabel(showLabel, "Open(" + rx2 + ")", CreateColor(216, 16, 16));
AddLabel(showLabel, "High(" + rx3 + ")", CreateColor(20, 219, 219));
AddLabel(showLabel, "Low(" + rx4 + ")", CreateColor(218, 136, 13));
#-- END of CODE
Lower Study:
CSS:
#// Indicator for TOS
#// © ChartPrime -- Lower Study ---
#indicator("Multiple Non-Linear Regression [ChartPrime]", shorttitle = "NonLR [ChartPrime]")
# Converted by Sam4Cok@Samer800 - 07/2024
declare lower;
input colorBars = yes;
input showLabel = yes;
input showCloud = no;
input gapBetweenSignals = 0;
input NormalizationDataLength = 50; # "Normalization Data Length"
input LearningRate = 0.1; # "Learning Rate"
input useSmoothing = no; # "Smooth?"
input smoothingType = AverageType.SIMPLE;
input smoothingLength = 5; # "Smooth Length"
input signalThresholdPercentage = 50;
#// Define your coefficients
input CoefficientClose = 0.5; # "Coefficient b1 (close)", maxval = 1, step = 0.01, minval = 0.25
input CoefficientOpen = 0.4; # "Coefficient b2 (open)", maxval = 1, step = 0.01, minval = 0.25
input CoefficientHigh = 0.35; # "Coefficient b3 (high)", maxval = 1, step = 0.01, minval = 0.25
input CoefficientLow = 0.25; # "Coefficient b4 (low)", maxval = 1, step = 0.01, minval = 0.25
def na = Double.NaN;
def last = IsNaN(close);
#-- Colors
DefineGlobalColor("x1", CreateColor(4, 58, 13));
DefineGlobalColor("x2", CreateColor(70, 5, 5));
DefineGlobalColor("x3", CreateColor(5, 57, 57));
DefineGlobalColor("x4", CreateColor(70, 44, 44));
#// Define your input variables
def th = Max(Min(signalThresholdPercentage/100,1), 0.01);
def b_1 = Max(Min(CoefficientClose,1), 0.25);
def b_2 = Max(Min(CoefficientOpen, 1), 0.25);
def b_3 = Max(Min(CoefficientHigh, 1), 0.25);
def b_4 = Max(Min(CoefficientLow, 1), 0.25);
def rate = Max(LearningRate + 1, 1.01);
def x1 = close;
def x2 = open;
def x3 = high;
def x4 = low;
#/ 𝙈𝙐𝙇𝙏𝙄𝙋𝙇𝙀 𝙉𝙊𝙉-𝙇𝙄𝙉𝙀𝘼𝙍 𝙍𝙀𝙂𝙍𝙀𝙎𝙎𝙄𝙊𝙉
#// Data Normalization Function (0 - 1)
script normaliztion_data {
input x = close;
input len = 50;
def hh = Highest(x, len);
def ll = Lowest(x, len);
def norm = (x - ll) / (hh - ll) ;
plot out = norm;
}
#// Multiple Non-linear Regression with 4 independent variables
script nonLinearRegression {
input x1 = close;
input x2 = open;
input x3 = high;
input x4 = low;
input b1 = 0.5;
input b2 = 0.4;
input b3 = 0.35;
input b4 = 0.25;
def b_1;# = b1
def b_2;# = b2
def b_3;# = b3
def b_4;# = b4
def sumB = b1 + b2 + b3 + b4;
if sumB > 1 {
b_1 = b1 / sumB;
b_2 = b2 / sumB;
b_3 = b3 / sumB;
b_4 = b4 / sumB;
} else {
b_1 = b1;
b_2 = b2;
b_3 = b3;
b_4 = b4;
}
def poly = b_1 * x1 + b_2 * x2 + b_3 * x3 + b_4 * x4;
plot out = poly;
}
def x_1 = normaliztion_data(x1, NormalizationDataLength);
def x_2 = normaliztion_data(x2, NormalizationDataLength);
def x_3 = normaliztion_data(x3, NormalizationDataLength);
def x_4 = normaliztion_data(x4, NormalizationDataLength);
def initial_val = x_1;
#// Compute the predicted values using the non-linear regression function
def predictedValues = nonLinearRegression(x_1, x_2, x_3, x_4, b_1, b_2, b_3, b_4);
#// Compute the error
def error = initial_val - predictedValues;
#// Update the coefficients using gradient descent
def b1 = b_1 - (rate * (error * x_1));
def b2 = b_2 - (rate * (error * x_2));
def b3 = b_3 - (rate * (error * x_3));
def b4 = b_4 - (rate * (error * x_4));
def non_linReg = nonLinearRegression(x_1, x_2, x_3, x_4, b1, b2, b3, b4) + error;
def non_linRegAvg = MovingAverage(smoothingType, non_linReg, smoothingLength);
def non_linerreg = if useSmoothing then non_linRegAvg else non_linReg;
def col = if isNaN(non_linerreg) then 0 else
if non_linerreg > 1 then 255 else
if non_linerreg < 0 then 0 else non_linerreg * 255;
# // 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
#// Plot Regression
plot NonLinReg = non_linerreg; #, color = color, linewidth = 2)
NonLinReg.SetLineWeight(2);
NonLinReg.AssignValueColor(CreateColor(255 - col, col, 0));
#// Plot Normalizate Data
plot close1 = x_1; # "close"
plot open1 = x_2; # "open"
plot high1 = x_3; # "high"
plot low1 = x_4; # "low"
close1.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
open1.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
high1.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
low1.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
close1.SetDefaultColor(GlobalColor("x1"));
open1.SetDefaultColor(GlobalColor("x2"));
high1.SetDefaultColor(GlobalColor("x3"));
low1.SetDefaultColor(GlobalColor("x4"));
plot midLine = if last then na else 0.5;
midLine.SetDefaultColor(Color.GRAY);
#-- Signal
def active;
def thUp = th;
def thDn = 1 - th;
def crossUp = active[1] >= gapBetweenSignals and (non_linerreg > thUp) and (non_linerreg[1] <= thUp);
def crossDn = active[1] >= gapBetweenSignals and (non_linerreg < thDn) and (non_linerreg[1] >= thDn);
active = if crossUp then 0 else if crossDn then 0 else active[1] + 1;
plot SigUp = if crossUp then -0.1 else na;
plot SigDn = if crossDn then 1.1 else na;
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);
SigUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
SigDn.SetPaintingStrategy(PaintingStrategy.SQUARES);
#-- Cloud
AddCloud(if showCloud then NonLinReg else na, thUp, Color.DARK_GREEN, Color.CURRENT);
AddCloud(if showCloud then thDn else na, NonLinReg, Color.DARK_RED, Color.CURRENT);
#-- bar Color
AssignPriceColor(if !colorBars then Color.CURRENT else CreateColor(255 - col, col, 0));
#-- Label
def rReg = Round(non_linerreg, 3);
def rx1 = Round(x_1, 3);
def rx2 = Round(x_2, 3);
def rx3 = Round(x_3, 3);
def rx4 = Round(x_4, 3);
AddLabel(showLabel, "Predicted Value(" + rReg + ")", CreateColor(255 - col, col, 0));
AddLabel(showLabel, "Close(" + rx1 + ")", CreateColor(38, 240, 72));
AddLabel(showLabel, "Open(" + rx2 + ")", CreateColor(216, 16, 16));
AddLabel(showLabel, "High(" + rx3 + ")", CreateColor(20, 219, 219));
AddLabel(showLabel, "Low(" + rx4 + ")", CreateColor(218, 136, 13));
#-- END of CODE