Linear Regression Oscillator [ChartPrime] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
GvVSxHV.png

* 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
 
This chart is beautiful, how did you get it to look this great? Can you share it?

I mean the theme, the study/indicator is great too, but the theme is just beautiful. You choose some nice working colors together.
 
Last edited by a moderator:
This chart is beautiful, how did you get it to look this great? Can you share it?

I mean the theme, the study/indicator is great too, but the theme is just beautiful. You choose some nice working colors together.
The color definitions
DefineGlobalColor("upBar", CreateColor(0, 178, 178));
DefineGlobalColor("dnBar", CreateColor(178, 0, 178));
DefineGlobalColor("upBG", CreateColor(0, 78, 78));
DefineGlobalColor("dnBG", CreateColor(78, 0, 78));


The progression of color is designed as follows:
Poppa Bear Bullish then Color.CYAN
Momma Bear Bullish then GlobalColor("upBar")
Baby Bear Bullish then GlobalColor("upBG")
Poppa Bear Bearish then Color.MAGENTA
Momma Bear Bearish then GlobalColor("dnBar")
Baby Bear Bearish then GlobalColor("dnBG") else Color.DARK_GRAY); ;)
 
Last edited:
I added my own little tweak to mine, the color-changing 20 EMA:
Ruby:
input price = close;
input length = 20;

assert(length > 0, "'length' must be positive: " + length);

def EMA = compoundValue(1, EMA[1] + 2 / (length + 1) * (price - EMA[1]), price);

#plot LegacyEMA = SimpleMovingAvg(length = length, price = close);
#LegacyEMA.AssignValueColor(if LegacyEMA > LegacyEMA[1] #then Color.CYAN else if LegacyEMA < LegacyEMA[1] then #Color.magenta else Color.CURRENT);

plot AvgExp = ExpAverage(price, length);
#plot UpSignal = price crosses above AvgExp;
#plot DownSignal = price crosses below AvgExp;

AvgExp.AssignValueColor(if AvgExp > AvgExp[1] then Color.CYAN else if AvgExp < AvgExp[1] then Color.MAGENTA else Color.CURRENT);
 
Last edited by a moderator:
@samer800 Which brings me to another query. I'd like to know your thoughts on adjusting the parameters like what the TradingView author suggests:
  • Setting the Look-Back Period:
    Adjust the "Length" input based on the timeframe and the type of trading you are conducting. Shorter periods are more suited for intraday trading, while longer periods can be used for swing trading.
  • Interpreting Thresholds:
    Use the upper and lower threshold inputs to fine-tune the sensitivity of the overbought and oversold signals. Higher absolute values reduce the number of signals but increase their reliability.
I played around with that some and it didn't seem to make much difference that I could detect.

Regarding the 20-period EMA, does it ever make sense to change the number of periods to comport with shorter time frames? Or should it be always left at the standard 20?
 
Last edited:
GvVSxHV.png

* 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
Samer...thanks for all you have done for us on coding. How would you compare this technique to Cosine Kernal Regression with a Triple Confirmation overlay as discussed on June 8 2024 and Feb 8 2024? Are there major differences for adding this to my daily chart for helping on probabilities. Many thanks again in advance.
 
Last edited by a moderator:
It is not possible to recommend one trend indicator over another. They are collinear; BUT as they are highlighting different aspects; it is important to choose the one that brings the most value to your strategy.
When choosing, put several different trend indicators on your chart; after trading for awhile, you will find which one is the most valuable.

ALWAYS, fine-tune the adjustable input settings of any indicator to better align with your strategy.
For some inputs, unless the adjustment is extreme, you will not always be able to detect a difference.

As far as, adjusting the moving average look-back, there is no single correct answer.
The default moving average length, when being used to establish trend, is "generally" between 18–30 bars. The accepted thinking: that is the number of bars required to determine trend. You can set as high as 160-200 to get a more of a range.

It is not recommended to look back less than 15 bars, as the trend would likely not be able to be recognized.
Note: this only applies to moving average lengths used to identify trend.

@jhorton56 @TechGuy
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
367 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top