Indicator · Pine Script v5 · On the chart

Auto Trendline [DojiEmoji]

by kevinhhl· code credited to DojiEmoji

Auto Trendline automatically identifies fractal pivot points on the price chart and connects them to draw trendlines.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code163
Settings16inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago6 Oct 2023

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

163 lines of Pine Script v5 from Scripts/Autotrendlines based on fractals.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Autotrendlines based on fractals.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DojiEmoji

// A tool that automatically draws out trend lines by connecting the most recent fractals.

//@version=5
indicator("Auto Trendline [DojiEmoji]", format=format.price, precision=0, overlay=true, max_lines_count=500)

var int TYPE_UP = 1
var int TYPE_DOWN = -1
var string LINE_WIDTH1_STR = "Width 1"
var string LINE_WIDTH2_STR = "Width 2"
_get_width(string str_input) =>
    switch str_input // {string:int}
        LINE_WIDTH1_STR => 1
        LINE_WIDTH2_STR => 2

// ----------------------------------------------------------------------------
// Settings:
// {
var string GROUP_FRACT  = "Fractals"
var int n               = input.int(10, title="Fractal Period", minval=2, group=GROUP_FRACT)
var color col_hl        = input.color(color.blue, title="Plot:", inline="plot_low", group=GROUP_FRACT)
var bool show_hl        = input.bool(true, title="HL", inline="plot_low", group=GROUP_FRACT)
var color col_ll        = input.color(color.gray, title=", Plot:", inline="plot_low", group=GROUP_FRACT)
var bool show_ll        = input.bool(false, title="LL", inline="plot_low", group=GROUP_FRACT)
var color col_lh        = input.color(color.red, title="Plot:", inline="plot_high", group=GROUP_FRACT)
var bool show_lh        = input.bool(true, title="LH", inline="plot_high", group=GROUP_FRACT)
var color col_hh        = input.color(color.gray, title=", Plot:", inline="plot_high", group=GROUP_FRACT)
var bool show_hh        = input.bool(false, title="HH", inline="plot_high", group=GROUP_FRACT) 

var string GROUP_ATL    = "Auto trendlines"
var string subgroup1    = "recent line"
var color ln_col_recent = input.color(color.new(color.purple, 0), title="Recent Line", group=GROUP_ATL, inline=subgroup1)
var int lnwidth_recent  = _get_width(input.string(LINE_WIDTH1_STR, options=[LINE_WIDTH1_STR, LINE_WIDTH2_STR], title="", inline=subgroup1, group=GROUP_ATL))
var string subgroup2    = "historical line"
var color ln_col_prev   = input.color(color.new(color.gray, 50), title="Historical Line", group=GROUP_ATL, inline=subgroup2)
var int lnwidth_prev    = _get_width(input.string(LINE_WIDTH1_STR, options=[LINE_WIDTH1_STR, LINE_WIDTH2_STR], title="", inline=subgroup2, group=GROUP_ATL))

var int max_tl          = input.int(1, title="Max pair of lines", maxval=250, minval=1, group=GROUP_ATL)*2
var string _str_extend  = input.string("Right", options=["Right", "Both ways"], title="Which way to extend lines", group=GROUP_ATL)
var string str_extend   = _str_extend == "Both ways" ? extend.both : extend.right

var bool show_crosses   = input.bool(false, title="Show crosses", tooltip="Instances when closing price of a bar has crossed lower/upper trendlines", group=GROUP_ATL)
// }

// ----------------------------------------------------------------------------
// Fractal UDT and other relevant data structures: 
// Handle fractals and trendlines associated with them
// {
type fractal
    int up_or_down = na // either TYPE_UP or TYPE_DOWN
    int xloc = na 
    float yloc = na
    int xloc_parent = na
    float yloc_parent = na

var fractal[] arr_fract = array.new<fractal>()  // Can be used for multiple purposes such as:
                                                // (a) connecting trendlines but added condition to skip X no. of fractals in between
                                                // (b) create a zigzag, since knowing foo.parent's x and y
                                                // ... possibilities are endless

var line[] arr_ln_up = array.new_line() // Array of lines, newest elements inserted to front
var line[] arr_ln_dn = array.new_line()

// @function init_fractal() returns instance of fractal that has been init'ed
init_fractal(int fract_type, int xloc, float yloc, int xparent, float yparent)=>
    f = fractal.new()
    f.up_or_down := fract_type
    f.xloc := xloc
    f.yloc := yloc
    f.xloc_parent := xparent
    f.yloc_parent := yparent

    ln = line.new(xloc, yloc, xparent, yparent, xloc.bar_index, str_extend, color=ln_col_recent, style=line.style_dashed, width=lnwidth_recent)
    if f.up_or_down == TYPE_UP
        array.unshift(arr_ln_up, ln) 
    else if f.up_or_down == TYPE_DOWN
        array.unshift(arr_ln_dn, ln)
    
    array.unshift(arr_fract, f) 
    f // <- return

// @function drop_and_roll(new fractal) returns void
// Clean up: Drop oldest trendlines, change colors for previous trendline
drop_and_roll(fractal f) =>
    arr_ln = f.up_or_down == TYPE_UP ? arr_ln_up : f.up_or_down == TYPE_DOWN ? arr_ln_dn : na
    if array.size(arr_ln) > 1
        line.set_color(array.get(arr_ln, 1), ln_col_prev)
        line.set_width(array.get(arr_ln, 1), lnwidth_prev)
        while array.size(arr_ln) > math.floor(max_tl/2)
            line.delete(array.pop(arr_ln))

// @function draw_trendline() returns void
draw_trendline(fract_type, x2, y2, x1, y1) =>
    f = init_fractal(fract_type, x2, y2, x1, y1)
    drop_and_roll(f)

// } end of handle for fractals

// ----------------------------------------------------------------------------
// Fractals, implemented using ta.pivot high && low 
// ----------------------------------------------------------------------------

// A Fractal is always a Pivot H/L (but a Pivot H/L is not always a Fractal):
float ph = ta.pivothigh(n, n)[1], bool upfract = not na(ph)
float pl = ta.pivotlow(n, n)[1],  bool downfract = not na(pl)

alertcondition(not na(ph) or not na(pl), title="New trendline formed", message="New trendline formed")

// Pointers -> Recent fractals
// {
var float recent_dn1   = na,  var int i_recent_dn1 = na
var float recent_up1   = na,  var int i_recent_up1 = na
var float recent_dn2   = na,  var int i_recent_dn2 = na
var float recent_up2   = na,  var int i_recent_up2 = na

if downfract 
    recent_dn2:=recent_dn1, i_recent_dn2 := i_recent_dn1
    recent_dn1:=low[n+1],   i_recent_dn1 := bar_index-n-1
    draw_trendline(TYPE_DOWN, i_recent_dn2, recent_dn2, i_recent_dn1, recent_dn1)

if upfract 
    recent_up2:=recent_up1, i_recent_up2 := i_recent_up1
    recent_up1:=high[n+1],  i_recent_up1 := bar_index-n-1
    draw_trendline(TYPE_UP, i_recent_up2, recent_up2, i_recent_up1, recent_up1)

// }

// Plotting fractals
bool hh = upfract and recent_up1 > recent_up2 ? high[n+1] : na
bool lh = upfract and recent_up1 < recent_up2 ? high[n+1] : na
bool hl = downfract and recent_dn1 > recent_dn2 ? low[n+1] : na
bool ll = downfract and recent_dn1 < recent_dn2 ? low[n+1] : na

plotshape(show_hh and hh, style=shape.circle, size=size.tiny, offset=-n-1, title="HH", text="HH", location=location.abovebar, textcolor=col_hh, color=na, editable=false)
plotshape(show_lh and lh, style=shape.circle, size=size.tiny, offset=-n-1, title="LH", text="LH", location=location.abovebar, textcolor=col_lh, color=na, editable=false)
plotshape(show_ll and ll, style=shape.circle, size=size.tiny, offset=-n-1, title="LL", text="LL", location=location.belowbar, textcolor=col_ll, color=na, editable=false)
plotshape(show_hl and hl, style=shape.circle, size=size.tiny, offset=-n-1, title="HL", text="HL", location=location.belowbar, textcolor=col_hl, color=na, editable=false)

// ----------------------------------------------------------------------------
// Finding crosses between closing price & pair of most recent trendlines
// ----------------------------------------------------------------------------

// @function get_slope()
get_slope(xA, yA, xB, yB) =>
    (yB - yA) / (xB - xA)

// Solving for price at current x (bar_index), given two pairs of fractals with x values < bar_index.
float m_dn = get_slope(i_recent_dn1, recent_dn1, i_recent_dn2, recent_dn2)
float y_dn = (m_dn * bar_index) + recent_dn1 - (m_dn * i_recent_dn1)
float m_up = get_slope(i_recent_up1, recent_up1, i_recent_up2, recent_up2)
float y_up = (m_up * bar_index) + recent_up1 - (m_up * i_recent_up1)

// Plotting crosses
bool cross_upper = ta.cross(close, y_up) 
bool cross_lower = ta.cross(close, y_dn) 
plotshape(show_crosses and cross_upper, title = "Crossed upper trendline", style = shape.xcross, 
 location = location.belowbar, color = color.new(color = color.blue, transp = 50), size = size.small)
plotshape(show_crosses and cross_lower, title = "Crossed lower trendline", style = shape.xcross, 
 location = location.abovebar, color = color.new(color = color.red, transp = 50), size = size.small)

alertcondition(cross_upper or cross_lower, title="Upper/lower trendline crossed", message="Upper/lower trendline crossed")

Overview

Auto Trendline [DojiEmoji] scans for higher highs, lower highs, higher lows, and lower lows over a customizable fractal period. Users can configure colors and visibility for fractal points, set the maximum number of active or historical trendlines, choose extension modes, and optionally highlight price crosses over upper and lower trendlines. The indicator renders directly on the main price chart, offering systematic visualization of support and resistance levels across chart timeframes.

Details

Published by

kevinhhl on GitHub

Code credit

DojiEmoji, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Fractal Periodint10n
Plot:colorcolor.bluecol_hl
HLbooltrueshow_hl
, Plot:colorcolor.graycol_ll
LLboolfalseshow_ll
Plot:colorcolor.redcol_lh
LHbooltrueshow_lh
, Plot:colorcolor.graycol_hh
HHboolfalseshow_hh
Recent Linecolorcolor.new(color.purple, 0)ln_col_recent
stringLINE_WIDTH1_STR
Historical Linecolorcolor.new(color.gray, 50)ln_col_prev
Show all 16 settings
SettingTypeDefaultVariable
stringLINE_WIDTH1_STR
Max pair of linesint1max_tl
Which way to extend linesstringRight_str_extend
Show crossesboolfalseshow_crosses
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

AI Trend

by dearvn
257 stars

AI Trend is an indicator that plots pivot points, trend lines, and support and resistance levels directly on the price chart. It marks high and low pivots based on user-defined length, draws trend lines that check for violations at specified points, and identi

Indicator v5 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of AI Trend

Ichimoku Cloud

by harryguiacorn
219 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud
219 stars

This indicator identifies local swing highs and swing lows to construct a Zig Zag pattern on the main price chart. It evaluates pivot points based on a customizable number of surrounding left and right bars. Using persistent variables, the script tracks previo

Indicator v4 Support & resistanceTrend Repaints
Updated 5 years ago · None stated GitHub repository of Teaching: Finding Highs and Lows 6

Dynamic Linear Regression

by TradersPost
218 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers