Average Price Line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Leviathans

//@version=5
indicator("Avg", overlay = true)

lineColor = input.color(#787B86, 'Line Color')

var float[] closes = array.new<float>()

timeLeft = chart.left_visible_bar_time
timeRight= chart.right_visible_bar_time

if time >= timeLeft and time < timeRight
    array.unshift(closes, close)

avgClose = array.avg(closes)

var line avgLine = line.new(na, na, na, na, xloc.bar_time, extend.right, lineColor, style=line.style_dotted)

if barstate.islastconfirmedhistory or barstate.isrealtime
    line.set_xy1(avgLine, timeLeft, avgClose)
    line.set_xy2(avgLine, timeRight, avgClose)

plot(avgClose, '', color.new(#142E61, 80), show_last = 1)