Volume [Educational]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LonesomeTheBlue

//@version=5
indicator("Volume [Educational]", "Vol", overlay = true, max_lines_count = 500, format = format.volume)
colorup = input.color(defval = color.new(#26a69a, 50), title = "Colors", inline = "cols")
colordn = input.color(defval = color.new(#ef5350, 50), title = "", inline = "cols")

var float bottomMargin = 8.0 / 82.0
var times = array.new_float(0)
array.unshift(times, time)

visibleBars = 0
if time == chart.right_visible_bar_time
    visibleBars := array.indexof(times, chart.left_visible_bar_time) + 1

lowest = ta.lowest(visibleBars > 0 ? visibleBars : 1)
bottomline =  lowest - (ta.highest(visibleBars > 0 ? visibleBars : 1) - lowest) * bottomMargin
maxVol = ta.highest(volume, visibleBars > 0 ? visibleBars : 1)

if visibleBars > 0 and time == chart.right_visible_bar_time
    var volLines = array.new_line(0)
    for x = 0 to (array.size(volLines) > 0 ? array.size(volLines) - 1 : na)
        line.delete(array.pop(volLines))
    maxVolLevel = (lowest - bottomline) * 4
    
    for x = 0 to math.min(visibleBars, 500)
        array.push(volLines, 
                   line.new(x1 = bar_index - x, y1 = bottomline, x2 = bar_index - x, y2 = bottomline + volume[x] * maxVolLevel / maxVol, 
                      width = 5, 
                      color = close[x] >= open[x] ? colorup : colordn))

plotarrow(volume, colorup=color.new(chart.fg_color, 100), colordown=color.new(chart.fg_color, 100), maxheight = 0)
