Future Pivots CPR - All Timeframes
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TonioVolcano

//Simple idea that allows you to display tomorrow CPR based on the high, low and close of today session. Likewise, it works for higher timeframes taking into account the high, low, close of the period (e.g. weekly, monthly, year). Just be aware that -regardless of the timeframe- if the period is still in development, the indicator will constantly/ live update the values untill the period is closed!! This indicator is meant to be used when preparing for the next trading period. If you want to use it live, I'd suggest using the function of this indicator which allows to display only current/closed pivots- 
//Similar to other script I publiushed, this indicator lower timeframes (Daily and Weekly) will work with lower timeframe bars, this is the Minutes and Hour bars. Conversely, higher timeframe CPR/Pivots will work better with timeframes/charts from Daily and above.

//@version=5

indicator('Future Pivots CPR - All Timeframes', shorttitle='Future Pivots', overlay=true)

// User inputs
tf1 = input.timeframe(defval='D', title='Timeframe', options=['D', 'W', 'M', '12M'])
showToday = input(defval=false, title='Show today\'s CPR')
showTomorrowCPR = input(defval=true, title='Show tomorrow\'s CPR')
showHistoricalCPR = input(defval=false, title='Show historical CPR')
showR3S3 = input(defval=false, title='Show R2-3 & S2-3')
cprColor = input.color(defval = color.white, title = 'Color current CPR')
cprColorY = input.color(defval = color.green, title = 'Color past/future CPR')

// Defaults
highs = high 
lows = low
pivot = hlc3
top = hl2

// Location of label and Line for Current Pivots
var cpb = 0
var ppb = 0

if tf1 == 'D' and hour < hour[1]
    ppb := cpb
    cpb := 1
    cpb
if tf1 == 'D' and hour >= hour[1]
    cpb := cpb + 1
    cpb
if tf1 == 'W' and dayofweek < dayofweek[1]
    ppb := cpb
    cpb := 1
    cpb
if tf1 == 'W' and dayofweek >= dayofweek[1]
    cpb := cpb + 1
    cpb
if tf1 == 'M' and dayofmonth < dayofmonth[1]
    ppb := cpb
    cpb := 1
    cpb
if tf1 == 'M' and dayofmonth >= dayofmonth[1]
    cpb := cpb + 1
    cpb
if tf1 == '12M' and month < month[1]
    ppb := cpb
    cpb := 1
    cpb
if tf1 == '12M' and month >= month[1]
    cpb := cpb + 1
    cpb

// Location of label for Tomorrow Pivots
tomstart = time_close

// Calculate Today's CPR
calcclose = request.security(syminfo.tickerid, tf1, close[1], barmerge.gaps_off, barmerge.lookahead_on) 
calchigh = request.security(syminfo.tickerid, tf1, highs[1], barmerge.gaps_off, barmerge.lookahead_on)
calclow = request.security(syminfo.tickerid, tf1, lows[1], barmerge.gaps_off, barmerge.lookahead_on)
calcpivot = math.round(request.security(syminfo.tickerid, tf1, pivot[1], barmerge.gaps_off, barmerge.lookahead_on), 2)
calcbott = math.round(request.security(syminfo.tickerid, tf1, top[1], barmerge.gaps_off, barmerge.lookahead_on), 2) 
calctop = math.round((calcpivot - calcbott) + calcpivot, 2)

// Resistance Levels
R1 = math.round((calcpivot * 2) - calclow, 2)
R2 = math.round(calcpivot + (calchigh - calclow), 2)
R3 = math.round(R1 + (calchigh - calclow), 2)

// Support Levels
S1 = math.round((calcpivot * 2) - calchigh, 2)
S2 = math.round(calcpivot - (calchigh - calclow), 2)
S3 = math.round(S1 - (calchigh - calclow), 2)

// Plot Today's CPR
if showToday
    if showR3S3
        _r3 = line.new(bar_index[cpb], R3, bar_index, R3, xloc.bar_index, color=cprColor)
        line.delete(_r3[1])
        _r2 = line.new(bar_index[cpb], R2, bar_index, R2, xloc.bar_index, color=cprColor)
        line.delete(_r2[1])
    _r1 = line.new(bar_index[cpb], R1, bar_index, R1, xloc.bar_index, color=cprColor)
    line.delete(_r1[1])


    _tc = line.new(bar_index[cpb], calctop, bar_index, calctop, xloc.bar_index, color=cprColor)
    line.delete(_tc[1])
    _p = line.new(bar_index[cpb], calcpivot, bar_index, calcpivot, xloc.bar_index, color=cprColor)
    line.delete(_p[1])
    _bc = line.new(bar_index[cpb], calcbott, bar_index, calcbott, xloc.bar_index, color=cprColor)
    line.delete(_bc[1])


    _s1 = line.new(bar_index[cpb], S1, bar_index, S1, xloc.bar_index, color=cprColor)
    line.delete(_s1[1])
    if showR3S3
        _s2 = line.new(bar_index[cpb], S2, bar_index, S2, xloc.bar_index, color=cprColor)
        line.delete(_s2[1])
        _s3 = line.new(bar_index[cpb], S3, bar_index, S3, xloc.bar_index, color=cprColor)
        line.delete(_s3[1])

// Plot Today's Labels
if showToday
    if showR3S3
        l_r3 = label.new(bar_index[cpb], R3, text='R3', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
        label.delete(l_r3[1])
        l_r2 = label.new(bar_index[cpb], R2, text='R2', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
        label.delete(l_r2[1])
    l_r1 = label.new(bar_index[cpb], R1, text='R1', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
    label.delete(l_r1[1])


    l_tc = label.new(bar_index[cpb], calctop, text='__', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
    label.delete(l_tc[1])
    l_p = label.new(bar_index[cpb], calcpivot, text='[P]', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
    label.delete(l_p[1])
    l_bc = label.new(bar_index[cpb], calcbott, text='__', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
    label.delete(l_bc[1])


    l_s1 = label.new(bar_index[cpb], S1, text='S1', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
    label.delete(l_s1[1])
    if showR3S3
        l_s2 = label.new(bar_index[cpb], S2, text='S2', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
        label.delete(l_s2[1])
        l_s3 = label.new(bar_index[cpb], S3, text='S3', xloc=xloc.bar_index, textcolor=cprColor, style=label.style_none)
        label.delete(l_s3[1])


// Calculate Tomorrow's CPR
Tcalchigh = request.security(syminfo.tickerid, tf1, highs[0], barmerge.gaps_off, barmerge.lookahead_on)
Tcalclow = request.security(syminfo.tickerid, tf1, lows[0], barmerge.gaps_off, barmerge.lookahead_on)
Tcalcpivot = math.round(request.security(syminfo.tickerid, tf1, pivot[0], barmerge.gaps_off, barmerge.lookahead_on), 2)
Tcalcbott = math.round(request.security(syminfo.tickerid, tf1, top[0], barmerge.gaps_off, barmerge.lookahead_on), 2) 
Tcalctop = math.round((Tcalcpivot - Tcalcbott) + Tcalcpivot, 2)

// Resistance Levels
tR1 = math.round((Tcalcpivot * 2) - Tcalclow, 2)
tR2 = math.round(Tcalcpivot + (Tcalchigh - Tcalclow), 2)
tR3 = math.round(tR1 + (Tcalchigh - Tcalclow), 2)

// Support Levels
tS1 = math.round((Tcalcpivot * 2) - Tcalchigh, 2)
tS2 = math.round(Tcalcpivot - (Tcalchigh - Tcalclow), 2)
tS3 = math.round(tS1 - (Tcalchigh - Tcalclow), 2)

// Plot Tomorrow's Labels
if showTomorrowCPR
    if showR3S3
        l_t_r3 = label.new(tomstart, tR3, text='R3', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(tR3)))
        label.delete(l_t_r3[1])
        l_t_r2 = label.new(tomstart, tR2, text='R2', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(tR2)))
        label.delete(l_t_r2[1])
    l_t_r1 = label.new(tomstart, tR1, text='R1', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(tR1)))
    label.delete(l_t_r1[1])

    l_t_tc = label.new(tomstart, Tcalctop, text='__', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(Tcalctop)))
    label.delete(l_t_tc[1])
    l_t_p = label.new(tomstart, Tcalcpivot, text='[P]', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(Tcalcpivot)))
    label.delete(l_t_p[1])
    l_t_bc = label.new(tomstart, Tcalcbott, text='__', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(Tcalcbott)))
    label.delete(l_t_bc[1])

    l_t_s1 = label.new(tomstart, tS1, text='S1', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(tS1)))
    label.delete(l_t_s1[1])
    if showR3S3
        l_t_s2 = label.new(tomstart, tS2, text='S2', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(tS2)))
        label.delete(l_t_s2[1])
        l_t_s3 = label.new(tomstart, tS3, text='S3', xloc=xloc.bar_time, textcolor=cprColorY, style=label.style_none, tooltip=str.tostring(float(tS3)))
        label.delete(l_t_s3[1])

p_cprTC = plot(showHistoricalCPR ? calctop : na, title=' TC', color=cprColorY, style= plot.style_circles)
p_cprP = plot(showHistoricalCPR ? calcpivot : na, title='[P]', color=cprColorY, style= plot.style_circles)
p_cprBC = plot(showHistoricalCPR ? calcbott : na, title=' BC', color=cprColorY, style= plot.style_circles)
