Anchored VWAPs + Deviations
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jmorgado89
//Doctor's Den - The Castle

//@version=5
indicator(title="Anchored VWAPs + Deviations", shorttitle="AVWAP[J1]", overlay=true)

gpvwap1 = "Anchored Vwap"
ggextravwap1 = "Extra AVWAPs"
ggavwapdevs = "Deviations"

avwapon = input(true, "#1", group=gpvwap1, inline="1")
startcalcdateavwap = input.time(timestamp("21 Oct 2021 03:45"), "", confirm=false, group=gpvwap1, inline="1")
avwpsrc = input(hlc3, title='Src1', group=gpvwap1, inline="1")

avwapon2 = input(true, "#2", group=gpvwap1, inline="2")
startcalcdateavwap2 = input.time(timestamp("29 Oct 2021 07:45"), "", confirm=false, group=gpvwap1, inline="2")
avwapsrc2 = input(hlc3, title='Src2', group=gpvwap1, inline="2")

avwapon3 = input(true, "#3", group=gpvwap1, inline="3")
startcalcdateavwap3 = input.time(timestamp("23 Jun 2021 01:00"), "", confirm=false, group=gpvwap1, inline="3")
avwapsrc3 = input(hlc3, title='Src3', group=gpvwap1, inline="3")

avwapon4 = input(true, "#4", group=gpvwap1, inline="4")
startcalcdateavwap4 = input.time(timestamp("14 Apr 2021 18:00"), "", confirm=false, group=gpvwap1, inline="4")
avwapsrc4 = input(hlc3, title='Src4', group=gpvwap1, inline="4")

avwapon5 = input(true, "#5", group=gpvwap1, inline="5")
startcalcdateavwap5 = input.time(timestamp("02 Aug 2020 13:00"), "", confirm=false, group=gpvwap1, inline="5")
avwapsrc5 = input(hlc3, title='Src5', group=gpvwap1, inline="5")

avwapon6 = input(true, "#6", group=gpvwap1, inline="6")
startcalcdateavwap6 = input.time(timestamp("13 Mar 2020 12:00"), "", confirm=false, group=gpvwap1, inline="6")
avwapsrc6 = input(hlc3, title='Src6', group=gpvwap1, inline="6")


//DEVIATIONS

DeviationsONOFF = input(false, "ON/OFF", group=ggavwapdevs, inline="5")
AVW1on = input(true, title="1", group=ggavwapdevs, inline="5")
AVW2on = input(true, title="2", group=ggavwapdevs, inline="5")
AVW3on = input(true, title="3", group=ggavwapdevs, inline="5")
AVW4on = input(true, title="4", group=ggavwapdevs, inline="5")
SelectAVWAPdev = input.string(defval="#1", title="Deviation for:", options=["#1", "#2", "#3", "#4", "#5", "#6"], group=ggavwapdevs, inline="5")
mirrorOn = input(true, title="Mirror ratio", group=ggavwapdevs, inline="5", tooltip="Mirror on = Up and Down deviation ratios are equal(First line of ratios)")

AVW1 = input(0.66, title="1", group=ggavwapdevs, inline="6")
AVW2 = input(1.66, title="2", group=ggavwapdevs, inline="6")
AVW3 = input(2.66, title="3", group=ggavwapdevs, inline="6")
AVW4 = input(3.66, title="4", group=ggavwapdevs, inline="6")

AVW5a = input(1.500, title="1", group=ggavwapdevs, inline="7")
AVW6b = input(1.900, title="2", group=ggavwapdevs, inline="7")
AVW7c = input(2.7500, title="3", group=ggavwapdevs, inline="7")
AVW8d = input(3.700, title="4", group=ggavwapdevs, inline="7")
//

// Formulas {
avwap2ndformula = true//input(true, "Use 2nd formula?", group=gpvwap1, inline="3")

f_emaxavwap(_src, _len) =>
    _alpha = 2 / (_len + 1)
    _ema = 0.0
    _ema := _src * _alpha + nz(_ema[1]) * (1 - _alpha)
    _ema

f_vwapdeviations(_newint, _src, _vol) =>
    _count = 0
    _count := _newint ? 1 : _count[1] + 1
    _vwap = f_emaxavwap(_src * _vol, _count) / f_emaxavwap(_vol, _count)
    _pd = math.abs(_src - _vwap)
    _a = 2 / (_count + 1)
    _dev = 0.0
    _dev := _newint ? 0.0 : _pd * _a + nz(_dev[1]) * (1 - _a)
    [_vwap, _dev]

// FORMULA 2
//1
before_time_range = time < startcalcdateavwap
start_time_range = time >= startcalcdateavwap and time[1] < startcalcdateavwap

sumSrc = avwpsrc * volume
sumVol = volume
sumSrc := start_time_range ? sumSrc : sumSrc + sumSrc[1]
sumVol := start_time_range ? sumVol : sumVol + sumVol[1]
avwap2nd = sumSrc / sumVol

//2
t2 = time < startcalcdateavwap2
start2 = time >= startcalcdateavwap2 and time[1] < startcalcdateavwap2

sumavwapsrc2 = avwapsrc2 * volume
sumVol2 = volume
sumavwapsrc2 := start2 ? sumavwapsrc2 : sumavwapsrc2 + sumavwapsrc2[1]
sumVol2 := start2 ? sumVol2 : sumVol2 + sumVol2[1]
avwap2nd2 = sumavwapsrc2 / sumVol2

//3
t3 = time < startcalcdateavwap3
start3 = time >= startcalcdateavwap3 and time[1] < startcalcdateavwap3

sumavwapsrc3 = avwapsrc3 * volume
sumVol3 = volume
sumavwapsrc3 := start3 ? sumavwapsrc3 : sumavwapsrc3 + sumavwapsrc3[1]
sumVol3 := start3 ? sumVol3 : sumVol3 + sumVol3[1]
avwap2nd3 = sumavwapsrc3 / sumVol3

//4
t4 = time < startcalcdateavwap4
start4 = time >= startcalcdateavwap4 and time[1] < startcalcdateavwap4

sumavwapsrc4 = avwapsrc4 * volume
sumVol4 = volume
sumavwapsrc4 := start4 ? sumavwapsrc4 : sumavwapsrc4 + sumavwapsrc4[1]
sumVol4 := start4 ? sumVol4 : sumVol4 + sumVol4[1]
avwap2nd4 = sumavwapsrc4 / sumVol4

//5
t5 = time < startcalcdateavwap5
start5 = time >= startcalcdateavwap5 and time[1] < startcalcdateavwap5

sumavwapsrc5 = avwapsrc5 * volume
sumVol5 = volume
sumavwapsrc5 := start5 ? sumavwapsrc5 : sumavwapsrc5 + sumavwapsrc5[1]
sumVol5 := start5 ? sumVol5 : sumVol5 + sumVol5[1]
avwap2nd5 = sumavwapsrc5 / sumVol5

//6
t6 = time < startcalcdateavwap6
start6 = time >= startcalcdateavwap6 and time[1] < startcalcdateavwap6

sumavwapsrc6 = avwapsrc6 * volume
sumVol6 = volume
sumavwapsrc6 := start6 ? sumavwapsrc6 : sumavwapsrc6 + sumavwapsrc6[1]
sumVol6 := start6 ? sumVol6 : sumVol6 + sumVol6[1]
avwap2nd6 = sumavwapsrc6 / sumVol6

// }

//
whichstarttime = SelectAVWAPdev == "#1" ? start_time_range : SelectAVWAPdev == "#2" ? start2 : SelectAVWAPdev == "#3" ? start3 : SelectAVWAPdev == "#4" ? start4 : SelectAVWAPdev == "#5" ? start5 : SelectAVWAPdev == "#6" ? start6 :start_time_range
[avwap, sddev] = f_vwapdeviations(whichstarttime, avwpsrc, volume)

whichAVWAP = SelectAVWAPdev == "#1" ? avwap2nd : SelectAVWAPdev == "#2" ? avwap2nd2 : SelectAVWAPdev == "#3" ? avwap2nd3 : SelectAVWAPdev == "#4" ? avwap2nd4 : SelectAVWAPdev == "#5" ? avwap2nd5 : SelectAVWAPdev == "#6" ? avwap2nd6 : avwap2nd
formulaswitch = whichAVWAP

aavwsdev1_pos = formulaswitch + sddev * AVW1
aavwsdev1_neg = mirrorOn ? formulaswitch - sddev * AVW1 : formulaswitch - sddev * AVW5a

aavwsdev2_pos = formulaswitch + sddev * AVW2
aavwsdev2_neg = mirrorOn ? formulaswitch - sddev * AVW2 : formulaswitch - sddev * AVW6b

aavwsdev3_pos = formulaswitch + sddev * AVW3
aavwsdev3_neg = mirrorOn ? formulaswitch - sddev * AVW3 : formulaswitch - sddev * AVW7c

aavwsdev4_pos = formulaswitch + sddev * AVW4
aavwsdev4_neg = mirrorOn ? formulaswitch - sddev * AVW4 : formulaswitch - sddev * AVW8d


// Plot shit {
plot(AVW4on and DeviationsONOFF ? aavwsdev4_pos : na, color=color.new(#1677C5, 30))
plot(AVW3on and DeviationsONOFF ? aavwsdev3_pos : na, color=color.new(#3DBDFF, 30))
plot(AVW2on and DeviationsONOFF ? aavwsdev2_pos : na, color=color.new(#64EEFA, 30))
plot(AVW1on and DeviationsONOFF ? aavwsdev1_pos : na, color=color.new(#A4FFEC, 30))

plot(avwapon ? avwap2nd   : na, "AVWAP1", color=color.green, linewidth=2)
plot(avwapon2 ? avwap2nd2 : na, title='AVWAP2', color=color.blue, linewidth=2)
plot(avwapon3 ? avwap2nd3 : na, title='AVWAP3', color=color.yellow, linewidth=2)
plot(avwapon4 ? avwap2nd4 : na, title='AVWAP4', color=color.red, linewidth=2)
plot(avwapon5 ? avwap2nd5 : na, title='AVWAP5', color=color.fuchsia, linewidth=2)
plot(avwapon6 ? avwap2nd6 : na, title='AVWAP6', color=color.purple, linewidth=2)

plot(AVW1on and DeviationsONOFF ? aavwsdev1_neg : na, color=color.new(#A4FFEC, 30))
plot(AVW2on and DeviationsONOFF ? aavwsdev2_neg : na, color=color.new(#64EEFA, 30))
plot(AVW3on and DeviationsONOFF ? aavwsdev3_neg : na, color=color.new(#3DBDFF, 30))
plot(AVW4on and DeviationsONOFF ? aavwsdev4_neg : na, color=color.new(#1677C5, 30))

/// }






