Bitcoin Limited Growth Model
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © QuantiLuxe

//@version=5
indicator("Bitcoin Limited Growth Model", "{Ʌ} -‌ LGS-S2F", true)

var a_step = 0.17, var b_step = 0.02, var c_step = 22.7
a = input.float(24.16, "Coefficient A", step = a_step)
b = input.float(0.397, "Coefficient B", step = b_step)
c = input.float(81.0, "Coefficient C", step = c_step)
stdev_len = input.int(200, "Stdev Length")
calc_mode = input.string("Normal", "Sigma Calculation Method", ["Normal", "Stdev"])

stock = request.security("GLASSNODE:BTC_SUPPLY", "D", close)
blocks = request.security("GLASSNODE:BTC_BLOCKS", "D", close)

f_stdev(src_,len) =>
    length = len
    pi = length / 1000
    src = src_ * float(1 + pi)
    mult = 3
    basis = ta.vwma(src, length)
    dev = mult * ta.stdev(src, length)
    
    basis - (0.236 * dev)

f_lg(a, b, c) =>
    1 / stock * ((c * math.pow(10, 12)) * math.exp((a * math.pow(3.64568 * math.pow(10, 6), b)) * (-math.pow(2, -4.7619 * math.pow(10, -6) * b * blocks) * math.pow(stock, -b))))

base = f_lg(a, b, c)

lh = f_lg(a + a_step, b + b_step, c + c_step)
hh = f_lg(a + a_step, b + 2 * b_step, c + c_step)
llh = f_lg(a, b, c + c_step)
hhl = f_lg(a, b, c - c_step)
hl = f_lg(a - a_step, b - b_step, c - c_step)
ll = f_lg(a - a_step, b - 2 * b_step, c - c_step)

switch calc_mode
    "Stdev" => 
        lh := f_stdev(lh, stdev_len)
        hh := f_stdev(hh, stdev_len)
        llh := f_stdev(llh, stdev_len)
        hhl := f_stdev(hhl, stdev_len)
        hl := f_stdev(hl, stdev_len)
        ll := f_stdev(ll, stdev_len)


p_b = plot(base, "", #ffffff80)
p_lh = plot(lh, "", #bb001081)
p_hh = plot(hh, "", #bb001081)
p_llh = plot(llh, "", #bb001081)
p_hhl = plot(hhl, "", #00b35169)
p_hl = plot(hl, "", #00b35169)
p_ll = plot(ll, "", #00b35169)

fill(p_ll, p_hl, #40ff4027)
fill(p_hl, p_hhl, #80ff8042)
fill(p_hhl, p_b, #c0ffc02f)
fill(p_b, p_llh, #ffc0c031)
fill(p_llh, p_lh, #ff5f5f3d)
fill(p_lh, p_hh, #ff40402d)
