MTF Vegas tunnel & pivots 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

// Thanks to benchch for his Major Minor Fib Points https://www.tradingview.com/script/9utjGwHh-Major-Minor-Fib-Points/
// I especially want to thank TradingView for its platform that facilitates development and learning.

// NOTES:
//  - Use vegas tunnel to identify trend, support, resistance
//  - Use major & minor points to draw fib
//  - Use key fibs level & mid vegas to enter a trade

//@version=4
study("MTF Vegas tunnel & pivots ", overlay=true)

// VEGAS1

_1 = input(true, "Display tunnel 1", group="Tunnel 1min")
ma_len = input(title="", type=input.integer, defval=144, inline="1", group="Tunnel 1min")
src = input(title="/", type=input.source, defval=close, inline="1", group="Tunnel 1min")
ma_offset = input(title="/", type=input.integer, defval=0, inline="1", group="Tunnel 1min")
res = input(title="/", type=input.resolution, defval="1", inline="1", group="Tunnel 1min")
htf_ma = ema(src, ma_len)
out1=security(syminfo.tickerid, res, htf_ma)
plotData1 = if (_1 == true)
    out1
else
    na
plot_1 = plot(plotData1, color=color.new(#00CED1, 100), offset=ma_offset, title='144 VG1')


ma_len2 = input(title="", type=input.integer, defval=169, inline="1b", group="Tunnel 1min")
src2 = input(title="/", type=input.source, defval=close, inline="1b", group="Tunnel 1min")
ma_offset2 = input(title="/", type=input.integer, defval=0, inline="1b", group="Tunnel 1min")
res2 = input(title="/", type=input.resolution, defval="1", inline="1b", group="Tunnel 1min")
htf_ma2 = ema(src2, ma_len2)
out2 = security(syminfo.tickerid, res2, htf_ma2) 
plotData2 = if (_1 == true)
    out2
else
    na
plot_2 = plot(plotData2,color=color.new(#00CED1, 60), offset=ma_offset2, title='169 VG1')
 
ma_len3 = input(title="", type=input.integer, defval=233, inline="1c", group="Tunnel 1min")
src3 = input(title="/", type=input.source, defval=close, inline="1c", group="Tunnel 1min")
ma_offset3 = input(title="/", type=input.integer, defval=0, inline="1c", group="Tunnel 1min")
res3 = input(title="/", type=input.resolution, defval="1", inline="1c", group="Tunnel 1min")
htf_ma3 = ema(src3, ma_len3)
out3 = security(syminfo.tickerid, res3, htf_ma3)
plotData3 = if (_1 == true)
    out3
else
    na
plot_3 = plot(plotData3, color=color.new(#00CED1, 100), offset=ma_offset3, title='233 VG1')
fill(plot_1, plot_3, color=color.new(#00CED1, 90), editable=true)
 
 
// VEGAS2
 
_2 = input(true, "Display tunnel 5", group="Tunnel 5min")
ma_len4 = input(title="", type=input.integer, defval=144, inline="2", group="Tunnel 5min")
src4 = input(title="/", type=input.source, defval=close, inline="2", group="Tunnel 5min")
ma_offset4 = input(title="/", type=input.integer, defval=0, inline="2", group="Tunnel 5min")
res4 = input(title="/", type=input.resolution, defval="5", inline="2", group="Tunnel 5min")
htf_ma4 = ema(src4, ma_len4)
out4 = security(syminfo.tickerid, res4, htf_ma4)
plotData4 = if (_2 == true)
    out4
else
    na
plot_4 = plot(plotData4, color=color.new(#00BFFF, 100), offset=ma_offset4, title='144 VG2')
 
ma_len5 = input(title="", type=input.integer, defval=169, inline="2b", group="Tunnel 5min")
src5 = input(title="/", type=input.source, defval=close, inline="2b", group="Tunnel 5min")
ma_offset5 = input(title="/", type=input.integer, defval=0, inline="2b", group="Tunnel 5min")
res5 = input(title="/", type=input.resolution, defval="5", inline="2b", group="Tunnel 5min")
htf_ma5 = ema(src5, ma_len5)
out5 = security(syminfo.tickerid, res5, htf_ma5)
plotData5 = if (_2 == true)
    out5
else
    na
plot_5 = plot(plotData5, color=color.new(#00BFFF, 60), offset=ma_offset5, title='169 VG2')
 
ma_len6 = input(title="", type=input.integer, defval=233, inline="2c", group="Tunnel 5min")
src6 = input(title="/", type=input.source, defval=close, inline="2c", group="Tunnel 5min")
ma_offset6 = input(title="/", type=input.integer, defval=0, inline="2c", group="Tunnel 5min")
res6 = input(title="/", type=input.resolution, defval="5", inline="2c", group="Tunnel 5min")
htf_ma6 = ema(src6, ma_len6)
out6 = security(syminfo.tickerid, res6, htf_ma6)
plotData6 = if (_2 == true)
    out6
else
    na
plot_6 = plot(plotData6, color=color.new(#00BFFF, 100), offset=ma_offset6, title='233 VG2')
fill(plot_4, plot_6, color=color.new(#00BFFF, 90), editable=true)
 
// VEGAS3
 
_3 = input(true, "Display tunnel 15", group="Tunnel 15min")
ma_len7 = input(title="", type=input.integer, defval=144, inline="3", group="Tunnel 15min")
src7 = input(title="/", type=input.source, defval=close, inline="3", group="Tunnel 15min")
ma_offset7 = input(title="/", type=input.integer, defval=0, inline="3", group="Tunnel 15min")
res7 = input(title="/", type=input.resolution, defval="15", inline="3", group="Tunnel 15min")
htf_ma7 = ema(src7, ma_len7)
out7 = security(syminfo.tickerid, res7, htf_ma7)
plotData7 = if (_3 == true)
    out7
else
    na
plot_7 = plot(plotData7, color=color.new(#4682B4, 100), offset=ma_offset7, title='144 VG3')
 
ma_len8 = input(title="", type=input.integer, defval=169, inline="3b", group="Tunnel 15min")
src8 = input(title="/", type=input.source, defval=close, inline="3b", group="Tunnel 15min")
ma_offset8 = input(title="/", type=input.integer, defval=0, inline="3b", group="Tunnel 15min")
res8 = input(title="/", type=input.resolution, defval="15", inline="3b", group="Tunnel 15min")
htf_ma8 = ema(src8, ma_len8)
out8 = security(syminfo.tickerid, res8, htf_ma8)
plotData8 = if (_3 == true)
    out8
else
    na
plot_8 = plot(plotData8, color=color.new(#4682B4, 60), offset=ma_offset8, title='169 VG3')
 
ma_len9 = input(title="", type=input.integer, defval=233, inline="3c", group="Tunnel 15min")
src9 = input(title="/", type=input.source, defval=close, inline="3c", group="Tunnel 15min")
ma_offset9 = input(title="/", type=input.integer, defval=0, inline="3c", group="Tunnel 15min")
res9 = input(title="/", type=input.resolution, defval="15", inline="3c", group="Tunnel 15min")
htf_ma9 = ema(src9, ma_len9)
out9 = security(syminfo.tickerid, res9, htf_ma9)
plotData9 = if (_3 == true)
    out9
else
    na
plot_9 = plot(plotData9, color=color.new(#4682B4, 100), offset=ma_offset9, title='233 VG3')
fill(plot_7, plot_9, color=color.new(#4682B4, 90), editable=true)
 
// VEGAS4
 
_4 = input(true, "Display tunnel H1", group="Tunnel H1")
ma_len10 = input(title="", type=input.integer, defval=144, inline="4", group="Tunnel H1")
src10 = input(title="/", type=input.source, defval=close, inline="4", group="Tunnel H1")
ma_offset10 = input(title="/", type=input.integer, defval=0, inline="4", group="Tunnel H1")
res10 = input(title="/", type=input.resolution, defval="60", inline="4", group="Tunnel H1")
htf_ma10 = ema(src10, ma_len10)
out10 = security(syminfo.tickerid, res10, htf_ma10)
plotData10 = if (_4 == true)
    out10
else
    na
plot_10 = plot(plotData10, color=color.new(#40E0D0, 100), offset=ma_offset10, title='144 VG4')
 
ma_len11 = input(title="", type=input.integer, defval=169, inline="4b", group="Tunnel H1")
src11 = input(title="/", type=input.source, defval=close, inline="4b", group="Tunnel H1")
ma_offset11 = input(title="/", type=input.integer, defval=0, inline="4b", group="Tunnel H1")
res11 = input(title="/", type=input.resolution, defval="60", inline="4b", group="Tunnel H1")
htf_ma11 = ema(src11, ma_len11)
out11 = security(syminfo.tickerid, res11, htf_ma11)
plotData11 = if (_4 == true)
    out11
else
    na
plot_11 = plot(plotData11, color=color.new(#40E0D0, 60), offset=ma_offset11, title='169 VG4')
 
ma_len12 = input(title="", type=input.integer, defval=233, inline="4c", group="Tunnel H1")
src12 = input(title="/", type=input.source, defval=close, inline="4c", group="Tunnel H1")
ma_offset12 = input(title="/", type=input.integer, defval=0, inline="4c", group="Tunnel H1")
res12 = input(title="/", type=input.resolution, defval="60", inline="4c", group="Tunnel H1")
htf_ma12 = ema(src12, ma_len12)
out12 = security(syminfo.tickerid, res12, htf_ma12)
plotData12 = if (_4 == true)
    out12
else
    na
plot_12 = plot(plotData12, color=color.new(#40E0D0, 100), offset=ma_offset12, title='233 VG4')
fill(plot_10, plot_12, color=color.new(#40E0D0, 90), editable=true)
 
// VEGAS5
 
_5 = input(true, "Display tunnel H4", group="Tunnel H4")
ma_len13 = input(title="", type=input.integer, defval=144, inline="5", group="Tunnel H4")
src13 = input(title="/", type=input.source, defval=close, inline="5", group="Tunnel H4")
ma_offset13 = input(title="/", type=input.integer, defval=0, inline="5", group="Tunnel H4")
res13 = input(title="/", type=input.resolution, defval="240", inline="5", group="Tunnel H4")
htf_ma13 = ema(src13, ma_len13)
out13 = security(syminfo.tickerid, res13, htf_ma13)
plotData13 = if (_5 == true)
    out13
else
    na
plot_13 = plot(plotData13, color=color.new(#40E0D0, 100), offset=ma_offset13, title='144 VG5')
 
ma_len14 = input(title="", type=input.integer, defval=169, inline="5b", group="Tunnel H4")
src14 = input(title="/", type=input.source, defval=close, inline="5b", group="Tunnel H4")
ma_offset14 = input(title="/", type=input.integer, defval=0, inline="5b", group="Tunnel H4")
res14 = input(title="/", type=input.resolution, defval="240", inline="5b", group="Tunnel H4")
htf_ma14 = ema(src14, ma_len14)
out14 = security(syminfo.tickerid, res14, htf_ma14)
plotData14 = if (_5 == true)
    out14
else
    na
plot_14 = plot(plotData14, color=color.new(#40E0D0, 60), offset=ma_offset14, title='169 VG5')
 
ma_len15 = input(title="", type=input.integer, defval=233, inline="5c", group="Tunnel H4")
src15 = input(title="/", type=input.source, defval=close, inline="5c", group="Tunnel H4")
ma_offset15 = input(title="/", type=input.integer, defval=0, inline="5c", group="Tunnel H4")
res15 = input(title="/", type=input.resolution, defval="240", inline="5c", group="Tunnel H4")
htf_ma15 = ema(src15, ma_len15)
out15 = security(syminfo.tickerid, res15, htf_ma15)
plotData15 = if (_5 == true)
    out15
else
    na
plot_15 = plot(plotData15, color=color.new(#40E0D0, 100), offset=ma_offset15, title='233 VG5')
fill(plot_13, plot_15, color=color.new(#40E0D0, 90), editable=true)
 
// VEGAS6
 
_6 = input(true, "Display tunnel D", group="Tunnel D")
ma_len16 = input(title="", type=input.integer, defval=144, inline="6", group="Tunnel D")
src16 = input(title="/", type=input.source, defval=close, inline="6", group="Tunnel D")
ma_offset16 = input(title="/", type=input.integer, defval=0, inline="6", group="Tunnel D")
res16 = input(title="/", type=input.resolution, defval="1D", inline="6", group="Tunnel D")
htf_ma16 = ema(src16, ma_len16)
out16 = security(syminfo.tickerid, res16, htf_ma16)
plotData16 = if (_6 == true)
    out16
else
    na
plot_16 = plot(plotData16, color=color.new(#40E0D0, 100), offset=ma_offset16, title='144 VG6')
 
ma_len17 = input(title="", type=input.integer, defval=169, inline="6b", group="Tunnel D")
src17 = input(title="/", type=input.source, defval=close, inline="6b", group="Tunnel D")
ma_offset17 = input(title="/", type=input.integer, defval=0, inline="6b", group="Tunnel D")
res17 = input(title="/", type=input.resolution, defval="1D", inline="6b", group="Tunnel D")
htf_ma17 = ema(src17, ma_len17)
out17 = security(syminfo.tickerid, res17, htf_ma17)
plotData17 = if (_6 == true)
    out17
else
    na
plot_17 = plot(plotData17, color=color.new(#40E0D0, 60), offset=ma_offset17, title='169 VG6')
 
ma_len18 = input(title="", type=input.integer, defval=233, inline="6c", group="Tunnel D")
src18 = input(title="/", type=input.source, defval=close, inline="6c", group="Tunnel D")
ma_offset18 = input(title="/", type=input.integer, defval=0, inline="6c", group="Tunnel D")
res18 = input(title="/", type=input.resolution, defval="1D", inline="6c", group="Tunnel D")
htf_ma18 = ema(src18, ma_len18)
out18 = security(syminfo.tickerid, res18, htf_ma18)
plotData18 = if (_6 == true)
    out18
else
    na
plot_18 = plot(plotData18, color=color.new(#40E0D0, 100), offset=ma_offset18, title='233 VG6')
fill(plot_16, plot_18, color=color.new(#40E0D0, 90), editable=true)


//------- Liquidity

h_left_min = input(title="Minor Value", type=input.integer, defval=26, group="Liquidity", inline="liquidity")

h_left_low_min = lowest(h_left_min)
h_left_high_min = highest(h_left_min)
newlow_min = low <= h_left_low_min
newhigh_min = high >= h_left_high_min

central_bar_low_min = low[h_left_min]
central_bar_high_min = high[h_left_min]
full_zone_low_min = lowest(h_left_min * 2)
full_zone_high_min = highest(h_left_min * 2)
highest_bar_min = central_bar_high_min >= full_zone_high_min
lowest_bar_min = central_bar_low_min <= full_zone_low_min
plotshape(highest_bar_min ? -1 : 0, offset=-h_left_min, style=shape.circle, location=location.abovebar, color=color.white) //, size=size.small)
plotshape(lowest_bar_min ? 1 : 0, offset=-h_left_min, style=shape.circle, location=location.belowbar, color=color.white) //, size=size.small)


h_left = input(title="Major Value", type=input.integer, defval=31, group="Liquidity", inline="liquidity")

h_left_low = lowest(h_left)
h_left_high = highest(h_left)
newlow = low <= h_left_low
newhigh = high >= h_left_high

central_bar_low = low[h_left]
central_bar_high = high[h_left]
full_zone_low = lowest(h_left * 2)
full_zone_high = highest(h_left * 2)
highest_bar = central_bar_high >= full_zone_high
lowest_bar = central_bar_low <= full_zone_low
plotshape(highest_bar ? -1 : 0, offset=-h_left, style=shape.circle, location=location.abovebar, color=color.blue) //, size=size.small)
plotshape(lowest_bar ? 1 : 0, offset=-h_left, style=shape.circle, location=location.belowbar, color=color.blue) //, size=size.small)

//--------- BB% crossing up (lower band) or down (upper band)

length = input(14, type=input.integer, minval=1, group="BB %")
srcSignal = input(close, title="Source", group="BB %")
mult = input(2.0, type=input.float, minval=0.001, maxval=50, title="StdDev", group="BB %")
basis = sma(srcSignal, length)
dev = mult * stdev(srcSignal, length)
upper = basis + dev
lower = basis - dev
bbr = (srcSignal - lower)/(upper - lower)

upperBand = input(0.80, type=input.float, title="Upper band signal", group="BB %")
lowerBand = input(0.20, type=input.float, title="Lower band signal", group="BB %")

plotshape(crossunder(bbr, upperBand), style=shape.triangledown, location=location.abovebar, color=color.purple) //, size=size.small)
plotshape(crossover(bbr, lowerBand), style=shape.triangleup, location=location.belowbar, color=color.purple) //, size=size.small)
