Multi Pivots
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tyffy_2k

//@version=5

indicator("Multi Pivots",overlay=true,max_lines_count=90)

runCPR = input.bool(title='RCPR', defval=false, inline='1', group='CPR')
showTomorrowCPR = input.bool(title='TCPR', defval=false, inline='1', group='CPR')
showTodayCPR = input.bool(title='DCPR', defval=false, inline='2', group='CPR')
showTodayRes = input.bool(title='DRES', defval=false, inline='2', group='CPR')
showTodaySup = input.bool(title='DSUP', defval=false, inline='2', group='CPR')
pzone = input.bool(title='PZone', defval=false, inline='2', group='CPR')
showCamRes = input.bool(title='CAMH', defval=false, inline='1', group='CAMARILLAS')
showCamSup = input.bool(title='CAML', defval=false, inline='1', group='CAMARILLAS')
showadr = input.bool(title='ADR', defval=false, inline='1', group='CAMARILLAS')
show_table = input(true,  "Dash", inline='11' , group = 'Others')
fibs = input.bool(false, title='Fibs', inline='11', group = 'Others')
use_tp      = input(false, "Signals" , inline='12')


//disp_drangeh = input.bool(false, title='DRangeH', inline='1', group='Day Range')
//disp_drangel = input.bool(false, title='DRangeL', inline='1', group='Day Range')

AUTO = 'Auto'
DAILY = 'Daily'
WEEKLY = 'Weekly'
MONTHLY = 'Monthly'
QUARTERLY = 'Quarterly'
YEARLY = 'Yearly'
BIYEARLY = 'Biyearly'
TRIYEARLY = 'Triyearly'
QUINQUENNIAL = 'Quinquennial'

//pivot_time_frame = input.string(title='Pivots Timeframe', defval=AUTO, options=[AUTO, DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, BIYEARLY, TRIYEARLY, QUINQUENNIAL])
pivot_time_frame = AUTO
get_pivot_resolution() =>
    resolution = 'M'
    if pivot_time_frame == AUTO
        if timeframe.isintraday
            resolution := timeframe.multiplier <= 15 ? 'D' : 'W'
            resolution
        else if timeframe.isweekly or timeframe.ismonthly
            resolution := '12M'
            resolution
    else if pivot_time_frame == DAILY
        resolution := 'D'
        resolution
    else if pivot_time_frame == WEEKLY
        resolution := 'W'
        resolution
    else if pivot_time_frame == MONTHLY
        resolution := 'M'
        resolution
    else if pivot_time_frame == QUARTERLY
        resolution := '3M'
        resolution
    else if pivot_time_frame == YEARLY or pivot_time_frame == BIYEARLY or pivot_time_frame == TRIYEARLY or pivot_time_frame == QUINQUENNIAL
        resolution := '12M'
        resolution
    resolution

resolution = get_pivot_resolution()


//Day CPR

// Function to calculate pivot
CalculatePivot(_o,dailyHigh,dailyLow,dailyClose)=>

    cprpivot = (dailyHigh + dailyLow + dailyClose) / 3  //Central Povit
    cprBC = (dailyHigh + dailyLow) / 2  //Below Central povit
    cprTC = cprpivot - cprBC + cprpivot  //Top Central povot

//3  support levels
    S1 = cprpivot * 2 - dailyHigh
    S2 = cprpivot - (dailyHigh - dailyLow)
    S3 = dailyLow - 2 * (dailyHigh - cprpivot)
    S4 = S3 - (S1 - S2)
    S5 = S4 - (S2 - S3)

//3  resistance levels
    R1 = cprpivot * 2 - dailyLow
    R2 = cprpivot + dailyHigh - dailyLow
    R3 = dailyHigh + 2 * (cprpivot - dailyLow)
    R4 = R3 + R2 - R1
    R5 = R4 + R3 - R2
    //[S1,S2,S3,S4,S5,cprBC,cprpivot,cprTC,R1,R2,R3,R4,R5,dailyHigh,dailyLow,dailyClose]
    
//Fib levels

    FS1 = cprpivot - (dailyHigh - dailyLow) * 0.382
    FS2 = cprpivot - (dailyHigh - dailyLow) * 0.618
    FS3 = cprpivot - (dailyHigh - dailyLow) * 1

    FR1 = cprpivot + (dailyHigh - dailyLow) * 0.382
    FR2 = cprpivot + (dailyHigh - dailyLow) * 0.618
    FR3 = cprpivot + (dailyHigh - dailyLow) * 1
    [S1,S2,S3,S4,S5,cprBC,cprpivot,cprTC,R1,R2,R3,R4,R5,dailyHigh,dailyLow,dailyClose,FS1,FS2,FS3,FR1,FR2,FR3]    
    

    

//MTF pivots 
//[S1,S2,S3,S4,S5,cprBC,cprpivot,cprTC,R1,R2,R3,R4,R5,D_High,D_Low,D_Close] = request.security(syminfo.tickerid, resolution, CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[S1,S2,S3,S4,S5,cprBC,cprpivot,cprTC,R1,R2,R3,R4,R5,D_High,D_Low,D_Close,FS1,FS2,FS3,FR1,FR2,FR3] = request.security(syminfo.tickerid, resolution, CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[ws1,ws2,ws3,ws4,ws5,wbc,wvpivot,wtc,wr1,wr2,wr3,wr4,wr5,wH,wL,wC] = request.security(syminfo.tickerid, resolution, CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[ms1,ms2,ms3,ms4,ms5,mbc,mvpivot,mtc,mr1,mr2,mr3,mr4,mr5,mH,mL,mC] = request.security(syminfo.tickerid, resolution , CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[dws1,dws2,dws3,dws4,dws5,dwbc,dwvpivot,dwtc,dwr1,dwr2,dwr3,dwr4,dwr5,dwH,dwL,dwC] = request.security(syminfo.tickerid, 'W', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[dms1,dms2,dms3,dms4,dms5,dmbc,dmvpivot,dmtc,dmr1,dmr2,dmr3,dmr4,dmr5,dmH,dmL,dmC] = request.security(syminfo.tickerid, '1M', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[dhigh,dlow,dclose,dopen] = request.security(syminfo.tickerid, 'D', [high,low,close,open])
[sec_open, sec_high, sec_low, sec_close] = request.security(syminfo.tickerid, resolution, [open, high, low, close], lookahead=barmerge.lookahead_on)
[dS1,dS2,dS3,dS4,dS5,dcprBC,dcprpivot,dcprTC,dR1,dR2,dR3,dR4,dR5,dD_High,dD_Low,dD_Close] = request.security(syminfo.tickerid, 'D', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)


intday = timeframe.isintraday and timeframe.multiplier < 31
intday1 = timeframe.isintraday and timeframe.multiplier < 30

// Function returns 1 when starting with first bar of the day 
firstbar(resolution) =>
    ch = 0
    if resolution == 'Y'
        t = year(time('D'))
        ch := ta.change(t) != 0 ? 1 : 0
        ch
    else
        t = time(resolution)
        ch := ta.change(t) != 0 ? 1 : 0
        ch
    ch

pp_res = resolution
bars_sinse = 0
bars_sinse := firstbar(pp_res) ? 0 : bars_sinse[1] + 1

truncate(number, decimals) =>
    factor = math.pow(10, decimals)
    int(number * factor) / factor

off_mult = 5
//l_size = size.tiny
l_size = size.small
ll_offset = timenow + math.round(ta.change(time) * off_mult)

lTransp = 30
cprColor = #6050DC
DayS1Color = S1 != S1[1] ? na : #26a69a
DayR1Color = R1 != R1[1] ? na : #e91e63
DHColor = D_High != D_High[1] ? na : color.yellow

line_TC = line.new(bar_index[math.min(bars_sinse, 376)], showTodayCPR?cprTC :na, bar_index, showTodayCPR?cprTC :na, color=cprColor,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_TC[1])
line_Pivot = line.new(bar_index[math.min(bars_sinse, 376)], showTodayCPR?cprpivot :na, bar_index, showTodayCPR?cprpivot :na, color=cprColor,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_Pivot[1])
line_BC = line.new(bar_index[math.min(bars_sinse, 376)], showTodayCPR?cprBC :na, bar_index, showTodayCPR?cprBC :na, color=cprColor,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_BC[1])

line_R1 = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes?R1 :na, bar_index, showTodayRes?R1 :na, color=DayR1Color,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_R1[1])
line_R2 = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes?R2 :na, bar_index, showTodayRes?R2 :na, color=DayR1Color,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_R2[1])
line_R3 = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes?R3 :na, bar_index, showTodayRes?R3 :na, color=DayR1Color,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_R3[1])
line_DH = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes?D_High :na, bar_index, showTodayRes?D_High :na, color=DHColor,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_DH[1])

line_S1 = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup?S1 :na, bar_index, showTodaySup?S1 :na, color=DayS1Color,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_S1[1])
line_S2 = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup?S2 :na, bar_index, showTodaySup?S2 :na, color=DayS1Color,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_S2[1])
line_S3 = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup?S3 :na, bar_index, showTodaySup?S3 :na, color=DayS1Color,  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_S3[1])
line_DL = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup?D_Low :na, bar_index, showTodaySup?D_Low :na, color=DHColor,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_DL[1])


l_WPivot = line.new(bar_index[math.max(bars_sinse, 100)], showTodayCPR and intday ? dwvpivot : na, bar_index, showTodayCPR ? dwvpivot : na, color=color.new(color.purple,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
line.delete(l_WPivot[1])
//l_MPivot = line.new(bar_index[math.max(bars_sinse, 100)], showTodayCPR and intday ? dmvpivot : na, bar_index, showTodayCPR ? dmvpivot : na, color=color.new(color.green,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
//line.delete(l_MPivot[1])


//l_dws1 = line.new(bar_index[math.max(bars_sinse, 100)], showTodaySup and intday ? dws1 : na, bar_index, showTodayCPR ? dws1 : na, color=color.new(DayS1Color,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
//line.delete(l_dws1[1])
//l_dwr1 = line.new(bar_index[math.max(bars_sinse, 100)], showTodayRes and intday ? dwr1 : na, bar_index, showTodayCPR ? dwr1 : na, color=color.new(DayR1Color,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
//line.delete(l_dwr1[1])
//l_WHigh = line.new(bar_index[math.max(bars_sinse, 100)], showTodayRes and intday ? dwH : na, bar_index, showTodayCPR ? dwH : na, color=color.new(#F3E5AB,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
//line.delete(l_WHigh[1])
//l_WLow = line.new(bar_index[math.max(bars_sinse, 100)], showTodaySup and intday ? dwL : na, bar_index, showTodayCPR ? dwL : na, color=color.new(#F3E5AB,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
//line.delete(l_WLow[1])


//Pivot Zones

_Index = syminfo.root

R1val = (_Index =='BANKNIFTY') ?   36 : 17

R2val = ((R1 - R2) /5 + (R2 - R3) /5) /2
R3val = ((R3 - R2) /5)/2

line_R1top = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes and pzone?R1 + R1val :na, bar_index, showTodayRes and pzone?R1 + R1val :na, color=color.new(DayR1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_R1top[1])
line_R1bot = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes and pzone?R1 - R1val :na, bar_index, showTodayRes and pzone?R1 - R1val :na, color=color.new(DayR1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_R1bot[1])

line_R2top = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes and pzone?R2 + R2val :na, bar_index, showTodayRes and pzone?R2 + R2val :na, color=color.new(DayR1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_R2top[1])
line_R2bot = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes and pzone?R2 - R2val :na, bar_index, showTodayRes and pzone?R2 - R2val :na, color=color.new(DayR1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_R2bot[1])

line_R3top = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes and pzone?R3 + R3val :na, bar_index, showTodayRes and pzone?R3 + R3val :na, color=color.new(DayR1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_R3top[1])
line_R3bot = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes and pzone?R3 - R3val :na, bar_index, showTodayRes and pzone?R3 - R3val :na, color=color.new(DayR1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_R3bot[1])

linefill.new(line_R1top, line_R1bot, color.new(color.red, 80))
linefill.new(line_R2top, line_R2bot, color.new(color.red, 80))
linefill.new(line_R3top, line_R3bot, color.new(color.red, 80))

S1val = (_Index =='BANKNIFTY') ?   36 : 17
S2val = ((S1 - S2) /5 + (S2 - S3) /5) /2
S3val = ((S3-S2) /5)/2

line_S1top = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup and pzone?S1 + S1val :na, bar_index, showTodaySup and pzone?S1 + S1val :na, color=color.new(DayS1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_S1top[1])
line_S1bot = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup and pzone?S1 - S1val :na, bar_index, showTodaySup and pzone?S1 - S1val :na, color=color.new(DayS1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_S1bot[1])

line_S2top = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup and pzone?S2 + S2val :na, bar_index, showTodaySup and pzone?S2 + S2val :na, color=color.new(DayS1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_S1top[1])
line_S2bot = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup and pzone?S2 - S2val :na, bar_index, showTodaySup and pzone?S2 - S2val :na, color=color.new(DayS1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_S2bot[1])

line_S3top = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup and pzone?S3 + S3val :na, bar_index, showTodaySup and pzone?S3 + S3val :na, color=color.new(DayS1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_S3top[1])
line_S3bot = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup and pzone?S3 - S3val :na, bar_index, showTodaySup and pzone?S3 - S3val :na, color=color.new(DayS1Color,100),  style =  line.style_solid,width=2, extend = extend.none)
line.delete(line_S3bot[1])


linefill.new(line_S1top, line_S1bot, color.new(color.green, 90))
linefill.new(line_S2top, line_S2bot, color.new(color.green, 90))
linefill.new(line_S3top, line_S3bot, color.new(color.green, 90))

//Pivot Zones

//Fib Pivots

line_FR1 = line.new(bar_index[math.min(bars_sinse, 376)], fibs ? FR1 : na, bar_index, fibs ? FR1 : na, color=DayR1Color,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_FR1[1])
line_FR2 = line.new(bar_index[math.min(bars_sinse, 376)], fibs ? FR2 : na, bar_index, fibs ? FR2 : na, color=DayR1Color,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_FR2[1])
line_FR3 = line.new(bar_index[math.min(bars_sinse, 376)], fibs ? FR3 : na, bar_index, fibs ? FR3 : na, color=DayR1Color,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_FR3[1])

line_FS1 = line.new(bar_index[math.min(bars_sinse, 376)], fibs ? FS1 : na, bar_index, fibs ? FS1 : na, color=DayS1Color,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_FS1[1])
line_FS2 = line.new(bar_index[math.min(bars_sinse, 376)], fibs ? FS2 : na, bar_index, fibs ? FS2 : na, color=DayS1Color,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_FS2[1])
line_FS3 = line.new(bar_index[math.min(bars_sinse, 376)], fibs ? FS3 : na, bar_index, fibs ? FS3 : na, color=DayS1Color,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_FS3[1])

//Running CPR

getSeries(e, timeFrame) => request.security(syminfo.tickerid, 'D', e, lookahead=barmerge.lookahead_on)
tH = getSeries(high, 'D')
tL = getSeries(low, 'D')
tC = getSeries(close, 'D')

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

pcolor2  = tTC < tBC ? color.blue:color.purple

vcpr = tL > math.max(dcprTC, dcprBC) or tH < math.min(dcprTC, dcprBC) ? color.yellow : #6050DC

//Plotting Day Pivot
//plot(disp_d_cpr ? DayPivot:na, title = "DPivot" , color = DayPivotColor, style = plot.style_circles, linewidth =1)
plot(runCPR and intday1 ? dcprpivot : na, title='DPivot', color=color.new(vcpr, 50), style=plot.style_circles, linewidth=1)
DBC = plot(runCPR and intday1 ? dcprBC : na, title='DBC', color=color.new(vcpr, 50), style=plot.style_circles, linewidth=1)
DTC = plot(runCPR and intday1 ? dcprTC : na, title='DTC', color=color.new(vcpr, 50), style=plot.style_circles, linewidth=1)

DayPivotColor = dcprpivot != dcprpivot[1] ? na : #6050DC

fTransp = 80
fcolor = dcprpivot != dcprpivot[1] ? na : color.blue
fcolor1 = dcprTC != dcprTC[1] ? na : color.purple
pcolor1 = dcprTC > dcprBC ? fcolor : fcolor1
fill(DTC, DBC, color=color.new(pcolor1, fTransp))


//Camarillas

r = D_High - D_Low

//Calculate pivots
CR6 = D_High / D_Low * D_Close  //Bull target 2
CR4 = D_Close + r * (1.1 / 2)  //Bear Last Stand
CR3 = D_Close + r * (1.1 / 4)  //Bear Reversal Low
CR5 = CR4 + 1.168 * (CR4 - CR3)  //Bull Target 1
CR35 = CR4 - (CR4 - CR3) / 2  //Bear Reversal High
CS3 = D_Close - r * (1.1 / 4)  //Bull Reversal High 
CS4 = D_Close - r * (1.1 / 2)  //Bull Last Stand 
CS6 = D_Close - (CR6 - D_Close)  //Bear Target 2
CS35 = CS3 - (CS3 - CS4) / 2  //Bull Reversal Low
CS5 = CS4 - 1.168 * (CS3 - CS4)  //Bear Target 1

line_CR4 = line.new(bar_index[math.min(bars_sinse, 376)], showCamRes?CR4 :na, bar_index, showCamRes?CR4 :na, color=color.new(#7CFC00, lTransp),  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_CR4[1])
line_CR3 = line.new(bar_index[math.min(bars_sinse, 376)], showCamRes?CR3 :na, bar_index, showCamRes?CR3 :na, color=color.new(#7CFC00, lTransp),  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_CR3[1])
line_CR35 = line.new(bar_index[math.min(bars_sinse, 376)], showCamRes?CR35 :na, bar_index, showCamRes?CR35 :na, color=color.new(color.red, lTransp),  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_CR35[1])

line_CS3 = line.new(bar_index[math.min(bars_sinse, 376)], showCamSup?CS3 :na, bar_index, showCamSup?CS3 :na, color=color.new(#7CFC00, lTransp),  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_CS3[1])
line_CS4 = line.new(bar_index[math.min(bars_sinse, 376)], showCamSup?CS4 :na, bar_index, showCamSup?CS4 :na, color=color.new(#7CFC00, lTransp),  style =  line.style_dotted,width=2, extend = extend.none)
line.delete(line_CS4[1])
line_CS35 = line.new(bar_index[math.min(bars_sinse, 376)], showCamRes?CS35 :na, bar_index, showCamSup?CS35 :na, color=color.new(color.green, lTransp),  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_CS35[1])



//Daily Range

onintraChart = timeframe.isintraday
lTransp1 = 30

OPEN = request.security(syminfo.tickerid, 'D', open, barmerge.gaps_off, barmerge.lookahead_on)

//ADR L
dayrange = high - low

r1 = request.security(syminfo.tickerid, resolution, dayrange[1], barmerge.gaps_off, barmerge.lookahead_on)
r2 = request.security(syminfo.tickerid, resolution, dayrange[2], barmerge.gaps_off, barmerge.lookahead_on)
r3 = request.security(syminfo.tickerid, resolution, dayrange[3], barmerge.gaps_off, barmerge.lookahead_on)
r4 = request.security(syminfo.tickerid, resolution, dayrange[4], barmerge.gaps_off, barmerge.lookahead_on)
r5 = request.security(syminfo.tickerid, resolution, dayrange[5], barmerge.gaps_off, barmerge.lookahead_on)
r6 = request.security(syminfo.tickerid, resolution, dayrange[6], barmerge.gaps_off, barmerge.lookahead_on)
r7 = request.security(syminfo.tickerid, resolution, dayrange[7], barmerge.gaps_off, barmerge.lookahead_on)
r8 = request.security(syminfo.tickerid, resolution, dayrange[8], barmerge.gaps_off, barmerge.lookahead_on)
r9 = request.security(syminfo.tickerid, resolution, dayrange[9], barmerge.gaps_off, barmerge.lookahead_on)
r10 = request.security(syminfo.tickerid, resolution, dayrange[10], barmerge.gaps_off, barmerge.lookahead_on)

adr_10 = (r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9 + r10) / 10
adr_9 = (r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9) / 9
adr_8 = (r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8) / 8
adr_7 = (r1 + r2 + r3 + r4 + r5 + r6 + r7) / 7
adr_6 = (r1 + r2 + r3 + r4 + r5 + r6) / 6
adr_5 = (r1 + r2 + r3 + r4 + r5) / 5
adr_4 = (r1 + r2 + r3 + r4) / 4
adr_3 = (r1 + r2 + r3) / 3
adr_2 = (r1 + r2) / 2
adr_1 = r1


//plot 
adrhigh10 = OPEN + adr_10 / 2
adrlow10 = OPEN - adr_10 / 2
adrhigh5 = OPEN + adr_5 / 2
adrlow5 = OPEN - adr_5 / 2

//if disp_drangeh

    //_d_adrhigh10 = line.new(start, adrhigh10, end, adrhigh10, xloc.bar_time, color=color.new(color.silver, lTransp1), style=line.style_dotted, width=1)
    //line.delete(_d_adrhigh10[1])
   // _d_adrhigh5 = line.new(start, adrhigh5, end, adrhigh5, xloc.bar_time, color=color.new(color.silver, lTransp1), style=line.style_dotted, width=1)
   // line.delete(_d_adrhigh5[1])
    
   // linefill.new(_d_adrhigh10, _d_adrhigh5, color.new(color.red, 80))

//if disp_drangel

    //_d_adrlow10 = line.new(start, adrlow10, end, adrlow10, xloc.bar_time, color=color.new(color.silver, lTransp1), style=line.style_dotted, width=1)
    //line.delete(_d_adrlow10[1])
   // _d_adrlow5 = line.new(start, adrlow5, end, adrlow5, xloc.bar_time, color=color.new(color.silver, lTransp1), style=line.style_dotted, width=1)
   // line.delete(_d_adrlow5[1])
    
    
   // linefill.new(_d_adrlow10, _d_adrlow5, color.new(color.green, 80))   
    

if onintraChart    
    line_adrhigh10 = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes?adrhigh10 :na, bar_index, showTodayRes?adrhigh10 :na, color=color.new(color.silver, lTransp1),  style =  line.style_dotted,width=1, extend = extend.none)
    line.delete(line_adrhigh10[1])   
    line_adrhigh5 = line.new(bar_index[math.min(bars_sinse, 376)], showTodayRes?adrhigh5 :na, bar_index, showTodayRes?adrhigh5 :na, color=color.new(color.silver, lTransp1),  style =  line.style_dotted,width=1, extend = extend.none)
    line.delete(line_adrhigh5[1])

    line_adrlow10 = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup?adrlow10 :na, bar_index, showTodaySup?adrlow10 :na, color=color.new(color.silver, lTransp1),  style =  line.style_dotted,width=1, extend = extend.none)
    line.delete(line_adrlow10[1])
    line_adrlow5 = line.new(bar_index[math.min(bars_sinse, 376)], showTodaySup?adrlow5 :na, bar_index, showTodaySup?adrlow5 :na, color=color.new(color.silver, lTransp1),  style =  line.style_dotted,width=1, extend = extend.none)
    line.delete(line_adrlow5[1])


    linefill.new(line_adrhigh10, line_adrhigh5, color.new(color.silver, 80))
    linefill.new(line_adrlow10, line_adrlow5, color.new(color.silver, 80))

//Daily Range

//200 EMA

longest = ta.ema(close, 200)
plot(longest, color = color.new(color.white,60))

//Signals

atrPeriod = 23
factor = 2.6




tp_trigger  = 0.7 * 0.01
ttp_trigger = 0.5 * 0.01
sl_trigger  = 0.4 * 0.01

[supertrend, direction] = ta.supertrend(factor, atrPeriod)


upTrend = plot(use_tp ? direction < 0 ? supertrend : na : na, "Up Trend", color = color.green, style=plot.style_circles, show_last = 5)
downTrend = plot(use_tp ? direction < 0? na : supertrend : na, "Down Trend", color = color.red, style=plot.style_circles, show_last = 5)

plotshape(use_tp ? ta.crossover(close,supertrend) : na ,text="BUY",color=color.green,textcolor=color.green,location=location.belowbar, show_last = 25)
plotshape(use_tp ? ta.crossunder(close, supertrend): na ,text="SELL",color=color.red,textcolor=color.red, show_last = 25)

buy=ta.crossover(close,supertrend)
sell=ta.crossunder(close,supertrend)


since_buy       = ta.barssince(buy)
since_sell      = ta.barssince(sell)
buy_trend       = since_sell > since_buy
sell_trend      = since_sell < since_buy
change_trend    = (buy_trend and sell_trend[1]) or (sell_trend and buy_trend[1])


entry_price = ta.valuewhen(buy or sell, close, 0)


var tp_price_trigger    = -1.0
var sl_price_trigger    = -1.0
var ttp_price_trigger    = -1.0
var is_tp_trigger_hit   = false
var tp_trail            = 0.0
tp_close_long       = false
tp_close_short      = false

if use_tp

    if change_trend

        tp_price_trigger    := entry_price * (1.0 + (buy_trend ? 1.0 : sell_trend ? -1.0 : na) * tp_trigger)
        sl_price_trigger    := entry_price * (1.0 + (buy_trend ? -1.0 : sell_trend ? 1.0 : na) * sl_trigger)
        ttp_price_trigger    := entry_price * (1.0 + (buy_trend ? 1.0 : sell_trend ? -2.4 : na) * tp_trigger + ttp_trigger)
        is_tp_trigger_hit   := false


    if buy_trend
 
        is_tp_trigger_hit   := (high >= tp_price_trigger) or (not change_trend and is_tp_trigger_hit[1])


        tp_trail            := math.max(high, change_trend ? tp_price_trigger : tp_trail[1])
        tp_close_long       := (is_tp_trigger_hit and (high <= tp_trail * (1.0 - ttp_trigger))) or (not change_trend and tp_close_long[1])
    else if sell_trend

        is_tp_trigger_hit   := (low <= tp_price_trigger) or (not change_trend and is_tp_trigger_hit[1])


        tp_trail            := math.min(low, change_trend ? tp_price_trigger : tp_trail[1])
        tp_close_short      := (is_tp_trigger_hit and (low >= tp_trail * (1.0 + ttp_trigger))) or (not change_trend and tp_close_short[1])


plot(use_tp and tp_price_trigger != -1.0 ? tp_price_trigger : na,title='Take Profit Price Trigger', color=color.blue,style=plot.style_cross, linewidth=1, show_last = 5)
plot(use_tp and sl_price_trigger != -1.0 ? sl_price_trigger : na,title='SL Trigger', color=color.red,style=plot.style_cross, linewidth=1, show_last = 5)
plot(use_tp ? entry_price : na,title='Entry Price Trigger', color=color.yellow,style=plot.style_cross, linewidth=1, show_last=5)
plot(use_tp and ttp_price_trigger != -1.0 ? ttp_price_trigger : na,title='Trailing Profit Price Trigger', color=color.blue,style=plot.style_cross, linewidth=1, show_last =5)
buytargethit = ta.crossover(high,tp_price_trigger)
selltargethit = ta.crossunder(low,tp_price_trigger)

selltargethit1 = ta.crossunder(low, ta.valuewhen(selltargethit,low,0))

plotshape(use_tp and buy_trend and buytargethit,color=color.green,location=location.abovebar,style=shape.diamond,size=size.tiny, show_last = 25)
plotshape(use_tp and sell_trend and selltargethit,color=color.red,location=location.belowbar,style=shape.diamond,size=size.tiny, show_last = 25)

cond1 = use_tp and buy_trend and open > entry_price and close > entry_price ? color.orange : na
cond2 = use_tp and sell_trend and open < entry_price and close < entry_price ? color.purple : na
//barcolor(cond1, show_last = 75)
//barcolor(cond2, show_last = 75)

//

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


    _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
    tom_end
    

//Day_High/Low

showDHL = input.bool(false, title='DHL', inline='11', group = 'Others')

line_th_d = line.new(bar_index[math.min(bars_sinse, 376)], showDHL?dhigh :na, bar_index, showDHL?dhigh :na, color=#0FC0FC,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_th_d[1])
label_th_d = label.new(bar_index, showDHL?dhigh : na, text="HoD",  textcolor=#0FC0FC, style= label.style_none, size=size.small)
label.delete(label_th_d[1])

line_tl_d = line.new(bar_index[math.min(bars_sinse, 376)], showDHL?dlow :na, bar_index, showDHL?dlow :na, color=#0FC0FC,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_tl_d[1])
label_tl_d = label.new(bar_index, showDHL?dlow : na, text="LoD", textcolor=#0FC0FC, style= label.style_none, size=size.small)
label.delete(label_tl_d[1])


// Get High & Low 60

showORB = input.bool(false, title='IB', inline='11', group = 'Others')

orbTimeFrame = "60"
sessSpec = "0915-1530"
rColor = color.red
sColor = color.green

getSeries1(_e, _timeFrame) => request.security(syminfo.tickerid, _timeFrame, _e, lookahead=barmerge.lookahead_on) 

is_newbar(res, sess) =>
    t = time(res, sess)
    na(t[1]) and not na(t) or t[1] < t

newbar = is_newbar("375", sessSpec)

var float orbH = na
var float orbL = na
var float orbH1 = na
var float orbL1 = na
if newbar
    if newbar
        orbH := getSeries1(high, '60')
        orbL := getSeries1(low, '60')
    
// Today's Session Start timestamp
y1 = year(timenow)
m1 = month(timenow)
d1 = dayofmonth(timenow)

// Start & End time for Today
start1 = timestamp(y1, m1, d1, 09, 15)
end1 = start1 + 86400000

// Plot only if session started
isToday1 = timenow > start1

// Plot selected timeframe's High, Low & Avg

line_orbH = line.new(bar_index[math.min(bars_sinse, 376)], showORB and intday ?orbH :na, bar_index, showORB?orbH :na, color=rColor,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_orbH[1])
label_orbH = label.new(bar_index, showORB and intday?orbH : na, text="IB",  textcolor=#0FC0FC, style= label.style_none, size=size.small)
label.delete(label_orbH[1])

line_orbL = line.new(bar_index[math.min(bars_sinse, 376)], showORB and intday ?orbL :na, bar_index, showORB?orbL :na, color=sColor,  style =  line.style_dotted,width=1, extend = extend.none)
line.delete(line_orbL[1])
label_orbL = label.new(bar_index, showORB and intday ?orbL : na, text="IB",  textcolor=#0FC0FC, style= label.style_none, size=size.small)
label.delete(label_orbL[1])

//Stats

//show_table = input(true,  "Dash", inline='12')


// RSI
rsi = ta.rsi(close, 14)
    
    //vwap
vwap1=(ta.vwap>close)?1:0
    

    // Supertrend
[sup_value, sup_dir] = ta.supertrend(2.7, 23)
    
    
        //---- ADX-DMI code start {----//
adxP = 14
adxS = 14
var ADXColor = color.white
var ADXText = ''

[diplus, diminus, adx] = ta.dmi(adxP, adxS)

//---- Calculate Change & %Change code start {----//
[ts1,ts1C,ts1PDH,ts1PDL] = request.security(syminfo.tickerid,'D',[close,close[1],high[1],low[1]])
ts1Chng = (ts1-ts1C)
ts1p = (ts1-ts1C)*100/ts1C    

///////////
// PLOTS //

var tbl = table.new(position.bottom_right, 7, 41, frame_color=#151715, frame_width=1, border_width=1, border_color=color.new(color.white, 60))

if barstate.islast and show_table

    table.cell(tbl, 0, 0, 'ADX',         text_halign = text.align_center, bgcolor = color.new(color.blue, 60), text_color = color.white, text_size = size.small)
    table.cell(tbl, 1, 0, 'RSI',         text_halign = text.align_center, bgcolor = color.new(color.blue, 60), text_color = color.white, text_size = size.small)
    table.cell(tbl, 2, 0, 'VWAP',        text_halign = text.align_center, bgcolor = color.new(color.blue, 60), text_color = color.white, text_size = size.small)
    table.cell(tbl, 3, 0, 'ST',          text_halign = text.align_center, bgcolor = color.new(color.blue, 60), text_color = color.white, text_size = size.small)
    table.cell(tbl, 4, 0, 'CH',          text_halign = text.align_center, bgcolor = color.new(color.blue, 60), text_color = color.white, text_size = size.small)
    

colgreen = color.new(color.green, 50)
colred = color.new(color.red, 60)

rsi_col  = rsi > 60    ? colgreen   : rsi < 40 ? colred : color.black 
            
vwap_text = vwap1 < 1 ? "Up"      : "Down"
vwap_col  = vwap1 < 1 ? colgreen: colred
           
sup_text = sup_dir > 0 ? "Down"      : "Up"
sup_col  = sup_dir > 0 ? colred : colgreen
            
adx_col = diplus > diminus    ? colgreen   : diplus < diminus  ? colred : color.black
            
chval_col  = ts1Chng > 0 ? colgreen : colred 

if show_table
    table.cell(tbl, 0, 1, str.tostring((adx), "#.##"),                   text_halign = text.align_center, bgcolor = adx_col,    text_color = color.white, text_size = size.auto)
    table.cell(tbl, 1, 1, str.tostring((rsi), "#.##"),                   text_halign = text.align_center, bgcolor = rsi_col,    text_color = color.white, text_size = size.auto)
    table.cell(tbl, 2, 1, str.tostring(vwap_text),                       text_halign = text.align_center, bgcolor = vwap_col,    text_color = color.white, text_size = size.auto)
    table.cell(tbl, 3, 1, sup_text,                                      text_halign = text.align_center, bgcolor = sup_col,    text_color = color.white, text_size = size.auto)
    table.cell(tbl, 4, 1, str.tostring((ts1Chng), "#.##"),               text_halign = text.align_center, bgcolor = chval_col,    text_color = color.white, text_size = size.auto)

//PCVwap

PlotVWAP = input.bool(false, title='VWAP', inline='12')

//Previous Day's Closing Vwap (Pvwap)
newday(res) =>
    t = time(res)
    ta.change(t) != 0 ? 1 : 0
new_day = newday('D')
prev_vwap = ta.valuewhen(new_day, int(ta.vwap[1]), 0)

//col = close > prev_vwap ? close > ta.vwap ? color.lime : color.blue : close < ta.vwap ? color.red : color.purple

line_pvwap = line.new(bar_index[math.min(bars_sinse, 376)], PlotVWAP and intday1 ? prev_vwap :na, bar_index, PlotVWAP and intday1 ? prev_vwap :na, color=color.new(color.yellow, 50),  style =  line.style_dashed,width=1, extend = extend.none)
line.delete(line_pvwap[1]) 

pvcolor = color.new(color.yellow,50)
plot(PlotVWAP and intday1 ? ta.vwap : na, color = color.new(pvcolor,(100/120)*(last_bar_index - bar_index)))

//mvwp

mvwap = ta.ema(ta.vwap, 50)
mvcolor = color.new(color.red,50)
//plot(PlotVWAP and intday1 ? mvwap : na, color = color.new(mvcolor,(100/100)*(last_bar_index - bar_index)))


vwapcol = PlotVWAP and close > prev_vwap and close > ta.vwap and close > mvwap  ? color.lime : 
     PlotVWAP and close < prev_vwap and close < ta.vwap and close < mvwap ? color.red : 
     PlotVWAP and close > ta.vwap and close < prev_vwap ? color.green : 
     PlotVWAP and close < ta.vwap and close > prev_vwap  ? color.maroon :  na


//barcolor(vwapcol, show_last = 75)

//Developing Pivot

show_d = input(false, title='DPivot', inline='12')

is_new_bar(t) =>
    ta.change(time(t)) != 0

get_ohlc(t) =>

    var float _open = na
    var float _high = na
    var float _low = na
    var float _close = na

    _switch = is_new_bar(t)

    _open := _switch ? open : nz(_open[1], open)
    _high := _switch or high > _high[1] ? high : nz(_high[1], high)
    _low := _switch or low < _low[1] ? low : nz(_low[1], low)
    _close := close

    [_open, _high, _low, _close]

//[o, h, l, c] = get_ohlc(tf)

[o, h, l, c] = get_ohlc(resolution)

float p_pivot = 0.0
float p_bc = 0.0
float p_tc = 0.0

float p_h = 0.0
float p_l = 0.0

float d_pivot = 0.0
float d_bc = 0.0
float d_tc = 0.0

d_pivot := math.avg(h, l, c)
d_bc := math.avg(h, l)
d_tc := d_pivot - d_bc + d_pivot

if is_new_bar(resolution)
    p_pivot := d_pivot[1]
    p_bc := d_bc[1]
    p_tc := d_tc[1]
    p_h := h[1]
    p_l := l[1]
    p_l
else
    p_pivot := p_pivot[1]
    p_bc := p_bc[1]
    p_tc := p_tc[1]
    p_h := p_h[1]
    p_l := p_l[1]
    p_l


r_1 = p_pivot + p_pivot - p_l
s_1 = p_pivot - (p_h - p_pivot)
r_2 = p_pivot + p_h - p_l
s_2 = p_pivot - (p_h - p_l)
r_3 = p_h + 2 * (p_pivot - p_l)
s_3 = p_l - 2 * (p_h - p_pivot)

//r_color = show_levels ? color.red : na
//s_color = show_levels ? color.green : na


// Developing
d_color = show_d ? d_tc > d_bc and d_pivot < p_pivot ? color.orange : d_tc < d_bc and d_pivot > p_pivot ? color.orange : d_pivot > d_bc ? color.green : d_pivot < d_bc ? color.red : color.silver : na

len = 5
q=ta.ema(d_pivot, len)
b=ta.ema(d_bc, len)
t=ta.ema(d_tc, len)
//plot(d_pivot, color=d_color, title='D_PIVOT')
plot(show_d ? q : na, color = color.new(d_color,(100/100)*(last_bar_index - bar_index)), title='D_PIVOT')

bc      = input(false, "BC" , inline='12')

barcolor(bc ? vwapcol : na, show_last = 75)
barcolor(bc ? cond1 : na, show_last = 75)
barcolor(bc ? cond2 : na, show_last = 75)

//vwap for index
string i_maType = syminfo.tickerid
Symb = switch i_maType
	"NSE:BANKNIFTY" => "NSE:BANKNIFTY1!"
	"NSE:NIFTY" => "NSE:NIFTY1!"
	// Default used when the cases first cases do not match.
	=> syminfo.tickerid

vwapp2  = request.security(Symb, "", ta.vwap)
//plot(vwapp2)

//Tomorrow CPR

if showTomorrowCPR

    _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])

    linefill.new(_t_tc, _t_bc, color.new(pcolor2, 80))
    
    _t_R1 = line.new(tom_start, tR1, tom_end, tR1, xloc.bar_time, color=color.new(DayR1Color, lTransp))
    line.delete(_t_R1[1])
    _t_R2 = line.new(tom_start, tR2, tom_end, tR2, xloc.bar_time, color=color.new(DayR1Color, lTransp))
    line.delete(_t_R2[1])
    
    _t_S1 = line.new(tom_start, tS1, tom_end, tS1, xloc.bar_time, color=color.new(DayS1Color, lTransp))
    line.delete(_t_S1[1])
    _t_S2 = line.new(tom_start, tS2, tom_end, tS2, xloc.bar_time, color=color.new(DayS1Color, lTransp))
    line.delete(_t_S2[1])
    

showFibo = input.bool(false, title='Fibo', inline='11', group = 'Others')

FPeriod = 500

Fhigh=ta.highest(FPeriod)
Flow=ta.lowest(FPeriod)
FH=ta.highestbars(high,FPeriod)
FL=ta.lowestbars(low,FPeriod)
downfibo = FH < FL

F0 = downfibo ? Flow : Fhigh
F236 = downfibo ? (Fhigh-Flow)*0.236+Flow : Fhigh-(Fhigh-Flow)*0.236
F382 = downfibo ? (Fhigh-Flow)*0.382+Flow : Fhigh-(Fhigh-Flow)*0.382
F500 = downfibo ? (Fhigh-Flow)*0.500+Flow : Fhigh-(Fhigh-Flow)*0.500
F618 = downfibo ? (Fhigh-Flow)*0.618+Flow : Fhigh-(Fhigh-Flow)*0.618
F786 = downfibo ? (Fhigh-Flow)*0.786+Flow : Fhigh-(Fhigh-Flow)*0.786
F1000 = downfibo ? (Fhigh-Flow)*1.000+Flow : Fhigh-(Fhigh-Flow)*1.000
F1618 = downfibo ? (Fhigh-Flow)*1.618+Flow : Fhigh-(Fhigh-Flow)*1.618

Fcolor = downfibo ? #0bf10b : #d44f56c4
Foffset = downfibo ? FH : FL

//plot(F0,color=Fcolor,linewidth=2,trackprice=true,show_last=1,title='0',transp=0)
//plot(F236,color=Fcolor,linewidth=1,trackprice=true,show_last=1,title='0.236',transp=0)
//plot(F382,color=Fcolor,linewidth=1,trackprice=true,show_last=1,title='0.382',transp=0)
//plot(F500,color=Fcolor,linewidth=2,trackprice=true,show_last=1,title='0.5',transp=0)
//plot(F618,color=Fcolor,linewidth=1,trackprice=true,show_last=1,title='0.618',transp=0)
//plot(F786,color=Fcolor,linewidth=1,trackprice=true,show_last=1,title='0.786',transp=0)
//plot(F1000,color=Fcolor,linewidth=2,trackprice=true,show_last=1,title='1',transp=0)
//plot(plotF1618 and F1618 ? F1618 : na,color=Fcolor,linewidth=3,trackprice=true,show_last=1,title='1.618',transp=0)

resolution1 = 'D'

// Function returns 1 when starting with first bar of the day 
firstbar1(resolution1) =>
    ch1 = 0
    if resolution1 == 'Y'
        t1 = year(time('D'))
        ch1 := ta.change(t) != 0 ? 1 : 0
        ch1
    else
        t1 = time(resolution1)
        ch1 := ta.change(t) != 0 ? 1 : 0
        ch1
    ch1

pp_res1 = resolution1
bars_sinse1 = 0
bars_sinse1 := firstbar1(pp_res1) ? 0 : bars_sinse1[1] + 1


l_F0 = line.new(bar_index[math.max(bars_sinse1, 100)], showFibo ? F0 : na, bar_index, showFibo ? F0 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=2)
line.delete(l_F0[1])
l_F236 = line.new(bar_index[math.max(bars_sinse1, 75)], showFibo ? F236 : na, bar_index, showFibo ? F236 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
line.delete(l_F236[1])
l_F382 = line.new(bar_index[math.max(bars_sinse1, 75)], showFibo ? F382 : na, bar_index, showFibo ? F382 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
line.delete(l_F382[1])
l_F500 = line.new(bar_index[math.max(bars_sinse1, 75)], showFibo ? F500 : na, bar_index, showFibo ? F500 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
line.delete(l_F500[1])
l_F618 = line.new(bar_index[math.max(bars_sinse1, 75)], showFibo ? F618 : na, bar_index, showFibo ? F618 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
line.delete(l_F618[1])
l_F786 = line.new(bar_index[math.max(bars_sinse1, 75)], showFibo ? F786 : na, bar_index, showFibo ? F786 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=1)
line.delete(l_F786[1])
l_F1000 = line.new(bar_index[math.max(bars_sinse1, 100)], showFibo ? F1000 : na, bar_index, showFibo ? F1000 : na, color=color.new(Fcolor,(100/25)*(last_bar_index - bar_index)), style=line.style_dotted, extend=extend.none, width=2)
line.delete(l_F1000[1])

//plotchar(F0 , 'high','High', location.absolute, show_last = 1, color = #ED381C, offset = 4, editable = false, size = size.tiny) 

label_F0 = showFibo ? label.new(x=ll_offset, y=F0, xloc=xloc.bar_time, yloc=yloc.price, text="High : " + str.tostring(F0), style=label.style_none, textcolor=color.yellow, size=l_size) : na
label.delete(label_F0[1])
label_F1000 = showFibo ? label.new(x=ll_offset, y=F1000, xloc=xloc.bar_time, yloc=yloc.price, text="Low : " + str.tostring(F1000), style=label.style_none, textcolor=color.yellow, size=l_size) : na
label.delete(label_F1000[1])

label_F236 = showFibo ? label.new(x=ll_offset, y=F236, xloc=xloc.bar_time, yloc=yloc.price, text="23.6" , style=label.style_none, textcolor=Fcolor, size=l_size) : na
label.delete(label_F236[1])
label_F382 = showFibo ? label.new(x=ll_offset, y=F382, xloc=xloc.bar_time, yloc=yloc.price, text="38.2" , style=label.style_none, textcolor=Fcolor, size=l_size) : na
label.delete(label_F382[1])
label_F500 = showFibo ? label.new(x=ll_offset, y=F500, xloc=xloc.bar_time, yloc=yloc.price, text="50" , style=label.style_none, textcolor=Fcolor, size=l_size) : na
label.delete(label_F500[1])
label_F618 = showFibo ? label.new(x=ll_offset, y=F618, xloc=xloc.bar_time, yloc=yloc.price, text="61.8" , style=label.style_none, textcolor=Fcolor, size=l_size) : na
label.delete(label_F618[1])
label_F786 = showFibo ? label.new(x=ll_offset, y=F786, xloc=xloc.bar_time, yloc=yloc.price, text="78.6" , style=label.style_none, textcolor=Fcolor, size=l_size) : na
label.delete(label_F786[1])

//EMA Bands

emabands = input(false, "EMABands" , inline='12')

ema_5 = ta.ema(close, 5)
ema_9 = ta.ema(close, 9)
ema_20 = ta.ema(close, 20)
a = plot(emabands ? ema_5 : na, display=display.none)
bb = plot(emabands ? ema_9 : na, display=display.none)
cc = plot(emabands ? ema_20 : na, display=display.none)
up = ema_5 > ema_20
down = ema_5 < ema_20
mycolor = up ? color.green : down ? color.red : na
fill(a, cc, color=color.new(mycolor, 70))


//ADR plots

//#region -------------------- Inputs
//hist_values = input.bool(defval = false, title = 'Show Past ADRs?')
//show_rec    = input.bool(defval = false, title = 'Show Recommended Ranges?')
//res         = input.timeframe(defval = 'D', title = 'Resolution', options = ['D','W','M'])
res = 'D'

//#endregion

//#region -------------------- Functions
daily_calc() =>
    [day_one_high, day_one_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[1], low[1]], lookahead = barmerge.lookahead_on)
    [day_two_high, day_two_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[2], low[2]], lookahead = barmerge.lookahead_on)
    [day_three_high, day_three_low] = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[3], low[3]], lookahead = barmerge.lookahead_on)
    [day_four_high, day_four_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[4], low[4]], lookahead = barmerge.lookahead_on)
    [day_five_high, day_five_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[5], low[5]], lookahead = barmerge.lookahead_on)

    adr = ((day_one_high - day_one_low) + (day_two_high - day_two_low) + (day_three_high - day_three_low) + (day_four_high - day_four_low) + (day_five_high - day_five_low)) / 5

yesterday_calc() =>
    [day_one_high, day_one_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[1], low[1]], lookahead = barmerge.lookahead_on)

    y_move = day_one_high - day_one_low

yesterday_adr() =>
    [day_one_high, day_one_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[2], low[2]], lookahead = barmerge.lookahead_on)
    [day_two_high, day_two_low]     = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[3], low[3]], lookahead = barmerge.lookahead_on)
    [day_three_high, day_three_low] = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[4], low[4]], lookahead = barmerge.lookahead_on)
    [day_four_high, day_four_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[5], low[5]], lookahead = barmerge.lookahead_on)
    [day_five_high, day_five_low]   = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [high[6], low[6]], lookahead = barmerge.lookahead_on)

    y_adr = ((day_one_high - day_one_low) + (day_two_high - day_two_low) + (day_three_high - day_three_low) + (day_four_high - day_four_low) + (day_five_high - day_five_low)) / 5

nround(x) =>
    n = math.round(x / syminfo.mintick) * syminfo.mintick

pip_to_whole(number) =>
    atr = ta.atr(14)
    if syminfo.type == 'forex'
        pips = atr < 1.0 ? (number / syminfo.mintick) / 10 : number
        pips := atr >= 1.0 and atr < 100.0 and (syminfo.currency == 'JPY' or syminfo.currency == 'HUF' or syminfo.currency == 'INR') ? pips * 100 : pips
    else
        number

//#endregion

//#region -------------------- Function Calls
adr = daily_calc()
adr_pips = pip_to_whole(adr)
adr_pips := math.round(adr_pips)

y_move = yesterday_calc()
y_move_pips = pip_to_whole(y_move)
y_move_pips := math.round(y_move_pips)

y_move_75 = yesterday_adr()
y_move_p = pip_to_whole(y_move_75) 
y_move_p := math.round(y_move_p)
y_move_75_pips = y_move_p * 0.75
y_move_75_pips := math.round(y_move_75_pips)
//#endregion

//#region -------------------- Daily Open Calls
//Calc Open
dopen1  = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, open, lookahead = barmerge.lookahead_on)
[dday, dclose_y, dclose_t] = request.security(ticker.new(syminfo.prefix, syminfo.ticker), res, [time_tradingday, time_close(res)[1], time_close(res)], lookahead = barmerge.lookahead_on)
//#endregion

//#region -------------------- Line / Label Vars and ADR Calculations
var line tl        = na
var line high_100  = na
var line high_75   = na
var line high_50   = na
var line high_25   = na
var line low_25    = na
var line low_50    = na
var line low_75    = na
var line low_100   = na

//Labels
var label label_t1_open  = na
var label label_high_100 = na
var label label_high_75  = na
var label label_high_50  = na
var label label_high_25  = na
var label label_low_25   = na
var label label_low_50   = na
var label label_low_75   = na
var label label_low_100  = na

//ADR Percentage Calculations
high_100_p = dopen1 + adr
high_75_p  = dopen1 + adr * 0.75 
high_50_P  = dopen1 + adr * 0.50
high_25_p  = dopen1 + adr * 0.25

low_25_p   = dopen1 - adr * 0.25
low_50_p   = dopen1 - adr * 0.50
low_75_p   = dopen1 - adr * 0.75
low_100_p  = dopen1 - adr
//#endregion

//#region -------------------- Drawing and Deleting Lines
//if hist_values
    //if ta.change(dclose_y)
        //tl       := line.new(dclose_y, dopen1, dclose_t, dopen1 , xloc = xloc.bar_time, color = color.new(color.white, 50), style = line.style_solid)
        //high_100 := line.new(dclose_y, high_100_p, dclose_t, high_100_p,      color = color.new(#5bd50f, 10),   style = line.style_solid, xloc = xloc.bar_time)
        //high_75  := line.new(dclose_y, high_75_p,  dclose_t, high_75_p,       color = color.new(#5bd50f, 20),   style = line.style_solid, xloc = xloc.bar_time)
        //high_50  := line.new(dclose_y, high_50_P,  dclose_t, high_50_P,       color = color.new(#5bd50f, 30),   style = line.style_solid, xloc = xloc.bar_time)
        //high_25  := line.new(dclose_y, high_25_p,  dclose_t, high_25_p,       color = color.new(#5bd50f, 40),   style = line.style_solid, xloc = xloc.bar_time)
        //low_25   := line.new(dclose_y, low_25_p,   dclose_t, low_25_p,        color = color.new(#ea2700, 40),   style = line.style_solid, xloc = xloc.bar_time)
        //low_50   := line.new(dclose_y, low_50_p,   dclose_t, low_50_p,        color = color.new(#ea2700, 30),   style = line.style_solid, xloc = xloc.bar_time)
        //low_75   := line.new(dclose_y, low_75_p,   dclose_t, low_75_p,        color = color.new(#ea2700, 20),   style = line.style_solid, xloc = xloc.bar_time)
        //low_100  := line.new(dclose_y, low_100_p,  dclose_t, low_100_p,       color = color.new(#ea2700, 10),   style = line.style_solid, xloc = xloc.bar_time)
if showadr
    tl       := line.new(dclose_y, dopen1, last_bar_time, dopen1 , xloc = xloc.bar_time, color = color.new(color.white, 50), style = line.style_solid)
    high_100 := line.new(dclose_y, high_100_p, last_bar_time, high_100_p,     color = color.new(#5bd50f, 10),   style = line.style_solid, xloc = xloc.bar_time)
    high_75  := line.new(dclose_y, high_75_p,  last_bar_time, high_75_p,      color = color.new(#5bd50f, 20),   style = line.style_solid, xloc = xloc.bar_time)
    high_50  := line.new(dclose_y, high_50_P,  last_bar_time, high_50_P,      color = color.new(#5bd50f, 30),   style = line.style_solid, xloc = xloc.bar_time)
    high_25  := line.new(dclose_y, high_25_p,  last_bar_time, high_25_p,      color = color.new(#5bd50f, 40),   style = line.style_solid, xloc = xloc.bar_time)
    low_25   := line.new(dclose_y, low_25_p,   last_bar_time, low_25_p,       color = color.new(#ea2700, 40),   style = line.style_solid, xloc = xloc.bar_time)
    low_50   := line.new(dclose_y, low_50_p,   last_bar_time, low_50_p,       color = color.new(#ea2700, 30),   style = line.style_solid, xloc = xloc.bar_time)
    low_75   := line.new(dclose_y, low_75_p,   last_bar_time, low_75_p,       color = color.new(#ea2700, 20),   style = line.style_solid, xloc = xloc.bar_time)
    low_100  := line.new(dclose_y, low_100_p,  last_bar_time, low_100_p,      color = color.new(#ea2700, 10),   style = line.style_solid, xloc = xloc.bar_time)

    label_t1_open  := label.new(last_bar_time, dopen1,      size = size.small, text = '                                  ' + 'Open - ' + str.tostring(dopen1), style = label.style_none, textcolor = color.new(color.white, 50), xloc = xloc.bar_time)
    label_high_100 := label.new(last_bar_time, high_100_p, size = size.small, text = '                                                      ' + '100.00% ' , style = label.style_none, textcolor = color.new(#5bd50f, 10), xloc = xloc.bar_time)
    label_high_75  := label.new(last_bar_time, high_75_p,  size = size.small, text = '                                                      ' + '75.00%  ', style = label.style_none, textcolor = color.new(#5bd50f, 20), xloc = xloc.bar_time)
    label_high_50  := label.new(last_bar_time, high_50_P,  size = size.small, text = '                                                      ' + '50.00% ', style = label.style_none, textcolor = color.new(#5bd50f, 30), xloc = xloc.bar_time)
    label_high_25  := label.new(last_bar_time, high_25_p,  size = size.small, text = '                                                      ' + '25.00% ', style = label.style_none, textcolor = color.new(#5bd50f, 40), xloc = xloc.bar_time)
    label_low_25   := label.new(last_bar_time, low_25_p,   size = size.small, text = '                                                      ' + '25.00% ', style = label.style_none, textcolor = color.new(#ea2700, 40), xloc = xloc.bar_time)
    label_low_50   := label.new(last_bar_time, low_50_p,   size = size.small, text = '                                                      ' + '50.00% ', style = label.style_none, textcolor = color.new(#ea2700, 30), xloc = xloc.bar_time)
    label_low_75   := label.new(last_bar_time, low_75_p,   size = size.small, text = '                                                      ' + '75.00% ', style = label.style_none, textcolor = color.new(#ea2700, 20), xloc = xloc.bar_time)
    label_low_100  := label.new(last_bar_time, low_100_p,  size = size.small, text = '                                                      ' + '100.00% ', style = label.style_none, textcolor = color.new(#ea2700, 10), xloc = xloc.bar_time)

    //Delete Label
    label.delete(label_t1_open[1])
    label.delete(label_high_100[1])
    label.delete(label_high_75[1])
    label.delete(label_high_50[1])
    label.delete(label_high_25[1])
    label.delete(label_low_25[1])
    label.delete(label_low_50[1])
    label.delete(label_low_75[1])
    label.delete(label_low_100[1])

    //Delete Line
    line.delete(tl[1])
    line.delete(high_100[1])
    line.delete(high_75[1])
    line.delete(high_50[1])
    line.delete(high_25[1])
    line.delete(low_25[1])
    line.delete(low_50[1])
    line.delete(low_75[1])
    line.delete(low_100[1])
//#endregion
