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

//@version=5
indicator("VFIBs Agreement")
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jpwall

bars_vwma_st = input(title="VWMA (Short Term) Bars Back", defval = 50)
bars_vwma_lt = input(title="VWMA (Long Term) Bars Back", defval = 200)
bars_atr = input(title="ATR Bars Back", defval = 14)
fib_1 = input(title="Fibonacci Value #1", defval = 1.618)
fib_2 = input(title="Fibonacci Value #2", defval = 2.36)
fib_3 = input(title="Fibonacci Value #3", defval = 3.82)
fib_4 = input(title="Fibonacci Value #4", defval = 5)
fib_5 = input(title="Fibonacci Value #5", defval = 6.18)
fib_6 = input(title="Fibonacci Value #6", defval = 10)

overbought_line_lvl = input(title="Overbought Line Level", defval = 15)
oversold_line_lvl = input(title="Oversold Line Level", defval = -15)

vwma_st_sma = input(title="VWMA Fibonacci Level Range (Short Term) SMA Length", defval=7)
vwma_lt_sma = input(title="VWMA Fibonacci Level Range (Long Term) SMA Length", defval=7)

overbought_vwma_lvl = input(title="VWMA Fibonacci Level Range Overbought (Long Term) Value", defval=18)
overbought_vwma_lvl_extra = input(title="VWMA Fibonacci Level Range Extra Overbought (Long Term) Value", defval=19.5)
oversold_vwma_lvl = input(title="VWMA Fibonacci Level Range Oversold (Long Term) Value", defval=-18)
oversold_vwma_lvl_extra = input(title="VWMA Fibonacci Level Range Extra Oversold (Long Term) Value", defval=-19.5)

vwma_st_base_color = input.color(title="VWMA Fibonacci Level Range (Short Term) Color", defval=color.yellow)
vwma_lt_base_color = input.color(title="VWMA Fibonacci Level Range (Long Term) Color", defval=color.teal)
overbought_line_c = input.color(title="Overbought Line Color", defval=color.red)
oversold_line_c = input.color(title="Oversold Line Color", defval=color.green)
bg_transp = input(title="Background Transparency", defval=75)
long_vwma_oversold = input.color(title="VWMA Fibonacci Level Range Oversold (Long Term) Background", defval=color.green)
long_vwma_oversold_xtra = input.color(title="VWMA Fibonacci Level Range Extra Oversold (Long Term) Background", defval=color.lime)
long_vwma_overbought = input.color(title="VWMA Fibonacci Level Range Overbought (Long Term) Background", defval=color.red)
long_vwma_overbought_xtra = input.color(title="VWMA Fibonacci Level Range Extra Overbought (Long Term) Background", defval=color.maroon)
default_bg = input.color(title="Default Background", defval=color.gray)

mid = ta.vwma(math.avg(high, low, close), bars_vwma_lt)
mid_st = ta.vwma(math.avg(high, low, close), bars_vwma_st)
atr = ta.atr(bars_atr)

tf1 = (atr * fib_1) + mid
tf2 = (atr * fib_2) + mid
tf3 = (atr * fib_3) + mid
tf4 = (atr * fib_4) + mid
tf5 = (atr * fib_5) + mid
tf6 = (atr * fib_6) + mid
bf1 = mid - (atr * fib_1)
bf2 = mid - (atr * fib_2)
bf3 = mid - (atr * fib_3)
bf4 = mid - (atr * fib_4)
bf5 = mid - (atr * fib_5)
bf6 = mid - (atr * fib_6)

tf1_st = (atr * fib_1) + mid_st
tf2_st = (atr * fib_2) + mid_st
tf3_st = (atr * fib_3) + mid_st
tf4_st = (atr * fib_4) + mid_st
tf5_st = (atr * fib_5) + mid_st
tf6_st = (atr * fib_6) + mid_st
bf1_st = mid_st - (atr * fib_1)
bf2_st = mid_st - (atr * fib_2)
bf3_st = mid_st - (atr * fib_3)
bf4_st = mid_st - (atr * fib_4)
bf5_st = mid_st - (atr * fib_5)
bf6_st = mid_st - (atr * fib_6)

float avg = math.avg(high, low, open, close)

get_loc() =>
    val = -6 * math.log(1 * 5)
    if close < mid
        if close < bf1
            val := -6 * math.log(2 * 5)
        if close < bf2
            val := -6 * math.log(3 * 5)
        if close < bf3
            val := -6 * math.log(4 * 5)
        if close < bf4
            val := -6 * math.log(5 * 5)
        if close < bf5
            val := -6 * math.log(6 * 5)
        if close < bf6
            val := -6 * math.log(7 * 5)
    else
        val := 6 * math.log(7 * 5)
        if close < tf6
            val := 6 * math.log(6 * 5)
        if close < tf5
            val := 6 * math.log(5 * 5)
        if close < tf4
            val := 6 * math.log(4 * 5)
        if close < tf3
            val := 6 * math.log(3 * 5)
        if close < tf2
            val := 6 * math.log(2 * 5)
        if close < tf1
            val := 6 * math.log(1 * 5)
    val
    
get_loc_st() =>
    val = -6 * math.log(1 * 5)
    if close < mid_st
        if close < bf1_st
            val := -6 * math.log(2 * 5)
        if close < bf2_st
            val := -6 * math.log(3 * 5)
        if close < bf3_st
            val := -6 * math.log(4 * 5)
        if close < bf4_st
            val := -6 * math.log(5 * 5)
        if close < bf5_st
            val := -6 * math.log(6 * 5)
        if close < bf6_st
            val := -6 * math.log(7 * 5)
    else
        val := 6 * math.log(7 * 5)
        if close < tf6_st
            val := 6 * math.log(6 * 5)
        if close < tf5_st
            val := 6 * math.log(5 * 5)
        if close < tf4_st
            val := 6 * math.log(4 * 5)
        if close < tf3_st
            val := 6 * math.log(3 * 5)
        if close < tf2_st
            val := 6 * math.log(2 * 5)
        if close < tf1_st
            val := 6 * math.log(1 * 5)
    val
    
float long_sma = ta.sma(get_loc(), vwma_lt_sma)
float short_sma = ta.sma(get_loc_st(), vwma_st_sma)

color barc = if long_sma >= overbought_vwma_lvl_extra
    long_vwma_overbought_xtra
else if long_sma >= overbought_vwma_lvl
    long_vwma_overbought
else if long_sma >= 0
    default_bg
else if long_sma <= oversold_vwma_lvl_extra
    long_vwma_oversold_xtra
else if long_sma <= oversold_vwma_lvl
    long_vwma_oversold
else
    default_bg

plot(long_sma, color=vwma_lt_base_color, linewidth=3)
plot(short_sma, color=vwma_st_base_color, linewidth=2)

bgcolor(barc, transp=bg_transp)

hline(overbought_line_lvl, color=overbought_line_c, linewidth=2)
hline(0)
hline(oversold_line_lvl, color=oversold_line_c, linewidth=2)