kpatel 5 min orb bollinger band with vwap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sskcharts
//@version=5
indicator(title='ORB-PreDay_PerM_Levels', shorttitle='ORB-PreDay_PerM_Levels', overlay=true)

//User Input
showORB = input(true, title='Show ORB', inline='1')
showORBExt = input(true, title='Show ORB Extension (Select Ext Below)', inline='1')
showExt1 = input(true, title='', inline='2')
valueExt1 = input.float(161.8, "%", inline='2', options = [127.2, 161.8, 200, 261.8])
showExt2 = input(true, "", inline='2')
valueExt2 = input.float(261.8, "%", inline='2', options = [127.2, 161.8, 200, 261.8])
showExt3 = input(true, "", inline='2')
valueExt3 = input.float(200, "%", inline='2', options = [127.2, 161.8, 200, 261.8])
showPMHL = input(true, title='Show Pre-Market H/L', inline='3') 
aftermarket = input(title='Include After-Market', defval=false, inline='3')
showPrevDayHighLow = input(true, title='Show previous day\'s High & Low')
showPrev2DayHighLow = input(false, title='Show previous 2+ days High & Low')
showPrevDayClose = input(true, title='Show previous day\'s Close')
orbTimeFrame = input.timeframe("15", title='ORB timeframe')
sessSpec = input.session('0930-1600', title='Session time')
ORBHColor = input.color(title='ORBH Color', defval=color.new(#0bd68a, 30), inline='10')
ORBLColor = input.color(title='ORBL Color', defval=color.new(#d3396d, 30), inline='10')
PDHColor = input.color(title='PDH Color', defval=color.new(#81c784, 70), inline='11')
PDLColor = input.color(title='PDL Color', defval=color.new(#f06292, 75), inline='11')
PDCColor = input.color(title='PDC Color', defval=color.new(#2196f3, 20), inline='12')
ORBExtColor = input.color(title='ORBExt Color', defval=color.new(#5d606b, 50), inline='13')
PMHLColor = input.color(title='PMHL Color', defval=color.new(#ff9800, 10), inline='14')

plotSignals = input(false, title='Show Buy/Sell Signal', inline='15')

ORBWidth = input.int(title='', defval=1, minval=1, inline='10')
PDHLWidth = input.int(title='', defval=3, minval=1, inline='11')
PDCWidth = input.int(title='', defval=1, minval=1, inline='12')
ORBExtWidth = input.int(title='', defval=2, minval=1, inline='13')
PMHLWidth = input.int(title='', defval=1, minval=1, inline='14')
ORBOption = input.string(title='', options=['solid', 'dotted', 'dashed'], defval='solid', inline='10')
PDHLOption = input.string(title='', options=['solid', 'dotted', 'dashed'], defval='solid', inline='11')
PDCOption = input.string(title='', options=['solid', 'dotted', 'dashed'], defval='dashed', inline='12')
ORBExtOption = input.string(title='', options=['solid', 'dotted', 'dashed'], defval='solid', inline='13')
PMHLOption = input.string(title='', options=['solid', 'dotted', 'dashed'], defval='dashed', inline='14')
ORBStyle = ORBOption == 'dotted' ? line.style_dotted : ORBOption == 'dashed' ? line.style_dashed : line.style_solid
PDHLStyle = PDHLOption == 'dotted' ? line.style_dotted : PDHLOption == 'dashed' ? line.style_dashed : line.style_solid
PDCStyle = PDCOption == 'dotted' ? line.style_dotted : PDCOption == 'dashed' ? line.style_dashed : line.style_solid
ORBExtStyle = ORBExtOption == 'dotted' ? line.style_dotted : ORBExtOption == 'dashed' ? line.style_dashed : line.style_solid
PMHLStyle = PMHLOption == 'dotted' ? line.style_dotted : PMHLOption == 'dashed' ? line.style_dashed : line.style_solid


// Get ORB
t = ticker.new(syminfo.prefix, syminfo.ticker)
getSeries(_e, _timeFrame) =>
    request.security(t, _timeFrame, _e, lookahead=barmerge.lookahead_on)

is_newbar(res, sess) =>
    t1 = time(res, sess)
    na(t1[1]) and not na(t1) or t1[1] < t1

newbar = is_newbar('1440', sessSpec)

var float orbH = na
var float orbL = na
if newbar
    orbH := getSeries(high[0], orbTimeFrame)
    orbH
    orbL := getSeries(low[0], orbTimeFrame)
    orbL

// Get ORB Extension

ORWidth = orbH-orbL
orb_ext_up1 = orbH + ORWidth*((valueExt1/100)-1)
orb_ext_down1 = orbL - ORWidth*((valueExt1/100)-1)
orb_ext_up2= orbH + ORWidth*((valueExt2/100)-1)
orb_ext_down2 = orbL - ORWidth*((valueExt2/100)-1)
orb_ext_up3= orbH + ORWidth*((valueExt3/100)-1)
orb_ext_down3 = orbL - ORWidth*((valueExt3/100)-1)

//Previous  Days High Low
PrevDayHigh = getSeries(high[1], 'D')
PrevDayLow = getSeries(low[1], 'D')
PrevDayOpen = getSeries(open[1], 'D')
PrevDayClose = getSeries(close[1], 'D')

//Previous 2 Days High Low
PrevDayHigh1 = getSeries(high[2], 'D') > PrevDayHigh ? getSeries(high[2], 'D') : getSeries(high[3], 'D') > PrevDayHigh ? getSeries(high[3], 'D') : na
PrevDayLow1 = getSeries(low[2], 'D') < PrevDayLow ? getSeries(low[2], 'D') : getSeries(low[3], 'D') < PrevDayLow ? getSeries(low[3], 'D') : na
PrevDayHigh2 = PrevDayHigh1 == getSeries(high[2], 'D') ? getSeries(high[3], 'D') > getSeries(high[2], 'D') ? getSeries(high[3], 'D') : getSeries(high[4], 'D') > getSeries(high[3], 'D') ? getSeries(high[4], 'D') : na : na
PrevDayLow2 = PrevDayLow1 == getSeries(low[2], 'D') ? getSeries(low[3], 'D') < getSeries(low[2], 'D') ? getSeries(low[3], 'D') : getSeries(low[4], 'D') < getSeries(low[3], 'D') ? getSeries(low[4], 'D') : na : na


//PM High Low
ending_hour = 9
ending_minute = 30
// LastOnly = input(title="Last only", type=input.bool, defval=false)

pmt = aftermarket == false ? time('1440', '0000-1530:23456') : time('1440', '1600-0000:23456')  
is_first = na(pmt[1]) and not na(pmt) or pmt[1] < pmt

pm_high = float(na)
pm_low = float(na)
k = int(na)

if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
    pm_high := high
    pm_low := low
    pm_low
else
    pm_high := pm_high[1]
    pm_low := pm_low[1]
    pm_low

if high > pm_high and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
    pm_high := high
    pm_high

if low < pm_low and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
    pm_low := low
    pm_low
    
    
// Today's Session Start timestamp
y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)
d1 = d - 4

// Start & End time for Today
start = timestamp(y, m, d, 09, 00)
pmstart = timestamp(y, m, d, 04, 00)
start1 = timestamp(y, m, d1, 09, 30)
end = start + 86400000
pmend = start + 36000000
daysLeft = 0

_MilliSec_In_Day = 24 * 60 * 60 * 1000
if (60*hour(timenow)+minute(timenow)) < (60*hour(time)+minute(time))
    daysLeft := math.floor((timenow - time) / _MilliSec_In_Day)+1
else
    daysLeft := math.floor((timenow - time) / _MilliSec_In_Day)

//if daysLeft == 0
//    daysLeft := daysLeft+1
    
orbstart = timestamp(y, m, d, 09, 00)
orbend = orbstart + 25200000

if time < timestamp(y, m, d, 09, 30)
    orbstart := timestamp(y, m, d-daysLeft, 09, 00)
    orbend := orbstart + 25200000
else
    orbstart := timestamp(y, m, d, 09, 00)
    orbend := orbstart + 25200000

if time < timestamp(y, m, d, 04, 00)
    pmstart := timestamp(y, m, d-daysLeft, 04, 00)
    pmend := pmstart + 43200000
else
    pmstart := timestamp(y, m, d, 04, 00)
    pmend := pmstart + 43200000

// Plot only if session started
isToday = timenow > start

// Plot selected timeframe's High, Low & Avg
// Plot lines
if isToday
    if showORB
        _h = line.new(orbstart, orbH, orbend, orbH, xloc.bar_time, color=ORBHColor, style=ORBStyle, width=ORBWidth)
        line.delete(_h[1])
        _l = line.new(orbstart, orbL, orbend, orbL, xloc.bar_time, color=ORBLColor, style=ORBStyle, width=ORBWidth)
        line.delete(_l[1])
    if showORBExt 
        if showExt1
            _orb_ext_up1 = line.new(orbstart, orb_ext_up1, orbend, orb_ext_up1, xloc.bar_time, color=ORBExtColor, style=ORBExtStyle, width=ORBExtWidth)
            line.delete(_orb_ext_up1[1])
            _orb_ext_down1 = line.new(orbstart, orb_ext_down1, orbend, orb_ext_down1, xloc.bar_time, color=ORBExtColor, style=ORBExtStyle, width=ORBExtWidth)
            line.delete(_orb_ext_down1[1])
        if showExt2
            _orb_ext_up2 = line.new(orbstart, orb_ext_up2, orbend, orb_ext_up2, xloc.bar_time, color=ORBExtColor, style=ORBExtStyle, width=ORBExtWidth)
            line.delete(_orb_ext_up2[1])
            _orb_ext_down2 = line.new(orbstart, orb_ext_down2, orbend, orb_ext_down2, xloc.bar_time, color=ORBExtColor, style=ORBExtStyle, width=ORBExtWidth)
            line.delete(_orb_ext_down2[1])
        if showExt3
            _orb_ext_up3 = line.new(orbstart, orb_ext_up3, orbend, orb_ext_up3, xloc.bar_time, color=ORBExtColor, style=ORBExtStyle, width=ORBExtWidth)
            line.delete(_orb_ext_up3[1])
            _orb_ext_down3 = line.new(orbstart, orb_ext_down3, orbend, orb_ext_down3, xloc.bar_time, color=ORBExtColor, style=ORBExtStyle, width=ORBExtWidth)
            line.delete(_orb_ext_down3[1])
        
    if showPrevDayHighLow
        _pdh = line.new(start1, PrevDayHigh, end, PrevDayHigh, xloc.bar_time, color=PDHColor, style=PDHLStyle, width=PDHLWidth)
        line.delete(_pdh[1])
        _pdl = line.new(start1, PrevDayLow, end, PrevDayLow, xloc.bar_time, color=PDLColor, style=PDHLStyle, width=PDHLWidth)
        line.delete(_pdl[1])
    if showPrevDayClose
        _pdc = line.new(orbstart, PrevDayClose, orbend, PrevDayClose, xloc.bar_time, color=PDCColor, style=PDCStyle, width=PDCWidth)
        line.delete(_pdc[1])

    if showPrev2DayHighLow
        _p2dh1 = line.new(start1, PrevDayHigh1, end, PrevDayHigh1, xloc.bar_time, color=PDHColor, style=PDHLStyle, width=PDHLWidth)
        line.delete(_p2dh1[1])
    if showPrev2DayHighLow
        _p2dl1 = line.new(start1, PrevDayLow1, end, PrevDayLow1, xloc.bar_time, color=PDLColor, style=PDHLStyle, width=PDHLWidth)
        line.delete(_p2dl1[1])
    if showPrev2DayHighLow
        _p2dh2 = line.new(start1, PrevDayHigh2, end, PrevDayHigh2, xloc.bar_time, color=PDHColor, style=PDHLStyle, width=PDHLWidth)
        line.delete(_p2dh2[1])
    if showPrev2DayHighLow
        _p2dl2 = line.new(start1, PrevDayLow2, end, PrevDayLow2, xloc.bar_time, color=PDLColor, style=PDHLStyle, width=PDHLWidth)
        line.delete(_p2dl2[1])

if timenow > pmstart 
    if showPMHL
        _pmh = line.new(pmstart, pm_high, pmend, pm_high, xloc.bar_time, color=PMHLColor, style=PMHLStyle, width=PMHLWidth)
        line.delete(_pmh[1])
        _pml = line.new(pmstart, pm_low, pmend, pm_low, xloc.bar_time, color=PMHLColor, style=PMHLStyle, width=PMHLWidth)
        line.delete(_pml[1])

plotshape(plotSignals ? ta.crossover(close, orbH) : na, style=shape.triangleup, location=location.belowbar, color=color.new(color.blue, 0), text='Buy', textcolor=color.new(color.blue, 0))
plotshape(plotSignals ? ta.crossover(orbL, close) : na, style=shape.triangledown, location=location.abovebar, color=color.new(color.blue, 0), text='Sell', textcolor=color.new(color.blue, 0))


//indicator(title="Bollinger Bands", overlay=true, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#363A45, offset = offset)
p1 = plot(upper, "Upper", color=#363A45, offset = offset)
p2 = plot(lower, "Lower", color=#363A45, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

plotvwap = input(title="plot VWAP", defval=true)

//////////SMA//////////
plotema5 = input(title="plot 5 SMA", defval=true)
sma5 = ta.sma(close, 5)
plot(plotema5 ==true ? sma5 : na, "5 SMA", color=#fcc703)

plotema9 = input(title="plot 9 SMA", defval=true)
sma9 = ta.sma(high, 9)
plot(plotema9 ==true ? sma9 : na, "9 SMA", color=#4CAF50)

//Intraday

longCondition = ta.crossover(sma5, sma9)
shortCondition = ta.crossunder(sma5, sma9)
plotshape(longCondition, title='Buy Label', style=shape.labelup, location=location.belowbar, size=size.normal, text='Buy', textcolor=color.new(color.white, 0), color=color.new(color.green, 0))
plotshape(shortCondition, title='Sell Label', style=shape.labeldown, location=location.abovebar, size=size.normal, text='Sell', textcolor=color.new(color.white, 0), color=color.new(color.red, 0))

long = ta.crossover(sma9, basis)
short = ta.crossunder(sma9, basis)
plotchar(short, "short", "▼", location.abovebar, color=color.red, size = size.tiny)
plotchar(long, "long", "▲", location.belowbar, color=color.green, size = size.tiny)

//// CONVERGENCE ////

plotema50 = input(title="plot 50 SMA", defval=true)
sma50 = ta.sma(close, 50)
plot(plotema50 ==true ? sma50 : na, "50 SMA", color=#dc7633)

plotema100 = input(title="plot 100 SMA", defval=true)
sma100 = ta.sma(close, 100)
plot(plotema100 ==true ? sma100 : na, "100 SMA", color=#873600)

plotema150 = input(title="plot 150 SMA", defval=true)
sma150 = ta.sma(close, 150)
plot(plotema150 ==true ? sma150 : na, "150 SMA", color=#3498db)

plotema200 = input(title="plot 200 SMA", defval=true)
sma200 = ta.sma(close, 200)
plot(plotema200 ==true ? sma200 : na, "200 SMA", color=#186a3b)

//////////VWAP////////////////
plot(timeframe.isintraday and plotvwap ==true ? ta.vwap : na , "VWAP", color=#f44336, linewidth=2)