4 Pole Butterworth
// 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("4 Pole Butterworth", "4PB", true)
fourpolebutter(float src, int len) =>
    a1     = 0.
    b1     = 0.
    c1     = 0.
    coef1  = 0.
    coef2  = 0.
    coef3  = 0.
    coef4  = 0.
    coef5  = 0.
    bttr   = 0.
    trig   = 0.
    a1    := math.exp(-0.5 * math.pi / len)
    b1    := 2.0 * a1 * math.cos(0.5 * math.pi / len)
    c1    := a1 * a1
    coef2 := b1 + c1
    coef3 := -(c1 + b1 * c1)
    coef4 := c1 * c1
    coef5 := (1 - b1 + c1) * (1 - c1) / 16
    bttr  := coef5 * (src + 4 * nz(src[1]) + 6 * nz(src[2]) + 4 * nz(src[3]) + nz(src[4])) + coef2 * nz(bttr[1]) + coef3 * nz(bttr[2]) + coef4 * nz(bttr[3])
    bttr  := bar_index < 5 ? src : bttr
    bttr

source = input.source(close, "Source")
length = input.int(14, "length", 5)

fourpolebutter = fourpolebutter(source, length)

plot(fourpolebutter, "Four Pole Butterworth")
