Ascend Trading Auto-Trendline Indicator for ThinkorSwim

CHOZEN

CHOZEN

New member
VIP
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

 
Last edited by a moderator:
PumpJack

PumpJack

New member
VIP
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
 
A

AcquireCurrency

New member
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.
 
netarchitech

netarchitech

Well-known member
VIP
Warehouse
@CHOZEN I think you might have unknowingly posted a pirated script here. While Ascend Trading does, in fact, have an Auto-Trendline script available for purchase on their website, I believe the genuine original script was done by the owner/scripter over at FunWithThinkscript.com.

Being a long-standing customer of FunWithThinkscript.com, I have come to see him come up with amazing scripts that extend far beyond what I thought were the boundaries of Thinkscript. If you have a moment, take a look and I think you might agree.

To satisfy my curiosity, I took the liberty of going ahead and comparing the the two source files to see if there was any significant difference between them. From what I can see, variable names have been changed and certain functional aspects have been altered, but the underlying logic appears to be equivalent.

In my travels, I came across another pirate website (thinkorswim.pro) that was blatantly selling FunWithThinkscript.com scripts. I contacted FunWithThinkscript.com and he informed me that he was already aware of the theft, but there was nothing he could do about it.

The sad part of this situation is that apparently there is no legal recourse for the original author. Thinkscript offers no protection of the source code. Other platforms do far more, for example, compiling the source code to a DLL.

I realize you've stated you found this undocumented script on your Google Drive. Since you are a new member, you may not be aware that @BenTen always makes sure to credit the original authors of the scripts posted here. I would encourage you to do the same going forward...
 
Last edited by a moderator:
markos

markos

Well-known member
VIP
Warehouse
@CHOZEN I think you might have unknowingly posted a pirated script here. While Ascend Trading does, in fact, have an Auto-Trendline script available for purchase on their website, I believe the genuine original script was done by the owner/scripter over at FunWithThinkscript.com.

The sad part of this situation is that apparently there is no legal recourse for the original author. Thinkscript offers no protection of the source code. Other platforms do far more, for example, compiling the source code to a DLL.

I realize you've stated you found this undocumented script on your Google Drive. Since you are a new member, you may not be aware that @BenTen always makes sure to credit the original authors of the scripts posted here. I would encourage you to do the same going forward...
@netarchitech thank you for bringing this up. Robert Payne from FunwithThinkscript is a member here. He made note of the fact that @BenTen had posted some of his scripts, which was okay, because they were not purchased scripts but freely given by Robert Payne.
The biggest point that he made was the fact that he appreciated that his name was left in the header has it should be for all scripts posted here.
This should be standard protocol for any of us that post a script on this site. It is the right thing to do.
 
netarchitech

netarchitech

Well-known member
VIP
Warehouse
@netarchitech thank you for bringing this up.
@markos You are certainly welcome.

This should be standard protocol for any of us that post a script on this site. It is the right thing to do.
I wholeheartedly agree with you! Without attribution and the potential for financial remuneration, entrepreneurial programmers/scripters would lose the incentive to spend considerable time and effort to develop the solutions the rest of us put to work.

Robert Payne from FunwithThinkscript is a member here.
Thanks for sharing that. That's great news! I don't know if you are aware that Robert spent a long time on another forum helping a lot of Thinkorswimmers (me included) with a variety of thinkscript issues, as well as offering numerous indicators for free (link). The thread he established became a goto resource for me. I learned a lot about thinkscript on that thread. It is still going, some might say on life support, but it has over 500,000 views and 2,400 posts!

I don't want get too far ahead of myself, but maybe we could get @BenTen's and Robert's permission to resurrect the Fun with Thinkscript thread here at uTS. The Xenforo software is so much more robust than the software on the other forum. I'm sure the additional traffic would be welcome as well...

Please don't hestiate to let me know if there is anything I can do if @BenTen and Robert are amenable to this...
 
Last edited by a moderator:
markos

markos

Well-known member
VIP
Warehouse
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.
@AcquireCurrency please post this in the Questions Forum. Please give detail of what you are looking for. Thanks
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@netarchitech Thank you for bringing this to our attention. I always give credit where credit is due. I hope everyone in our community can do the same. We must embrace those who share their knowledge (big or small).

I learned a lot from Robert and his Fun with ThinkScript thread. I also re-shared some of his public work on here. That being said, I'm always open to collaborating with @RobertPayne. :)
 
RobertPayne

RobertPayne

Member
I appreciate you trying to look after my interest, but that isn't my work. So, credit to whomever the original author is.
 
netarchitech

netarchitech

Well-known member
VIP
Warehouse
@RobertPayne Thank you for the clarification. Since I sounded the alarm, please accept my apologies for bringing you into the controversy. The script is undocumented, but @CHOZEN attributed it to Ascend Trading in the title of the this thread...

@CHOZEN I want to apologize to you as well for assuming that because your contribution was undocumented, it was potentially yet another pirated script. I guess I also assumed that, given the quality of the code, you or someone somewhere along the way did pay for it and then decided to "share" it...

In the end, the spirit of sharing here at uTS is most important, as long as the original creator has his/her work fully attributed and they give their permission as well...
 
CHOZEN

CHOZEN

New member
VIP
@RobertPayne Thank you for the clarification. Since I sounded the alarm, please accept my apologies for bringing you into the controversy. The script is undocumented, but @CHOZEN attributed it to Ascend Trading in the title of the this thread...

@CHOZEN I want to apologize to you as well for assuming that because your contribution was undocumented, it was potentially yet another pirated script. I guess I also assumed that, given the quality of the code, you or someone somewhere along the way did pay for it and then decided to "share" it...

In the end, the spirit of sharing here at uTS is most important, as long as the original creator has his/her work fully attributed and they give their permission as well...
No worries. I had paid for the script awhile back and completely forgot about until i stumbled upon it in a folder on my laptop
 
H

haha54

New member
Can anyone come up with a nightly scan for finding stocks that cross above a particular trend-line using the Ascending Auto-Trend line program that was posted here in June. I have tried running a scan where the close crosses above Trend-line #1 but it won't allow me to run a scan. Any help or ideas would be appreciated. Thanks
 
H

HighBredCloud

Active member
A good auto trend dont think Ive ever seen this one before
@J007RMC This is the same trend line as the one in this post...I should mention that I had an incident while running this trend line in a live market where it delayed the candle sticks way too much when a new one was suppose to form...Just a heads up for others here...Perhaps maybe it was a fluke on my end...
 

Top