Central Pivot Point(CPR) indicator for Trading View Platform

Central Pivot Range (CPR) is one of the most popular indicators for intraday trading. Similar to traditional pivot points, it shows support/resistance levels for the day along with the pivot point. However, the pivot point in the case of CPR consists of 3 levels – Pivot, BC, and TC. In this post, we are going to look at the CPR Pine script code.

//@version=4

study(title="Pivot CPR", shorttitle="CPR Pivot:", overlay=true)
pivottimeframe = input(title="Pivot Resolution", defval="D", options=["D", "W", "M"])
dp = input(true, title="Display Floor Pivots")
cp = input(true, title="Display Camarilla Pivots")
hl = input(true, title="Display M, W, D Highs/Lows")
tp = input(false, title="Display Tomorrow Pivots")

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

//Expanded Floor 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 = r1 + dphigh - dplow
r4 = r3 + r2 - r1
s1 = pivot * 2 - dphigh
s2 = pivot - (dphigh - dplow)
s3 = s1 - (dphigh - dplow)
s4 = s3 - (s1 - s2)

//Expanded Camarilla Pivots Formula
h1 = dpclose + dprange * (1.1 / 12)
h2 = dpclose + dprange * (1.1 / 6)
h3 = dpclose + dprange * (1.1 / 4)
h4 = dpclose + dprange * (1.1 / 2)
h5 = dphigh / dplow * dpclose
l1 = dpclose - dprange * (1.1 / 12)
l2 = dpclose - dprange * (1.1 / 6)
l3 = dpclose - dprange * (1.1 / 4)
l4 = dpclose - dprange * (1.1 / 2)
l5 = dpclose - (h5 - dpclose)

//Tomorrow's Pivot Calculation

tpopen = security(syminfo.tickerid, pivottimeframe, open, barmerge.gaps_off, barmerge.lookahead_on)
tphigh = security(syminfo.tickerid, pivottimeframe, high, barmerge.gaps_off, barmerge.lookahead_on)
tplow = security(syminfo.tickerid, pivottimeframe, low, barmerge.gaps_off, barmerge.lookahead_on)
tpclose = security(syminfo.tickerid, pivottimeframe, close, barmerge.gaps_off, barmerge.lookahead_on)
tprange = tphigh - tplow

tppivot = (tphigh + tplow + tpclose) / 3.0
tpbc = (tphigh + tplow) / 2.0
tptc = tppivot - tpbc + tppivot
tpr1 = tppivot * 2 - tplow
tps1 = tppivot * 2 - tphigh
tph3 = tpclose + tprange * (1.1 / 4)
tpl3 = tpclose - tprange * (1.1 / 4)

//m,w,d in the prefix implies monthly, weekly and daily
mhigh = security(syminfo.tickerid, "M", high[1], lookahead=barmerge.lookahead_on)
mlow = security(syminfo.tickerid, "M", low[1], 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)
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) 

//Plotting
plot(dp and pivot ? pivot : na, title="Pivot", color=#FF007F, style=plot.style_cross, transp=0)
plot(dp and bc ? bc : na, title="BC", color=color.blue, style=plot.style_cross, transp=0)
plot(dp and tc ? tc : na, title="TC", color=color.blue, style=plot.style_cross, transp=0)
plot(dp and r1 ? r1 : na, title="R1", color=color.green, style=plot.style_cross, transp=0)
plot(dp and r2 ? r2 : na, title="R2", color=color.green, style=plot.style_cross, transp=0)
plot(dp and r3 ? r3 : na, title="R3", color=color.green, style=plot.style_cross, transp=0)
plot(dp and r4 ? r4 : na, title="R4", color=color.green, style=plot.style_cross, transp=0)
plot(dp and s1 ? s1 : na, title="S1", color=color.red, style=plot.style_cross, transp=0)
plot(dp and s2 ? s2 : na, title="S2", color=color.red, style=plot.style_cross, transp=0)
plot(dp and s3 ? s3 : na, title="S3", color=color.red, style=plot.style_cross, transp=0)
plot(dp and s4 ? s4 : na, title="S4", color=color.red, style=plot.style_cross, transp=0)

plot((timeframe.isintraday or timeframe.isdaily or timeframe.isweekly) and hl ? mhigh : na, title="Monthly High", style=plot.style_circles, color=#FF7F00, transp=0)
plot((timeframe.isintraday or timeframe.isdaily or timeframe.isweekly) and hl ? mlow : na, title="Monthly Low", style=plot.style_circles, color=#FF7F00, transp=0)
plot((timeframe.isintraday or timeframe.isdaily) and hl ? whigh : na, title="Weekly High", style=plot.style_circles, color=#FF7F00, transp=0)
plot((timeframe.isintraday or timeframe.isdaily) and hl ? wlow : na, title="Weekly Low", style=plot.style_circles, color=#FF7F00, transp=0)
plot(timeframe.isintraday and hl ? dhigh : na, title="Daily High", style=plot.style_circles, color=#FF7F00, transp=0)
plot(timeframe.isintraday and hl ? dlow : na, title="Daily Low", style=plot.style_circles, color=#FF7F00, transp=0)
plot(timeframe.isintraday and hl ? dclose : na, title="Daily Close",style=plot.style_circles, color=#000000, transp=0) 

plot(tp and tppivot ? tppivot : na, title="Pivot", color=color.blue, style=plot.style_cross, transp=0)
plot(tp and tpbc ? tpbc : na, title="BC", color=color.blue, style=plot.style_cross, transp=0)
plot(tp and tptc ? tptc : na, title="TC", color=color.blue, style=plot.style_cross, transp=0)
plot(tp and tpr1 ? tpr1 : na, title="R1", color=color.green, style=plot.style_cross, transp=0)
plot(tp and tps1 ? tps1 : na, title="S1", color=color.red, style=plot.style_cross, transp=0)
plot(tp and tph3 ? tph3 : na, title="H3", color=tph3 != tph3[1] ? na : color.black, transp=0)
plot(tp and tpl3 ? tpl3 : na, title="L3", color=tpl3 != tpl3[1] ? na : color.black, transp=0)

plot(timeframe.isintraday and tp ? tphigh : na, title="High", style=plot.style_circles, color=#FF7F00, transp=0)
plot(timeframe.isintraday and tp ? tplow : na, title="Low", style=plot.style_circles, color=#FF7F00, transp=0)


//Candle Stick Patterns

DJ1 = abs(open - close) < (high - low) * 0.1 and high - low > atr(14)
plotshape(DJ1, title="Doji", location=location.abovebar, color=color.blue, style=shape.xcross)

OR1 = open[1] > close[1] and open < close and low[1] > low and close > high[1] and 
   high - low > atr(14) * 1.25  // or close[1] > open
plotshape(OR1, title="Bullish Engulfing", style=shape.arrowup, color=color.green, location=location.belowbar)

OR2 = open[1] < close[1] and open > close and high[1] < high and close < low[1] and 
   high - low > atr(14) * 1.25  // or close[1] < open
plotshape(OR2, title="Bearish Engulfing", style=shape.arrowdown, color=color.red)

WR1 = low < low[1] and abs(low - min(open, close)) > abs(open - close) * 2 and 
   abs(high - close) < (high - low) * 0.35 and high - low > atr(14)
plotshape(WR1, title="Hammer", location=location.belowbar, color=color.green, style=shape.arrowup)

WR2 = high > high[1] and high - max(open, close) > abs(open - close) * 2 and 
   abs(close - low) < (high - low) * 0.35 and high - low > atr(14)
plotshape(WR2, title="Shooting Star", color=color.red, style=shape.arrowdown)

ER1 = high[1] - low[1] > atr(14) * 2 and 
   abs(open[1] - close[1]) > (high[1] - low[1]) * 0.5 and open[1] > close[1] and 
   open < close  //and abs(open[1] - close[1]) < (high[1]-low[1]) * 0.85
plotshape(ER1, title="Bullish E.Reversal", location=location.belowbar, color=color.green, style=shape.arrowup)  // E denotes Extreme

ER2 = high[1] - low[1] > atr(14) * 2 and 
   abs(open[1] - close[1]) > (high[1] - low[1]) * 0.5 and open[1] < close[1] and 
   open > close  //and abs(open[1] - close[1]) < (high[1]-low[1]) * 0.85
plotshape(ER2, title="Bearish E.Reversal", location=location.abovebar, color=color.red, style=shape.arrowdown)


Disclaimer:

All the pine script’s posted in this section are for learning purpose. Website does not necessarily own these pine script’s and we don’t have any intellectual property rights on them. We might copy useful pine script’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at tradingwithpawan@gmail.com

Leave a Reply

Your email address will not be published.