Ascend Trading Auto-Trendline Indicator for ThinkorSwim

CHOZEN

New member
2019 Donor
EBPwFql.png

This is a auto trend I found in a file I had on my google drive. I don't know who made it.

The Ascend Trading Auto-Trendline Script does the following:
  • Plots anywhere from 1-8 Trendlines depending on user’s preference selected in the scripts settings.
  • The most current Trendline will be solid WHITE. Historical Trendlines will be on the chart as dashed lines.
  • When a new Trendline is detected, the old one becomes dashed and remain for reference. The color of each line can be changed in the scripts settings.
  • Plots a CYAN colored arrow either pointing Up or Down depending on if the candle closed above or below the current solid trendline. This annotates the point at which the trend was broken. When this happens you will also get an Audible
  • Alert message with a sound saying “Possible Breakout” or “Possible Breakdown”. The arrow color can be changed and the audible alert can be disabled and messaged change if you wish.
  • Displays a box at top left of chart giving you either a RED box saying “Below Current Trend” or a GREEN box saying
  • “Above Current Trend”. This allows for fast reference to see where the ticker is at in its trend continuation.

thinkScript Code

Code:
#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);

Shareable Link

https://tos.mx/XXmcKk
 
Last edited by a moderator:

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

Nice! Good work. Best one I've used so far. Been looking for a good one. This one by far the best I've used. TY
 
Hey guys! Is there a scanner for this? To alert when the close is below the current / most recent trendline or when it has just crossed.
 
Thanks for the code but when i try to add to TOS the study don't fit and the screen squeeze is there a way to fix it.
 
TOS removed the alert based on manually-drawn trendlines. Does anyone know of an indicator that can do this? Greatly appreciated!
 
TOS removed the alert based on manually-drawn trendlines. Does anyone know of an indicator that can do this? Greatly appreciated!

You might just have your chart in Log Scale. TOS only allows trendline alerts when in Arithmetic Scale, so you may have to switch off the Log Scale in the price axis chart settings.
 
@sparhawk Not sure why I am noticing this thread now, I have searched Auto Trend on this forum earlier. Somebody shared the exact same script with me asking for a scanner and here is my attempt.

Save the below script as if this is a chart Study, but don't add it to chart.

Ruby:
#AutoTrendLine_Scan
#Modified By @SuryaKiranC Author Unknown, Will be happy to add if somebody is aware of the Original Author.

#################################################
# 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;

#################################################
# 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;

#################################################
# 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 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);

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

def  Trend = trendline(HighestAll(start1), price1, HighestAll(end1), price01, 5, no);

#################################################
# scan variables
#################################################
def Bull = (if Trend < Trend[1] and close crosses above Trend then 1 else 0);
def Bear = (if Trend > Trend[1] and close crosses below Trend then 1 else 0);

plot Scan = (Bull or Bear);


Following are the examples for Scan queries.

Ruby:
AutoTrendLine_Scan()."Bull" within 5 bars

or

Ruby:
AutoTrendLine_Scan()."Bear" within 5 bars

P.S: Lately HighestALL Function in TOS behaving odd, please run your queries during the hours 4:00 AM EST to 8:00 PM EST only, Also try and restrict this to smaller watch list, bigger watch list such as S&P500 or larger would cause timeout error. I run this on a Watch list of 200 Ticker or so. If you still receiving timeout error, close the scanner window and re-launch it try again.
 
Also for those of you having performance issue with the original AutoTrendLine Script, here is the light version, Possibly works on Mobile too, Only plots current Trend and Previous Trend.


Ruby:
#AutoTrend_Lite
#Modified By @SuryaKiranC Author Unknown, Will be happy to add if somebody is aware of the Original Author.

declare upper;
declare once_per_bar;
#################################################
# 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;

#################################################
# 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;

#################################################
# 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 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);

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

plot Trend1 = trendline(HighestAll(start1), price1, HighestAll(end1), price01, 5, no);
Trend1.SetStyle(Curve.FIRM);
Trend1.SetDefaultColor(Color.WHITE);
plot Trend2 = trendline(HighestAll(start2), price2, HighestAll(end2), price02, 5, no);
Trend2.SetDefaultColor(Color.WHITE);
Trend2.SetStyle(Curve.SHORT_DASH);
#################################################
# Arrows
#################################################
input showArrows = yes;

#Up Arrows
plot DownArrow = Trend1 > Trend1[1] and close crosses below Trend1;
DownArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownArrow.SetDefaultColor(Color.WHITE);
DownArrow.SetLineWeight(3);
DownArrow.SetHiding(!showArrows);

#Down Arrows
plot UpArrow = Trend1 < Trend1[1] and close crosses above Trend1;
UpArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpArrow.SetDefaultColor(Color.WHITE);
UpArrow.SetLineWeight(3);
UpArrow.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 UpArrow, GetSymbol() + " Possible Breakout.", Alert.BAR, Sound.Bell);
Alert(AudibleAlerts and DownArrow, GetSymbol() + " Possible Breakdown.", Alert.BAR, Sound.Ding);

P.S: I realize this is a old thread, yet think my post should help others here.
 
Further Stripped down of the labels and unnecessary stuff, that is not supported on Mobile.


Ruby:
#AutoTrend_Mobile
#Modified By @SuryaKiranC, Author Unknown, Will be happy to add if somebody is aware of the Original Author.

declare upper;
declare once_per_bar;
#################################################
# 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;

#################################################
# 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;

#################################################
# 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 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);

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

plot Trend1 = trendline(HighestAll(start1), price1, HighestAll(end1), price01, 5, no);
Trend1.SetStyle(Curve.FIRM);
Trend1.SetDefaultColor(Color.WHITE);
plot Trend2 = trendline(HighestAll(start2), price2, HighestAll(end2), price02, 5, no);
Trend2.SetDefaultColor(Color.WHITE);
Trend2.SetStyle(Curve.SHORT_DASH);
#################################################
# Arrows
#################################################

#Up Arrows
plot DownArrow = Trend1 > Trend1[1] and close crosses below Trend1;
DownArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownArrow.SetDefaultColor(Color.WHITE);
DownArrow.SetLineWeight(5);

#Down Arrows
plot UpArrow = Trend1 < Trend1[1] and close crosses above Trend1;
UpArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpArrow.SetDefaultColor(Color.WHITE);
UpArrow.SetLineWeight(5);

P.S: You would still have go to setting and adjust the last trend to dotted lines or dashed lines.
 
Hi @SuryaKiranC, I copied your script from post #30, but I notice a bit of lag when using it. Im on a laptop and it can take a few seconds for the trend lines to get drawn. If i have a multiple charts and all of them are drawing trend lines with your script, then there is noticeable lag/slowness. It would take even longer to draw. Any tips how to speed up my chart drawing trend line refresh rate?
 
Hi @SuryaKiranC, I copied your script from post #30, but I notice a bit of lag when using it. Im on a laptop and it can take a few seconds for the trend lines to get drawn. If i have a multiple charts and all of them are drawing trend lines with your script, then there is noticeable lag/slowness. It would take even longer to draw. Any tips how to speed up my chart drawing trend line refresh rate?

Sorry missed your message. TOS is a Java swing UI and uses Single Core on a system, Basically the better the single core in your System perform the better TOS behaves. one way to test this out by creating a separate Grid cell/ Workspace with just this one study and only Chart and see if you observe the same results. If all is good, keep adding one study at a time, the usual one you would use and see where it breaks.
Obviously that means you may have to sacrifice one trading day to fine where the system breaks.

Bottom line try one of the relatively new CPU, like no more than 2 - 3 yrs old CPU and better Mhz/Ghz per core than going with more cores.

-S
 
EBPwFql.png

This is a auto trend I found in a file I had on my google drive. I don't know who made it.



thinkScript Code

Code:
#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);

Shareable Link

https://tos.mx/XXmcKk
Thank you for this AMAZING CODE. Is there a way to have the trendlines be plotted based off of time frame? I would like to see the 5min trend line on the one minute chart. I hope someone can help. Thanks in advance!
 
  • Like
Reactions: ASC

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
407 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