* Upper and lower Study
Author Message:
Linear Regression Oscillator Indicator
Overview:
The Linear Regression Oscillator is a custom TradingView indicator designed to provide insights into potential mean reversion and trend conditions. By calculating a linear regression on the closing prices over a user-defined period, this oscillator helps identify overbought and oversold levels and highlights trend changes. The indicator also offers visual cues and color-coded price bars to aid in quick decision-making.
find more here : https://www.tradingview.com/v/MyKmv8br/
CODE - Upper Study:
CSS:
#https://www.tradingview.com/v/MyKmv8br/
#// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © ChartPrime
#indicator("Linear Regression Oscillator [ChartPrime]", overlay=false)
# Upper Study - converted by Sam4Cok@Samer800 - 06/2024
input barColor = no; #, "Plot Bar Color")
input showLabels = no;
input timeframe = {default "Chart TF", "Manual TF"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input source = FundamentalType.CLOSE;
input length = 20; #, "Length")
input upperThreshold = 1.5;
input lowerThreshold = -1.5;
def na = Double.NaN;
def last = IsNaN(close);
#--color
DefineGlobalColor("upBar", CreateColor(0, 178, 178));
DefineGlobalColor("dnBar", CreateColor(178, 0, 178));
DefineGlobalColor("upBG", CreateColor(0, 78, 78));
DefineGlobalColor("dnBG", CreateColor(78, 0, 78));
#// Source
def src;def h; def l;
Switch (timeframe) {
Case "Manual TF" :
src = if !last then Fundamental(FundamentalType = source, Period = manualTimeframe) else src[1];
h = high(Period = manualTimeframe);
l = low(Period = manualTimeframe);
Default : src = if !last then Fundamental(FundamentalType = source) else src[1];
h = high;
l = low;
}
#// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
#//@function Calculation of slope and intercept
#//@param length LookBack Period
#//@returns Linear Regression Oscillator
script linear_regression_osc {
input source = close;
input length = 20;
def len = length;
def n = length;
def last = IsNaN(close);
def bar = GetTime(); #BarNumber();
def sum_x = fold j0 = 0 to n with q0 do
q0 + j0;
def sum_y = fold j1 = 0 to n with q1 do
q1 + source[j1];
def sum_xy = fold j2 = 0 to n with q2 do
q2 + j2 * source[j2];
def sum_xq = fold j3 = 0 to n with q3 do
q3 + j3 * j3;
def m = (len * sum_xy - sum_x * sum_y) / (len * sum_xq - sum_x * sum_x);
def c = (sum_y - m * sum_x) / len;
def linear_regression = ((m * bar + c) * -1);
plot out = if last then Double.NaN else linear_regression;
}
#// Calculate linear regression Oscillator
def linReg = linear_regression_osc(src, length);
#// Normaliztion
def linear_regression = (linReg - Average(linReg, 100)) / StDev(linReg, 100);
#// Conditions of Mean Reversion and Trends
def cond1 = (linear_regression crosses below 0);
def cond2 = (linear_regression crosses above 0);
def crossUp = (linear_regression crosses above linear_regression[2]);
def crossDn = (linear_regression crosses below linear_regression[2]);
def cond_1 = (crossDn and linear_regression > upperThreshold);
def cond_2 = (crossUp and linear_regression < lowerThreshold);
def points = if cond1 then Highest(h, 5) else
if cond2 then Lowest(l, 5) else points[1];
#// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
#// Defined Colors
def colUp = if linear_regression < 0.75 then 50 else if linear_regression > 2 then 255 else linear_regression * 50 * 2.55;
def colDn = if linear_regression > -0.75 then 50 else if linear_regression < -2 then 255 else linear_regression * 50 * -2.55;
def colUp2 = if linear_regression < 0 then 0 else if linear_regression > 1 then 255 else linear_regression * 255;
def colDn2 = if linear_regression > 0 then 0 else if linear_regression < -1 then 255 else linear_regression * -255;
#-- bar Color
AssignPriceColor(if !barColor then Color.CURRENT else
if linear_regression > 2 then Color.CYAN else
if linear_regression > 1 then GlobalColor("upBar") else
if linear_regression > 0 then GlobalColor("upBG") else
if linear_regression < -2 then Color.MAGENTA else
if linear_regression < -1 then GlobalColor("dnBar") else
if linear_regression < 0 then GlobalColor("dnBG") else Color.DARK_GRAY);
#/ Oscillator Mean Reversion above or below Thresholds
plot ob = if cond_1 then high else na; # "◇",
plot os = if cond_2 then low else na ;#
ob.SetLineWeight(2);
os.SetLineWeight(2);
ob.SetDefaultColor(Color.RED);
os.SetDefaultColor(Color.GREEN);
ob.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
os.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
#// Invalidation Price Levels
def chgPt = (points - points[1]);
plot invLine = if !chgPt then points else na;
invLine.SetLineWeight(2);
invLine.AssignValueColor(if linear_regression > 0 then CreateColor(0, colUp2, colUp2) else CreateColor(colDn2, 0, colDn2));
def labCond = showLabels and chgPt;
AddChartBubble(labCond, points[1], "Inv.Lvl", if linear_regression < 0 then Color.CYAN else Color.MAGENTA,
if linear_regression < 0 then no else yes);
#-- end of CODE
CODE - LOWER STUDY:
CSS:
#https://www.tradingview.com/v/MyKmv8br/
#// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © ChartPrime
#indicator("Linear Regression Oscillator [ChartPrime]", overlay=false)
# lower Study - converted by Sam4Cok@Samer800 - 06/2024
Declare lower;
input barColor = no; #, "Plot Bar Color")
input timeframe = {Default "Chart TF", "Manual TF"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input source = FundamentalType.CLOSE;
input length = 20; #, "Length")
input upperThreshold = 1.5; #, "Upper Threshold"
input lowerThreshold = -1.5; #, "Lower Threshold"
def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
#--color
DefineGlobalColor("upBar", CreateColor(0, 178, 178));
DefineGlobalColor("dnBar", CreateColor(178, 0, 178));
DefineGlobalColor("upBG", CreateColor(0, 78, 78));
DefineGlobalColor("dnBG", CreateColor(78, 0, 78));
#// Source
def src;
Switch (timeframe) {
Case "Manual TF" : src = if !last then Fundamental(FundamentalType = source, Period = manualTimeframe) else src[1];
Default : src = if !last then Fundamental(FundamentalType = source) else src[1];
}
#// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
#//@function Calculation of slope and intercept
#//@param length LookBack Period
#//@returns Linear Regression Oscillator
Script linear_regression_osc {
input source = close;
input length = 20;
def len = length;
def n = length;
def last = isNaN(close);
def bar = GetTime(); #BarNumber();
def sum_x = fold j0 = 0 to n with q0 do
q0 + j0;
def sum_y = fold j1 = 0 to n with q1 do
q1 + source[j1];
def sum_xy = fold j2 = 0 to n with q2 do
q2 + j2 * source[j2];
def sum_xq = fold j3 = 0 to n with q3 do
q3 + j3 * j3;
def m = (len * sum_xy - sum_x * sum_y) / (len * sum_xq - sum_x * sum_x);
def c = (sum_y - m * sum_x) / len;
def linear_regression = ((m * bar + c)*-1);
plot out = if last then Double.NaN else linear_regression;
}
#// Calculate linear regression Oscillator
def linReg = linear_regression_osc(src, length);
#// Normaliztion
def linear_regression = (linReg - Average(linReg, 100)) / stdev(linReg, 100);
#// Conditions of Mean Reversion and Trends
def cond1 = (linear_regression Crosses Below 0);
def cond2 = (linear_regression Crosses Above 0);
def crossUp = (linear_regression Crosses Above linear_regression[2]);
def crossDn = (linear_regression Crosses Below linear_regression[2]);
def cond_1 = (crossDn and linear_regression > upperThreshold);
def cond_2 = (crossUp and linear_regression < lowerThreshold);
#def points = if cond1 then highest(high, 5) else
# if cond2 then lowest(low, 5) else points[1];
#// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
#// Defined Colors
def colUp = if linear_regression < 0.75 then 50 else if linear_regression > 2 then 255 else linear_regression * 50 * 2.55;
def colDn = if linear_regression > -0.75 then 50 else if linear_regression < -2 then 255 else linear_regression * 50 * -2.55;
#// Plot Oscillator
plot linRegLine = linear_regression; #, color = color, style = plot.style_area)
plot linRegHist = linear_regression; #, color = color, style = plot.style_area)
linRegHist.SetLineWeight(5);
linRegHist.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
linRegLine.SetLineWeight(2);
linRegHist.AssignValueColor(if linear_regression>0 then CreateColor(0, colUp, colUp) else
CreateColor(colDn, 0, colDn));
linRegLine.AssignValueColor(if linear_regression>0 then CreateColor(0, colUp, colUp) else
CreateColor(colDn, 0, colDn));
#// Zero Line
plot zero = if last then na else 0; #, color = color1, linewidth = 2)
zero.AssignValueColor(if linear_regression>0 then CreateColor(0, colUp, colUp) else
CreateColor(colDn, 0, colDn));
#// Upper and Lower Thresholds
def p1 = if last then na else upperThreshold;
def p3 = if last then na else lowerThreshold;
def p4 = if last then na else -4;
def p2 = if last then na else 4;
def p14 = (p1 + 4) / 2;
def p32 = (p3 - 4) / 2;
AddCloud(pos, p1, GlobalColor("upBG"), GlobalColor("upBG"));
AddCloud(p14, p1, GlobalColor("upBG"), GlobalColor("upBG"));
AddCloud(p3, neg, GlobalColor("dnBG"), GlobalColor("dnBG"));
AddCloud(p3, p32, GlobalColor("dnBG"), GlobalColor("dnBG"));
#-- bar Color
AssignPriceColor(if !barColor then Color.CURRENT else
if linear_regression>2 then Color.CYAN else
if linear_regression>1 then GlobalColor("upBar") else
if linear_regression>0 then GlobalColor("upBG") else
if linear_regression<-2 then Color.MAGENTA else
if linear_regression<-1 then GlobalColor("dnBar") else
if linear_regression<0 then GlobalColor("dnBG") else Color.DARK_GRAY);
#/ Oscillator Mean Reversion above or below Thresholds
plot ob = if cond_1[-1] then linear_regression else na; # "◇",
plot os = if cond_2[-1] then linear_regression else na ;#
#// Oscillator Crosses zero Line
plot zeroDn = if cond1[-1] then 0 else na;
plot zeroUp = if cond2[-1] then 0 else na;
ob.SetLineWeight(2);
os.SetLineWeight(2);
ob.SetDefaultColor(Color.MAGENTA);
os.SetDefaultColor(Color.CYAN);
zeroDn.SetDefaultColor(Color.RED);
zeroUp.SetDefaultColor(Color.GREEN);
ob.SetPaintingStrategy(PaintingStrategy.SQUARES);
os.SetPaintingStrategy(PaintingStrategy.SQUARES);
zeroDn.SetPaintingStrategy(PaintingStrategy.SQUARES);
zeroUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
#-- end of CODE