CPR PIVOT, 2ST, 5MA, VWAP
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AdnynSabir

//@version=4
study(title="CPR PIVOT, 2ST, 5MA, VWAP", shorttitle="A I O I", overlay=true)
pivottimeframe = input(title="Pivot Resolution", defval="D", options=["D","W","M"])
dp = input(true, title="Show Pivot Ranges")
sp = input(true, title="Show Pivot Support Levels")
rp = input(true, title="Show Pivot Resistance Levels")
cp = input(true, title="Show Camarilla Pivots")
showTodaysCamarilla = input(title="Show Today's Camarilla", type=input.bool, defval=true)


//dp in the prefix implies daily pivot calculation
dpopen = security(syminfo.tickerid, pivottimeframe, open[1], barmerge.gaps_off, barmerge.lookahead_on)
dphigh = security(syminfo.tickerid, pivottimeframe, high[1], barmerge.gaps_off, barmerge.lookahead_on)
dplow = security(syminfo.tickerid, pivottimeframe, low[1], barmerge.gaps_off, barmerge.lookahead_on)
dpclose = security(syminfo.tickerid, pivottimeframe, close[1], barmerge.gaps_off, barmerge.lookahead_on)
dprange = dphigh - dplow



//Pivot Boss Pivots Formula
pivot = (dphigh + dplow + dpclose) / 3.0
bc = (dphigh + dplow) / 2.0
tc = pivot - bc + pivot
r1 = (pivot*2)-dplow
r2 = pivot + (dphigh - dplow)
r3 = dphigh + 2*(pivot - dplow)
s1 = (pivot*2) - dphigh
s2 = pivot - (dphigh - dplow)
s3 = dplow - 2*(dphigh - pivot)
r4 = r3 + (r2 - r1)
s4 = s3 - (s1 - s2)


//Expanded Camarilla Pivots Formula
h3 = dpclose + dprange * (1.1 / 4)
h4 = dpclose + dprange * (1.1 / 2)

l3 = dpclose - dprange * (1.1 / 4)
l4 = dpclose - dprange * (1.1 / 2)


//m,w,d in the prefix implies daily and hourly


dhigh = security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
dlow = security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
dclose = security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)
dopen = security(syminfo.tickerid, "D", open, lookahead=barmerge.lookahead_on)


//Plotting
plot(dp and pivot ? pivot : na, title="Pivot", color=pivot != pivot[1] ? na : color.blue, transp=0)
plot(dp and bc ? bc : na, title="BC", color=bc != bc[1] ? na : color.blue, transp=0)
plot(dp and tc ? tc : na, title="TC", color=tc != tc[1] ? na : color.blue, transp=0)
plot(rp and r1 ? r1 : na, title="R1", color=r1 != r1[1] ? na : color.red, transp=0)
plot(rp and r2 ? r2 : na, title="R2", color=r2 != r2[1] ? na : color.red, transp=0)
plot(rp and r3 ? r3 : na, title="R3", color=r3 != r3[1] ? na : color.red, transp=0)
plot(rp and r4 ? r4 : na, title="R4", color=r4 != r4[1] ? na : color.red, transp=0)
plot(sp and s1 ? s1 : na, title="S1", color=s1 != s1[1] ? na : color.green, transp=0)
plot(sp and s2 ? s2 : na, title="S2", color=s2 != s2[1] ? na : color.green, transp=0)
plot(sp and s3 ? s3 : na, title="S3", color=s3 != s3[1] ? na : color.green, transp=0)
plot(sp and s4 ? s4 : na, title="S4", color=s4 != s4[1] ? na : color.green, transp=0)
plot(cp and h4 ? h4 : na, title="H4", color=h4 != h4[1] ? na : color.black, transp=0, linewidth=2)
plot(cp and h3 ? h3 : na, title="H3", color=h3 != h3[1] ? na : color.black, transp=0, linewidth=2)
plot(cp and l3 ? l3 : na, title="L3", color=l3 != l3[1] ? na : color.black, transp=0, linewidth=2)
plot(cp and l4 ? l4 : na, title="L4", color=l4 != l4[1] ? na : color.black, transp=0, linewidth=2)






// Global Variables & Flags
// TODO : Update the No of Holidays
noOfHolidays = 13


// Global Functions
// TODO : Update the list of Holiday here in format YYYY, MM, DD, 09, 15 
// **09, 15 are session start hour & minutes
IsHoliday(_date) =>
    iff(_date == timestamp(2021, 02, 21, 09, 15), true, 
         iff(_date == timestamp(2021, 01, 26, 09, 15), true, 
         iff(_date == timestamp(2021, 03, 11, 09, 15), true, 
         iff(_date == timestamp(2021, 03, 29, 09, 15), true, 
         iff(_date == timestamp(2021, 04, 02, 09, 15), true, 
         iff(_date == timestamp(2021, 04, 14, 09, 15), true,
         iff(_date == timestamp(2021, 04, 21, 09, 15), true, 
         iff(_date == timestamp(2021, 05, 13, 09, 15), true, 
         iff(_date == timestamp(2021, 07, 21, 09, 15), true, 
         iff(_date == timestamp(2021, 08, 19, 09, 15), true, 
         iff(_date == timestamp(2021, 09, 10, 09, 15), true,
         iff(_date == timestamp(2021, 10, 15, 09, 15), true, 
         iff(_date == timestamp(2021, 11, 05, 09, 15), true,
         iff(_date == timestamp(2021, 11, 19, 09, 15), true, 
         false))))))))))))))

// Note: Week of Sunday=1...Saturday=7
IsWeekend(_date) =>
    dayofweek(_date) == 7 or dayofweek(_date) == 1

// Skip Weekend
SkipWeekend(_date) =>
    _d = dayofweek(_date)
    _mul = _d == 6 ? 3 : _d == 7 ? 2 : 1
    
    _date + (_mul * 86400000)


// Get Next Working Day
GetNextWorkingDay(_date) =>
    _dt = SkipWeekend(_date)
    
    for i = 1 to noOfHolidays
        if IsHoliday(_dt)
            _dt := SkipWeekend(_dt)
            continue
        else
            break
    
    _dt













/// VWAP ///

vw = input(false,"VWAP")

vwaplength= input(title="Length", type=input.integer, defval=1)
cvwap = ema(vwap,vwaplength)
plotvwap = plot(vw ? cvwap : na ,color= color.yellow, transp=0, title = "VWAP", linewidth=1)











// EMA




len11 = input(21, minval=1, title="Length")
src11 = input(high, title="Source")
out11 = ema(src11, len11)
plot(out11, color=#ffff00, transp=0, linewidth=2, title="1MA")

len22 = input(21, minval=1, title="Length")
src22 = input(low, title="Source")
out22 = ema(src22, len22)
plot(out22, color=#33ccff, transp=0, linewidth=2, title="2MA")

len33 = input(8, minval=1, title="Length")
src33 = input(close, title="Source")
out33 = ema(src33, len33)
plot(out33, color=#ff66ff, transp=0, linewidth=2, title="3MA")

len44 = input(50, minval=1, title="Length")
src44 = input(close, title="Source")
out44 = ema(src44, len44)
plot(out44, color=#ff66ff, transp=0, linewidth=2, title="4MA")


len55 = input(200, minval=1, title="Length")
src55 = input(close, title="Source")
out55 = ema(src55, len55)
plot(out55, color=#ffffff, transp=0, linewidth=2, title="5MA")





// SUPERTREND



Periods1 = input(title="ATR Period", type=input.integer, defval=10)
src1 = input(hl2, title="Source")
Multiplier1 = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR1= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals1 = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting1 = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr1 = sma(tr, Periods1)
atr2= changeATR1 ? atr(Periods1) : atr1
up=src1-(Multiplier1*atr2)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src1+(Multiplier1*atr2)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals1 ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals1 ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting1 ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting1 ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")



Periods2 = input(title="ATR Period", type=input.integer, defval=10)
src2 = input(hl2, title="Source")
Multiplier2 = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR2= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals2 = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting2 = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr3 = sma(tr, Periods2)
atr4= changeATR2 ? atr(Periods2) : atr3
up2=src1-(Multiplier2*atr4)
up3 = nz(up2[1],up2)
up2 := close[1] > up3 ? max(up2,up3) : up2
dn2=src1+(Multiplier2*atr4)
dn3 = nz(dn2[1], dn2)
dn2 := close[1] < dn3 ? min(dn2, dn3) : dn2
trend1 = 1
trend1 := nz(trend1[1], trend1)
trend1 := trend1 == -1 and close > dn3 ? 1 : trend1 == 1 and close < up3 ? -1 : trend1
up2Plot = plot(trend1 == 1 ? up2 : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal1 = trend1 == 1 and trend1[1] == -1
plotshape(buySignal1 ? up2 : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal1 and showsignals1 ? up2 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dn2Plot = plot(trend1 == 1 ? na : dn2, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal1 = trend1 == -1 and trend1[1] == 1
plotshape(sellSignal1 ? dn2 : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal1 and showsignals1 ? dn2 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot1 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor1 = highlighting1 ? (trend1 == 1 ? color.green : color.white) : color.white
shortFillColor1 = highlighting1 ? (trend1 == -1 ? color.red : color.white) : color.white
fill(mPlot1, up2Plot, title="UpTrend Highligter", color=longFillColor1)
fill(mPlot1, dn2Plot, title="DownTrend Highligter", color=shortFillColor1)
alertcondition(buySignal1, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond1 = trend1 != trend1[1]
alertcondition(changeCond1, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")
