Box-Cox Log Bands
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5
indicator("Box-Cox Log Bands", overlay=true)

import RicardoSantos/FunctionBoxCoxTransform/1 as bct

float source = input.source(close)
int length = input.int(10)
float lambda = input.float(-0.5)
float upper_band_scale_factor = input(0.01)
float lower_band_scale_factor = input(0.01)

var float[] data = array.new_float(length)
if barstate.isfirst
    for _i=0 to length-1
        array.push(data, source)
        float(na)
else
    array.unshift(data, source)
    array.pop(data)


float[] transformed = bct.regular(data, lambda)
float upper = math.abs(array.avg(bct.inverse(transformed, lambda+upper_band_scale_factor)))
float lower = math.abs(array.avg(bct.inverse(transformed, lambda+lower_band_scale_factor)))

plot(upper, color=color.aqua)
plot(lower, color=color.blue)
