Educational: lines, linefill, labels & boxes
// 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  ("Educational: lines, linefill, labels & boxes"           , max_lines_count=200, overlay=true)

selection  = input.string    ('Linefill'    , options=['Linefill'            ])
options    = input.string    ('extend lines', options=['extend lines', 'none'])

// –––––––––––––––––––––[ Fetch previous Weekly high/low ]–––––––––––––––––––––
prevHigh_W = request.security(syminfo.tickerid, 'W', nz(high[1]), barmerge.gaps_off, barmerge.lookahead_on)
prevLow_W  = request.security(syminfo.tickerid, 'W', nz(low [1]), barmerge.gaps_off, barmerge.lookahead_on)

// –––––––––––––––––––––[         Create array's         ]–––––––––––––––––––––
var linefill[] lnF = array.new <linefill> ()
var int     [] dir = array.new <  int   > ()

// –––––––––––––––––––––[      Add 1 linefill.new()      ]–––––––––––––––––––––
if barstate.isfirst or timeframe.change('W')
    line1 = line.new(bar_index, prevHigh_W, bar_index +1, prevHigh_W, color= color.new(color.silver, 50) )
    line2 = line.new(bar_index, prevLow_W , bar_index +1, prevLow_W , color= color.new(color.silver, 50) )
    array.unshift   (lnF      ,                   linefill.new(line1, line2, color.new(color.blue  , 75)))
    array.unshift   (dir      ,                                    0                                       )

// –––––––––––––––––––––[      If broken, change color,                      
//                             otherwise,   make lines
//                             1     bar_index  longer   ]–––––––––––––––––––––
getDir       = array.get(dir, 0)
if getDir == 0
    getLineF = array.get(lnF, 0)
    getLine1 = linefill.get_line1(getLineF)
    getLine2 = linefill.get_line2(getLineF)
    // if close >  top  line  -> change color -> lime  ,  set dir to   1
    if close > line.get_price    (getLine1, bar_index  )
        linefill.set_color       (getLineF, color.new(color.lime, 75))
        array.set                (dir     ,      0 ,  1)
    else
    // if close < bottom line -> change color -> red   ,  set dir to  -1
        if close < line.get_price(getLine2, bar_index  )
            linefill.set_color   (getLineF, color.new(#FF0000   , 75))
            array.set            (dir     ,      0 , -1)
    //
    // notice the 'difference' and 'simularity'  between 'extend lines' & 'none'
    if options == 'extend lines'   
        line.set_x2              (getLine1, bar_index  )
        line.set_x2              (getLine2, bar_index  )

plot(   array.size(linefill.all), display = display.status_line)