nusta paisa gang
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CheckMateTrades

//@version=4
study(title="CheckmateTrades", shorttitle="CheckmateTrade 30 cpr", overlay=true)
pivottimeframe = input(title="Pivot Resolution", defval="15", options=["15","30","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")
dl = input(true, title="Show Daily Highs/Lows")
wl = input(false, title="Show Weekly Highs/Lows")
ml = input(false, title="Show Monthly Highs/Lows")
showTomorrowCPR = input(title="Show tomorrow's CPR", type=input.bool, defval=false)
showHistoricalCPR = input(title="Show historical CPR", type=input.bool, defval=false)
showR3S3 = input(title="Show R3 & S3", type=input.bool, defval=true)
showPDHL = input(title="Show previous day's High & Low", type=input.bool, defval=true)
showPDC = input(title="Show previous day's Close", type=input.bool, defval=false)
showTomorrowCamarilla = input(title="Show tomorrow's Camarilla", type=input.bool, defval=false)
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)

whigh = security(syminfo.tickerid, "W", high[1], lookahead=barmerge.lookahead_on)
wlow = security(syminfo.tickerid, "W", low[1], lookahead=barmerge.lookahead_on)
wclose = security(syminfo.tickerid, "W", close[1], lookahead=barmerge.lookahead_on)
wopen = security(syminfo.tickerid, "W", open, lookahead=barmerge.lookahead_on)

mhigh = security(syminfo.tickerid, "M", high[1], lookahead=barmerge.lookahead_on)
mlow = security(syminfo.tickerid, "M", low[1], lookahead=barmerge.lookahead_on)
mclose = security(syminfo.tickerid, "M", close[1], lookahead=barmerge.lookahead_on)
mopen = security(syminfo.tickerid, "M", 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)



plot(dl and dhigh ? dhigh : na, title="Previous Day High", style=plot.style_line, color= dhigh != dhigh[1] ? na : color.gray, transp=0)
plot(dl and dlow ? dlow : na, title="Previous Day Low", style=plot.style_line, color= dlow != dlow[1] ? na : color.gray, transp=0)
plot(wl and whigh ? whigh : na, title="Previous Week High", style=plot.style_line, color= whigh != whigh[1] ? na : color.gray, transp=0)
plot(wl and wlow ? wlow : na, title="Previous Week Low", style=plot.style_line, color= wlow != wlow[1] ? na : color.gray, transp=0)
plot(ml and mhigh ? mhigh : na, title="Previous Month High", style=plot.style_line, color= mhigh != mhigh[1] ? na : color.gray, transp=0)
plot(ml and mlow ? mlow : na, title="Previous Month Low", style=plot.style_line, color= mlow != mlow[1] ? na : color.gray, transp=0)



//SMA
PlotSMA1 = input(title = "Plot SMA1?", type=input.bool, defval=true)
SMALength1 = input(title="SMA Length", type=input.integer, defval=20)
SMASource1 = input(title="SMA Source", type=input.source, defval=close)
SMAvg1 = sma (SMASource1, SMALength1)
plot(PlotSMA1 ? SMAvg1 : na,  color= color.green, title="SMA1")

PlotSMA2 = input(title = "Plot SMA2?", type=input.bool, defval=true)
SMALength2 = input(title="SMA Length", type=input.integer, defval=50)
SMASource2 = input(title="SMA Source", type=input.source, defval=close)
SMAvg2 = sma (SMASource2, SMALength2)
plot(PlotSMA2 ? SMAvg2 : na,  color= color.red, title="SMA2")

PlotSMA3 = input(title = "Plot SMA3?", type=input.bool, defval=true)
SMALength3 = input(title="SMA Length", type=input.integer, defval=200)
SMASource3 = input(title="SMA Source", type=input.source, defval=close)
SMAvg3 = sma (SMASource3, SMALength3)
plot(PlotSMA3 ? SMAvg3 : na,  color= color.black, title="SMA3")





// User inputs


// Defaults
// CPR Colors
cprColor = color.purple
rColor = color.red
sColor = color.green
cColor = color.black
camColor = color.black

// Line style & Transparency
lStyle = plot.style_line
lTransp = 20

//Fill Transparency
fTransp = 95


// 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


// Today's Session Start timestamp
y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)

// Start & End time for Today's CPR
start = timestamp(y, m, d, 09, 15)
end = start + 86400000

// Plot Today's CPR
shouldPlotToday = timenow > start

tom_start = start
tom_end = end

// Start & End time for Tomorrow's CPR
if shouldPlotToday
    tom_start := GetNextWorkingDay(start)
    tom_end := tom_start + 86400000


// Get series
getSeries(e, timeFrame) => security(syminfo.tickerid, "D", e, lookahead=barmerge.lookahead_on) 

// Calculate Today's CPR
//Get High, Low and Close
H = getSeries(high[1], 'D')
L = getSeries(low[1], 'D')
C = getSeries(close[1], 'D')

// Pivot Range
P = (H + L + C) / 3 
TC = (H + L)/2
BC = (P - TC) + P

// Resistance Levels
R3 = H + 2*(P - L)
R2 = P + (H - L)
R1 = (P * 2) - L

// Support Levels
S1 = (P * 2) - H
S2 = P - (H - L)
S3 = L - 2*(H - P)

// Camarilla Levels
H3 = C + (H - L)*(1.1/4)
H4 = C + (H - L)*(1.1/2)
L3 = C - (H - L)*(1.1/4)
L4 = C - (H - L)*(1.1/2)


// Plot Today's CPR
if not(IsHoliday(start)) and not(IsWeekend(start)) and shouldPlotToday
    if showR3S3
        _r3 = line.new(start, R3, end, R3, xloc.bar_time, color=color.new(rColor, lTransp))
        line.delete(_r3[1])
    _r2 = line.new(start, R2, end, R2, xloc.bar_time, color=color.new(rColor, lTransp))
    line.delete(_r2[1])
    _r1 = line.new(start, R1, end, R1, xloc.bar_time, color=color.new(rColor, lTransp))
    line.delete(_r1[1])
    
    _tc = line.new(start, TC, end, TC, xloc.bar_time, color=color.new(cprColor, lTransp))
    line.delete(_tc[1])
    _p = line.new(start, P, end, P, xloc.bar_time, color=color.new(cprColor, lTransp))
    line.delete(_p[1])
    _bc = line.new(start, BC, end, BC, xloc.bar_time, color=color.new(cprColor, lTransp))
    line.delete(_bc[1])
    
    _s1 = line.new(start, S1, end, S1, xloc.bar_time, color=color.new(sColor, lTransp))
    line.delete(_s1[1])
    _s2 = line.new(start, S2, end, S2, xloc.bar_time, color=color.new(sColor, lTransp))
    line.delete(_s2[1])
    if showR3S3
        _s3 = line.new(start, S3, end, S3, xloc.bar_time, color=color.new(sColor, lTransp))
        line.delete(_s3[1])
    if showPDHL
        _pdh = line.new(start, H, end, H, xloc.bar_time, color=color.new(rColor, lTransp), style=line.style_dotted, width=2)
        line.delete(_pdh[1])
        _pdl = line.new(start, L, end, L, xloc.bar_time, color=color.new(sColor, lTransp), style=line.style_dotted, width=2)
        line.delete(_pdl[1])
    if showPDC
        _pdc = line.new(start, C, end, C, xloc.bar_time, color=color.new(cColor, lTransp), style=line.style_dotted, width=2)
        line.delete(_pdc[1])

    if showTodaysCamarilla
        _h3 = line.new(start, H3, end, H3, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
        line.delete(_h3[1])
        _h4 = line.new(start, H4, end, H4, xloc.bar_time, color=color.new(camColor, lTransp), width=2) 
        line.delete(_h4[1])
        _l3 = line.new(start, L3, end, L3, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
        line.delete(_l3[1])
        _l4 = line.new(start, L4, end, L4, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
        line.delete(_l4[1])

    

// Plot Today's Labels
if not(IsHoliday(start)) and not(IsWeekend(start)) and shouldPlotToday
	if showR3S3
		l_r3 = label.new(start, R3, text="R3", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_r3[1])
	l_r2 = label.new(start, R2, text="R2", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
	label.delete(l_r2[1])
	l_r1 = label.new(start, R1, text="R1", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
	label.delete(l_r1[1])

	l_tc = label.new(start, TC, text="TC", xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_tc[1])
	l_p = label.new(start, P, text="P", xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_p[1])
	l_bc = label.new(start, BC, text="BC", xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_bc[1])

	l_s1 = label.new(start, S1, text="S1", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
	label.delete(l_s1[1])
	l_s2 = label.new(start, S2, text="S2", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
	label.delete(l_s2[1])
	if showR3S3
		l_s3 = label.new(start, S3, text="S3", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_s3[1])
	if showPDHL
		l_pdh = label.new(start, H, text="PD High", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_pdh[1])
		l_pdl = label.new(start, L, text="PD Low", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_pdl[1])
	if showPDC
		l_pdc = label.new(start, C, text="PD Close", xloc=xloc.bar_time, textcolor=cColor, style=label.style_none)
		label.delete(l_pdc[1])

    


// Calculate Tomorrow's CPR
// Get High, Low and Close
tH = getSeries(high, 'D')
tL = getSeries(low, 'D')
tC = getSeries(close, 'D')

// Pivot Range
tP = (tH + tL + tC) / 3 
tTC = (tH + tL)/2
tBC = (tP - tTC) + tP

// Resistance Levels
tR3 = tH + 2*(tP - tL)
tR2 = tP + (tH - tL)
tR1 = (tP * 2) - tL

// Support Levels
tS1 = (tP * 2) - tH
tS2 = tP - (tH - tL)
tS3 = tL - 2*(tH - tP)

// Camarilla Levels
tH3 = tC + (tH - tL)*(1.1/4)
tH4 = tC + (tH - tL)*(1.1/2)
tL3 = tC - (tH - tL)*(1.1/4)
tL4 = tC - (tH - tL)*(1.1/2)

// Plot Tomorrow's CPR
if showTomorrowCPR
	if showR3S3
		_t_r3 = line.new(tom_start, tR3, tom_end, tR3, xloc.bar_time, color=color.new(rColor, lTransp))
		line.delete(_t_r3[1])
	_t_r2 = line.new(tom_start, tR2, tom_end, tR2, xloc.bar_time, color=color.new(rColor, lTransp))
	line.delete(_t_r2[1])
	_t_r1 = line.new(tom_start, tR1, tom_end, tR1, xloc.bar_time, color=color.new(rColor, lTransp))
	line.delete(_t_r1[1])

	_t_tc = line.new(tom_start, tTC, tom_end, tTC, xloc.bar_time, color=color.new(cprColor, lTransp))
	line.delete(_t_tc[1])
	_t_p = line.new(tom_start, tP, tom_end, tP, xloc.bar_time, color=color.new(cprColor, lTransp))
	line.delete(_t_p[1])
	_t_bc = line.new(tom_start, tBC, tom_end, tBC, xloc.bar_time, color=color.new(cprColor, lTransp))
	line.delete(_t_bc[1])

	_t_s1 = line.new(tom_start, tS1, tom_end, tS1, xloc.bar_time, color=color.new(sColor, lTransp))
	line.delete(_t_s1[1])
	_t_s2 = line.new(tom_start, tS2, tom_end, tS2, xloc.bar_time, color=color.new(sColor, lTransp))
	line.delete(_t_s2[1])
	if showR3S3
		_t_s3 = line.new(tom_start, tS3, tom_end, tS3, xloc.bar_time, color=color.new(sColor, lTransp))
		line.delete(_t_s3[1])
    if showPDHL
        _pdth = line.new(tom_start, tH, tom_end, tH, xloc.bar_time, color=color.new(rColor, lTransp), style=line.style_dotted, width=2)
        line.delete(_pdth[1])
        _pdtl = line.new(tom_start, tL, tom_end, tL, xloc.bar_time, color=color.new(sColor, lTransp), style=line.style_dotted, width=2)
        line.delete(_pdtl[1])
    if showPDC
        _pdtc = line.new(tom_start, tC, tom_end, tC, xloc.bar_time, color=color.new(cColor, lTransp), style=line.style_dotted, width=2)
        line.delete(_pdtc[1])
        
    if showTomorrowCamarilla
        _t_h3 = line.new(tom_start, tH3, tom_end, tH3, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
		line.delete(_t_h3[1])
        _t_h4 = line.new(tom_start, tH4, tom_end, tH4, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
		line.delete(_t_h4[1])
        _t_l3 = line.new(tom_start, tL3, tom_end, tL3, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
		line.delete(_t_l3[1])
        _t_l4 = line.new(tom_start, tL4, tom_end, tL4, xloc.bar_time, color=color.new(camColor, lTransp), width=2)
		line.delete(_t_l4[1])



// Plot Tomorrow's Labels
if showTomorrowCPR
	if showR3S3
		l_t_r3 = label.new(tom_start, tR3, text="R3", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_t_r3[1])
	l_t_r2 = label.new(tom_start, tR2, text="R2", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
	label.delete(l_t_r2[1])
	l_t_r1 = label.new(tom_start, tR1, text="R1", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
	label.delete(l_t_r1[1])

	l_t_tc = label.new(tom_start, tTC, text="TC", xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_t_tc[1])
	l_t_p = label.new(tom_start, tP, text="P", xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_t_p[1])
	l_t_bc = label.new(tom_start, tBC, text="BC", xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_t_bc[1])

	l_t_s1 = label.new(tom_start, tS1, text="S1", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
	label.delete(l_t_s1[1])
	l_t_s2 = label.new(tom_start, tS2, text="S2", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
	label.delete(l_t_s2[1])
	if showR3S3
		l_t_s3 = label.new(tom_start, tS3, text="S3", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_t_s3[1])
	if showPDHL
		l_pdth = label.new(tom_start, tH, text="PD High", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_pdth[1])
		l_pdtl = label.new(tom_start, tL, text="PD Low", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_pdtl[1])
	if showPDC
		l_pdtc = label.new(tom_start, tC, text="PD Close", xloc=xloc.bar_time, textcolor=cColor, style=label.style_none)
		label.delete(l_pdtc[1])
    if showTomorrowCamarilla 
        l_t_h3 = label.new(tom_start, tH3, text="TH3", xloc=xloc.bar_time, textcolor=camColor, style=label.style_none)
		label.delete(l_t_h3[1])
        l_t_h4 = label.new(tom_start, tH4, text="TH4", xloc=xloc.bar_time, textcolor=camColor, style=label.style_none)
		label.delete(l_t_h4[1])
        l_t_l3 = label.new(tom_start, tL3, text="TL3", xloc=xloc.bar_time, textcolor=camColor, style=label.style_none)
		label.delete(l_t_l3[1])   
        l_t_l4 = label.new(tom_start, tL4, text="TL4", xloc=xloc.bar_time, textcolor=camColor, style=label.style_none)
		label.delete(l_t_l4[1]) 



//Plot Historical CPR
p_r3 = plot(showHistoricalCPR ? showR3S3 ? R3 : na : na, title=' R3', color=rColor, transp=lTransp)
p_r2 = plot(showHistoricalCPR ? R2 : na, title=' R2', color=rColor, transp=lTransp)
p_r1 = plot(showHistoricalCPR ? R1 : na, title=' R1', color=rColor, transp=lTransp)

p_cprTC = plot(showHistoricalCPR ? TC : na, title=' TC', color=cprColor, transp=lTransp)
p_cprP = plot(showHistoricalCPR ? P : na, title=' P', color=cprColor, transp=lTransp)
p_cprBC = plot(showHistoricalCPR ? BC : na, title=' BC', color=cprColor, transp=lTransp)

s11 = plot(showHistoricalCPR ? S1 : na, title=' S1', color=sColor, transp=lTransp)
s22 = plot(showHistoricalCPR ? S2 : na, title=' S2', color=sColor, transp=lTransp)
s33 = plot(showHistoricalCPR ? showR3S3 ? S3 : na : na, title=' S3', color=sColor, transp=lTransp)

fill(p_cprTC, p_cprBC, color=color.purple, transp=fTransp)
