ATR Based Trendlines - JD For ThinkOrSwim

dsvitale

New member
The author states:
This script draws trendlines from the pivot points in the price chart.

The angle of the trendlines is determined by (a percentage of) the atr.

The angle follows the change in price, dictated by the atr at the moment where the pivot point is detected.

The atr percentage determines if the trendline follows the rate of change of the atr or a fraction ( value < 100) or a multiple ( value > 100) of that

tfXclIJ.png


https://www.tradingview.com/script/bugFWu53-ATR-Based-Trendlines-JD/
 
Last edited by a moderator:

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

https://www.tradingview.com/script/bugFWu53-ATR-Based-Trendlines-JD/

//@version=4
//
/////////////////////////////////////////////////////
/// © Copyright 2020 to present, Joris Duyck (JD) ///
/////////////////////////////////////////////////////
//{
// This script draws trendlines from the pivot points in the price chart.

// The angle of the trendlines is determined by (a percentage of) the atr.
// The angle follows the change in price, compared to the atr at the moment where the pivot point is detected
// The atr percentage determines if the trendline follows the rate of change of the atr or a fraction ( value < 100) or a multiple ( value > 100) of that

// JD.

// #NotTradingAdvice #DYOR

// Disclaimer.
// I AM NOT A FINANCIAL ADVISOR.
// THESE IDEAS ARE NOT ADVICE AND ARE FOR EDUCATION PURPOSES ONLY.
// ALWAYS DO YOUR OWN RESEARCH!
//}

study(title = "ATR Based Trendlines - JD", shorttitle="ATR Trendlines - JD", overlay=true)

// Input variables
//{
len = input(30, title = "loockback length pivots")
atr_percent = input(100, title = "atr target percentage", minval = 0) / 100
wicks = input(true, title = "Draw lines from wicks (checked) or real bodies (unchecked)?")
do_mono = input(false, title = "checked = monochrome lines, unchecked = direction colored lines?")
//}


// Initialising color scheme
//{
var color line_color_high = do_mono ? color.teal : color.lime
var color line_color_low = do_mono ? color.teal : color.fuchsia
//}

/////// Function declarations ///////
//{
angle_to_slope_percent (_degrees) =>
_slope = (_degrees/100 * atr(len / 2))
//}

/////// Start of main script ///////

// Calculate pivot points
//{
high_point = fixnan(pivothigh(wicks ? high : (max(close, open)), len, len /2 ))
low_point = fixnan(pivotlow(wicks ? low : (min(close, open)), len, len / 2))
//}

// plot trendlines //
//{
var line trendline_high = na
var line trendline_low = na
trendline_high := na
trendline_low := na

slope_high = atr_percent * angle_to_slope_percent(len / 2)
slope_low = atr_percent * angle_to_slope_percent(len / 2)

if change(high_point) != 0
trendline_high := line.new(
bar_index - len/2, high_point,
bar_index , high_point - (slope_high * len / 2), xloc = xloc.bar_index,
extend = extend.right, color = line_color_high, style = line.style_dotted, width = 1)

// plot Low trendlines //
if change(low_point) != 0
trendline_low := line.new(
bar_index - len/2, low_point,
bar_index , low_point + (slope_low * len / 2), xloc = xloc.bar_index,
extend = extend.right, color = line_color_low, style = line.style_dotted, width = 1)
check the below. and you can try similar indicator on below post:
https://usethinkscript.com/threads/trendlines-with-breaks-lux-for-thinkorswim.12025/post-108235


CSS:
#/@version=4
# © Copyright 2020 to present, Joris Duyck (JD) ///
#// This script draws trendlines from the pivot points in the price chart.
#// The angle of the trendlines is determined by (a percentage of) the atr.
#// The angle follows the change in price, compared to the atr at the moment where the pivot point is detected
#// The atr percentage determines if the trendline follows the rate of change of the atr or a fraction ( value < 100) or a multiple ( value > 100) of that
#study(title = "ATR Based Trendlines - JD", shorttitle="ATR Trendlines - JD", overlay=true)
# Converted by Sam4Cok@samer800    - 05/2024
input maxLinesLength = 100;
input ShowBrakouts = {"Don't Show Breakouts", default "Only Confirmed Breakouts", "Show All Breakouts"};
input loockbackLengthPivots = 30;#    title = "loockback length pivots")
input atrTargetPercentage = 100; #,   title = "atr target percentage", minval = 0) / 100
input DrawLinesFromWicks = yes; #,  title = "Draw lines from wicks (checked) or real bodies (unchecked)?")
input ShowTodayOnly = no;

def na = Double.NaN;
def last = IsNaN(close);
def today =  GetDay() == GetLastDay();
def atr_percent = atrTargetPercentage / 100;

script Pivots {
    input series    = close;
    input leftBars  = 10;
    input rightBars = 10;
    input isHigh = yes;
    def na = Double.NaN;
    def HH = series == Highest(series, leftBars + 1);
    def LL = series == Lowest(series, leftBars + 1);
    def pivotRange = (leftBars + rightBars + 1);
    def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
    def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
    def barIndexH = if pvtCond then
                    fold i = 1 to rightBars + 1 with p=1 while p do
                    series > GetValue(series, - i) else na;
    def barIndexL = if pvtCond then
                    fold j = 1 to rightBars + 1 with q=1 while q do
                    series < GetValue(series, - j) else na;
    def PivotPoint;
    if isHigh {
        PivotPoint = if HH and barIndexH then series else na;
    } else {
        PivotPoint = if LL and barIndexL then series else na;
    }
    plot pvt = PivotPoint;
}
#/////// Function declarations ///////
script angle_to_slope_percent {
    input _degrees = 15;
    input len = 30;
    def _slope = (_degrees / 100 * ATR(Length = Floor(len / 2)));
    plot out = _slope;
}
#/////// Start of main script ///////
def srcHi = if DrawLinesFromWicks then high else (Max(close, open));
def srcLo = if DrawLinesFromWicks then low else  (Min(close, open));
def ph = Pivots(srcHi, loockbackLengthPivots, Floor(loockbackLengthPivots / 2), yes);
def pl = Pivots(srcLo, loockbackLengthPivots, Floor(loockbackLengthPivots / 2), no);

def high_point = if !IsNaN(ph) then ph else high_point[1];
def low_point  = if !IsNaN(pl) then pl else low_point[1];

#/ plot trendlines //

def slope_high = atr_percent * angle_to_slope_percent(loockbackLengthPivots / 2);
def slope_low  = atr_percent * angle_to_slope_percent(loockbackLengthPivots / 2);

def nzPh = !IsNaN(ph); #(high_point-high_point[1]) !=0; #!IsNaN(ph);
def nzPl = !IsNaN(pl); #(low_point-low_point[1]) != 0 ;#!IsNaN(pl);


def slope_ph = if !slope_ph[1] then (slope_high ) else
               if nzPh then (slope_high ) else slope_ph[1];
def slope_pl = if !slope_pl[1] then (slope_low ) else
               if nzPl then (slope_low ) else slope_pl[1];
def cnUp = if nzPh then 0 else cnUp[1] + 1;
def cnDn = if nzPl then 0 else cnDn[1] + 1;

def upper = if !upper[1] then high_point else
            if nzPh then high_point else upper[1] - slope_ph;
def lower = if !lower[1] then low_point else
            if nzPl then low_point else lower[1] + slope_pl;


def UpPH   = if last then na else if !nzPh[-1] then upper else na;
def LoLH   = if last then na else if !nzPl[-1] then lower else na;

#//----
def single_upper = if (close[1] > upper) then 0 else
                   if nzPh then 1 else single_upper[1];
def single_lower = if (close[1] < lower) then 0 else
                   if nzPl then 1 else single_lower[1];

def UpSigUp;
def UpsigDn;

#def ShowSignal;
switch (ShowBrakouts) {
case "Show All Breakouts" :
    UpSigUp = yes;
    UpsigDn = yes;
case "Don't Show Breakouts" :
    UpSigUp = na;
    UpsigDn = na;
default :
    UpSigUp = close > close[1] and volume > volume[1];
    UpsigDn = close < close[1] and volume > volume[1];
}

def upper_breakout = single_upper[1] and close[1] > upper and UpSigUp;
def lower_breakout = single_lower[1] and close[1] < lower and UpsigDn;

def UpBreak = if upper_breakout then low else na;
def LoBreak = if lower_breakout then high else na;

def Labelup = if !ShowTodayOnly then UpBreak else
              if !today then na else UpBreak;
def Labeldn = if !ShowTodayOnly then LoBreak else
              if !today then na else LoBreak;


plot upperPH = if cnUp > maxLinesLength then na else
               if !ShowTodayOnly then UpPH else
               if !today then na else UpPH;
upperPH.SetDefaultColor(Color.CYAN);

plot upperDash = if cnUp > maxLinesLength then na else
                 if IsNaN(UpPH) then upper else na;
upperDash.SetStyle(Curve.MEDIUM_DASH);
upperDash.SetDefaultColor(Color.CYAN);


plot LowerLH =  if cnDn > maxLinesLength then na else
               if !ShowTodayOnly then LoLH else
               if !today then na else LoLH;
LowerLH.SetDefaultColor(Color.MAGENTA);

plot LowerDash = if cnDn > maxLinesLength then na else
                 if IsNaN(LoLH) then lower else na;
LowerDash.SetStyle(Curve.MEDIUM_DASH);
LowerDash.SetDefaultColor(Color.MAGENTA);

#-------- Breakout Bubbles
AddChartBubble(Labelup, Labelup, "Break", Color.CYAN, no);
AddChartBubble(Labeldn, Labeldn, "Break", Color.MAGENTA, yes);

#-- END of CODE
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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