CPR with High of the day
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VBV
// © Modifed by VBV 07/22/2022

//@version=4
study("CPR with Hih of the day", shorttitle="CPR_HighDay", overlay=true)

// User inputs
showTomorrowCPR = input(title="Show tomorrow's CPR", type=input.bool, defval=false)
showHistoricalCPR = input(title="Show historical CPR Daily", type=input.bool, defval=true)
showInterval= input("D", options = ["3", "5", "15", "30", "45", "D"], title = "CPR Interval")
showTomorrowCPRDynamic = input(title="Show tomorrow's CPR Dynamically", type=input.bool, defval=false)
shotLineTransp = input(50, options=[5,50,75,95], title="lineTransp")

showR1S1 = input(title="Show HR1 & HS1", type=input.bool, defval=true)
showR2S2 = input(title="Show HR2 & HS2", type=input.bool, defval=true)
showR3S3 = input(title="Show HR3 & HS3", type=input.bool, defval=true)
showR4S4 = input(title="Show HR4 & HS4", 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=true)
showLines = input(title="Show Lines", type=input.bool, defval=false)

showWeekly = input(title="Show Weekly", type=input.bool, defval=false)
//0 for next week and 1 for current week
showCWNWInterval= input(1, options = [0, 1], title = "CPR based on Week 1-current,0-next")
showPWHL = input(title="Show previous weeks's High & Low", type=input.bool, defval=false)
showNextWeekCPR = input(title="Show next week's CPR", type=input.bool, defval=false)

showMonthly = input(title="Show Monthly", type=input.bool, defval=false)
//0 for next Month and 1 for current Month
showCMNMInterval= input(1, options = [0, 1], title = "CPR based on Month 1-current,0-next")
showPMHL = input(title="Show previous monthly's High & Low", type=input.bool, defval=false)
showNextMonthCPR = input(title="Show next month's CPR", type=input.bool, defval=false)

showQuaterly = input(title="Show Quaterly", type=input.bool, defval=false)
//0 for next Month and 1 for current Quater
showCQNQInterval= input(1, options = [0, 1], title = "CPR based on Quater 1-current,0-next")

showYearly = input(title="Show Yearly", type=input.bool, defval=false)
//0 for next Month and 1 for current Year
showCYNYInterval= input(1, options = [0, 1], title = "CPR based on Year 1-current,0-next")


// Defaults
// CPR Colors
cprColor = color.purple
rColor = color.red
sColor = color.green
cColor = color.black
hlColor = color.fuchsia
tcprColor = color.yellow
twcprColor = color.maroon
tmcprColor = color.navy
tqcprColor = color.silver
tycprColor = color.teal

// Line style & Transparency
lStyle = plot.style_line
lTransp = shotLineTransp //Flip this value to 5 to see the lines on the chart
lTranspCPR = 0

//Fill Transparency
fTransp = 95


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


// 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(2020, 02, 21, 09, 15), true,
         iff(_date == timestamp(2020, 03, 10, 09, 15), true,
         iff(_date == timestamp(2020, 04, 02, 09, 15), true,
         iff(_date == timestamp(2020, 04, 06, 09, 15), true,
         iff(_date == timestamp(2020, 04, 10, 09, 15), true,
         iff(_date == timestamp(2020, 04, 14, 09, 15), true,
         iff(_date == timestamp(2020, 05, 01, 09, 15), true,
         iff(_date == timestamp(2020, 05, 25, 09, 15), true,
         iff(_date == timestamp(2020, 10, 02, 09, 15), true,
         iff(_date == timestamp(2020, 11, 16, 09, 15), true,
         iff(_date == timestamp(2020, 11, 30, 09, 15), true,
         iff(_date == timestamp(2020, 12, 25, 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

//----tomorrows plot------
tom_start = start
tom_end = end

// Start & End time for Tomorrow's CPR
if shouldPlotToday
    tom_start := GetNextWorkingDay(start)
    tom_end := tom_start + 86400000
//-----------weekly plot-------------
nw_start = start
nw_end = end

// Start & End time for Tomorrow's CPR
if shouldPlotToday
    nw_start := GetNextWorkingDay(start)
    nw_end := nw_start + 432000000 //draw line for next 5 days --86400000*5
//----------monthly plot---------------
nm_start = start
nm_end = end

// Start & End time for Tomorrow's CPR
if shouldPlotToday
    nm_start := GetNextWorkingDay(start)
    nm_end := nm_start + 2937600000 //draw line for next 34 days --86400000*34 // this is still not accurate but it is ok

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

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

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

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

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

// Values to consider for trade directions and depth
DHL=(H-L)
PD=(TC-BC)
Dir=(PD/C)*100
SR4=S4-R4

bool alert= (Dir>=0.01 and DHL>=1)
//alert:=true

// Plot Today's CPR
if not(IsHoliday(start)) and not(IsWeekend(start)) and shouldPlotToday
    if showR4S4
        _r4 = line.new(start, R4, end, R4, xloc.bar_time, color=color.new(rColor, lTransp))
        line.delete(_r4[1])
    if showR3S3
        _r3 = line.new(start, R3, end, R3, xloc.bar_time, color=color.new(rColor, lTransp))
        line.delete(_r3[1])
	if showR2S2
		_r2 = line.new(start, R2, end, R2, xloc.bar_time, color=color.new(rColor, lTransp))
		line.delete(_r2[1])
	if showR1S1
		_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])

    if showR1S1
		_s1 = line.new(start, S1, end, S1, xloc.bar_time, color=color.new(sColor, lTransp))
		line.delete(_s1[1])
	if showR2S2
		_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 showR4S4
        _s4 = line.new(start, S4, end, S4, xloc.bar_time, color=color.new(sColor, lTransp))
        line.delete(_s4[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])

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

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

	if showR1S1
		l_s1 = label.new(start, S1, text="HS1", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_s1[1])
	if showR2S2
		l_s2 = label.new(start, S2, text="HS2", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_s2[1])
	if showR3S3
		l_s3 = label.new(start, S3, text="HS3", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_s3[1])
	if showR4S4
		l_s4 = label.new(start, S4, text="HS4", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_s4[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])

// Get series Dynamically for day
getSeriesD(e, timeFrame) => security(syminfo.tickerid, 'D', e, lookahead=barmerge.lookahead_on)

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

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

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

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

// Values to consider for trade directions and depth
tDHL=(tH-tL)
tPD=(tTC-tBC)
tDir=(tPD/tC)*100
tSR4=tS4-tR4

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

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

	if showR1S1
		_t_s1 = line.new(tom_start, tS1, tom_end, tS1, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_t_s1[1])
	if showR2S2
		_t_s2 = line.new(tom_start, tS2, tom_end, tS2, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_t_s2[1])
	if showR3S3
		_t_s3 = line.new(tom_start, tS3, tom_end, tS3, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_t_s3[1])
    if showR4S4
		_t_s4 = line.new(tom_start, tS4, tom_end, tS4, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_t_s4[1])
	if showPDHL
        _pdth = line.new(tom_start, tH, tom_end, tH, xloc.bar_time, color=color.new(rColor, lTranspCPR), 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, lTranspCPR), 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, lTranspCPR), style=line.style_dotted, width=2)
        line.delete(_pdtc[1])

// Plot Tomorrow's Labels
if showTomorrowCPR

	if showR4S4
		l_t_r4 = label.new(tom_start, tR4, text="R4: "+tostring(tR4), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_t_r4[1])
	if showR3S3
		l_t_r3 = label.new(tom_start, tR3, text="R3: "+tostring(tR3), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_t_r3[1])
	if showR2S2
		l_t_r2 = label.new(tom_start, tR2, text="R2: "+tostring(tR2), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_t_r2[1])
	if showR1S1
		l_t_r1 = label.new(tom_start, tR1, text="R1: "+tostring(tR1), 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: "+tostring(tTC), 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: " +tostring(tP), 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: "+tostring(tBC), xloc=xloc.bar_time, textcolor=cprColor, style=label.style_none)
	label.delete(l_t_bc[1])

	if showR1S1
		l_t_s1 = label.new(tom_start, tS1, text="S1: "+tostring(tS1), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_t_s1[1])
	if showR2S2
		l_t_s2 = label.new(tom_start, tS2, text="S2: "+tostring(tS2), 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: "+tostring(tS3), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_t_s3[1])
	if showR4S4
		l_t_s4 = label.new(tom_start, tS4, text="S4: "+tostring(tS4), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_t_s4[1])
	if showPDHL
		l_pdth = label.new(tom_start, tH, text="PD High: "+tostring(tH), 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: "+tostring(tL), 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: "+tostring(tC), xloc=xloc.bar_time, textcolor=cColor, style=label.style_none)
		label.delete(l_pdtc[1])

// --------- Get series Daily -----------End------

// --------- Get series weekly -----------start------
// Get series
getSeriesW(e, timeFrame) => security(syminfo.tickerid, 'W', e, lookahead=barmerge.lookahead_on)

// -------Calculate Weekly's CPR current week and next week---------------
// Get High, Low and Close for the week

twH = getSeriesW(high[showCWNWInterval], 'W')
twL = getSeriesW(low[showCWNWInterval], 'W')
twC = getSeriesW(high[showCWNWInterval], 'W')

// Pivot Range
twP = (twH + twL + twC) / 3
twBC = (twH + twL)/2
twTC = (twP - twBC) + twP

// Resistance Levels
twR1 = (twP * 2) - twL
twR2 = twP + (twH - twL)
twR3 = twR1 + (twH - twL)
twR4 = twR3 + (twR2 - twR1)

// Support Levels
twS1 = (twP * 2) - twH
twS2 = twP - (twH - twL)
twS3 = twS1 - (twH - twL)
twS4 = twS3 - (twS1 - twS2)

// for current week values are plotted historically at the end of the scripts plotting section 
// the below coding is needed only to draw the lines and show the values manually on tomorrows date

// ----------calculate next weekly  values based on the current week as of now available OHLC values and this would be accurate by end of week end closing------------
// Get High, Low and Close for the week

nwH = getSeriesW(high[0], 'W')
nwL = getSeriesW(low[0], 'W')
nwC = getSeriesW(high[0], 'W')

// Pivot Range
nwP = (nwH + nwL + nwC) / 3
nwBC = (nwH + nwL)/2
nwTC = (nwP - nwBC) + nwP

// Resistance Levels
nwR1 = (nwP * 2) - nwL
nwR2 = nwP + (nwH - nwL)
nwR3 = nwR1 + (nwH - nwL)
nwR4 = nwR3 + (nwR2 - nwR1)

// Support Levels
nwS1 = (nwP * 2) - nwH
nwS2 = nwP - (nwH - nwL)
nwS3 = nwS1 - (nwH - nwL)
nwS4 = nwS3 - (nwS1 - nwS2)


//-------- Plot next weekly values & CPR Range on tomorrow date-- need to work on this date logic to show fully for next week. ---------
//for now we can see it if this flag is set to true on tomorrows date
if (showNextWeekCPR)
	if showR4S4
		_nw_r4 = line.new(nw_start, nwR4, nw_end, nwR4, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nw_r4[1])
	if showR3S3
		_nw_r3 = line.new(nw_start, nwR3, nw_end, nwR3, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nw_r3[1])
	if showR2S2
		_nw_r2 = line.new(nw_start, nwR2, nw_end, nwR2, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nw_r2[1])
	if showR1S1
		_nw_r1 = line.new(nw_start, nwR1, nw_end, nwR1, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nw_r1[1])

	_nw_tc = line.new(nw_start, nwTC, nw_end, nwTC, xloc.bar_time, color=color.new(twcprColor, lTranspCPR), width=2)
	line.delete(_nw_tc[1])
	_nw_p = line.new(nw_start, nwP, nw_end, nwP, xloc.bar_time, color=color.new(twcprColor, lTranspCPR), width=2)
	line.delete(_nw_p[1])
	_nw_bc = line.new(nw_start, nwBC, nw_end, nwBC, xloc.bar_time, color=color.new(twcprColor, lTranspCPR), width=2)
	line.delete(_nw_bc[1])

	if showR1S1
		_nw_s1 = line.new(nw_start, nwS1, nw_end, nwS1, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nw_s1[1])
	if showR2S2
		_nw_s2 = line.new(nw_start, nwS2, nw_end, nwS2, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nw_s2[1])
	if showR3S3
		_nw_s3 = line.new(nw_start, nwS3, nw_end, nwS3, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nw_s3[1])
	if showR4S4
		_nw_s4 = line.new(nw_start, nwS4, nw_end, nwS4, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nw_s4[1])

	if showPWHL
		_pdnwh = line.new(nw_start, nwH, nw_end, nwH, xloc.bar_time, color=color.new(rColor, lTranspCPR), style=line.style_dotted, width=2)
		line.delete(_pdnwh[1])
		_pdnwl = line.new(nw_start, nwL, nw_end, nwL, xloc.bar_time, color=color.new(sColor, lTranspCPR), style=line.style_dotted, width=2)
		line.delete(_pdnwl[1])
	//if showPDC
	//	_pdnwc = line.new(nw_start, nwC, nw_end, nwC, xloc.bar_time, color=color.new(cColor, lTranspCPR), style=line.style_dotted, width=2)
	//	line.delete(_pdnwc[1])

// Plot next weekly Labels
if (showNextWeekCPR)
	if showR4S4
		l_nw_r4 = label.new(nw_start, nwR4, text="nwR4: "+tostring(nwR4), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nw_r4[1])
	if showR3S3
		l_nw_r3 = label.new(nw_start, nwR3, text="nwR3: "+tostring(nwR3), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nw_r3[1])
	if showR2S2
		l_nw_r2 = label.new(nw_start, nwR2, text="nwR2: "+tostring(nwR2), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nw_r2[1])
	if showR1S1
		l_nw_r1 = label.new(nw_start, nwR1, text="nwR1: "+tostring(nwR1), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nw_r1[1])

	l_nw_tc = label.new(nw_start, nwTC, text="nwTC: "+tostring(nwTC), xloc=xloc.bar_time, textcolor=twcprColor, style=label.style_none)
	label.delete(l_nw_tc[1])
	l_nw_p = label.new(nw_start, nwP, text="nwP: "+tostring(nwP), xloc=xloc.bar_time, textcolor=twcprColor, style=label.style_none)
	label.delete(l_nw_p[1])
	l_nw_bc = label.new(nw_start, nwBC, text="nwBC: "+tostring(nwBC), xloc=xloc.bar_time, textcolor=twcprColor, style=label.style_none)
	label.delete(l_nw_bc[1])

	if showR1S1
		l_nw_s1 = label.new(nw_start, nwS1, text="nwS1: "+tostring(nwS1), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nw_s1[1])
	if showR2S2
		l_nw_s2 = label.new(nw_start, nwS2, text="nwS2: "+tostring(nwS2), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nw_s2[1])
	if showR3S3
		l_nw_s3 = label.new(nw_start, nwS3, text="nwS3: "+tostring(nwS3), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nw_s3[1])
	if showR4S4
		l_nw_s4 = label.new(nw_start, nwS4, text="nwS4: "+tostring(nwS4), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nw_s4[1])

	if showPWHL
		l_pdnwh = label.new(nw_start, nwH, text="nwH: "+tostring(nwH), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_pdnwh[1])
		l_pdnwl = label.new(nw_start, nwL, text="nwL: "+tostring(nwL), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_pdnwl[1])
	//if showPDC
	//	l_pdnwc = label.new(nw_start, nwC, text="nwC: "+tostring(nwC), xloc=xloc.bar_time, textcolor=cColor, style=label.style_none)
	//	label.delete(l_pdnwc[1])

// --------- Get series Weekly -----------End------

// --------- Get series Monthly -----------start------
getSeriesM(e, timeFrame) => security(syminfo.tickerid, 'M', e, lookahead=barmerge.lookahead_on)

// Calculate Weekly's CPR for current month and next month
// Get High, Low and Close

// if  showCMNMInterval =0 for next Month and 1 for current month based on last week HLC canldle close values
tmH = getSeriesM(high[showCMNMInterval], 'M')
tmL = getSeriesM(low[showCMNMInterval], 'M')
tmC = getSeriesM(high[showCMNMInterval], 'M')

// Pivot Range
tmP = (tmH + tmL + tmC) / 3
tmBC = (tmH + tmL)/2
tmTC = (tmP - tmBC) + tmP

// Resistance Levels
tmR1 = (tmP * 2) - tmL
tmR2 = tmP + (tmH - tmL)
tmR3 = tmR1 + (tmH - tmL)
tmR4 = tmR3 + (tmR2 - tmR1)

// Support Levels
tmS1 = (tmP * 2) - tmH
tmS2 = tmP - (tmH - tmL)
tmS3 = tmS1 - (tmH - tmL)
tmS4 = tmS3 - (tmS1 - tmS2)

// for current month values are plotted historically at the end of the scripts plotting section 
// the below coding is needed only to draw the lines and show the values manually on tomorrows date

// ----------calculate next month  values based on the current month as of now available OHLC values and this would be accurate by end of month end closing------------
// Get High, Low and Close for the month

nmH = getSeriesM(high[0], 'M')
nmL = getSeriesM(low[0], 'M')
nmC = getSeriesM(high[0], 'M')

// Pivot Range
nmP = (nmH + nmL + nmC) / 3
nmBC = (nmH + nmL)/2
nmTC = (nmP - nmBC) + nmP

// Resistance Levels
nmR1 = (nmP * 2) - nmL
nmR2 = nmP + (nmH - nmL)
nmR3 = nmR1 + (nmH - nmL)
nmR4 = nmR3 + (nmR2 - nmR1)

// Support Levels
nmS1 = (nmP * 2) - nmH
nmS2 = nmP - (nmH - nmL)
nmS3 = nmS1 - (nmH - nmL)
nmS4 = nmS3 - (nmS1 - nmS2)

//-------- Plot next month values & CPR Range on tomorrow date-- need to work on this date logic to show fully for next month. ---------
//for now we can see it if this flag is set to true on tomorrows date
if (showNextMonthCPR)
	if showR4S4
		_nm_r4 = line.new(nm_start, nmR4, nm_end, nmR4, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nm_r4[1])
	if showR3S3
		_nm_r3 = line.new(nm_start, nmR3, nm_end, nmR3, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nm_r3[1])
	if showR2S2
		_nm_r2 = line.new(nm_start, nmR2, nm_end, nmR2, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nm_r2[1])
	if showR1S1
		_nm_r1 = line.new(nm_start, nmR1, nm_end, nmR1, xloc.bar_time, color=color.new(rColor, lTranspCPR))
		line.delete(_nm_r1[1])

	_nm_tc = line.new(nm_start, nmTC, nm_end, nmTC, xloc.bar_time, color=color.new(tmcprColor, lTranspCPR), width=2)
	line.delete(_nm_tc[1])
	_nm_p = line.new(nm_start, nmP, nm_end, nmP, xloc.bar_time, color=color.new(tmcprColor, lTranspCPR), width=2)
	line.delete(_nm_p[1])
	_nm_bc = line.new(nm_start, nmBC, nm_end, nmBC, xloc.bar_time, color=color.new(tmcprColor, lTranspCPR), width=2)
	line.delete(_nm_bc[1])

	if showR1S1
		_nm_s1 = line.new(nm_start, nmS1, nm_end, nmS1, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nm_s1[1])
	if showR2S2
		_nm_s2 = line.new(nm_start, nmS2, nm_end, nmS2, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nm_s2[1])
	if showR3S3
		_nm_s3 = line.new(nm_start, nmS3, nm_end, nmS3, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nm_s3[1])
	if showR4S4
		_nm_s4 = line.new(nm_start, nmS4, nm_end, nmS4, xloc.bar_time, color=color.new(sColor, lTranspCPR))
		line.delete(_nm_s4[1])

	if showPMHL
		_pdnmh = line.new(nm_start, nmH, nm_end, nmH, xloc.bar_time, color=color.new(rColor, lTranspCPR), style=line.style_dotted, width=2)
		line.delete(_pdnmh[1])
		_pdnml = line.new(nm_start, nmL, nm_end, nmL, xloc.bar_time, color=color.new(sColor, lTranspCPR), style=line.style_dotted, width=2)
		line.delete(_pdnml[1])
	//if shonmPDC
	//	_pdnmc = line.new(nm_start, nmC, nm_end, nmC, xloc.bar_time, color=color.new(cColor, lTranspCPR), style=line.style_dotted, width=2)
	//	line.delete(_pdnmc[1])

// Plot next month Labels
if (showNextMonthCPR)
	if showR4S4
		l_nm_r4 = label.new(nm_start, nmR4, text="nmR4: "+tostring(nmR4), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nm_r4[1])
	if showR3S3
		l_nm_r3 = label.new(nm_start, nmR3, text="nmR3: "+tostring(nmR3), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nm_r3[1])
	if showR2S2
		l_nm_r2 = label.new(nm_start, nmR2, text="nmR2: "+tostring(nmR2), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nm_r2[1])
	if showR1S1
		l_nm_r1 = label.new(nm_start, nmR1, text="nmR1: "+tostring(nmR1), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_nm_r1[1])

	l_nm_tc = label.new(nm_start, nmTC, text="nmTC: "+tostring(nmTC), xloc=xloc.bar_time, textcolor=tmcprColor, style=label.style_none)
	label.delete(l_nm_tc[1])
	l_nm_p = label.new(nm_start, nmP, text="nmP: "+tostring(nmP), xloc=xloc.bar_time, textcolor=tmcprColor, style=label.style_none)
	label.delete(l_nm_p[1])
	l_nm_bc = label.new(nm_start, nmBC, text="nmBC: "+tostring(nmBC), xloc=xloc.bar_time, textcolor=tmcprColor, style=label.style_none)
	label.delete(l_nm_bc[1])

	if showR1S1
		l_nm_s1 = label.new(nm_start, nmS1, text="nmS1: "+tostring(nmS1), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nm_s1[1])
	if showR2S2
		l_nm_s2 = label.new(nm_start, nmS2, text="nmS2: "+tostring(nmS2), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nm_s2[1])
	if showR3S3
		l_nm_s3 = label.new(nm_start, nmS3, text="nmS3: "+tostring(nmS3), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nm_s3[1])
	if showR4S4
		l_nm_s4 = label.new(nm_start, nmS4, text="nmS4: "+tostring(nmS4), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_nm_s4[1])

	if showPMHL
		l_pdnmh = label.new(nm_start, nmH, text="nmH: "+tostring(nmH), xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
		label.delete(l_pdnmh[1])
		l_pdnml = label.new(nm_start, nmL, text="nmL: "+tostring(nmL), xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
		label.delete(l_pdnml[1])
	//if shownmPDC
	//	l_pdnmc = label.new(nm_start, nmC, text="nmC: "+tostring(nmC), xloc=xloc.bar_time, textcolor=cColor, style=label.style_none)
	//	label.delete(l_pdnmc[1])
	
// --------- Get series Monthly -----------End------
// --------- Get series Quaterly -----------Start------

getSeriesQ(e, timeFrame) => security(syminfo.tickerid, '3M', e, lookahead=barmerge.lookahead_on)

// Calculate Quaterly's CPR for current Quaterly and next Quaterly
// Get High, Low and Close

// if  showCMNMInterval =0 for next Quaterly and 1 for current Quaterly based on last week HLC canldle close values
tqH = getSeriesQ(high[showCQNQInterval], '3M')
tqL = getSeriesQ(low[showCQNQInterval], '3M')
tqC = getSeriesQ(high[showCQNQInterval], '3M')

// Pivot Range
tqP = (tqH + tqL + tqC) / 3
tqBC = (tqH + tqL)/2
tqTC = (tqP - tqBC) + tqP

// Resistance Levels
tqR1 = (tqP * 2) - tqL
tqR2 = tqP + (tqH - tqL)
tqR3 = tqR1 + (tqH - tqL)
tqR4 = tqR3 + (tqR2 - tqR1)

// Support Levels
tqS1 = (tqP * 2) - tqH
tqS2 = tqP - (tqH - tqL)
tqS3 = tqS1 - (tqH - tqL)
tqS4 = tqS3 - (tqS1 - tqS2)


// --------- Get series Quaterly -----------End------

// --------- Get series Yearly -----------Start------

getSeriesY(e, timeFrame) => security(syminfo.tickerid, '12M', e, lookahead=barmerge.lookahead_on)

// Calculate Yearly's CPR for current Year and next year
// Get High, Low and Close

// if  showCMNMInterval =0 for next year and 1 for current year based on last week HLC canldle close values
tyH = getSeriesY(high[showCYNYInterval], '12M')
tyL = getSeriesY(low[showCYNYInterval], '12M')
tyC = getSeriesY(high[showCYNYInterval], '12M')

// Pivot Range
tyP = (tyH + tyL + tyC) / 3
tyBC = (tyH + tyL)/2
tyTC = (tyP - tyBC) + tyP

// Resistance Levels
tyR1 = (tyP * 2) - tyL
tyR2 = tyP + (tyH - tyL)
tyR3 = tyR1 + (tyH - tyL)
tyR4 = tyR3 + (tyR2 - tyR1)

// Support Levels
tyS1 = (tyP * 2) - tyH
tyS2 = tyP - (tyH - tyL)
tyS3 = tyS1 - (tyH - tyL)
tyS4 = tyS3 - (tyS1 - tyS2)


// --------- Get series Yearly -----------End------

// dispaly the dayofweek -- remember these numbers displyed righ most in CPR line
DOW=dayofweek(time)
//mon=2,tue=3,wed=4,thu=5,friday6
//spy is 2 day expiry and expires on mon=2,wed=4,friday6

//Plot Historical CPR for daily
p_r4 = plot(showHistoricalCPR ? showR4S4 ? showLines  ? R4: R4 : na : na, title=' HR4', color=rColor, transp=lTransp, style=lStyle)
p_r3 = plot(showHistoricalCPR ? showR3S3 ? showLines  ? R3: R3 : na : na, title=' HR3', color=rColor, transp=lTransp, style=lStyle)
p_r2 = plot(showHistoricalCPR ? showR2S2 ? showLines  ? R2: R2 : na : na, title=' HR2', color=rColor, transp=lTransp, style=lStyle)
p_r1 = plot(showHistoricalCPR ? showR1S1 ? showLines  ? R1: R1 : na : na, title=' HR1', color=rColor, transp=lTransp, style=lStyle)

p_cprTC = plot(showHistoricalCPR ? TC : na, title=' HTC', color=color.red, transp=lTranspCPR, style=lStyle)
p_cprP = plot(showHistoricalCPR ? P : na, title=' HP', color=color.orange, transp=lTranspCPR, style=lStyle)
p_cprBC = plot(showHistoricalCPR ? BC : na, title=' HBC', color=color.green, transp=lTranspCPR, style=lStyle)

s1 = plot(showHistoricalCPR ? showR1S1 ? S1 : na : na, title=' HS1', color=sColor, transp=lTransp, style=lStyle)
s2 = plot(showHistoricalCPR ? showR2S2 ? S2 : na : na, title=' HS2', color=sColor, transp=lTransp, style=lStyle)
s3 = plot(showHistoricalCPR ? showR3S3 ? S3 : na : na, title=' HS3', color=sColor, transp=lTransp, style=lStyle)
s4 = plot(showHistoricalCPR ? showR4S4 ? S4 : na : na, title=' HS4', color=sColor, transp=lTransp, style=lStyle)

// showPDHL- High/Low --show previous day High and low values
p_dH = plot(showHistoricalCPR ? showPDHL ? H : na : na, title=' Pre-Day High', color=hlColor, transp=lTransp, style=lStyle)
p_dL = plot(showHistoricalCPR ? showPDHL ? L : na : na, title=' Pre-Day Low', color=hlColor, transp=lTransp, style=lStyle)

// cutom strategy calculations display start....
p_DHL= plot(showHistoricalCPR ? true ? DHL : na : na, title=' DHL', color=hlColor, transp=lTransp, style=lStyle)
p_PD= plot(showHistoricalCPR ? true ? PD : na : na, title=' PD', color=hlColor, transp=lTransp, style=lStyle)

//this is the main value we need to consider the direction of the trade to enter
p_Dir= plot(showHistoricalCPR ? true ? Dir : na : na, title=' Dir', color=color.orange, transp=lTransp, style=lStyle)
p_SR4= plot(showHistoricalCPR ? true ? SR4 : na : na, title=' SR4', color=hlColor, transp=lTransp, style=lStyle)
//Day of Week --this is used in Back testing to know  0 DTE's
p_DOW= plot(showHistoricalCPR ? true ? DOW : na : na, title=' DOW', color=color.black, transp=lTransp, style=lStyle)
// cutom strategy calculations display end....

//if showTomorrowCPRDynamically with current day HLC values present at the moment
p_cprtTC = plot(showTomorrowCPRDynamic ? tTC : na, title=' HtTC', color=tcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtP = plot(showTomorrowCPRDynamic ? tP : na, title=' HtP', color=tcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtBC = plot(showTomorrowCPRDynamic ? tBC : na, title=' HtBC', color=tcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)

//if showTomorrowCPRDynamic flip tc and bc
//p_cprtBC = plot(showTomorrowCPRDynamic ? tBC : na, title=' tBC', color=tcprColor, transp=lTranspCPR, style=lStyle)
//p_cprtP = plot(showTomorrowCPRDynamic ? tP : na, title=' tP', color=tcprColor, transp=lTranspCPR, style=lStyle)
//p_cprtTC = plot(showTomorrowCPRDynamic ? tTC : na, title=' tTC', color=tcprColor, transp=lTranspCPR, style=lStyle)

// cutom strategy calculations display start.... not working/using on these yet -- commented for now
// this is to display values  of tomorrow calculations by EOD it will be occurate as OHLC  are keep changing 
// for now there is no strategy on these so commenting it for now
//p_tDHL= plot(showHistoricalCPR ? true ? tDHL : na : na, title=' tDHL', color=hlColor, transp=lTransp, style=lStyle)
//p_tPD= plot(showHistoricalCPR ? true ? tPD : na : na, title=' tPD', color=hlColor, transp=lTransp, style=lStyle)
//this is the main value we need to consider the direction of the trade to enter
//p_tDir= plot(showHistoricalCPR ? true ? tDir : na : na, title=' tDir', color=color.orange, transp=lTransp, style=lStyle)
//p_tSR4= plot(showHistoricalCPR ? true ? tSR4 : na : na, title=' tSR4', color=hlColor, transp=lTransp, style=lStyle)
// cutom strategy calculations display end....
///end daily

//start weekly for this week ---- plots weekly historically
p_rw4 = plot(showWeekly ? showR4S4 ? showLines  ? twR4: twR4 : na : na, title=' HWR4', color=rColor, transp=lTransp, style=lStyle)
p_rw3 = plot(showWeekly ? showR3S3 ? showLines  ? twR3: twR3 : na : na, title=' HWR3', color=rColor, transp=lTransp, style=lStyle)
p_rw2 = plot(showWeekly ? showR2S2 ? showLines  ? twR2: twR2 : na : na, title=' HWR2', color=rColor, transp=lTransp, style=lStyle)
p_rw1 = plot(showWeekly ? showR1S1 ? showLines  ? twR1: twR1 : na : na, title=' HWR1', color=rColor, transp=lTransp, style=lStyle)

//if showWeekly CPR Range
p_cprtwTC = plot(showWeekly ? twTC : na, title=' HWTC', color=twcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtwP = plot(showWeekly ? twP : na, title=' HWP', color=twcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtwBC = plot(showWeekly ? twBC : na, title=' HWBC', color=twcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)

//if showWeekly flip tc, bc
//p_cprtwBC = plot(showWeekly ? twBC : na, title=' WBC', color=twcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
//p_cprtwP = plot(showWeekly ? twP : na, title=' WP', color=twcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
//p_cprtwTC = plot(showWeekly ? twTC : na, title=' WTC', color=twcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)

p_ws1 = plot(showWeekly ? showR1S1 ? twS1 : na : na, title=' HWS1', color=sColor, transp=lTransp, style=lStyle)
p_ws2 = plot(showWeekly ? showR2S2 ? twS2 : na : na, title=' HWS2', color=sColor, transp=lTransp, style=lStyle)
p_ws3 = plot(showWeekly ? showR3S3 ? twS3 : na : na, title=' HWS3', color=sColor, transp=lTransp, style=lStyle)
p_ws4 = plot(showWeekly ? showR4S4 ? twS4 : na : na, title=' HWS4', color=sColor, transp=lTransp, style=lStyle)

// showPWHL- High/Low
p_twH = plot(showWeekly ? showPWHL ? twH : na : na, title=' W High', color=hlColor, transp=lTransp, style=lStyle)
p_twL = plot(showWeekly ? showPWHL ? twL : na : na, title=' W Low', color=hlColor, transp=lTransp, style=lStyle)
//end weekly

//start monthly -- plots monthly historically
p_rm4 = plot(showMonthly ? showR4S4 ? showLines  ? tmR4: tmR4 : na : na, title=' HMR4', color=rColor, transp=lTransp, style=lStyle)
p_rm3 = plot(showMonthly ? showR3S3 ? showLines  ? tmR3: tmR3 : na : na, title=' HMR3', color=rColor, transp=lTransp, style=lStyle)
p_rm2 = plot(showMonthly ? showR2S2 ? showLines  ? tmR2: tmR2 : na : na, title=' HMR2', color=rColor, transp=lTransp, style=lStyle)
p_rm1 = plot(showMonthly ? showR1S1 ? showLines  ? tmR1: tmR1 : na : na, title=' HMR1', color=rColor, transp=lTransp, style=lStyle)

//if showMonthly CPR range
p_cprtmTC = plot(showMonthly ? tmTC : na, title=' HMTC', color=tmcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtmP = plot(showMonthly ? tmP : na, title=' HMP', color=tmcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtmBC = plot(showMonthly ? tmBC : na, title=' HMBC', color=tmcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)

p_ms1 = plot(showMonthly ? showR1S1 ? tmS1 : na : na, title=' HMS1', color=sColor, transp=lTransp, style=lStyle)
p_ms2 = plot(showMonthly ? showR2S2 ? tmS2 : na : na, title=' HMS2', color=sColor, transp=lTransp, style=lStyle)
p_ms3 = plot(showMonthly ? showR3S3 ? tmS3 : na : na, title=' HMS3', color=sColor, transp=lTransp, style=lStyle)
p_ms4 = plot(showMonthly ? showR4S4 ? tmS4 : na : na, title=' HMS4', color=sColor, transp=lTransp, style=lStyle)

// showPMHL- High/Low
p_tmH = plot(showMonthly ? showPMHL ? tmH : na : na, title=' M High', color=hlColor, transp=lTransp, style=lStyle)
p_tmL = plot(showMonthly ? showPMHL ? tmL : na : na, title=' M Low', color=hlColor, transp=lTransp, style=lStyle)
//end Monthly


//start quaterly --- plots quaterly historically
//_rq4 = plot(showQuaterly ? showR4S4 ? showLines  ? tqR4: tqR4 : na : na, title=' QR4', color=rColor, transp=lTransp, style=lStyle)
//_rq3 = plot(showQuaterly ? showR3S3 ? showLines  ? tqR3: tqR3 : na : na, title=' QR3', color=rColor, transp=lTransp, style=lStyle)
_rq2 = plot(showQuaterly ? showR2S2 ? showLines  ? tqR2: tqR2 : na : na, title=' HQR2', color=rColor, transp=lTransp, style=lStyle)
_rq1 = plot(showQuaterly ? showR1S1 ? showLines  ? tqR1: tqR1 : na : na, title=' HQR1', color=rColor, transp=lTransp, style=lStyle)

//if Quaterly CPR
p_cprtqTC = plot(showQuaterly ? tqTC : na, title=' HQTC', color=tqcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtqP = plot(showQuaterly ? tqP : na, title=' HQP', color=tqcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtqBC = plot(showQuaterly ? tqBC : na, title=' HQBC', color=tqcprColor, transp=lTranspCPR, style=lStyle, linewidth=2)

//_sq4 = plot(showQuaterly ? showR4S4 ? showLines  ? tqS4: tqS4 : na : na, title=' QS4', color=sColor, transp=lTransp, style=lStyle)
//_sq3 = plot(showQuaterly ? showR3S3 ? showLines  ? tqS3: tqS3 : na : na, title=' QS3', color=sColor, transp=lTransp, style=lStyle)
_sq2 = plot(showQuaterly ? showR2S2 ? showLines  ? tqS2: tqS2 : na : na, title=' HQS2', color=sColor, transp=lTransp, style=lStyle)
_sq1 = plot(showQuaterly ? showR1S1 ? showLines  ? tqS1: tqS1 : na : na, title=' HQS1', color=sColor, transp=lTransp, style=lStyle)
//end quaterly

//start yearly --- plots yealy historically
_ry4 = plot(showYearly ? showR4S4 ? showLines  ? tyR4: tyR4 : na : na, title=' HYR4', color=rColor, transp=lTransp, style=lStyle)
_ry3 = plot(showYearly ? showR3S3 ? showLines  ? tyR3: tyR3 : na : na, title=' HYR3', color=rColor, transp=lTransp, style=lStyle)
_ry2 = plot(showYearly ? showR2S2 ? showLines  ? tyR2: tyR2 : na : na, title=' HYR2', color=rColor, transp=lTransp, style=lStyle)
_ry1 = plot(showYearly ? showR1S1 ? showLines  ? tyR1: tyR1 : na : na, title=' HYR1', color=rColor, transp=lTransp, style=lStyle)

//if Yearly CPR 
p_cprtyTC = plot(showYearly ? tyTC : na, title=' HYTC', color=tycprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtyP = plot(showYearly ? tyP : na, title=' HYP', color=tycprColor, transp=lTranspCPR, style=lStyle, linewidth=2)
p_cprtyBC = plot(showYearly ? tyBC : na, title=' HYBC', color=tycprColor, transp=lTranspCPR, style=lStyle, linewidth=2)

//_sy4 = plot(showYearly ? showR4S4 ? showLines  ? tyS4: tyS4 : na : na, title=' YS4', color=sColor, transp=lTransp, style=lStyle)
//_sy3 = plot(showYearly ? showR3S3 ? showLines  ? tyS3: tyS3 : na : na, title=' YS3', color=sColor, transp=lTransp, style=lStyle)
//_sy2 = plot(showYearly ? showR2S2 ? showLines  ? tyS2: tyS2 : na : na, title=' YS2', color=sColor, transp=lTransp, style=lStyle)
sy1 = plot(showYearly ? showR1S1 ? showLines  ? tyS1: tyS1 : na : na, title=' HYS1', color=sColor, transp=lTransp, style=lStyle)
//end yearly


//line_on = input(title="show vertical line", type=input.bool, defval=true)
//label_on = input(title="show label", type=input.bool, defval=true)
//color_chosen = input(title="color", type=input.color, defval=color.new(color.silver, 70))

// this plots label on each bar so commented it for now
// barTime = time_close - time

// if (shouldPlotToday) and (dayofweek == dayofweek.monday or dayofweek == dayofweek.wednesday or dayofweek == dayofweek.friday)
//     if line_on
//         line.new(x1=bar_index[0], x2=bar_index[0], y1=1, y2=2, extend=extend.both, color=color_chosen, style=line.style_dashed, width=3)
//     if label_on
//         label.new(x=bar_index[0], y=0, yloc=yloc.abovebar, text="Opt\nExp", color=color_chosen)

// this alert section is not working at this moment--TBD
alertcondition(alert,title="1 - Direction", message=' Dir = {{p_Dir}}')

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