Auto Trend Line

british43

Member
VIP
I'm trying to get the top trend line for the code below. I tried to but failed. Any assistance is greatly appreciated.

input n = 20;

def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def hh = HighestAll(h);
def lastValue = if IsNaN(c[-1]) then c else lastValue[1];
def hhBar = if h == hh
then bar
else Double.NaN;
def ll = LowestAll(l);
def llBar = if l == ll
then bar
else Double.NaN;
def minBar = Min(HighestAll(hhBar), HighestAll(llBar));
def firstPoint = if minBar == HighestAll(hhBar)
then hh
else ll;
def currentBar = if IsNaN(c[-1])
then bar
else currentBar[1];
def h_h = fold i = 1 to n + 1
with p = 1
while p
do h > GetValue(h, -i);
def PivotH = if (bar > n and
h == Highest(h, n) and
h_h)
then h
else Double.NaN;
def PHValue = if !IsNaN(PivotH)
then PivotH
else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
then bar
else Double.NaN;
def l_l = fold j = 1 to n + 1
with q = 1
while q
do l < GetValue(l, -j);
def PivotL = if (bar > n and
l == Lowest(l, n) and
l_l)
then l
else Double.NaN;
def PLValue = if !IsNaN(PivotL)
then PivotL
else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
then bar
else PLBarOrigin[1];
def secPoint = if firstPoint == hh
then PHValue
else if firstPoint == ll
then PLValue
else Double.NaN;
def secBar = if firstPoint == hh
then PHBarOrigin
else if firstPoint == ll
then PLBarOrigin
else Double.NaN;
def run = if bar == minBar then 1 else run[1] + 1;
def rise = if bar == HighestAll(secBar) then secPoint - firstPoint else Double.NaN;
def slope = rise / run[1];
def slopeX = if !IsNaN(slope) then slope else slopeX[1];
def lineX = if !IsNaN(slope) then secPoint else lineX[1] + slopeX;
plot R1 = if bar >= HighestAll(PHBarOrigin)
then HighestAll(if IsNaN(c[-1])
then PHValue
else Double.NaN)
else Double.NaN;
plot S1 = if bar >= HighestAll(PLBarOrigin)
then HighestAll(if IsNaN(c[-1])
then PLValue
else Double.NaN)
else Double.NaN;
plot trendLine = if bar == minBar
then firstPoint
else if bar == HighestAll(secBar)
then secPoint
else Double.NaN;
trendLine.EnableApproximation();
trendLine.SetStyle(Curve.FIRM);
trendLine.SetDefaultColor(Color.CYAN);
plot LineExtension = if lineX > 0 then lineX else Double.NaN;
LineExtension.SetDefaultColor(Color.CYAN);
LineExtension.SetStyle(Curve.FIRM);
 
i haven't looked at your code yet, but did find a couple of examples that may help.

the people that run this site prefer that code is posted within tags. you can do that by, cutting your code and pasting inside code tags. click the </> symbol on top of the post edit screen, and choose ruby, then paste in your code.

when using a fair amount of code from another study, it's good practice to keep the header info to identify it and to help others search for variations of it.

i found 2 examples of similar codes that draw lines between pivots, hopefully they will help.
i searched for, what i thought would be unique words from your study. when i used this,
HighestAll(PLBarOrigin)
i found these.
https://usethinkscript.com/threads/bulkowskis-1-2-3-trend-change-indicator-for-thinkorswim.141/
Bulkowski's 1-2-3 Trend Change Indicator for ThinkorSwim

https://usethinkscript.com/threads/auto-trend-lines-indicator-for-thinkorswim-free-download.31/
Auto Trend Lines Indicator for ThinkorSwim (Free Download)
 
the original study draws 1 trendline, either above or below the candles.
it finds the barnumbers for the highest high and lowest low
if the highest high has a lower barnumber, the trendline is drawn above the candles.
if the lowest low has a lower barnumber, the trendline is drawn below the candles.

it draws a resistance line from the endpoint of the higher trendline
it draws a support line from the endpoint of the lower trendline

------------------
here is a modified study

i changed the original so it draws 2 trendlines, 1 above , and 1 below, instead of 1. i copied what was there and made 2 sets of the code, by adding hi or lo to many variable names.

this study has its quirks.
this link may be a more accurate trendline study
https://usethinkscript.com/threads/auto-trend-lines-indicator-for-thinkorswim-free-download.31/

Code:
# AutoTrendLine_02

# -----------
# halcyonguy
# 21-06-23
# change original so it draws 2 trendlines, 1 above , and 1 below, instead of 1

# https://usethinkscript.com/threads/auto-trend-line.6915/
# Auto Trend Line

def na = double.nan;
input n = 20;

def hi = high;
def lo = low;
def c = close;
def bar = BarNumber();

# high trendline ---------------------
def hihi = HighestAll(hi);
def hhBar = if hi == hihi
  then bar
  else Double.NaN;

def hifirstPoint = hihi;
def hifirstbar = HighestAll(hhBar);

def h_h = fold i = 1 to n + 1
  with p = 1
  while p
  do hi > GetValue(hi, -i);

def PivotH = if (bar > n and hi == Highest(hi, n) and h_h)
  then hi
  else Double.NaN;
def PHValue = if !IsNaN(PivotH)
  then PivotH
  else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
  then bar
  else Double.NaN;

def hisecPoint = if hifirstPoint == hihi then PHValue else Double.NaN;
def hisecBar = if hifirstPoint == hihi then PHBarOrigin else Double.NaN;

def hirun = if bar == hifirstBar then 1 else hirun[1] + 1;
def hirise = if bar == HighestAll(hisecBar) then hisecPoint - hifirstPoint else Double.NaN;
def hislope = hirise / hirun[1];
def hislopeX = if !IsNaN(hislope) then hislope else hislopeX[1];
def hilineX = if !IsNaN(hislope) then hisecPoint else hilineX[1] + hislopeX;

#  hi trend line
plot hitrendLine = if bar == hifirstBar then hifirstPoint
  else if bar == HighestAll(hisecBar) then hisecPoint
  else Double.NaN;
hitrendLine.EnableApproximation();
hitrendLine.SetStyle(Curve.FIRM);
hitrendLine.SetDefaultColor(Color.green);

# trend line1 ext
plot hiLine1Ext = if hilineX > 0 then hilineX else Double.NaN;
hiLine1Ext.SetDefaultColor(Color.green);
hiLine1Ext.SetStyle(Curve.MEDIUM_DASH);


# low trendline ---------------------
def lolo = LowestAll(lo);
def llBar = if lo == lolo
  then bar
  else Double.NaN;

def lofirstPoint = lolo;
def lofirstbar = HighestAll(llBar);

def l_l = fold j = 1 to n + 1
  with q = 1
  while q
  do lo < GetValue(lo, -j);
def PivotL = if (bar > n and
  lo == Lowest(lo, n) and l_l)
  then lo
  else Double.NaN;
def PLValue = if !IsNaN(PivotL)
  then PivotL
  else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
  then bar
  else PLBarOrigin[1];

def losecPoint = if lofirstPoint == lolo then PLValue else Double.NaN;
def losecBar = if lofirstPoint == lolo then PLBarOrigin else Double.NaN;

def lorun = if bar == lofirstBar then 1 else lorun[1] + 1;
def lorise = if bar == HighestAll(losecBar) then losecPoint - lofirstPoint else Double.NaN;
def loslope = lorise / lorun[1];
def loslopeX = if !IsNaN(loslope) then loslope else loslopeX[1];
def lolineX = if !IsNaN(loslope) then losecPoint else lolineX[1] + loslopeX;

#  lo trend line
plot lotrendLine = if bar == lofirstBar then lofirstPoint
  else if bar == HighestAll(losecBar) then losecPoint
  else Double.NaN;
lotrendLine.EnableApproximation();
lotrendLine.SetStyle(Curve.FIRM);
lotrendLine.SetDefaultColor(Color.magenta);

# lo trend line ext
plot loLine1Ext = if lolineX > 0 then lolineX else Double.NaN;
loLine1Ext.SetDefaultColor(Color.magenta);
loLine1Ext.SetStyle(Curve.MEDIUM_DASH);


# ----------------------------------

# horz resistance line
plot R1 = if bar >= HighestAll(PHBarOrigin)
  then HighestAll(if IsNaN(c[-1])
    then PHValue
      else Double.NaN)
    else Double.NaN;
r1.SetDefaultColor(Color.orange);

# horz support line , from lower trendline end
plot S1 = if bar >= HighestAll(PLBarOrigin)
  then HighestAll(if IsNaN(c[-1])
  then PLValue
  else Double.NaN)
  else Double.NaN;
s1.SetDefaultColor(Color.yellow);

#

21-6-23 NXST 2hour chart
LqL7ZMm.jpg
 
Last edited:
Thank you. I will keep your recommendation in mind. Is there a way to make a cloud around the trendlines. like this code.

input price = close;
input price1 = high;
input displace = 0;
input length = 200;
input length1 = 500;
input length2 = 100;
input length4 = 1000;
input atrmod=2;
def bn = barnumber();
def lastBar = highestall(if isnan(close) then 0 else bn);
def offset = bn - lastbar;

input averageType = AverageType.EXPONENTIAL;


input length3 = 9;
input showBreakoutSignals = no;

input length5 = 252;
input averageType1 = AverageType.WILDERS;

def atr = GetValue(ATR(length), offset);


# User Inputs

def Num_Dev_Dn = atr;
def Num_Dev_up = -atr;

plot top200AvgExp = ExpAverage(price[-displace], length) * 1.06;
plot top200AvgExpupBand = top200AvgExp + Num_Dev_Dn ;
top200AvgExpupBand.Hide();
plot top200AvgExplowBand = top200AvgExp + Num_Dev_up ;
top200AvgExplowBand.Hide();
AddCloud(top200AvgExp, top200AvgExpupBand, Color.WHITE, Color.WHITE);
AddCloud(top200AvgExp, top200AvgExplowBand, Color.WHITE, Color.WHITE);
top200AvgExp.SetDefaultColor(Color.MAGENTA);
######################################################################

plot top100AvgExp = ExpAverage(price[-displace], length2) * 1.08;
plot top100AvgExpupBand = top100AvgExp + Num_Dev_up ;
top100AvgExpupBand.Hide();
plot top100AvgExplowBand = top100AvgExp + Num_Dev_Dn ;
top100AvgExplowBand.Hide();
AddCloud(top100AvgExp, top100AvgExpupBand, Color.WHITE, Color.WHITE);
AddCloud(top100AvgExp, top100AvgExplowBand, Color.WHITE, Color.WHITE);
top100AvgExp.SetDefaultColor(Color.CYAN);


##################################################################
plot top500AvgExp = ExpAverage(price[-displace], length1) * 1.05;
top500AvgExp.SetDefaultColor(Color.YELLOW);
plot top500AvgExpupBand = top500AvgExp + Num_Dev_Dn ;
top500AvgExpupBand.Hide();
plot top500AvgExplowBand = top500AvgExp + Num_Dev_up ;
top500AvgExplowBand.Hide();
AddCloud(top500AvgExp, top500AvgExpupBand, Color.WHITE, Color.WHITE);
AddCloud(top500AvgExp, top500AvgExplowBand, Color.WHITE, Color.WHITE);

##########################################################################
plot top100AvgExp1 = ExpAverage(price[-displace], length2) * 1.19;
plot top100AvgExp1upBand = top100AvgExp1 + Num_Dev_Dn ;
top100AvgExp1upBand.Hide();
plot top100AvgExp1lowBand = top100AvgExp1 + Num_Dev_up ;
top100AvgExp1lowBand.Hide();
AddCloud(top100AvgExp1, top100AvgExp1upBand, Color.WHITE, Color.WHITE);
AddCloud(top100AvgExp1, top100AvgExp1lowBand, Color.WHITE, Color.WHITE);
top100AvgExp1.SetDefaultColor(Color.DARK_ORANGE);

def data = close;
def top200 = top200AvgExp - data;
plot dist200 = Round(top200 / top100AvgExp * 100, 2);
 
yes, a cloud can be added anywhere on a chart, between 2 price levels.
in that code, it looks like they used 2 clouds. one cloud is above some line and another cloud is below some line. doing it this way allows for them to have different colors.

you can add this to the end of my study
Code:
input cloudht = 0.5;
addcloud(hitrendLine + cloudht, hitrendLine - cloudht, color.light_gray, color.light_gray);
addcloud(lotrendLine + cloudht, lotrendLine - cloudht, color.light_gray, color.light_gray);

addcloud info
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddCloud

when you post code, click on this icon at the top </>. then paste your code in the pop up window. for code type, pick ruby. it doesn't really matter what type you use, different types will change the look of the code.

if you mark up an image and post it, from imgur, everyone will know what you are talking about.
adding pictures in a post
post at top of questions section
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/
 
The cloud did not work. I'm trying to get the PR1 to plot Highest(l) for the Highest(H) bar. I tried but the line is not plotting where I need it. I'm trying to upload an image but unsuccessful.


Ruby:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019
input length4 = 14;
input movingAvgLength = 1;

input length = 252;
input length1 = 30;
input atrmod=2;
def bn = barnumber();
def lastBar = highestall(if isnan(close) then 0 else bn);
def offset = bn - lastbar;

# User Inputs
input n = 21; #hint n: periods used for pivot calculations.


# Internal Script Reference
script LinePlot {
    input BarID = 0;
    input Value = 0;
    input BarOrigin = 0;
    def ThisBar = HighestAll(BarOrigin);
    def ValueLine = if BarOrigin == ThisBar
                then Value
                else Double.NaN;
    plot P = if ThisBar - BarID <= BarOrigin
             then HighestAll(ValueLine)
             else Double.NaN;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);
# Parent High
def ParentHigh = HighestAll(h);

def ParentHighL = l;

def ParentHBarOrigin = if h == ParentHigh
                       then bar
                       else ParentHBarOrigin[1];

def ParentHLBarOrigin = if h == ParentHigh
                       then l
                       else ParentHBarOrigin[1];

def ParentHBarID = bar - HighestAll(ParentHBarOrigin);
def ParentHLBarID = bar - HighestAll(ParentHLBarOrigin);

# R1
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
        
def PivotH = if (bar > n and
                 h == Highest(h, n) and
                 hh)
            then h
            else Double.NaN;
            
def PivotHL = if (bar > n and
                 h == Highest(h, n) and
                 hh)
            then l
            else Double.NaN;
            

def PHValue = if !IsNaN(PivotH)
              then PivotH
              else PHValue[1];
              
def PHLValue = if !IsNaN(PivotHL)
              then PivotHL
              else PHLValue[1];             
              
def PHBarOrigin = if !IsNaN(PivotH)
                  then bar
                  else PHBarOrigin[1];

def PHLBarOrigin = if !IsNaN(PivotHL)
                  then bar
                  else PHLBarOrigin[1];                 
                                    
def PHBarID = bar - PHBarOrigin;
def PHLBarID = bar - PHLBarOrigin;

# R2
def R2PHValue = if PHBarOrigin != PHBarOrigin[1]
              then PHValue[1]
              else R2PHValue[1];

def R2PHLValue = if PHLBarOrigin != PHLBarOrigin[1]
              then PHLValue[1]
              else R2PHLValue[1];
              
              
def R2PHBarOrigin = if PHBarOrigin != PHBarOrigin[1]
                  then PHBarOrigin[1]
                  else R2PHBarOrigin[1];

def R2PHLBarOrigin = if PHLBarOrigin != PHLBarOrigin[1]
                  then PHLBarOrigin[1]
                  else R2PHLBarOrigin[1];
                  
def R2PHBarID = bar - R2PHBarOrigin;
def R2PHLBarID = bar - R2PHLBarOrigin;


# R3
def R3PHValue = if R2PHBarOrigin != R2PHBarOrigin[1]
              then R2PHValue[1]
              else R3PHValue[1];
              
def R3PHLValue = if R2PHLBarOrigin != R2PHLBarOrigin[1]
              then R2PHLValue[1]
              else R3PHLValue[1];               
              
def R3PHBarOrigin = if R2PHBarOrigin != R2PHBarOrigin[1]
                  then R2PHBarOrigin[1]
                  else R3PHBarOrigin[1];
                  
def R3PHLBarOrigin = if R2PHLBarOrigin != R2PHLBarOrigin[1]
                  then R2PHLBarOrigin[1]
                  else R3PHLBarOrigin[1];
                                    
                  
def R3PHBarID = bar - R3PHBarOrigin;
def R3PHLBarID = bar - R3PHLBarOrigin;

# R4
def R4PHValue = if R3PHBarOrigin != R3PHBarOrigin[1]
              then R3PHValue[1]
              else R4PHValue[1];
              
def R4PHLValue = if R3PHLBarOrigin != R3PHLBarOrigin[1]
              then R3PHLValue[1]
              else R4PHLValue[1];             
              
              
def R4PHBarOrigin = if R3PHBarOrigin != R3PHBarOrigin[1]
                  then R3PHBarOrigin[1]
                  else R4PHBarOrigin[1];
                  
def R4PHLBarOrigin = if R3PHLBarOrigin != R3PHLBarOrigin[1]
                  then R3PHLBarOrigin[1]
                  else R4PHLBarOrigin[1];                 
                  
def R4PHBarID = bar - R4PHBarOrigin;

def R4PHLBarID = bar - R4PHLBarOrigin;

# Parent Low
def ParentLow = LowestAll(l);
def ParentLBarOrigin = if l == ParentLow
                       then bar
                       else ParentLBarOrigin[1];
def ParentLBarID = bar - HighestAll(ParentLBarOrigin);
# S1
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else Double.NaN;
            
def PivotLH = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then h
             else Double.NaN;
            
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
              
def PLHValue = if !IsNaN(PivotLH)
              then PivotLH
              else PLHValue[1];
                
def PLBarOrigin = if !IsNaN(PivotL)
                  then bar
                  else PLBarOrigin[1];
                  
def PLHBarOrigin = if !IsNaN(PivotLH)
                  then bar
                  else PLHBarOrigin[1];
                  
def PLBarID = bar - PLBarOrigin;
def PLHBarID = bar - PLHBarOrigin;

# S2
def S2PLValue = if PLBarOrigin != PLBarOrigin[1]
              then PLValue[1]
              else S2PLValue[1];
              
def S2PLHValue = if PLHBarOrigin != PLHBarOrigin[1]
              then PLHValue[1]
              else S2PLHValue[1];             
                        
def S2PLBarOrigin = if PLBarOrigin != PLBarOrigin[1]
                  then PLBarOrigin[1]
                  else S2PLBarOrigin[1];
                  
def S2PLHBarOrigin = if PLHBarOrigin != PLHBarOrigin[1]
                  then PLHBarOrigin[1]
                  else S2PLHBarOrigin[1];                 
                  
def S2PLBarID = bar - S2PLBarOrigin;
def S2PLHBarID = bar - S2PLHBarOrigin;

# S3
def S3PLValue = if S2PLBarOrigin != S2PLBarOrigin[1]
              then S2PLValue[1]
              else S3PLValue[1];

def S3PLHValue = if S2PLHBarOrigin != S2PLHBarOrigin[1]
              then S2PLHValue[1]
              else S3PLHValue[1];             
                            
def S3PLBarOrigin = if S2PLBarOrigin != S2PLBarOrigin[1]
                  then S2PLBarOrigin[1]
                  else S3PLBarOrigin[1];

def S3PLHBarOrigin = if S2PLHBarOrigin != S2PLHBarOrigin[1]
                  then S2PLHBarOrigin[1]
                  else S3PLHBarOrigin[1];                 
                        
def S3PLBarID = bar - S3PLBarOrigin;
def S3PLHBarID = bar - S3PLHBarOrigin;

# S4
def S4PLValue = if S3PLBarOrigin != S3PLBarOrigin[1]
              then S3PLValue[1]
              else S4PLValue[1];
              
def S4PLHValue = if S3PLHBarOrigin != S3PLHBarOrigin[1]
              then S3PLHValue[1]
              else S4PLHValue[1];             
              
              
def S4PLBarOrigin = if S3PLBarOrigin != S3PLBarOrigin[1]
                  then S3PLBarOrigin[1]
                  else S4PLBarOrigin[1];

def S4PLHBarOrigin = if S3PLHBarOrigin != S3PLHBarOrigin[1]
                  then S3PLHBarOrigin[1]
                  else S4PLHBarOrigin[1];                 
                  
                  
def S4PLBarID = bar - S4PLBarOrigin;
def S4PLHBarID = bar - S4PLHBarOrigin;

# Plots
plot PR1 = LinePlot(BarID = ParentHBarID,
                    Value = ParentHigh,
                    BarOrigin = HighestAll(ParentHBarOrigin));

plot PR1L = LinePlot(BarID = ParentHBarID,
                    Value = ParentHighl,
                    BarOrigin = HighestAll(ParentHLBarOrigin));


PR1.SetDefaultColor(Color.GREEN);
PR1L.SetDefaultColor(Color.GREEN);

#addChartBubble(Bar == HighestAll(ParentHBarOrigin), ParentHigh, "High", color.yellow, 1);
plot R1 = LinePlot(BarID = PHBarID,
                   Value = PHValue,
                   BarOrigin = PHBarOrigin);
                  
plot R1L = LinePlot(BarID = PHLBarID,
                   Value = PHLValue,
                   BarOrigin = PHLBarOrigin);
                  
R1.SetDefaultColor(Color.GREEN);
R1L.SetDefaultColor(Color.GREEN);

plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);

plot R2L = LinePlot(BarID = R2PHLBarID,
                   Value = R2PHLValue,
                   BarOrigin = R2PHBarOrigin);                   
                  
R2.SetDefaultColor(Color.GREEN);
R2L.SetDefaultColor(Color.GREEN);

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
                  
plot R3L = LinePlot(BarID = R3PHLBarID,
                   Value = R3PHLValue,
                   BarOrigin = R3PHLBarOrigin);
                  
R3.SetDefaultColor(Color.GREEN);
R3L.SetDefaultColor(Color.GREEN);

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
                  
plot R4L = LinePlot(BarID = R4PHLBarID,
                   Value = R4PHLValue,
                   BarOrigin = R4PHLBarOrigin);
                  
R4.SetDefaultColor(Color.GREEN);
R4L.SetDefaultColor(Color.GREEN);


plot PS1 = LinePlot(BarID = ParentLBarID,
                   Value = ParentLow,
                   BarOrigin = HighestAll(ParentLBarOrigin));
PS1.SetDefaultColor(Color.RED);

#AddChartBubble(bar == HighestAll(ParentLBarOrigin), ParentLow, "Low", Color.YELLOW, 0);

plot S1 = LinePlot(BarID = PLBarID,
                   Value = PLValue,
                   BarOrigin = PLBarOrigin);
                  
plot S1H = LinePlot(BarID = PLHBarID,
                   Value = PLHValue,
                   BarOrigin = PLHBarOrigin);                   
                  
S1.SetDefaultColor(Color.RED);
S1H.SetDefaultColor(Color.RED);

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
                  

plot S2H = LinePlot(BarID = S2PLHBarID,
                   Value = S2PLHValue,
                   BarOrigin = S2PLHBarOrigin);
                                      
S2.SetDefaultColor(Color.RED);
S2H.SetDefaultColor(Color.RED);

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
                  
plot S3H = LinePlot(BarID = S3PLHBarID,
                   Value = S3PLHValue,
                   BarOrigin = S3PLHBarOrigin);                   
                  
S3.SetDefaultColor(Color.RED);
S3H.SetDefaultColor(Color.RED);

plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
                  
plot S4H = LinePlot(BarID = S4PLHBarID,
                   Value = S4PLHValue,
                   BarOrigin = S4PLHBarOrigin);
                  
S4.SetDefaultColor(Color.RED);
S4H.SetDefaultColor(Color.RED);
 
The cloud did not work. I'm trying to get the PR1 to plot Highest(l) for the Highest(H) bar. I tried but the line is not plotting where I need it. I'm trying to upload an image but unsuccessful.


Ruby:
# Support/Resistance Zones around pivot S/R points.
#Added the zones using ATR to the Theotrade Pivots study.
#Additions by Horserider 9/30/2019
input length4 = 14;
input movingAvgLength = 1;

input length = 252;
input length1 = 30;
input atrmod=2;
def bn = barnumber();
def lastBar = highestall(if isnan(close) then 0 else bn);
def offset = bn - lastbar;

# User Inputs
input n = 21; #hint n: periods used for pivot calculations.


# Internal Script Reference
script LinePlot {
    input BarID = 0;
    input Value = 0;
    input BarOrigin = 0;
    def ThisBar = HighestAll(BarOrigin);
    def ValueLine = if BarOrigin == ThisBar
                then Value
                else Double.NaN;
    plot P = if ThisBar - BarID <= BarOrigin
             then HighestAll(ValueLine)
             else Double.NaN;
}
# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def BBar = bar == HighestAll(bar);
# Parent High
def ParentHigh = HighestAll(h);

def ParentHighL = l;

def ParentHBarOrigin = if h == ParentHigh
                       then bar
                       else ParentHBarOrigin[1];

def ParentHLBarOrigin = if h == ParentHigh
                       then l
                       else ParentHBarOrigin[1];

def ParentHBarID = bar - HighestAll(ParentHBarOrigin);
def ParentHLBarID = bar - HighestAll(ParentHLBarOrigin);

# R1
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
       
def PivotH = if (bar > n and
                 h == Highest(h, n) and
                 hh)
            then h
            else Double.NaN;
           
def PivotHL = if (bar > n and
                 h == Highest(h, n) and
                 hh)
            then l
            else Double.NaN;
           

def PHValue = if !IsNaN(PivotH)
              then PivotH
              else PHValue[1];
             
def PHLValue = if !IsNaN(PivotHL)
              then PivotHL
              else PHLValue[1];            
             
def PHBarOrigin = if !IsNaN(PivotH)
                  then bar
                  else PHBarOrigin[1];

def PHLBarOrigin = if !IsNaN(PivotHL)
                  then bar
                  else PHLBarOrigin[1];                
                                   
def PHBarID = bar - PHBarOrigin;
def PHLBarID = bar - PHLBarOrigin;

# R2
def R2PHValue = if PHBarOrigin != PHBarOrigin[1]
              then PHValue[1]
              else R2PHValue[1];

def R2PHLValue = if PHLBarOrigin != PHLBarOrigin[1]
              then PHLValue[1]
              else R2PHLValue[1];
             
             
def R2PHBarOrigin = if PHBarOrigin != PHBarOrigin[1]
                  then PHBarOrigin[1]
                  else R2PHBarOrigin[1];

def R2PHLBarOrigin = if PHLBarOrigin != PHLBarOrigin[1]
                  then PHLBarOrigin[1]
                  else R2PHLBarOrigin[1];
                 
def R2PHBarID = bar - R2PHBarOrigin;
def R2PHLBarID = bar - R2PHLBarOrigin;


# R3
def R3PHValue = if R2PHBarOrigin != R2PHBarOrigin[1]
              then R2PHValue[1]
              else R3PHValue[1];
             
def R3PHLValue = if R2PHLBarOrigin != R2PHLBarOrigin[1]
              then R2PHLValue[1]
              else R3PHLValue[1];              
             
def R3PHBarOrigin = if R2PHBarOrigin != R2PHBarOrigin[1]
                  then R2PHBarOrigin[1]
                  else R3PHBarOrigin[1];
                 
def R3PHLBarOrigin = if R2PHLBarOrigin != R2PHLBarOrigin[1]
                  then R2PHLBarOrigin[1]
                  else R3PHLBarOrigin[1];
                                   
                 
def R3PHBarID = bar - R3PHBarOrigin;
def R3PHLBarID = bar - R3PHLBarOrigin;

# R4
def R4PHValue = if R3PHBarOrigin != R3PHBarOrigin[1]
              then R3PHValue[1]
              else R4PHValue[1];
             
def R4PHLValue = if R3PHLBarOrigin != R3PHLBarOrigin[1]
              then R3PHLValue[1]
              else R4PHLValue[1];            
             
             
def R4PHBarOrigin = if R3PHBarOrigin != R3PHBarOrigin[1]
                  then R3PHBarOrigin[1]
                  else R4PHBarOrigin[1];
                 
def R4PHLBarOrigin = if R3PHLBarOrigin != R3PHLBarOrigin[1]
                  then R3PHLBarOrigin[1]
                  else R4PHLBarOrigin[1];                
                 
def R4PHBarID = bar - R4PHBarOrigin;

def R4PHLBarID = bar - R4PHLBarOrigin;

# Parent Low
def ParentLow = LowestAll(l);
def ParentLBarOrigin = if l == ParentLow
                       then bar
                       else ParentLBarOrigin[1];
def ParentLBarID = bar - HighestAll(ParentLBarOrigin);
# S1
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PivotL = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then l
             else Double.NaN;
           
def PivotLH = if (bar > n and
                 l == Lowest(l, n) and
                 ll)
             then h
             else Double.NaN;
           
def PLValue = if !IsNaN(PivotL)
              then PivotL
              else PLValue[1];
             
def PLHValue = if !IsNaN(PivotLH)
              then PivotLH
              else PLHValue[1];
               
def PLBarOrigin = if !IsNaN(PivotL)
                  then bar
                  else PLBarOrigin[1];
                 
def PLHBarOrigin = if !IsNaN(PivotLH)
                  then bar
                  else PLHBarOrigin[1];
                 
def PLBarID = bar - PLBarOrigin;
def PLHBarID = bar - PLHBarOrigin;

# S2
def S2PLValue = if PLBarOrigin != PLBarOrigin[1]
              then PLValue[1]
              else S2PLValue[1];
             
def S2PLHValue = if PLHBarOrigin != PLHBarOrigin[1]
              then PLHValue[1]
              else S2PLHValue[1];            
                       
def S2PLBarOrigin = if PLBarOrigin != PLBarOrigin[1]
                  then PLBarOrigin[1]
                  else S2PLBarOrigin[1];
                 
def S2PLHBarOrigin = if PLHBarOrigin != PLHBarOrigin[1]
                  then PLHBarOrigin[1]
                  else S2PLHBarOrigin[1];                
                 
def S2PLBarID = bar - S2PLBarOrigin;
def S2PLHBarID = bar - S2PLHBarOrigin;

# S3
def S3PLValue = if S2PLBarOrigin != S2PLBarOrigin[1]
              then S2PLValue[1]
              else S3PLValue[1];

def S3PLHValue = if S2PLHBarOrigin != S2PLHBarOrigin[1]
              then S2PLHValue[1]
              else S3PLHValue[1];            
                           
def S3PLBarOrigin = if S2PLBarOrigin != S2PLBarOrigin[1]
                  then S2PLBarOrigin[1]
                  else S3PLBarOrigin[1];

def S3PLHBarOrigin = if S2PLHBarOrigin != S2PLHBarOrigin[1]
                  then S2PLHBarOrigin[1]
                  else S3PLHBarOrigin[1];                
                       
def S3PLBarID = bar - S3PLBarOrigin;
def S3PLHBarID = bar - S3PLHBarOrigin;

# S4
def S4PLValue = if S3PLBarOrigin != S3PLBarOrigin[1]
              then S3PLValue[1]
              else S4PLValue[1];
             
def S4PLHValue = if S3PLHBarOrigin != S3PLHBarOrigin[1]
              then S3PLHValue[1]
              else S4PLHValue[1];            
             
             
def S4PLBarOrigin = if S3PLBarOrigin != S3PLBarOrigin[1]
                  then S3PLBarOrigin[1]
                  else S4PLBarOrigin[1];

def S4PLHBarOrigin = if S3PLHBarOrigin != S3PLHBarOrigin[1]
                  then S3PLHBarOrigin[1]
                  else S4PLHBarOrigin[1];                
                 
                 
def S4PLBarID = bar - S4PLBarOrigin;
def S4PLHBarID = bar - S4PLHBarOrigin;

# Plots
plot PR1 = LinePlot(BarID = ParentHBarID,
                    Value = ParentHigh,
                    BarOrigin = HighestAll(ParentHBarOrigin));

plot PR1L = LinePlot(BarID = ParentHBarID,
                    Value = ParentHighl,
                    BarOrigin = HighestAll(ParentHLBarOrigin));


PR1.SetDefaultColor(Color.GREEN);
PR1L.SetDefaultColor(Color.GREEN);

#addChartBubble(Bar == HighestAll(ParentHBarOrigin), ParentHigh, "High", color.yellow, 1);
plot R1 = LinePlot(BarID = PHBarID,
                   Value = PHValue,
                   BarOrigin = PHBarOrigin);
                 
plot R1L = LinePlot(BarID = PHLBarID,
                   Value = PHLValue,
                   BarOrigin = PHLBarOrigin);
                 
R1.SetDefaultColor(Color.GREEN);
R1L.SetDefaultColor(Color.GREEN);

plot R2 = LinePlot(BarID = R2PHBarID,
                   Value = R2PHValue,
                   BarOrigin = R2PHBarOrigin);

plot R2L = LinePlot(BarID = R2PHLBarID,
                   Value = R2PHLValue,
                   BarOrigin = R2PHBarOrigin);                  
                 
R2.SetDefaultColor(Color.GREEN);
R2L.SetDefaultColor(Color.GREEN);

plot R3 = LinePlot(BarID = R3PHBarID,
                   Value = R3PHValue,
                   BarOrigin = R3PHBarOrigin);
                 
plot R3L = LinePlot(BarID = R3PHLBarID,
                   Value = R3PHLValue,
                   BarOrigin = R3PHLBarOrigin);
                 
R3.SetDefaultColor(Color.GREEN);
R3L.SetDefaultColor(Color.GREEN);

plot R4 = LinePlot(BarID = R4PHBarID,
                   Value = R4PHValue,
                   BarOrigin = R4PHBarOrigin);
                 
plot R4L = LinePlot(BarID = R4PHLBarID,
                   Value = R4PHLValue,
                   BarOrigin = R4PHLBarOrigin);
                 
R4.SetDefaultColor(Color.GREEN);
R4L.SetDefaultColor(Color.GREEN);


plot PS1 = LinePlot(BarID = ParentLBarID,
                   Value = ParentLow,
                   BarOrigin = HighestAll(ParentLBarOrigin));
PS1.SetDefaultColor(Color.RED);

#AddChartBubble(bar == HighestAll(ParentLBarOrigin), ParentLow, "Low", Color.YELLOW, 0);

plot S1 = LinePlot(BarID = PLBarID,
                   Value = PLValue,
                   BarOrigin = PLBarOrigin);
                 
plot S1H = LinePlot(BarID = PLHBarID,
                   Value = PLHValue,
                   BarOrigin = PLHBarOrigin);                  
                 
S1.SetDefaultColor(Color.RED);
S1H.SetDefaultColor(Color.RED);

plot S2 = LinePlot(BarID = S2PLBarID,
                   Value = S2PLValue,
                   BarOrigin = S2PLBarOrigin);
                 

plot S2H = LinePlot(BarID = S2PLHBarID,
                   Value = S2PLHValue,
                   BarOrigin = S2PLHBarOrigin);
                                     
S2.SetDefaultColor(Color.RED);
S2H.SetDefaultColor(Color.RED);

plot S3 = LinePlot(BarID = S3PLBarID,
                   Value = S3PLValue,
                   BarOrigin = S3PLBarOrigin);
                 
plot S3H = LinePlot(BarID = S3PLHBarID,
                   Value = S3PLHValue,
                   BarOrigin = S3PLHBarOrigin);                  
                 
S3.SetDefaultColor(Color.RED);
S3H.SetDefaultColor(Color.RED);

plot S4 = LinePlot(BarID = S4PLBarID,
                   Value = S4PLValue,
                   BarOrigin = S4PLBarOrigin);
                 
plot S4H = LinePlot(BarID = S4PLHBarID,
                   Value = S4PLHValue,
                   BarOrigin = S4PLHBarOrigin);
                 
S4.SetDefaultColor(Color.RED);
S4H.SetDefaultColor(Color.RED);

sorry about that. i guess those sloped line formulas are too complex to use in a cloud. looking for another way....
 
I am having issue running a scan where "plot slope is true". Any help would be greatly appreciated. Thanks.


Ruby:
# AutoTrendLine_02

# -----------
# halcyonguy
# 21-06-23
# change original so it draws 2 trendlines, 1 above , and 1 below, instead of 1

# https://usethinkscript.com/threads/auto-trend-line.6915/
# Auto Trend Line

def na = double.nan;
input n = 20;

def hi = high;
def lo = low;
def c = close;
def bar = BarNumber();

# high trendline ---------------------
def hihi = HighestAll(hi);
def hhBar = if hi == hihi
  then bar
  else Double.NaN;

def hifirstPoint = hihi;
def hifirstbar = HighestAll(hhBar);

def h_h = fold i = 1 to n + 1
  with p = 1
  while p
  do hi > GetValue(hi, -i);

def PivotH = if (bar > n and hi == Highest(hi, n) and h_h)
  then hi
  else Double.NaN;
def PHValue = if !IsNaN(PivotH)
  then PivotH
  else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
  then bar
  else Double.NaN;

def hisecPoint = if hifirstPoint == hihi then PHValue else Double.NaN;
def hisecBar = if hifirstPoint == hihi then PHBarOrigin else Double.NaN;

def hirun = if bar == hifirstBar then 1 else hirun[1] + 1;
def hirise = if bar == HighestAll(hisecBar) then hisecPoint - hifirstPoint else Double.NaN;
def hislope = hirise / hirun[1];
def hislopeX = if !IsNaN(hislope) then hislope else hislopeX[1];
def hilineX = if !IsNaN(hislope) then hisecPoint else hilineX[1] + hislopeX;

#  hi trend line
plot hitrendLine = if bar == hifirstBar then hifirstPoint
  else if bar == HighestAll(hisecBar) then hisecPoint
  else Double.NaN;
hitrendLine.EnableApproximation();
hitrendLine.SetStyle(Curve.FIRM);
hitrendLine.SetDefaultColor(Color.green);

#trend line1 ext
plot hiLine1Ext = if hilineX > 0 then hilineX else Double.NaN;
hiLine1Ext.SetDefaultColor(Color.green);
hiLine1Ext.SetStyle(Curve.firm);


# low trendline ---------------------
def lolo = LowestAll(lo);
def llBar = if lo == lolo
  then bar
  else Double.NaN;



def lofirstPoint = lolo;
def lofirstbar = HighestAll(llBar);



def l_l = fold j = 1 to n + 1
  with q = 1
  while q
  do lo < GetValue(lo, -j);
def PivotL = if (bar > n and
  lo == Lowest(lo, n) and l_l)
  then lo
  else Double.NaN;
def PLValue = if !IsNaN(PivotL)
  then PivotL
  else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
  then bar
  else PLBarOrigin[1];

def losecPoint = if lofirstPoint == lolo then PLValue else Double.NaN;
def losecBar = if lofirstPoint == lolo then PLBarOrigin else Double.NaN;


def lorun = if bar == lofirstBar then 1 else lorun[1] + 1;
def lorise = if bar == HighestAll(losecBar) then losecPoint - lofirstPoint else Double.NaN;
def loslope = lorise / lorun[1];
def loslopeX = if !IsNaN(loslope) then loslope else loslopeX[1];
def lolineX = if !IsNaN(loslope) then losecPoint else lolineX[1] + loslopeX;

#  lo trend line
plot lotrendLine = if bar == lofirstBar then lofirstPoint
  else if bar == HighestAll(losecBar) then losecPoint
  else Double.NaN;
lotrendLine.EnableApproximation();
lotrendLine.SetStyle(Curve.FIRM);
lotrendLine.SetDefaultColor(Color.magenta);


# lo trend line ext
plot loLine1Ext = if lolineX > 0 then lolineX else Double.NaN;
loLine1Ext.SetDefaultColor(Color.magenta);
loLine1Ext.SetStyle(Curve.firm);


# ----------------------------------

# horz resistance line
plot R1 = if bar >= HighestAll(PHBarOrigin)
  then HighestAll(if IsNaN(c[-1])
    then PHValue
      else Double.NaN)
    else Double.NaN;
r1.SetDefaultColor(Color.orange);



# horz support line , from lower trendline end
plot S1 = if bar >= HighestAll(PLBarOrigin)
  then HighestAll(if IsNaN(c[-1])
  then PLValue
  else Double.NaN)
  else Double.NaN;
s1.SetDefaultColor(Color.yellow);



# Mobius_Scalper_Pivots
# V01.2011

input length = 14;
input averageType = AverageType.WILDERS;

plot ADX = DMI(length, averageType).ADX;
ADX.hide();
plot Forecast = MarketForecast().Intermediate;
forecast.hide();

input price = close;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
RSI.hide();


#AddChartBubble(isNaN(close[-1]) and !isNaN(close[1]), high, "ADX:"+" "+round(ADX,0)+" | "+"RSI:"+" "+round(RSI,0)+" | "+"MF:"+" "+round(forecast,0),color.cyan, yes);

input cloudht = 0.5;
addcloud(hitrendLine + cloudht, hitrendLine - cloudht, color.light_gray, color.light_gray);
addcloud(lotrendLine + cloudht, lotrendLine - cloudht, color.light_gray, color.light_gray);

plot g=hilineX;
g.hide();
plot h=lolineX;
h.hide();

plot slope=lorun*close/hirun*close > 10;
slope.hide();
 
codes for a scan need to have just 1 plot, and for it to be true/false. there are many plots in that study. change all the plots , except last 1, to def
 
AutoTrendLine Scan Version Only
@british43 @halcyonguy is correct. With all the extraneous plots and statements not related to the scan removed the Auto Trend Line script will work in the scanner.
Ruby:
# AutoTrendLine_02 ****SCAN VERSION ONLY***
# -----------
# halcyonguy
# 21-06-23
# change original so it draws 2 trendlines, 1 above , and 1 below, instead of 1

# https://usethinkscript.com/threads/auto-trend-line.6915/
# Auto Trend Line
def na = double.nan;
input n = 20;

def hi = high;
def lo = low;
def c = close;
def bar = BarNumber();

# high trendline ---------------------
def hihi = HighestAll(hi);
def hhBar = if hi == hihi
  then bar
  else Double.NaN;

def hifirstPoint = hihi;
def hifirstbar = HighestAll(hhBar);

def h_h = fold i = 1 to n + 1
  with p = 1
  while p
  do hi > GetValue(hi, -i);

def PivotH = if (bar > n and hi == Highest(hi, n) and h_h)
  then hi
  else Double.NaN;
def PHValue = if !IsNaN(PivotH)
  then PivotH
  else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
  then bar
  else Double.NaN;

def hisecPoint = if hifirstPoint == hihi then PHValue else Double.NaN;
def hisecBar = if hifirstPoint == hihi then PHBarOrigin else Double.NaN;

def hirun = if bar == hifirstBar then 1 else hirun[1] + 1;

# low trendline ---------------------
def lolo = LowestAll(lo);
def llBar = if lo == lolo
  then bar
  else Double.NaN;

def lofirstPoint = lolo;
def lofirstbar = HighestAll(llBar);

def l_l = fold j = 1 to n + 1
  with q = 1
  while q
  do lo < GetValue(lo, -j);
def PivotL = if (bar > n and
  lo == Lowest(lo, n) and l_l)
  then lo
  else Double.NaN;
def PLValue = if !IsNaN(PivotL)
  then PivotL
  else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
  then bar
  else PLBarOrigin[1];

def losecPoint = if lofirstPoint == lolo then PLValue else Double.NaN;
def losecBar = if lofirstPoint == lolo then PLBarOrigin else Double.NaN;

def lorun = if bar == lofirstBar then 1 else lorun[1] + 1;

plot slope=lorun*close/hirun*close > 10;
slope.hide();
 
Last edited:
@halcyonguy I'm trying to shorten the trendlines to +/-3 bars from the beginning and at the end instead of an extension. Is there a easy way to do this?

Ruby:
#Auto Trendline

declare upper;

#################################################
# Defining High, Low and Pivot Points
#################################################
def Hi = high;
def Lo = low;
def P1H = (Hi > Hi[1] or (Hi > Hi[2] and Hi == Hi[1])) and Hi > Hi[-1];
def P1L = (Lo < Lo[1] or (Lo < Lo[2] and Lo == Lo[1])) and Lo < Lo[-1];
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def Piv1h = if P1H[1] then 1 else Piv1h[1] + 1;
def Piv1l = if P1L[1] then 1 else Piv1l[1] + 1;
def PivN1h = fold m = 1 to lastBar with a = Double.NaN while IsNaN(a) do if GetValue(P1H, -m) then -m else Double.NaN;
def PivN1l = fold n = 1 to lastBar with b = Double.NaN while IsNaN(b) do if GetValue(P1L, -n) then -n else Double.NaN;

def prevPivhigh = GetValue(Hi, Piv1h);
def prevPivlow = GetValue(Lo, Piv1l);
def nextPivhigh = GetValue(Hi, PivN1h);
def nextPivlow = GetValue(Lo, PivN1l);
def P2H = Hi > prevPivhigh and Hi > nextPivhigh;
def P2L = Lo < prevPivlow and Lo < nextPivlow;
def p2x2 = P2H and P2L;
def Piv2h = if P2H[1] then 1 else Piv2h[1] + 1;
def Piv2l = if P2L[1] then 1 else Piv2l[1] + 1;
def PivN2h = fold o = 1 to lastBar with c = Double.NaN while IsNaN(c) do if GetValue(P2H, -o) then -o else Double.NaN;
def PivN2l = fold p = 1 to lastBar with d = Double.NaN while IsNaN(d) do if GetValue(P2L, -p) then -p else Double.NaN;
def NextP2H = if PivN2h > PivN2l then 1 else Double.NaN;
def NextP2L = if PivN2l > PivN2h then 1 else Double.NaN;
def NextP2Both = if PivN2h == PivN2l then 1 else Double.NaN;
def PrevP2H = Piv2h < Piv2l;
def PrevP2L = Piv2l < Piv2h;
def PrevP2Both = Piv2h == Piv2l;
def nearLow = if P2L and PrevP2H then Lo else nearLow[1];
def AL0 = if P2L and PrevP2L and Lo > nearLow then 1 else Double.NaN;
def AL1 = if P2L and NextP2L and GetValue(Lo, PivN2l) <= Lo then 1 else Double.NaN;
def AL2 = if p2x2 and PrevP2L and Lo > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def AL3 = if P2L and PrevP2Both and !NextP2H and Lo > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def AL4 = if P2L and PrevP2L and Lo > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def AL5 = if P2L and PrevP2H and NextP2Both and Lo > GetValue(Lo, PivN2l) and GetValue(Hi, PivN2h) < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def A2L = P2L and IsNaN(AL0) and IsNaN(AL1) and IsNaN(AL2) and IsNaN(AL3) and IsNaN(AL4) and IsNaN(AL5);
def recentHigh = if P2H and PrevP2L then Hi else recentHigh[1];
def AH0 = if P2H and PrevP2H and Hi < recentHigh then 1 else Double.NaN;
def AH1 = if P2H and NextP2H and GetValue(Hi, PivN2h) >= Hi then 1 else Double.NaN;
def AH2 = if p2x2 and PrevP2H and Hi < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def AH3 = if P2H and PrevP2Both and !NextP2L and Hi < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def AH4 = if P2H and PrevP2H and Hi < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def AH5 = if P2H and PrevP2L and NextP2Both and Hi < GetValue(Hi, PivN2h) and GetValue(Lo, PivN2l) > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def A2H = P2H and IsNaN(AH0) and IsNaN(AH1) and IsNaN(AH2) and IsNaN(AH3) and IsNaN(AH4) and IsNaN(AH5);

def PPH = if A2H[1] then 1 else PPH[1] + 1;
def PPL = if A2L[1] then 1 else PPL[1] + 1;
def HighLow = if A2L and Lo > GetValue(Lo, PPL) then 1 else Double.NaN;
def LowHigh = if A2H and Hi < GetValue(Hi, PPH) then 1 else Double.NaN;
def PivTime = if !IsNaN(HighLow) or !IsNaN(LowHigh) then PivTime[1] + 1 else PivTime[1];

#################################################
# Trend Line End Numbers
#################################################
def end1 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) then BarNumber() else 0;
def end2 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 1 then BarNumber() else 0;
def end3 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 2 then BarNumber() else 0;
def end4 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 3 then BarNumber() else 0;
def end5 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 4 then BarNumber() else 0;
def end6 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 5 then BarNumber() else 0;
def end7 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 6 then BarNumber() else 0;
def end8 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 7 then BarNumber() else 0;

#################################################
# Trend Line Start Numbers
#################################################
def start1 = if end1 and !IsNaN(HighLow) then end1 - PPL else if end1 and !IsNaN(LowHigh) then end1 - PPH else 0;
def start2 = if end2 and !IsNaN(HighLow) then end2 - PPL else if end2 and !IsNaN(LowHigh) then end2 - PPH else 0;
def start3 = if end3 and !IsNaN(HighLow) then end3 - PPL else if end3 and !IsNaN(LowHigh) then end3 - PPH else 0;
def start4 = if end4 and !IsNaN(HighLow) then end4 - PPL else if end4 and !IsNaN(LowHigh) then end4 - PPH else 0;
def start5 = if end5 and !IsNaN(HighLow) then end5 - PPL else if end5 and !IsNaN(LowHigh) then end5 - PPH else 0;
def start6 = if end6 and !IsNaN(HighLow) then end6 - PPL else if end6 and !IsNaN(LowHigh) then end6 - PPH else 0;
def start7 = if end7 and !IsNaN(HighLow) then end7 - PPL else if end7 and !IsNaN(LowHigh) then end7 - PPH else 0;
def start8 = if end8 and !IsNaN(HighLow) then end8 - PPL else if end8 and !IsNaN(LowHigh) then end8 - PPH else 0;

#################################################
# Price Data
#################################################
def price1 = HighestAll(if end1 and A2L then GetValue(Lo, end1 - start1) else if end1 and A2H then GetValue(Hi, end1 - start1) else 0);
def price2 = HighestAll(if end2 and A2L then GetValue(Lo, end2 - start2) else if end2 and A2H then GetValue(Hi, end2 - start2) else 0);
def price3 = HighestAll(if end3 and A2L then GetValue(Lo, end3 - start3) else if end3 and A2H then GetValue(Hi, end3 - start3) else 0);
def price4 = HighestAll(if end4 and A2L then GetValue(Lo, end4 - start4) else if end4 and A2H then GetValue(Hi, end4 - start4) else 0);
def price5 = HighestAll(if end5 and A2L then GetValue(Lo, end5 - start5) else if end5 and A2H then GetValue(Hi, end5 - start5) else 0);
def price6 = HighestAll(if end6 and A2L then GetValue(Lo, end6 - start6) else if end6 and A2H then GetValue(Hi, end6 - start6) else 0);
def price7 = HighestAll(if end7 and A2L then GetValue(Lo, end7 - start7) else if end7 and A2H then GetValue(Hi, end7 - start7) else 0);
def price8 = HighestAll(if end8 and A2L then GetValue(Lo, end8 - start8) else if end8 and A2H then GetValue(Hi, end8 - start8) else 0);

def price01 = HighestAll(if end1 and A2L then Lo else if end1 and A2H then Hi else 0);
def price02 = HighestAll(if end2 and A2L then Lo else if end2 and A2H then Hi else 0);
def price03 = HighestAll(if end3 and A2L then Lo else if end3 and A2H then Hi else 0);
def price04 = HighestAll(if end4 and A2L then Lo else if end4 and A2H then Hi else 0);
def price05 = HighestAll(if end5 and A2L then Lo else if end5 and A2H then Hi else 0);
def price06 = HighestAll(if end6 and A2L then Lo else if end6 and A2H then Hi else 0);
def price07 = HighestAll(if end7 and A2L then Lo else if end7 and A2H then Hi else 0);
def price08 = HighestAll(if end8 and A2L then Lo else if end8 and A2H then Hi else 0);

#################################################
# Defining Trend Line
#################################################
script trendLine
{
input startBar = 0;
input startPrice = 0;
input endBar = 0;
input endPrice = 0;
input leftExt = 0;
input rightExt = 0;
input limitRight = no;

def TrendV = (endPrice - startPrice) / (endBar - startBar);
def NewBar = BarNumber() - endBar;
plot TheTrend; if !limitRight then {TheTrend = if BarNumber() >= startBar - leftExt then endPrice + NewBar * TrendV else Double.NaN;} else {TheTrend = if BarNumber() > endBar + rightExt or BarNumber() < startBar - leftExt then Double.NaN else endPrice + NewBar * TrendV;}
}

#################################################
# Trend Lines
#################################################
input howManyTrendLinesMax_8 = 4;

plot Trend1 = trendline(HighestAll(start1), price1, HighestAll(end1), price01, 5, no);
Trend1.SetDefaultColor(Color.WHITE);
Trend1.SetStyle(Curve.FIRM);
plot Trend2 = trendline(HighestAll(start2), price2, HighestAll(end2), price02, 5, no);
Trend2.setDefaultColor(color.WHITE);
Trend2.SetStyle(Curve.SHORT_DASH);
Trend2.SetHiding(howManyTrendLinesMax_8 < 2);
plot Trend3 = trendline(HighestAll(start3), price3, HighestAll(end3), price03, 5, no);
Trend3.SetDefaultColor(color.WHITE);
Trend3.SetStyle(Curve.SHORT_DASH);
Trend3.SetHiding(howManyTrendLinesMax_8 < 3);
plot Trend4 = trendline(HighestAll(start4), price4, HighestAll(end4), price04, 5, no);
Trend4.SetDefaultColor(color.WHITE);
Trend4.SetStyle(Curve.SHORT_DASH);
Trend4.SetHiding(howManyTrendLinesMax_8 < 4);
plot Trend5 = trendline(HighestAll(start5), price5, HighestAll(end5), price05, 5, no);
Trend5.SetDefaultColor(color.WHITE);
Trend5.SetStyle(Curve.SHORT_DASH);
Trend5.SetHiding(howManyTrendLinesMax_8 < 5);
plot Trend6 = trendline(HighestAll(start6), price6, HighestAll(end6), price06, 5, no);
Trend6.SetDefaultColor(color.WHITE);
Trend6.SetStyle(Curve.SHORT_DASH);
Trend6.SetHiding(howManyTrendLinesMax_8 < 6);
plot Trend7 = trendline(HighestAll(start7), price7, HighestAll(end7), price07, 5, no);
Trend7.SetDefaultColor(color.WHITE);
Trend7.SetStyle(Curve.SHORT_DASH);
Trend7.SetHiding(howManyTrendLinesMax_8 < 7);
plot Trend8 = trendline(HighestAll(start8), price8, HighestAll(end8), price08, 5, no);
Trend8.SetDefaultColor(color.WHITE);
Trend8.SetStyle(Curve.SHORT_DASH);
Trend8.SetHiding(howManyTrendLinesMax_8 < 8);

#################################################
# Arrows
#################################################
input showArrows = yes;

#Up Arrows
plot DownArrow1 = Trend1 > Trend1[1] and close crosses below Trend1;
DownArrow1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownArrow1.SetDefaultColor(color.CYAN);
DownArrow1.SetLineWeight(5);
DownArrow1.SetHiding(!showArrows);

#Down Arrows
plot UpArrow1 = Trend1 < Trend1[1] and close crosses above Trend1;
UpArrow1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpArrow1.SetDefaultColor(color.CYAN);
UpArrow1.SetLineWeight(5);
UpArrow1.SetHiding(!showArrows);

#################################################
# Display Box
#################################################
input ShowDisplayBox = yes;
AddLabel(ShowDisplayBox, " ->", color.DARK_GRAY);
AddLabel(ShowDisplayBox, if close > Trend1 then "Above Current Trend" else "Below Current Trend", if close > Trend1 then color.GREEN else color.RED);
AddLabel(ShowDisplayBox, "<- ", color.DARK_GRAY);

#################################################
# Alerts
#################################################
input AudibleAlerts = yes;
Alert(AudibleAlerts and UpArrow1, GetSymbol() + " Possible Breakout.", Alert.BAR, Sound.Bell);
Alert(AudibleAlerts and DownArrow1, GetSymbol() + " Possible Breakdown.", Alert.BAR, Sound.Bell);
#################################################
def data = close;
def mathh = close-trend4;
plot disth = round(mathh/close*100,2);
disth.hide();
plot near=disth <1 within 1 bar;
near.hide();
addlabel(1,if disth < 1 and disth >-1 then "TRENDLINE" else " ");
 
@halcyonguy @MerryDay I'm trying to plot extension lines for both the hitrendline and lowtrendline 5-10 bars at the start and end of the line. I tried but failed. Here is the code I was using for plotting an extension but it was not plotting right.

{input price = high;
def startBar = barnumber1;
def endBar = barnumber2;

plot Extension = point_slope(price, startBar, endBar);
extension.SetStyle(Curve.firm);}

Here is the code for the entire script

Ruby:
# AutoTrendLine_02

# -----------
# halcyonguy
# 21-06-23
# change original so it draws 2 trendlines, 1 above , and 1 below, instead of 1

# https://usethinkscript.com/threads/auto-trend-line.6915/
# Auto Trend Line

def na = double.nan;
input n = 20;

def hi = high;
def lo = low;
def c = close;
def bar = BarNumber();

# high trendline ---------------------
def hihi = Highest(hi);
def hhBar = if hi == hihi
  then bar
  else Double.NaN;

def hifirstPoint = hihi;
def hifirstbar = HighestAll(hhBar);

def h_h = fold i = 1 to n + 1
  with p = 1
  while p
  do hi > GetValue(hi, -i);

def PivotH = if (bar > n and hi == Highest(hi, n) and h_h)
  then hi
  else Double.NaN;
def PHValue = if !IsNaN(PivotH)
  then PivotH
  else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
  then bar
  else Double.NaN;

def hisecPoint = if hifirstPoint == hihi then PHValue else Double.NaN;
def hisecBar = if hifirstPoint == hihi then PHBarOrigin else Double.NaN;

def hirun = if bar == hifirstBar then 1 else hirun[1] + 1;
def hirise = if bar == HighestAll(hisecBar) then hisecPoint - hifirstPoint else Double.NaN;
def hislope = hirise / hirun[1];
def hislopeX = if !IsNaN(hislope) then hislope else hislopeX[1];
def hilineX = if !IsNaN(hislope) then hisecPoint else hilineX[1] + hislopeX;

#  hi trend line
plot hitrendLine = if bar == hifirstBar then hifirstPoint
  else if bar == HighestAll(hisecBar) then hisecPoint
  else Double.NaN;
hitrendLine.EnableApproximation();
hitrendLine.SetStyle(Curve.FIRM);
hitrendLine.SetDefaultColor(Color.green);

# low trendline ---------------------
def lolo = Lowest(lo);
def llBar = if lo == lolo
  then bar
  else Double.NaN;

def lofirstPoint = lolo;
def lofirstbar = HighestAll(llBar);

def l_l = fold j = 1 to n + 1
  with q = 1
  while q
  do lo < GetValue(lo, -j);
def PivotL = if (bar > n and
  lo == Lowest(lo, n) and l_l)
  then lo
  else Double.NaN;
def PLValue = if !IsNaN(PivotL)
  then PivotL
  else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
  then bar
  else PLBarOrigin[1];

def losecPoint = if lofirstPoint == lolo then PLValue else Double.NaN;
def losecBar = if lofirstPoint == lolo then PLBarOrigin else Double.NaN;

def lorun = if bar == lofirstBar then 1 else lorun[1] + 1;
def lorise = if bar == HighestAll(losecBar) then losecPoint - lofirstPoint else Double.NaN;
def loslope = lorise / lorun[1];
def loslopeX = if !IsNaN(loslope) then loslope else loslopeX[1];
def lolineX = if !IsNaN(loslope) then losecPoint else lolineX[1] + loslopeX;

#  lo trend line
plot lotrendLine = if bar == lofirstBar then lofirstPoint
  else if bar == HighestAll(losecBar) then losecPoint
  else Double.NaN;
lotrendLine.EnableApproximation();
lotrendLine.SetStyle(Curve.FIRM);
lotrendLine.SetDefaultColor(Color.magenta);


keOfYpy.png
 
@halcyonguy @MerryDay I'm trying to plot extension lines for both the hitrendline and lowtrendline 5-10 bars at the start and end of the line. I tried but failed.


changed v02, from post3
can pick a how many bars, the extension lines will exist. default is 10 bars.
input extend_lines_for_x_bars = 10;
if 0 is entered, the lines extend to the right edge of the screen.

i add lines like these, around the new code lines
#//////////////////////////////
it finds the barnumber of the recent peak. a variable keeps that value. a formula compares the barnumber, to see if it is within a range of the peak barnumber and the peak barnumber + the extend quantity. same thing for valleys.


added an input to turn horizontal lines on or off.

Ruby:
# AutoTrendLine_03

# chg ext lines, to exist for x bars

# -----------
# halcyonguy
# 21-06-23
# change original so it draws 2 trendlines, 1 above , and 1 below, instead of 1

# original
# the original study draws 1 trendline
#  finds the barnumbers for the highest high and lowest low
#   if the highest high has a lower barnumber, the trendline is drawn above the candles.
#   if the lowest low has a lower barnumber, the trendline is drawn below the candles.

def na = double.nan;
input n = 20;

def hi = high;
def lo = low;
def c = close;
def bar = BarNumber();

# how many bars to extend lines past last pivot. 0 = extend to the right edge of screen.
input extend_lines_for_x_bars = 10;
def exl = if extend_lines_for_x_bars == 0 then highestall(bar) else extend_lines_for_x_bars;


# high trendline ---------------------
def hihi = HighestAll(hi);
def hhBar = if hi == hihi
  then bar
  else Double.NaN;

def hifirstPoint = hihi;
def hifirstbar = HighestAll(hhBar);

def h_h = fold i = 1 to n + 1
  with p = 1
  while p
  do hi > GetValue(hi, -i);

def PivotH = if (bar > n and hi == Highest(hi, n) and h_h)
  then hi
  else Double.NaN;
def PHValue = if !IsNaN(PivotH)
  then PivotH
  else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
  then bar
  else Double.NaN;

#//////////////////////////////
# find last peak. count from that bar, x bars, for displaying ext line

#def hipeakbn = highestall(PHBarOrigin );
#addverticalline(hipeakbn == bar , "hipeakbn", color.cyan);

# retain bn of recent peak
#def PHBarOrigin2 = if !IsNaN(PivotH)
def PHBarOrigin2 = if !IsNaN(PivotH)
  then bar
  else PHBarOrigin2[1];

#input test1 = no;
#addchartbubble(test1, low, PHBarOrigin2, color.yellow, no);

def extlinebarsh = if ( bar >= PHBarOrigin2 and bar <= (PHBarOrigin2 + exl)) then 1 else 0;

#input test2 = no;
#addverticalline(test2 and extlinebarsh, "ext", color.yellow);
#//////////////////////////////


def hisecPoint = if hifirstPoint == hihi then PHValue else Double.NaN;
def hisecBar = if hifirstPoint == hihi then PHBarOrigin else Double.NaN;

def hirun = if bar == hifirstBar then 1 else hirun[1] + 1;
def hirise = if bar == HighestAll(hisecBar) then hisecPoint - hifirstPoint else Double.NaN;
def hislope = hirise / hirun[1];
def hislopeX = if !IsNaN(hislope) then hislope else hislopeX[1];
def hilineX = if !IsNaN(hislope) then hisecPoint else hilineX[1] + hislopeX;


#----------------------------

#  hi trend line
plot hitrendLine = if bar == hifirstBar then hifirstPoint
  else if bar == HighestAll(hisecBar) then hisecPoint
  else Double.NaN;
hitrendLine.EnableApproximation();
hitrendLine.SetStyle(Curve.FIRM);
hitrendLine.SetDefaultColor(Color.green);

# trend line1 ext
#plot hiLine1Ext = if hilineX > 0 then hilineX else Double.NaN;
#//////////////////////////////
plot hiLine1Ext = if (hilineX > 0 and extlinebarsh) then hilineX else Double.NaN;
#//////////////////////////////
hiLine1Ext.SetDefaultColor(Color.green);
hiLine1Ext.SetStyle(Curve.MEDIUM_DASH);


# low trendline ---------------------
def lolo = LowestAll(lo);
def llBar = if lo == lolo
  then bar
  else Double.NaN;

def lofirstPoint = lolo;
def lofirstbar = HighestAll(llBar);

def l_l = fold j = 1 to n + 1
  with q = 1
  while q
  do lo < GetValue(lo, -j);
def PivotL = if (bar > n and
  lo == Lowest(lo, n) and l_l)
  then lo
  else Double.NaN;
def PLValue = if !IsNaN(PivotL)
  then PivotL
  else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
  then bar
  else PLBarOrigin[1];

#//////////////////////////////
# find last valley. count from that bar, x bars, for displaying ext line

#def hipeakbn = highestall(PHBarOrigin );
#addverticalline(hipeakbn == bar , "hipeakbn", color.cyan);

# retain bn of recent valley
def PLBarOrigin2 = if !IsNaN(PivotL)
  then bar
  else PLBarOrigin2[1];

#input test1 = no;
#addchartbubble(test1, low, PLBarOrigin2, color.yellow, no);

def extlinebarsl = if ( bar >= PlBarOrigin2 and bar <= (PlBarOrigin2 + exl)) then 1 else 0;

#input test3 = no;
#addverticalline(test3 and extlinebarsl, "ext", color.yellow);
#//////////////////////////////

def losecPoint = if lofirstPoint == lolo then PLValue else Double.NaN;
def losecBar = if lofirstPoint == lolo then PLBarOrigin else Double.NaN;

def lorun = if bar == lofirstBar then 1 else lorun[1] + 1;
def lorise = if bar == HighestAll(losecBar) then losecPoint - lofirstPoint else Double.NaN;
def loslope = lorise / lorun[1];
def loslopeX = if !IsNaN(loslope) then loslope else loslopeX[1];
def lolineX = if !IsNaN(loslope) then losecPoint else lolineX[1] + loslopeX;

#  lo trend line
plot lotrendLine = if bar == lofirstBar then lofirstPoint
  else if bar == HighestAll(losecBar) then losecPoint
  else Double.NaN;
lotrendLine.EnableApproximation();
lotrendLine.SetStyle(Curve.FIRM);
lotrendLine.SetDefaultColor(Color.magenta);

# lo trend line ext
#plot loLine1Ext = if lolineX > 0 then lolineX else Double.NaN;
#//////////////////////////////
plot loLine1Ext = if (lolineX > 0 and extlinebarsl) then lolineX else Double.NaN;
#//////////////////////////////
loLine1Ext.SetDefaultColor(Color.magenta);
loLine1Ext.SetStyle(Curve.MEDIUM_DASH);


# ----------------------------------

input show_horz_lines = yes;

# horz resistance line
plot R1 = if !show_horz_lines then na 
  else if bar >= HighestAll(PHBarOrigin)
  then HighestAll(if IsNaN(c[-1])
    then PHValue
    else Double.NaN)
    else Double.NaN;
r1.SetDefaultColor(Color.orange);

# horz support line , from lower trendline end
plot S1 = if !show_horz_lines then na 
  else if bar >= HighestAll(PLBarOrigin)
  then HighestAll(if IsNaN(c[-1])
    then PLValue
    else Double.NaN)
    else Double.NaN;
s1.SetDefaultColor(Color.yellow);
#
 
Maybe Im doing something wrong how do I get this study to auto draw trend lines like this(blue line)??


i'm looking at this trying to remember how it works. i just modified what someone else made.

short answer, it probably can't.
it might get close once in awhile, but as new bars appear, the line will be drawn to a different end point.

lower trendline
the start point is the lowest point on the chart.
the end point is the last lowest point on the chart.
the last lowest point is determined by looking at the past 20 bars and the 20 future bars. (n = 20) and picking the lowest bar.

this is a simple study.
what you are asking for is very math intensive. it would need to run multiple loops, to compare many points, to find the best fit of a line, tangent to many points.
 
I'm trying to get the top trend line for the code below. I tried to but failed. Any assistance is greatly appreciated.

input n = 20;

def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def hh = HighestAll(h);
def lastValue = if IsNaN(c[-1]) then c else lastValue[1];
def hhBar = if h == hh
then bar
else Double.NaN;
def ll = LowestAll(l);
def llBar = if l == ll
then bar
else Double.NaN;
def minBar = Min(HighestAll(hhBar), HighestAll(llBar));
def firstPoint = if minBar == HighestAll(hhBar)
then hh
else ll;
def currentBar = if IsNaN(c[-1])
then bar
else currentBar[1];
def h_h = fold i = 1 to n + 1
with p = 1
while p
do h > GetValue(h, -i);
def PivotH = if (bar > n and
h == Highest(h, n) and
h_h)
then h
else Double.NaN;
def PHValue = if !IsNaN(PivotH)
then PivotH
else PHValue[1];
def PHBarOrigin = if !IsNaN(PivotH)
then bar
else Double.NaN;
def l_l = fold j = 1 to n + 1
with q = 1
while q
do l < GetValue(l, -j);
def PivotL = if (bar > n and
l == Lowest(l, n) and
l_l)
then l
else Double.NaN;
def PLValue = if !IsNaN(PivotL)
then PivotL
else PLValue[1];
def PLBarOrigin = if !IsNaN(PivotL)
then bar
else PLBarOrigin[1];
def secPoint = if firstPoint == hh
then PHValue
else if firstPoint == ll
then PLValue
else Double.NaN;
def secBar = if firstPoint == hh
then PHBarOrigin
else if firstPoint == ll
then PLBarOrigin
else Double.NaN;
def run = if bar == minBar then 1 else run[1] + 1;
def rise = if bar == HighestAll(secBar) then secPoint - firstPoint else Double.NaN;
def slope = rise / run[1];
def slopeX = if !IsNaN(slope) then slope else slopeX[1];
def lineX = if !IsNaN(slope) then secPoint else lineX[1] + slopeX;
plot R1 = if bar >= HighestAll(PHBarOrigin)
then HighestAll(if IsNaN(c[-1])
then PHValue
else Double.NaN)
else Double.NaN;
plot S1 = if bar >= HighestAll(PLBarOrigin)
then HighestAll(if IsNaN(c[-1])
then PLValue
else Double.NaN)
else Double.NaN;
plot trendLine = if bar == minBar
then firstPoint
else if bar == HighestAll(secBar)
then secPoint
else Double.NaN;
trendLine.EnableApproximation();
trendLine.SetStyle(Curve.FIRM);
trendLine.SetDefaultColor(Color.CYAN);
plot LineExtension = if lineX > 0 then lineX else Double.NaN;
LineExtension.SetDefaultColor(Color.CYAN);
LineExtension.SetStyle(Curve.FIRM);
Try this one and use the input setting for 9 or 26 for daily chart and 257 for intraday

## ProjectionPivots_v03_JQ
## 03.04.2019
## Original Code and Concept by Mobius:
# V01.08.2012 Projection Pivots
# mobius

# Notes:
# 03.04.2019 added linits on extensions
# 03.05.2019 adjusted limits on extensions by adding user input upper and lower extenion percent limits

#declare Once_Per_Bar;


## Inputs
input n = 21.00;
input showLines = yes;
input showValues = no;
input showBarNumbers = no;
input ExtensionLengthBars = 20; # added to control length of Entension
input UpperExtensionPercentLimit = 5;
input LowerExtensionPercentLimit = 5;
input DisplayLabel = yes; ## JQ 7.8.2018 added
addlabel (DisplayLabel, "Projection Pivots n:" + n + " " , color.WHITE); ## JQ 7.8.2018 added


# Universal Header _v030429019 _JQ
# code from various sources including Mobius, NoLongerNube and others
# Comment out unnecessary portions to preserve tos memory and enhance speed

# Universal Definitions using Padawan variable naming convention (JQ) v03.04.2019
# iData Definitions
def vHigh = high; # creates the variable vHigh. Use of the variable reduce data calls to tos iData server
# def initHigh = CompoundValue(1, high, high); # creates and initialized variable for High
def vLow = low;
# def initLow = CompoundValue(1, low, low);
def vOpen = open;
# def initOpen = CompoundValue(1, open, open);
def vClose = close;
# def initClose = CompoundValue(1, close, close);
def vVolume = volume;
# def initVolume = CompoundValue(1, volume, volume);
def nan = Double.NaN;
# Bar Time & Date
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(vHigh) then bn else nan);
# def Today = GetDay() ==GetLastDay();
# def time = GetTime();
# def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
# def globeX_v2 = if time crosses below RegularTradingEnd(GetYYYYMMDD()) then bn else GlobeX[1];
# def RTS = RegularTradingStart(GetYYYYMMDD());
# def RTE = RegularTradingEnd(GetYYYYMMDD());
# def RTH = GetTime() > RegularTradingStart(GetYYYYMMDD());
# def RTH_v2 = if time crosses above RegularTradingStart(GetYYYYMMDD()) then bn else RTH[1];

# bars that start and end the sessions #(borrowed from nube)
# def rthStartBar = CompoundValue(1,
# if !IsNaN(vClose)
# && time crosses above RegularTradingStart(GetYYYYMMDD())
# then bn
# else rthStartBar[1], 0);
# def rthEndBar = CompoundValue(1,
# if !IsNaN(vClose)
# && time crosses above RegularTradingEnd(GetYYYYMMDD())
# then bn
# else rthEndBar[1], 1);
# def globexStartBar = CompoundValue(1,
# if !IsNaN(vClose)
# && time crosses below RegularTradingEnd(GetYYYYMMDD())
# then bn
# else globexStartBar[1], 1);
# def rthSession = if bn crosses above rthStartBar #+ barsExtendedBeyondSession
# then 1
# else if bn crosses above rthEndBar #+ barsExtendedBeyondSession
# then 0
# else rthSession[1];


# Bubble Locations
def x_AxisLastExpansionBar = BarNumber() == HighestAll(BarNumber()); #corrected 11.12.2018 (JQ)
# syntax: addChartBubble(x_AxisLastExpansionBar, y-axis coordinate," text", Color.LIME); #verified 12.25.2018 (JQ)



def PH;
def PL;
def hh = fold i = 1 to n + 1
with p = 1
while p
do vHigh > getValue(vHigh, -i);
PH = if (bn > n and
vHigh == highest(vHigh, n) and
hh)
then vHigh
else double.NaN;
def ll = fold j = 1 to n + 1
with q = 1
while q
do vLow < getValue(low, -j);
PL = if (bn > n and
vLow == lowest(vLow, n) and
ll)
then vLow
else double.NaN;
def PHBar = if !isNaN(PH)
then bn
else PHBar[1];
def PLBar = if !isNaN(PL)
then bn
else PLBar[1];
def PHL = if !isNaN(PH)
then PH
else PHL[1];
def priorPHBar = if PHL != PHL[1]
then PHBar[1]
else priorPHBar[1];
def PLL = if !isNaN(PL)
then PL
else PLL[1];
def priorPLBar = if PLL != PLL[1]
then PLBar[1]
else priorPLBar[1];
def HighPivots = bn >= highestAll(priorPHBar);
def LowPivots = bn >= highestAll(priorPLBar);
def FirstRpoint = if HighPivots
then bn - PHBar
else 0;
def PriorRpoint = if HighPivots
then bn - PriorPHBar
else 0;
def RSlope = (getvalue(PH, FirstRpoint) - getvalue(PH, PriorRpoint))
/ (PHBar - PriorPHBar);
def FirstSpoint = if LowPivots
then bn - PLBar
else 0;
def PriorSpoint = if LowPivots
then bn - PriorPLBar
else 0;
def SSlope = (getvalue(PL, FirstSpoint) - getvalue(PL, PriorSpoint))
/ (PLBar - PriorPLBar);
def RExtend = if bn == highestall(PHBar)
then 1
else RExtend[1];
def SExtend = if bn == highestall(PLBar)
then 1
else SExtend[1];

plot pivotHigh = if HighPivots
then PH
else double.NaN;
pivotHigh.SetDefaultColor(GetColor(1));
pivotHigh.setPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.setHiding(!showValues);

plot pivotHighLine = if PHL > 0 and
HighPivots
then PHL
else double.NaN;
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES
pivotHighLine.setDefaultColor(color.yellow); ## JQ 7.8.2018 added
pivotHighLine.setHiding(!showLines);

plot RLine = pivotHigh;
RLine.enableApproximation();
RLine.SetDefaultColor(Color.LIGHT_GRAY);
RLine.SetStyle(Curve.Short_DASH);

# Added code to limit resistance estension line (JQ 03.04.2019)
def calc_ResistanceExtension = if RExtend
then (bn - PHBar) * RSlope + PHL
else double.NaN;
plot line_ResistanceExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_ResistanceExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_ResistanceExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_ResistanceExtension else double.nan;
line_ResistanceExtension.SetStyle(Curve.Short_DASH);
line_ResistanceExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_ResistanceExtension.setLineWeight(1);



# Low Plots
plot pivotLow = if LowPivots
then PL
else double.NaN;
pivotLow.setDefaultColor(GetColor(4));
pivotLow.setPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.setHiding(!showValues);

plot pivotLowLine = if PLL > 0 and
LowPivots
then PLL
else double.NaN;
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES); # Mobius original was DASHES
pivotLowLine.setDefaultColor(color.yellow);# # JQ 7.8.2018 added
pivotLowLine.setHiding(!showLines);

plot SupportLine = pivotLow;
SupportLine.enableApproximation();
SupportLine.SetDefaultColor(color.LIGHT_GRAY);
SUpportLine.SetStyle(Curve.Short_DASH);

# Added code to limit support estension line (JQ 03.04.2019)
def calc_SupportExtension = if SExtend
then (bn - PLBar) * SSlope + PLL
else double.NaN;
plot line_SupportExtension = if bn <= (Currentbar + ExtensionLengthBars)
and calc_SupportExtension[1] >= (lowestall(vLow) * (1-(lowerExtensionPercentLimit/100)))
and calc_SupportExtension[1] <= (Highestall(vHigh) * (1 + (upperExtensionPercentLimit/100)))
then calc_supportExtension else double.nan;
line_SupportExtension.SetDefaultColor(color.LIGHT_GRAY); #was 7
line_SupportExtension.SetStyle(Curve.Short_DASH);
line_SupportExtension.setLineWeight(1);

plot BarNumbersBelow = bn;
BarNumbersBelow.SetDefaultColor(GetColor(0));
BarNumbersBelow.setHiding(!showBarNumbers);
BarNumbersBelow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);

plot PivotDot = if !isNaN(pivotHigh)
then pivotHigh
else if !isNaN(pivotLow)
then pivotLow
else double.NaN;
pivotDot.SetDefaultColor(GetColor(7));
pivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
pivotDot.SetLineWeight(3);



# End Code
 
QxBKUJV.png
This one is I saw someone is using (a TradingView one)

and this one is the script I used from above code. See difference? there is one white line. Can it match the tradingview version?
jueUTGX.png
 
@halcyonguy I'm trying to shorten the trendlines to +/-3 bars from the beginning and at the end instead of an extension. Is there a easy way to do this?

Ruby:
#Auto Trendline

declare upper;

#################################################
# Defining High, Low and Pivot Points
#################################################
def Hi = high;
def Lo = low;
def P1H = (Hi > Hi[1] or (Hi > Hi[2] and Hi == Hi[1])) and Hi > Hi[-1];
def P1L = (Lo < Lo[1] or (Lo < Lo[2] and Lo == Lo[1])) and Lo < Lo[-1];
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def Piv1h = if P1H[1] then 1 else Piv1h[1] + 1;
def Piv1l = if P1L[1] then 1 else Piv1l[1] + 1;
def PivN1h = fold m = 1 to lastBar with a = Double.NaN while IsNaN(a) do if GetValue(P1H, -m) then -m else Double.NaN;
def PivN1l = fold n = 1 to lastBar with b = Double.NaN while IsNaN(b) do if GetValue(P1L, -n) then -n else Double.NaN;

def prevPivhigh = GetValue(Hi, Piv1h);
def prevPivlow = GetValue(Lo, Piv1l);
def nextPivhigh = GetValue(Hi, PivN1h);
def nextPivlow = GetValue(Lo, PivN1l);
def P2H = Hi > prevPivhigh and Hi > nextPivhigh;
def P2L = Lo < prevPivlow and Lo < nextPivlow;
def p2x2 = P2H and P2L;
def Piv2h = if P2H[1] then 1 else Piv2h[1] + 1;
def Piv2l = if P2L[1] then 1 else Piv2l[1] + 1;
def PivN2h = fold o = 1 to lastBar with c = Double.NaN while IsNaN(c) do if GetValue(P2H, -o) then -o else Double.NaN;
def PivN2l = fold p = 1 to lastBar with d = Double.NaN while IsNaN(d) do if GetValue(P2L, -p) then -p else Double.NaN;
def NextP2H = if PivN2h > PivN2l then 1 else Double.NaN;
def NextP2L = if PivN2l > PivN2h then 1 else Double.NaN;
def NextP2Both = if PivN2h == PivN2l then 1 else Double.NaN;
def PrevP2H = Piv2h < Piv2l;
def PrevP2L = Piv2l < Piv2h;
def PrevP2Both = Piv2h == Piv2l;
def nearLow = if P2L and PrevP2H then Lo else nearLow[1];
def AL0 = if P2L and PrevP2L and Lo > nearLow then 1 else Double.NaN;
def AL1 = if P2L and NextP2L and GetValue(Lo, PivN2l) <= Lo then 1 else Double.NaN;
def AL2 = if p2x2 and PrevP2L and Lo > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def AL3 = if P2L and PrevP2Both and !NextP2H and Lo > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def AL4 = if P2L and PrevP2L and Lo > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def AL5 = if P2L and PrevP2H and NextP2Both and Lo > GetValue(Lo, PivN2l) and GetValue(Hi, PivN2h) < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def A2L = P2L and IsNaN(AL0) and IsNaN(AL1) and IsNaN(AL2) and IsNaN(AL3) and IsNaN(AL4) and IsNaN(AL5);
def recentHigh = if P2H and PrevP2L then Hi else recentHigh[1];
def AH0 = if P2H and PrevP2H and Hi < recentHigh then 1 else Double.NaN;
def AH1 = if P2H and NextP2H and GetValue(Hi, PivN2h) >= Hi then 1 else Double.NaN;
def AH2 = if p2x2 and PrevP2H and Hi < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def AH3 = if P2H and PrevP2Both and !NextP2L and Hi < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def AH4 = if P2H and PrevP2H and Hi < GetValue(Hi, Piv2h) then 1 else Double.NaN;
def AH5 = if P2H and PrevP2L and NextP2Both and Hi < GetValue(Hi, PivN2h) and GetValue(Lo, PivN2l) > GetValue(Lo, Piv2l) then 1 else Double.NaN;
def A2H = P2H and IsNaN(AH0) and IsNaN(AH1) and IsNaN(AH2) and IsNaN(AH3) and IsNaN(AH4) and IsNaN(AH5);

def PPH = if A2H[1] then 1 else PPH[1] + 1;
def PPL = if A2L[1] then 1 else PPL[1] + 1;
def HighLow = if A2L and Lo > GetValue(Lo, PPL) then 1 else Double.NaN;
def LowHigh = if A2H and Hi < GetValue(Hi, PPH) then 1 else Double.NaN;
def PivTime = if !IsNaN(HighLow) or !IsNaN(LowHigh) then PivTime[1] + 1 else PivTime[1];

#################################################
# Trend Line End Numbers
#################################################
def end1 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) then BarNumber() else 0;
def end2 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 1 then BarNumber() else 0;
def end3 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 2 then BarNumber() else 0;
def end4 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 3 then BarNumber() else 0;
def end5 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 4 then BarNumber() else 0;
def end6 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 5 then BarNumber() else 0;
def end7 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 6 then BarNumber() else 0;
def end8 = if (!IsNaN(HighLow) or !IsNaN(LowHigh)) and PivTime == HighestAll(PivTime) - 7 then BarNumber() else 0;

#################################################
# Trend Line Start Numbers
#################################################
def start1 = if end1 and !IsNaN(HighLow) then end1 - PPL else if end1 and !IsNaN(LowHigh) then end1 - PPH else 0;
def start2 = if end2 and !IsNaN(HighLow) then end2 - PPL else if end2 and !IsNaN(LowHigh) then end2 - PPH else 0;
def start3 = if end3 and !IsNaN(HighLow) then end3 - PPL else if end3 and !IsNaN(LowHigh) then end3 - PPH else 0;
def start4 = if end4 and !IsNaN(HighLow) then end4 - PPL else if end4 and !IsNaN(LowHigh) then end4 - PPH else 0;
def start5 = if end5 and !IsNaN(HighLow) then end5 - PPL else if end5 and !IsNaN(LowHigh) then end5 - PPH else 0;
def start6 = if end6 and !IsNaN(HighLow) then end6 - PPL else if end6 and !IsNaN(LowHigh) then end6 - PPH else 0;
def start7 = if end7 and !IsNaN(HighLow) then end7 - PPL else if end7 and !IsNaN(LowHigh) then end7 - PPH else 0;
def start8 = if end8 and !IsNaN(HighLow) then end8 - PPL else if end8 and !IsNaN(LowHigh) then end8 - PPH else 0;

#################################################
# Price Data
#################################################
def price1 = HighestAll(if end1 and A2L then GetValue(Lo, end1 - start1) else if end1 and A2H then GetValue(Hi, end1 - start1) else 0);
def price2 = HighestAll(if end2 and A2L then GetValue(Lo, end2 - start2) else if end2 and A2H then GetValue(Hi, end2 - start2) else 0);
def price3 = HighestAll(if end3 and A2L then GetValue(Lo, end3 - start3) else if end3 and A2H then GetValue(Hi, end3 - start3) else 0);
def price4 = HighestAll(if end4 and A2L then GetValue(Lo, end4 - start4) else if end4 and A2H then GetValue(Hi, end4 - start4) else 0);
def price5 = HighestAll(if end5 and A2L then GetValue(Lo, end5 - start5) else if end5 and A2H then GetValue(Hi, end5 - start5) else 0);
def price6 = HighestAll(if end6 and A2L then GetValue(Lo, end6 - start6) else if end6 and A2H then GetValue(Hi, end6 - start6) else 0);
def price7 = HighestAll(if end7 and A2L then GetValue(Lo, end7 - start7) else if end7 and A2H then GetValue(Hi, end7 - start7) else 0);
def price8 = HighestAll(if end8 and A2L then GetValue(Lo, end8 - start8) else if end8 and A2H then GetValue(Hi, end8 - start8) else 0);

def price01 = HighestAll(if end1 and A2L then Lo else if end1 and A2H then Hi else 0);
def price02 = HighestAll(if end2 and A2L then Lo else if end2 and A2H then Hi else 0);
def price03 = HighestAll(if end3 and A2L then Lo else if end3 and A2H then Hi else 0);
def price04 = HighestAll(if end4 and A2L then Lo else if end4 and A2H then Hi else 0);
def price05 = HighestAll(if end5 and A2L then Lo else if end5 and A2H then Hi else 0);
def price06 = HighestAll(if end6 and A2L then Lo else if end6 and A2H then Hi else 0);
def price07 = HighestAll(if end7 and A2L then Lo else if end7 and A2H then Hi else 0);
def price08 = HighestAll(if end8 and A2L then Lo else if end8 and A2H then Hi else 0);

#################################################
# Defining Trend Line
#################################################
script trendLine
{
input startBar = 0;
input startPrice = 0;
input endBar = 0;
input endPrice = 0;
input leftExt = 0;
input rightExt = 0;
input limitRight = no;

def TrendV = (endPrice - startPrice) / (endBar - startBar);
def NewBar = BarNumber() - endBar;
plot TheTrend; if !limitRight then {TheTrend = if BarNumber() >= startBar - leftExt then endPrice + NewBar * TrendV else Double.NaN;} else {TheTrend = if BarNumber() > endBar + rightExt or BarNumber() < startBar - leftExt then Double.NaN else endPrice + NewBar * TrendV;}
}

#################################################
# Trend Lines
#################################################
input howManyTrendLinesMax_8 = 4;

plot Trend1 = trendline(HighestAll(start1), price1, HighestAll(end1), price01, 5, no);
Trend1.SetDefaultColor(Color.WHITE);
Trend1.SetStyle(Curve.FIRM);
plot Trend2 = trendline(HighestAll(start2), price2, HighestAll(end2), price02, 5, no);
Trend2.setDefaultColor(color.WHITE);
Trend2.SetStyle(Curve.SHORT_DASH);
Trend2.SetHiding(howManyTrendLinesMax_8 < 2);
plot Trend3 = trendline(HighestAll(start3), price3, HighestAll(end3), price03, 5, no);
Trend3.SetDefaultColor(color.WHITE);
Trend3.SetStyle(Curve.SHORT_DASH);
Trend3.SetHiding(howManyTrendLinesMax_8 < 3);
plot Trend4 = trendline(HighestAll(start4), price4, HighestAll(end4), price04, 5, no);
Trend4.SetDefaultColor(color.WHITE);
Trend4.SetStyle(Curve.SHORT_DASH);
Trend4.SetHiding(howManyTrendLinesMax_8 < 4);
plot Trend5 = trendline(HighestAll(start5), price5, HighestAll(end5), price05, 5, no);
Trend5.SetDefaultColor(color.WHITE);
Trend5.SetStyle(Curve.SHORT_DASH);
Trend5.SetHiding(howManyTrendLinesMax_8 < 5);
plot Trend6 = trendline(HighestAll(start6), price6, HighestAll(end6), price06, 5, no);
Trend6.SetDefaultColor(color.WHITE);
Trend6.SetStyle(Curve.SHORT_DASH);
Trend6.SetHiding(howManyTrendLinesMax_8 < 6);
plot Trend7 = trendline(HighestAll(start7), price7, HighestAll(end7), price07, 5, no);
Trend7.SetDefaultColor(color.WHITE);
Trend7.SetStyle(Curve.SHORT_DASH);
Trend7.SetHiding(howManyTrendLinesMax_8 < 7);
plot Trend8 = trendline(HighestAll(start8), price8, HighestAll(end8), price08, 5, no);
Trend8.SetDefaultColor(color.WHITE);
Trend8.SetStyle(Curve.SHORT_DASH);
Trend8.SetHiding(howManyTrendLinesMax_8 < 8);

#################################################
# Arrows
#################################################
input showArrows = yes;

#Up Arrows
plot DownArrow1 = Trend1 > Trend1[1] and close crosses below Trend1;
DownArrow1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownArrow1.SetDefaultColor(color.CYAN);
DownArrow1.SetLineWeight(5);
DownArrow1.SetHiding(!showArrows);

#Down Arrows
plot UpArrow1 = Trend1 < Trend1[1] and close crosses above Trend1;
UpArrow1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpArrow1.SetDefaultColor(color.CYAN);
UpArrow1.SetLineWeight(5);
UpArrow1.SetHiding(!showArrows);

#################################################
# Display Box
#################################################
input ShowDisplayBox = yes;
AddLabel(ShowDisplayBox, " ->", color.DARK_GRAY);
AddLabel(ShowDisplayBox, if close > Trend1 then "Above Current Trend" else "Below Current Trend", if close > Trend1 then color.GREEN else color.RED);
AddLabel(ShowDisplayBox, "<- ", color.DARK_GRAY);

#################################################
# Alerts
#################################################
input AudibleAlerts = yes;
Alert(AudibleAlerts and UpArrow1, GetSymbol() + " Possible Breakout.", Alert.BAR, Sound.Bell);
Alert(AudibleAlerts and DownArrow1, GetSymbol() + " Possible Breakdown.", Alert.BAR, Sound.Bell);
#################################################
def data = close;
def mathh = close-trend4;
plot disth = round(mathh/close*100,2);
disth.hide();
plot near=disth <1 within 1 bar;
near.hide();
addlabel(1,if disth < 1 and disth >-1 then "TRENDLINE" else " ");

Thanks for codes, are you able script Scan for #Up Arrows
 

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
436 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