CPR with Developing Pivot Range
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Saravanan_Ragavan

//@version=4
study("DPR", overlay=true)
Range = input(title="Developing Pivot Range On/off", type=input.bool, defval=true)


//Getting Value of Day's High, Low and Close
D= time("D")
D_con = na(D[1]) or D > D[1]
D_check = D_con ? 1 : 2
D_start = valuewhen(D_check==1, bar_index, 0)
D_length = (bar_index - D_start)+1
D_lower = lowest(D_length)
D_upper = highest(D_length)
D_close = close

//Developing Pivot Range calculation
D_p = (D_upper + D_lower + D_close)/3
D_c = (D_upper + D_lower) /2
D_m = D_p + (D_p - D_c)

// plotting DCPR
fillcolor = D_c > D_m ? color.red : color.green
plot(Range ? D_p:na, color=fillcolor, linewidth=1)
dh = plot(Range ? D_c:na, color=color.green, linewidth=0, display=display.none)
dl = plot(Range ? D_m:na, color=color.red, linewidth=0, display=display.none)


// Fill color based on the trend
fill(dh, dl,
     color=color.new(fillcolor, 90))



// CPR 

CPR_show = input(title="CPR On", type=input.bool, defval=true)
pivot = (high + low + close ) / 3
bc = (high + low ) / 2
tc = (pivot - bc) + pivot



//Daily Pivot Range 
dtime_pivot = security(syminfo.tickerid, 'D', pivot[1],  lookahead=true) 
dtime_bc = security(syminfo.tickerid, 'D', bc[1], lookahead=true) 
dtime_tc = security(syminfo.tickerid, 'D', tc[1], lookahead=true) 

plot(CPR_show ? dtime_pivot : na, title="Daily Pivot", style=plot.style_circles, color=color.black,linewidth=2) 
plot(CPR_show ? dtime_bc : na, title="Daily BC",style=plot.style_circles, color=color.aqua,linewidth=2)
plot(CPR_show ? dtime_tc : na, title="Daily TC",style=plot.style_circles, color=color.aqua,linewidth=2)

