Auto Trend Lines Indicator for ThinkorSwim (Free Download)

BenTen

Administrative
Staff
VIP
This is a free indicator for ThinkorSwim that will automatically draw trend lines on your trading charts. It works with any timeframes. The indicator includes short, medium, and long length of trend lines.

I found this code while exploring this topic on Research Trade.

MgrwEKh.png


thinkScript Code

Rich (BB code):
#
#Trend Line Plot    
#
input TrendLineLength1 = 50;
input TrendLineLength2 = 30;
input TrendLineLength3 = 10;

def Inertia1 = InertiaAll(close, TrendLineLength1);
def Inertia2 = InertiaAll(close, TrendLineLength2);
def Inertia3 = InertiaAll(close, TrendLineLength3);

def TL_Bull1 = Inertia1 - (HighestAll(AbsValue(Inertia1 - close)) * 0.8);
def TL_Bear1 = Inertia1 + (HighestAll(AbsValue(Inertia1 - close)) * 0.8);
def slope1a = TL_Bull1 > TL_Bull1[1];
def slope1b = TL_Bear1 > TL_Bear1[1];

def TL_Bull2 = Inertia2 - (HighestAll(AbsValue(Inertia2 - close)) * 0.8);
def TL_Bear2 = Inertia2 + (HighestAll(AbsValue(Inertia2 - close)) * 0.8);
def slope2a = TL_Bull2 > TL_Bull2[1];
def slope2b = TL_Bear2 > TL_Bear2[1];

def TL_Bull3 = Inertia3 - (HighestAll(AbsValue(Inertia3 - close)) * 0.8);
def TL_Bear3 = Inertia3 + (HighestAll(AbsValue(Inertia3 - close)) * 0.8);
def slope3a = TL_Bull3 > TL_Bull3[1];
def slope3b = TL_Bear3 > TL_Bear3[1];
#Long length
plot TrendLine1a = if slope1a > 0 then TL_Bull1 else TL_Bear1;
TrendLine1a.SetStyle(curve.long_dash);
TrendLine1a.SetLineWeight(1);
TrendLine1a.assignvaluecolor(if slope1a and IsAscending(close, 10) then color.WHITE else if slope1a then color.white else if !IsAscending(close, 10)then color.white else color.WHITE);

plot TrendLine1b = if slope1b > 0 then TL_Bear1 else TL_Bull1;
TrendLine1b.SetStyle(curve.long_dash);
TrendLine1b.SetLineWeight(1);
TrendLine1b.assignvaluecolor(if slope1b and IsAscending(close, 10) then color.white else if slope1b then color.white else if !IsAscending(close, 10)then color.white else color.white);
#Medium length
plot TrendLine2a = if slope2a > 0 then TL_Bull2 else TL_Bear2;
TrendLine2a.SetStyle(curve.medium_dash);
TrendLine2a.SetLineWeight(2);
TrendLine2a.assignvaluecolor(if slope2a and IsAscending(close, 10) then color.yellow else if slope2a then color.yellow else if !IsAscending(close, 10)then color.light_RED else color.light_RED);

plot TrendLine2b = if slope2b > 0 then TL_Bear2 else TL_Bull2;
TrendLine2b.SetStyle(curve.medium_dash);
TrendLine2b.SetLineWeight(2);
TrendLine2b.assignvaluecolor(if slope2b and IsAscending(close, 10) then color.yellow else if slope2b then color.yellow else if !IsAscending(close, 10)then color.light_RED else color.light_RED);
#Short length
plot TrendLine3a = if slope3a > 0 then TL_Bull3 else TL_Bear3;
TrendLine3a.SetStyle(curve.short_dash);
TrendLine3a.SetLineWeight(3);
TrendLine3a.assignvaluecolor(if slope3a and IsAscending(close, 10) then color.yellow else if slope3a then color.yellow else if !IsAscending(close, 10)then color.light_RED else color.light_RED);

plot TrendLine3b = if slope3b > 0 then TL_Bear3 else TL_Bull3;
TrendLine3b.SetStyle(curve.short_dash);
TrendLine3b.SetLineWeight(3);
TrendLine3b.assignvaluecolor(if slope3b and IsAscending(close, 10) then color.yellow else if slope3b then color.yellow else if !IsAscending(close, 10)then color.light_RED else color.light_RED);

Shareable Link

https://tos.mx/2wsFva

Video Tutorial

 
Last edited:

sir

New member
This script is awesome. But it seems to draw lines at the close price. Can you tell me how to draw it at the peaks of the wicks? highs and lows?
thanks

Code:
# Auto-Trend Line
# Mobius
# V01.11.2017

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);
 
Is there a way to create a scan for this that populates stocks that are in all trend stages (short, medium, long) at the same time? I would think that this would be a pretty good indication of a bullish stock, especially for day traders and scalpers.

I should also add, that if you can't configure a scan to find stocks that are in an uptrend for all three stages, two out of the three at the same time would also suffice - since finding stocks that are in an uptrend in all three stages won't happen often.

Is there a way to adjust the long term trendline to look for the swing lows with the most amount of touches going back X periods. As an example, I'd like it to trail the last 50, 100, or 300 1M Candles.
 
@BenTen Is there a way to create a scan to populate results like the image below? If this is possible, this would be a great scan for spotting high probability pullback entries.

Criteria:
  1. The 50 and 30 lines are in an uptrend
  2. The 50 and 30 lines are parallel to each other
  3. The lower 10 lines are in a downtrend

Auto-Trend-Plot-Long-and-Mid-Trend-Intersecting-with-Short-Trend-at-Support.jpg
 

horserider

Well-known member
VIP
This may be the origin of this code.

# Variation of the Trend Line code.
# TL's above and below price channels over 3 periods
# Mar/13/2014
# from Jaime Pinto

I think this is a deviation channel. It is finding the mean and multiplying by .8 to get each channel line. This should be the same as a standard deviation channel with a deviation of 1.6. Just so ya'll are aware it is not drawing trend lines between highs or between lows as might be done manually. So you could use the ToS SDC built in scanning code to construct your scan.
 

StevenCraig

New member
Hello all, new to usethinkscripts and loving it. I am just starting to learn about scripting so I was wondering if anyone here can help out with a snippet of code that could add a center line to the channels of this Auto Trend Lines Indicator? As depicted in the image. Thanks!!

 
Last edited:

horserider

Well-known member
VIP
@StevenCraig As noted in post #20 these are not trend lines but deviation channels. Which can be used to show trends. Since that is the case your solution is easy. Just have the center channel plot for any channel you wish. Change def to plot as shown below.

plot Inertia1 = InertiaAll(close, TrendLineLength1);
def Inertia2 = InertiaAll(close, TrendLineLength2);
def Inertia3 = InertiaAll(close, TrendLineLength3);
 

StevenCraig

New member
Hi Horserider, Thank you so much for that! It's perfect. As you say, they are deviation channels however, I sometimes use "studies" that are meant for one thing, tweak it, and use it for another. This is a great study, and thank you again for that fix!
 

Ahmar824

Member
Hi Horserider, Thank you so much for that! It's perfect. As you say, they are deviation channels however, I sometimes use "studies" that are meant for one thing, tweak it, and use it for another. This is a great study, and thank you again for that fix!
Hello Steve, can share the script please because I don't know how to change it
 

StevenCraig

New member
Hello Steve, can share the script please because I don't know how to change it

Hi Ahmar, sure. There is no need to add anything. All you need to do is change the def to plot.

Below is the the frist few lines of the original script. Just change the def to the word plot. I changed the first one for you as an example.

#
#Trend Line Plot
#
input TrendLineLength1 = 50;
input TrendLineLength2 = 30;
input TrendLineLength3 = 10;

plot Inertia1 = InertiaAll(close, TrendLineLength1);
def Inertia2 = InertiaAll(close, TrendLineLength2);
def Inertia3 = InertiaAll(close, TrendLineLength3);
 

Similar threads

Top