Volume Delta
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © peacefulLizard50262

//@version=5
indicator("Volume Delta", "VD", overlay = false, format = format.volume)

length = input.int(1, "Delta Length", 1, 10)

delta() =>
    float delta = 0.0
    if length == 1
        delta := ta.cum(volume-volume[1])
    if length == 2
        delta := ta.cum((volume-volume[2])/2)
    else if length == 3
        delta := ta.cum((volume+volume[1]-volume[2]-volume[3])/4)
    else if length == 4
        delta := ta.cum((volume+2*volume[1]-2*volume[3]-volume[4])/8)
    else if length == 5
        delta := ta.cum((volume+3*volume[1]+2*volume[2]-2*volume[3]-3*volume[4]-volume[5])/16)
    else if length == 6
        delta := ta.cum((volume+4*volume[1]+5*volume[2]-5*volume[4]-4*volume[5]-volume[6])/32)
    else if length == 7
        delta := ta.cum((volume+5*volume[1]+9*volume[2]+5*volume[3]-5*volume[4]-9*volume[5]-5*volume[6]-volume[7])/64)
    else if length == 8
        delta := ta.cum((volume+6*volume[1]+14*volume[2]+14*volume[3]-14*volume[5]-14*volume[6]-6*volume[7]-volume[8])/128)
    else if length == 9
        delta := ta.cum((volume+7*volume[1]+20*volume[2]+28*volume[3]+14*volume[4]-14*volume[5]-28*volume[6]-20*volume[7]-7*volume[8]-volume[9])/256)
    else if length == 10
        delta := ta.cum((volume+8*volume[1]+27*volume[2]+48*volume[3]+42*volume[4]-42*volume[6]-48*volume[7]-27*volume[8]-8*volume[9]-volume[10])/512)
    delta
volume_delta = delta()

plot(volume_delta <= 0 ? na : volume_delta, style = plot.style_columns, color = open < close ? color.green : color.red)
