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

//@version=5
library("my_choppyness")

// firstPriority = input.float(1, "First priority", minval=1)
length = input.int(56, "length", minval=1)
Hline = input.float(55, "HLine", minval=1)
Lline = input.float(45, "LLine", minval=1)
priority = input.float(1, "priority", minval=1)


export choppyness(int length, float prio) =>
    totalAmp = 0.0
    positiveAmp = 0.0
    negativeAmp = 0.0
    for i = 0 to length
        if (close[i] and open[i])
            amp = math.abs(close[i] - open[i]) * math.pow(prio, length - i)
            totalAmp := totalAmp + amp
            if (close[i] > open[i])
                positiveAmp := positiveAmp + amp
            else
                negativeAmp := negativeAmp + amp
    positiveAmp / totalAmp
        

chopp = choppyness(length, priority)

plot(chopp * 100, "Choppyness", color=color.blue)
h0 = hline(Hline, "Upper Band", color=#787B86)
h1 = hline(Lline, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")