Auto Trend Lines Indicator for ThinkorSwim

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

Horserider since these are deviation channels it reminds me of linear regression channels that can be drawn in TOS. Is there a script to place linear regression on charts automatically in various time frames from intraday until month. Also have it optional so can show or not show. Thanks
 
Last edited:
@soary ToS has standarddevchannel , search it.

yes they do and I use them but I was wondering if 1) there is an auto linear regression and 2) an outo chanel that draws premarket high low and the 50% point of that range that so that I don't have to add them to each ticker I look at. Thanks
 
Last edited:
@soary
Code:
declare once_per_bar;

input PlotPreMktLinesHrsPastOpen = yes;
def bar = BarNumber();

def nan = Double.NaN;
def vHigh = high;
def vLow = low;

def PMhrs = RegularTradingStart (GetYYYYMMDD()) > GetTime();
def RMhrs = RegularTradingStart (GetYYYYMMDD()) < GetTime();
def PMStart = RMhrs[1] and PMhrs;
def PMHigh = CompoundValue(1, if PMStart then vHigh else if PMhrs then Max(vHigh, PMHigh[1]) else PMHigh[1], 0);
def PMLow = CompoundValue(1, if PMStart then vLow else if PMhrs then Min(vLow, PMLow[1]) else PMLow[1], 0);
def highBar = if PMhrs and vHigh == PMHigh then bar else nan;
def lowBar = if PMhrs and vLow == PMLow then bar else nan;
def PMHighBar = if bar == HighestAll(highBar) then PMHigh else PMHighBar[1];
def PMLowBar = if bar == HighestAll(lowBar) then PMLow else PMLowBar[1];

plot PMH =  if PlotPreMktLinesHrsPastOpen and PMHighBar != 0
            then PMHighBar
            else nan;
plot PML =  if PlotPreMktLinesHrsPastOpen and PMLowBar != 0
            then PMLowBar
            else nan;
plot PMMid = if PlotPreMktLinesHrsPastOpen and PMHighBar != 0 and PMLowBar != 0
             then (PMHighBar + PMLowBar) / 2
             else nan;

AddChartBubble(ShowChartBubbles and bar == HighestAll(highBar),
  PMHigh,
  "PM High",
  Color.Gray,
  1);

AddChartBubble(ShowChartBubbles and bar == HighestAll(lowBar),
  PMLow,
  "PM Low",
  Color.Gray,
  0);
 
Are there any studies can show auto trend lines of triangles such as
  • ascending triangles
  • descending triangles
  • symmetric triangles ?
 
Top right hand-corner of ToS, click on the Patterns option.

EatmeIe.png
 
@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);
@horserider Do you know of any scripts that can make auto trendlines to show breaks for CCI highs and lows? I was trying to look at this (fractal pivot trader) to convert it but I am not exactly sure where to look/start.
Code:
# May 27 2012 - FractalTrader
#
#This script will show support and resistance lines based on either
# user-defined price pivots, or automatically based on pivot points
# calculated by the script. In the case of automatic determination, only
# the last two matching points are used.
#hint numBars: For automatic pivot calculation: How many bars to use in calculation of pivotPoints. ie. current high is higher than both prior X bars and following X bars
#hint showLines: Show a line extending from high and low price pivots
#hint showValues: Show the numeric value of price occurring at the pivot point
#hint showBarNumbers: For manually entered pivots: used to determine bar numbers for two price pivot points to connect. It is recommended to temporarily turn off showValues.
#hint TrendResistanceStart: Starting point of a resistance trend line (connecting the highs), entered as a bar number. It is recommended to turn on showBarNumbers temporarily to determine the value.
#hint TrendResistanceEnd: Ending point of a resistance trend line, entered as a bar number. It is recommended to turn on showBarNumbers temporarily to determine the value.
#hint TrendSupportStart: Starting point of a support trend line (connecting the lows), entered as a bar number. It is recommended to turn on showBarNumbers temporarily to determine the value.
#hint TrendSupportEnd: Ending Point of a support trend line (connecting the lows), entered as a bar number. It is recommended to turn on showBarNumbers temporarily to determine the value.
input numBars = 5;
input showLines = yes;
input showValues = yes;
input showBarNumbers = no;
input TrendResistanceStart = 0;
input TrendResistanceEnd = 0;
input TrendSupportStart = 0;
input TrendSupportEnd = 0;

def UserSetResistance = TrendResistanceStart > 0 and TrendResistanceEnd > 0;
def UserSetSupport = TrendSupportStart > 0 and TrendSupportEnd > 0;
def currentHigh = high;
def currentLow = low;
def currentBar = BarNumber();
def PH;
def PL;
def isHigherThanNextBars = fold i = 1 to numBars + 1 with p = 1
while p do currentHigh > GetValue(high, -i);
PH = if UserSetResistance and ( currentBar == TrendResistanceStart or currentBar == TrendResistanceEnd ) then currentHigh else if !UserSetResistance and (currentBar > numBars and currentHigh == Highest(currentHigh, numBars) and isHigherThanNextBars) then currentHigh else Double.NaN;
def isLowerThanNextBars = fold j = 1 to numBars + 1 with q = 1
while q do currentLow < GetValue(low, -j);
PL = if UserSetSupport and ( currentBar == TrendSupportStart or currentBar == TrendSupportEnd ) then currentLow else if !UserSetSupport and (currentBar > numBars and currentLow == Lowest(currentLow, numBars) and isLowerThanNextBars) then currentLow else Double.NaN;
rec PHBar = if UserSetResistance then TrendResistanceEnd else if !IsNaN(PH) then currentBar else PHBar[1];
rec PLBar = if UserSetSupport then TrendSupportEnd else if !IsNaN(PL) then currentBar else PLBar[1];
rec PHL = if !IsNaN(PH) then PH else PHL[1];
rec priorPHBar = if UserSetResistance then TrendResistanceStart else if PHL != PHL[1] then PHBar[1] else priorPHBar[1];
rec PLL = if !IsNaN(PL) then PL else PLL[1];
rec priorPLBar = if UserSetSupport then TrendSupportStart else if PLL != PLL[1] then PLBar[1] else priorPLBar[1];
def isFinalTwoHighPivots = currentBar >= HighestAll(priorPHBar);
def isFinalTwoLowPivots = currentBar >= HighestAll(priorPLBar);
def ResistanceFinishOffset = if isFinalTwoHighPivots then currentBar - PHBar else 0;
def ResistanceStartOffset = if isFinalTwoHighPivots then currentBar - priorPHBar else 0;
def ResistanceSlope = (GetValue(PH, ResistanceFinishOffset) - GetValue(PH, ResistanceStartOffset)) / (PHBar - priorPHBar);
def SupportFinishOffset = if isFinalTwoLowPivots then currentBar - PLBar else 0;
def SupportStartOffset = if isFinalTwoLowPivots then currentBar - priorPLBar else 0;
def SupportSlope = (GetValue(PL, SupportFinishOffset) - GetValue(PL, SupportStartOffset)) / (PLBar - priorPLBar);
rec ResistanceExtend = if currentBar == HighestAll(PHBar) then 1 else ResistanceExtend[1];
rec SupportExtend = if currentBar == HighestAll(PLBar) then 1 else SupportExtend[1];

plot pivotHigh = if
#isFinalTwoHighPivots
ph>0 then PH else Double.NaN;
plot pivotHighLine = if PHL > 0 and isFinalTwoHighPivots then PHL else Double.NaN;

plot ResistanceLine = pivotHigh;
plot ResistanceExtension = if ResistanceExtend then (currentBar - PHBar) * ResistanceSlope + PHL else Double.NaN;
plot pivotLow = if
#isFinalTwoLowPivots
pl>0 then PL else Double.NaN;
plot pivotLowLine = if PLL > 0 and isFinalTwoLowPivots then PLL else Double.NaN;
plot SupportLine = pivotLow;
plot SupportExtension = if SupportExtend then (currentBar - PLBar) * SupportSlope + PLL else Double.NaN;
plot BN = currentBar;
plot PivotDot = if !IsNaN(pivotHigh) then pivotHigh else if !IsNaN(pivotLow) then pivotLow else Double.NaN;
plot BisectLine = if !isNaN(pivotHigh) then pivotHigh else if !isNaN(pivotLow) then pivotLow else double.NaN;
pivotHigh.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
pivotHigh.SetHiding(!showValues);
pivotLow.SetDefaultColor(GetColor(4));
pivotLow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
pivotLow.SetHiding(!showValues);
ResistanceLine.EnableApproximation();
ResistanceLine.SetDefaultColor(GetColor(7));
ResistanceLine.SetStyle(Curve.SHORT_DASH);
ResistanceExtension.SetStyle(Curve.SHORT_DASH);
ResistanceExtension.SetDefaultColor(GetColor(7));
SupportLine.EnableApproximation();
SupportLine.SetDefaultColor(GetColor(7));
SupportLine.SetStyle(Curve.SHORT_DASH);
SupportExtension.SetDefaultColor(GetColor(7));
SupportExtension.SetStyle(Curve.SHORT_DASH);
pivotHighLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotHighLine.SetHiding(!showLines);
pivotLowLine.SetPaintingStrategy(PaintingStrategy.DASHES);
pivotLowLine.SetHiding(!showLines);
BisectLine.EnableApproximation();
BisectLine.SetLineWeight(2);
PivotDot.SetDefaultColor(GetColor(7));
PivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
PivotDot.SetLineWeight(3);
BN.SetDefaultColor(GetColor(0));
BN.SetHiding(!showBarNumbers);
BN.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
 
@sunnyr Drawing trends on a CCI study? If that is what you mean it is possible. I cannot share what I did right now,sorry.
@horserider Yes I would like to draw trendlines on a cci oscillator. I only fond one scipt that was writting on tradingview for RSI that would be what im looking for. Here is the link for it if your interested. I think this could be a really incredible script to write so we can get auto trendlines and S/R on oscillators. Im surprised no one has done this here. https://www.tradingview.com/script/q6qk6ROG-strategy-of-RSI-with-trendlines-and-S-R/
 
Search more and maybe you will find where it is done in thinkscript for RSI anyway. As I said I cannot share what I did with CCI.
 
I find the FractalTrader Trendlines pretty interesting because they connect only William´s Fractals. What i don´t understand yet is why not all William´s Fractals are regcognized or considered. For example today TRIL showes a William´s Fractal on the daily but the indicator ignores it. Have no clue why. The formula ist too complicated for me to understand.
 
is there any way we can tighten the trendlines. most professionals ive chatted with use line charts to create trendlines/support resistance zones. when i put your trendline thingy on a line chart it wasnt as tight as i could do by hand. not sure if its an easy fix or if im being difficult, just wanted to offer a suggestion
 
This indicator works, but I would like the buy arrow to show only when they are both true at the time of the SuperTrend start.
Currently the arrow won't appear until the pattern trends up(which is good that it wont show when trending down) but it will then will apply the arrow like 5 candles back. Can anyone help with only allowing the buy arrow to show when the trend is > 0 and SuperTrend at the same time?

Thanks in advance!

Code:
# Mobius
# SuperTrend
# Chat Room Request
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);
# End Code SuperTrend

#
#Trend Line Plot 
#found @@@ https://usethinkscript.com/threads/auto-trend-lines-indicator-for-thinkorswim-free-download.31/
#
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);

# End Code Trend Lines


################## ARROWS ##################
def buy = close crosses above ST and slope3a > 0 or close crosses above ST and slope2b > 0;
def sell = close crosses below ST and Trendline2b;

plot buy_arrow = buy;
buy_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy_arrow.SetLineWeight(1);
buy_arrow.SetDefaultColor(Color.CYAN);

plot sell_arrow = sell;
sell_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell_arrow.SetLineWeight(1);
sell_arrow.SetDefaultColor(Color.MAGENTA);
 
Last edited by a moderator:
This indicator works, but I would like the buy arrow to show only when they are both true at the time of the SuperTrend start.
Currently the arrow won't appear until the pattern trends up(which is good that it wont show when trending down) but it will then will apply the arrow like 5 candles back. Can anyone help with only allowing the buy arrow to show when the trend is > 0 and SuperTrend at the same time?

Thanks in advance!

Code:
>>>>>

################## ARROWS ##################
def buy = close crosses above ST and slope3a > 0 or close crosses above ST and slope2b > 0;
def sell = close crosses below ST and Trendline2b;

plot buy_arrow = buy;
buy_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy_arrow.SetLineWeight(1);
buy_arrow.SetDefaultColor(Color.CYAN);

plot sell_arrow = sell;
sell_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell_arrow.SetLineWeight(1);
sell_arrow.SetDefaultColor(Color.MAGENTA);
My friend you said to plot the arrow at BUY or at Sell, which is where when the cross of the trendlines occurs which I suspect is why your arrow hits a couple spaces behind you... Because the trendline is straight sort of like if you were carrying a very long wooden beam through a doorway and turned suddenly you would hit the hallway.

Try this might have a typo
Code:
################## ARROWS ##################
def buy = close crosses above ST and slope3a > 0 or close crosses above ST and slope2b > 0;
def sell = close crosses below ST and Trendline2b;

plot buy_arrow = if buy==1 then low else Double.NAN;
buy_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy_arrow.SetLineWeight(1);
buy_arrow.SetDefaultColor(Color.CYAN);


plot sell_arrow = if sell==1 then high else Double.NAN;
sell_arrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell_arrow.SetLineWeight(1);
sell_arrow.SetDefaultColor(Color.MAGENTA);
 
@MattATM Thanks for the response, same problem. I'm guessing you're talking about this line specifically "def buy = close crosses above ST and slope3a > 0 or close crosses above ST and slope2b > 0;" its actually just 2 different buy signals, there are 2 slopes that its calculating there. They are both if close crosses ABOVE st, neither are a sell. The indicator is not behind in terms of giving signals, it just shows "buys" from 5 candles ago because both rules were finally met, however that is useless info beyond the bar it happens on. I just want to eliminate the arrow showing popping up 5 candles later when both are met. I just want an arrow to show if both are met in the same bar. If that makes sense?
 
I believe I have found a flaw in this deviation lines script.

Has anyone else ran into this?

I am trying to plot a crossover of the lower line. I assumed the lower line was 2b. Some maps it works, others it will not. I was very confused as to what was going on. I ended up plotting the TrendLine2b in a different color to see what was going on. What I realized was that the instruction (plot TrendLine2b = if slope2b > 0 then TL_Bear2 else TL_Bull2; was actually flipping the 2 lines. So essentially Slope B became the bear line at the top instead of the bull line at the bottom. I corrected this in my code by pointing to TL_Bull2 for the bottom line instead of pointing at TrendLine2b. I just wanted to throw this out in case someone else runs into it.
 

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