Indicator · Pine Script v5 · On the chart

Average Lines

by regalouisei· code credited to fikira

The Average Lines indicator calculates and plots average values of detected pivot highs and pivot lows directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code138
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 2026

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

138 lines of Pine Script v5 from pinescript/editors_picks/average-lines.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

average-lines.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fikira
//@version=5
indicator("Average Lines", max_lines_count=500, overlay=true)

// -----------[ inputs ]-----------

var int     left  = input.int      (10  , 'leftbars'   , group='settings')
var int     right = input.int      ( 1  , 'rightbars'  , group='settings')
var bool    fill  = input.bool     (true, 'fill lines?', group='settings')
var string  choice= input.string   ('ph', 'choice'     , group='settings', options=['ph', 'pl', 'both', 'none'])
var int     max   = input.int      (500 , 'max pivot points for calculations'           )
var int     maxX  = input.int      (1000, 'max bars between concerning bar and last bar')

var color   c_pih = input.color    (color.new(color.red , 85), 'connecting line between (pivots)', group='colours pivot high')
var color   c_lh  = input.color    (color.new(color.red , 99), 'lines'                           , group='colours pivot high')
var color   c_avh = input.color    (color.new(#00bcd4   ,  0), 'average'                         , group='colours pivot high')
var color   c_fh  = input.color    (color.new(color.red , 93), 'linefill'                        , group='colours pivot high')

var color   c_pil = input.color    (color.new(color.lime, 85), 'connecting line between (pivots)', group='colours pivot low' )
var color   c_ll  = input.color    (color.new(color.blue, 99), 'lines'                           , group='colours pivot low' )
var color   c_avl = input.color    (color.new(#388e3c   ,  0), 'average'                         , group='colours pivot low' )
var color   c_fl  = input.color    (color.new(color.lime, 93), 'linefill'                        , group='colours pivot low' )

// -----------[ variables ]-----------

var float   avh   = 0.
var float   avl   = 0.

// no var

float       ph    = ta.pivothigh   (left, right)
float       pl    = ta.pivotlow    (left, right)
float[]     a_avh = array.new_float()
float[]     a_avl = array.new_float()

// -----------[ array's ]-----------

var float   [] a_php = array.new_float   ()
var float   [] a_plp = array.new_float   ()
var int     [] a_plb = array.new_int     ()
var int     [] a_phb = array.new_int     ()
var line    [] a_lh  = array.new_line    ()
var linefill[] a_lhF = array.new_linefill()
var line    [] a_ll  = array.new_line    ()
var linefill[] a_llF = array.new_linefill()


switch
    choice == 'both' =>
        if array.size(a_lh ) > 0 and array.size(a_ll ) > 0 
            if (array.size(a_lh )  + array.size(a_ll )) > 500
                if array.size(a_lh ) > array.size(a_ll )
                    line.delete(array.pop(a_lh ))
                else
                    line.delete(array.pop(a_ll ))
        if array.size(a_lhF) > 0 and array.size(a_llF) > 0 
            if (array.size(a_lhF)  + array.size(a_llF)) > 500
                if array.size(a_lhF) > array.size(a_llF)
                    lineF         = array.pop(a_lhF)
                    line.delete(linefill.get_line1(lineF))
                    line.delete(linefill.get_line2(lineF))
                    linefill.delete(lineF)
                else
                    lineF         = array.pop(a_llF)
                    line.delete(linefill.get_line1(lineF))
                    line.delete(linefill.get_line2(lineF))
                    linefill.delete(lineF)
    choice == 'ph' =>
        if array.size(a_lh) > 500
            line.delete(array.pop(a_lh))
        if array.size(a_lhF) > 500
            lineF         = array.pop(a_lhF)
            line.delete(linefill.get_line1(lineF))
            line.delete(linefill.get_line2(lineF))
            linefill.delete(lineF)
    choice == 'pl' =>
        if array.size(a_ll) > 500
            line.delete(array.pop(a_ll))  
        if array.size(a_llF) > 500
            lineF         = array.pop(a_llF)
            line.delete(linefill.get_line1(lineF))
            line.delete(linefill.get_line2(lineF))
            linefill.delete(lineF)
    
// -----------[ functions ]-----------

calc_avg(array_lines, array_average) =>
    var average = 0.
    if array.size(array_lines) > 0
        for i = 0 to array.size(array_lines) -1
            array.unshift(array_average, line.get_price(array.get(array_lines, i), bar_index))
            average := array.avg(array_average)
    [average]
 
draw_lines(piv, array_piv_price, array_piv_index, array_lines, array_fill, col_lines, col_fill) => 
    //
    //var linefill ln_fill = na
    max_bars_back(piv, 5000)
    if piv and last_bar_index - bar_index < maxX
        array.unshift(array_piv_price, piv              )
        array.unshift(array_piv_index, bar_index - right)
        //
        if array.size(array_piv_price) > 0
            for i = 0 to array.size(array_piv_price) -1
                x1 = array.get(array_piv_index, i)
                y1 = array.get(array_piv_price, i)
                x2 = array.get(array_piv_index, 0)
                y2 = array.get(array_piv_price, 0)
                //
                array.unshift (array_lines    , line.new(x1, y1, x2, y2, color=col_lines, extend=extend.right))
                if array.size(array_lines) > 500
                    line.delete(array.pop(array_lines))
                    line.delete(array.pop(array_lines))
                if fill and array.size(array_lines) > 1
                    array.unshift(array_fill, linefill.new(array.get(array_lines, i + 1), array.get(array_lines, i), col_fill))
                    //    


if choice == 'ph' or choice == 'both'
    draw_lines(ph, a_php, a_phb, a_lh, a_lhF, c_lh, c_fh)  
    [_avh]   = calc_avg(a_lh, a_avh), avh := _avh

if choice == 'pl' or choice == 'both'
    draw_lines(pl, a_plp, a_plb, a_ll, a_llF, c_ll, c_fl)  
    [_avl]   = calc_avg(a_ll, a_avl), avl := _avl
    
if array.size(a_php) > max 
    array.pop(a_php)
    array.pop(a_phb)
if array.size(a_plp) > max 
    array.pop(a_plp)
    array.pop(a_plb)

plot((choice == 'ph' or choice == 'both') and avh > 0 ? avh : na, color=c_avh)
plot((choice == 'pl' or choice == 'both') and avl > 0 ? avl : na, color=c_avl)
plot((choice == 'ph' or choice == 'both')             ? ph  : na, color=c_pih, offset=-right)
plot((choice == 'pl' or choice == 'both')             ? pl  : na, color=c_pil, offset=-right)

Overview

Average Lines allows users to set left and right bar counts for pivot detection, choose which averages to display (pivot highs only, lows only, both, or none), and enable optional line filling. Colors can be customized for connecting lines between pivots, individual lines, averages, and fills for each pivot type. The script maintains arrays to limit plotted lines to 500 points for performance. It draws its elements as an overlay on the price chart.

Details

Published by

regalouisei on GitHub

Code credit

fikira, as written in the script's header

Draws

On the price chart

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
leftbarsint10left
rightbarsint1right
fill lines?booltruefill
choicestringphchoice
max pivot points for calculationsint500max
max bars between concerning bar and last barint1000maxX
connecting line between (pivots)colorcolor.new(color.red , 85)c_pih
linescolorcolor.new(color.red , 99)c_lh
averagecolorcolor.new(#00bcd4 , 0)c_avh
linefillcolorcolor.new(color.red , 93)c_fh
connecting line between (pivots)colorcolor.new(color.lime, 85)c_pil
linescolorcolor.new(color.blue, 99)c_ll
Show all 14 settings
SettingTypeDefaultVariable
averagecolorcolor.new(#388e3c , 0)c_avl
linefillcolorcolor.new(color.lime, 93)c_fl
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

947 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 12 days ago · GPL-3.0 GitHub repository of Moving Average Channel

PivotHighLow

by Alorse
330 stars

This indicator calculates pivot high and pivot low points on the price chart based on configurable left and right bar counts. Confirmed pivot highs and lows are plotted with negative bar offsets directly on the price chart using line and cross styles. Addition

Indicator v4 Support & resistanceOscillators & momentum Repaints
Updated 2 months ago · None stated GitHub repository of PivotHighLow

Midnight VWAP

by TradersPost
228 stars

The Midnight VWAP indicator calculates and overlays two distinct Volume Weighted Average Price (VWAP) lines on the price chart based on customizable session timeframes. It tracks an overnight or pre-market session alongside regular trading hours, resetting cal

Indicator v6 Moving averagesSessions & time
Updated 3 months ago · None stated GitHub repository of Midnight VWAP

Ichimoku Cloud

by harryguiacorn
220 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

Deduct

by TaichiS
219 stars

This indicator displays moving average deduction reference points directly on the price chart. It plots dynamic labels on historical bars corresponding to short, medium, and long lookback periods, defaulted to 20, 60, and 120 bars. Each text label displays the

Indicator v4 Moving averagesSupport & resistance
Updated 5 years ago · None stated GitHub repository of Deduct

More Moving averages scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers