MTF Pivots Zones [tanayroy]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tanayroy

//@version=5
indicator("MTF Pivots Zones [tanayroy]",shorttitle='MTF Pivots', overlay=true)

// Array to store pivots levels
dPivot = array.new_float(13)
wPivot = array.new_float(13)
mPivot = array.new_float(13)
qPivot = array.new_float(13)
yPivot = array.new_float(13)

// Array to store Pivot Name
dPivotName = array.new_string(13)
wPivotName = array.new_string(13)
mPivotName = array.new_string(13)
qPivotName = array.new_string(13)
yPivotName = array.new_string(13)

// Array to store display option
dPivotDisplay = array.new_bool(13)
wPivotDisplay = array.new_bool(13)
mPivotDisplay = array.new_bool(13)
qPivotDisplay = array.new_bool(13)
yPivotDisplay = array.new_bool(13)

// Function to calculate pivot
CalculatePivot(_o,dailyHigh,dailyLow,dailyClose)=>
    pivot = (dailyHigh+dailyLow+dailyClose)/3
    abc = (dailyHigh+dailyLow)/2
    atc = (pivot-abc)+pivot
    tc = abc>=atc?abc:atc
    bc = abc>=atc?atc:abc
    r1 = (2*pivot)-dailyLow
    r2 = pivot+(dailyHigh-dailyLow)
    r3 = r1+(dailyHigh-dailyLow)
    r4 = r3+(r2-r1)
    s1 = (2*pivot)-dailyHigh
    s2 = pivot-(dailyHigh-dailyLow)
    s3 = s1-(dailyHigh-dailyLow)
    s4 = s3-(s1-s2)
    [s1,s2,s3,s4,bc,pivot,tc,r1,r2,r3,r4,dailyHigh,dailyLow]
    
// Inputs
higherTF = input.timeframe ("12M", title='Your Choice for Pivot',tooltip="This will replace yearly pivot")
inputMultiplyer = input.float(0.5,'ATR Multiplyer', minval=0.01)

//MTF pivots 
[s1,s2,s3,s4,bc,pivot,tc,r1,r2,r3,r4,dH,dL] = request.security(syminfo.tickerid, 'D', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[ws1,ws2,ws3,ws4,wbc,wvpivot,wtc,wr1,wr2,wr3,wr4,wH,wL] = request.security(syminfo.tickerid, 'W', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[ms1,ms2,ms3,ms4,mbc,mvpivot,mtc,mr1,mr2,mr3,mr4,mH,mL] = request.security(syminfo.tickerid, '1M', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[qs1,qs2,qs3,qs4,qbc,qvpivot,qtc,qr1,qr2,qr3,qr4,qH,qL] = request.security(syminfo.tickerid, '3M', CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)
[ys1,ys2,ys3,ys4,ybc,yvpivot,ytc,yr1,yr2,yr3,yr4,yH,yL] = request.security(syminfo.tickerid, higherTF, CalculatePivot(open[1],high[1],low[1],close[1]), lookahead=barmerge.lookahead_on)

//Daily ATR
atr14 = request.security(syminfo.tickerid, 'D', ta.atr(14), lookahead=barmerge.lookahead_on)

//Display levels
displayOptionHigh = close+atr14*inputMultiplyer
displayOptionLow = close-atr14*inputMultiplyer

//Function to insert array
insertInArray(arrayName,arrayPlaceName,arrayDisplay,s1,s2,s3,s4,bc,pivot,tc,r1,r2,r3,r4,h,l)=>
    array.set(arrayName,0,s1)
    array.set(arrayPlaceName,0,'S1')
    if displayOptionHigh>=s1 and s1>=displayOptionLow
        array.set(arrayDisplay,0,true)
    else
        array.set(arrayDisplay,0,false)
    
    array.set(arrayName,1,s2)
    array.set(arrayPlaceName,1,'S2')
    if displayOptionHigh>=s2 and s2>=displayOptionLow
        array.set(arrayDisplay,1,true)
    else
        array.set(arrayDisplay,1,false)
    
    array.set(arrayName,2,s3)
    array.set(arrayPlaceName,2,'S3')
    if displayOptionHigh>=s3 and s3>=displayOptionLow
        array.set(arrayDisplay,2,true)
    else
        array.set(arrayDisplay,2,false)
        
    array.set(arrayName,3,s4)
    array.set(arrayPlaceName,3,'S4')
    if displayOptionHigh>=s4 and s4>=displayOptionLow
        array.set(arrayDisplay,3,true)
    else
        array.set(arrayDisplay,3,false)
        
    array.set(arrayName,4,bc)
    array.set(arrayPlaceName,4,'BC')
    if displayOptionHigh>=bc and bc>=displayOptionLow
        array.set(arrayDisplay,4,true)
    else
        array.set(arrayDisplay,4,false)
        
    array.set(arrayName,5,pivot)
    array.set(arrayPlaceName,5,'P')
    if displayOptionHigh>=pivot and pivot>=displayOptionLow
        array.set(arrayDisplay,5,true)
    else
        array.set(arrayDisplay,5,false)
        
    array.set(arrayName,6,tc)
    array.set(arrayPlaceName,6,'TC')
    if displayOptionHigh>=tc and tc>=displayOptionLow
        array.set(arrayDisplay,6,true)
    else
        array.set(arrayDisplay,6,false)
        
    array.set(arrayName,7,r1)
    array.set(arrayPlaceName,7,'R1')
    if displayOptionHigh>=r1 and r1>=displayOptionLow
        array.set(arrayDisplay,7,true)
    else
        array.set(arrayDisplay,7,false)
        
    array.set(arrayName,8,r2)
    array.set(arrayPlaceName,8,'R2')
    if displayOptionHigh>=r2 and r2>=displayOptionLow
        array.set(arrayDisplay,8,true)
    else
        array.set(arrayDisplay,8,false)
        
    array.set(arrayName,9,r3)
    array.set(arrayPlaceName,9,'R3')
    if displayOptionHigh>=r3 and r3>=displayOptionLow
        array.set(arrayDisplay,9,true)
    else
        array.set(arrayDisplay,9,false)
        
    array.set(arrayName,10,r4)
    array.set(arrayPlaceName,10,'R4')
    if displayOptionHigh>=r4 and r4>=displayOptionLow
        array.set(arrayDisplay,10,true)
    else
        array.set(arrayDisplay,10,false)
        
    array.set(arrayName,11,h)
    array.set(arrayPlaceName,11,'H')
    if displayOptionHigh>=h and h>=displayOptionLow
        array.set(arrayDisplay,11,true)
    else
        array.set(arrayDisplay,11,false)
    
    array.set(arrayName,12,l)
    array.set(arrayPlaceName,12,'L')
    if displayOptionHigh>=l and l>=displayOptionLow
        array.set(arrayDisplay,12,true)
    else
        array.set(arrayDisplay,12,false)
  
   
insertInArray(dPivot,dPivotName,dPivotDisplay,s1,s2,s3,s4,bc,pivot,tc,r1,r2,r3,r4,dH,dL)
insertInArray(wPivot,wPivotName,wPivotDisplay,ws1,ws2,ws3,ws4,wbc,wvpivot,wtc,wr1,wr2,wr3,wr4,wH,wL)
insertInArray(mPivot,mPivotName,mPivotDisplay,ms1,ms2,ms3,ms4,mbc,mvpivot,mtc,mr1,mr2,mr3,mr4,mH,mL)
insertInArray(qPivot,qPivotName,qPivotDisplay,qs1,qs2,qs3,qs4,qbc,qvpivot,qtc,qr1,qr2,qr3,qr4,qH,qL)
insertInArray(yPivot,yPivotName,yPivotDisplay,ys1,ys2,ys3,ys4,ybc,yvpivot,ytc,yr1,yr2,yr3,yr4,yH,yL)


//Display Pivots.
isNewPeriod = ta.change(time('D'))
tickp = syminfo.mintick*10
var box pivotBox = na
var prevBarIndex = bar_index
leftBody = prevBarIndex
rightBody =  barstate.islast ? bar_index : bar_index[1]
if isNewPeriod 
    prevBarIndex := bar_index

for a=0 to array.size(dPivot)-1
    if array.get(dPivotDisplay,a)
        box.new(leftBody,array.get(dPivot,a),rightBody,array.get(dPivot,a),border_color=close>array.get(dPivot,a)?color.lime:color.maroon,bgcolor=close>array.get(dPivot,a)?color.lime:color.maroon)
        label.new(leftBody,array.get(dPivot,a),text='D '+array.get(dPivotName,a),style= label.style_none,textcolor=close>array.get(dPivot,a)?color.lime:color.maroon)
    
for a=0 to array.size(wPivot)-1
    if array.get(wPivotDisplay,a)
        box.new(leftBody,array.get(wPivot,a),rightBody,array.get(wPivot,a),border_color=close>array.get(wPivot,a)?color.lime:color.maroon,bgcolor=close>array.get(wPivot,a)?color.lime:color.maroon)
        label.new(leftBody,array.get(wPivot,a),text='W '+array.get(wPivotName,a),style= label.style_none,textcolor=close>array.get(wPivot,a)?color.lime:color.maroon)

for a=0 to array.size(mPivot)-1
    if array.get(mPivotDisplay,a)
        box.new(leftBody,array.get(mPivot,a),rightBody,array.get(mPivot,a),border_color=close>array.get(mPivot,a)?color.lime:color.maroon,bgcolor=close>array.get(mPivot,a)?color.lime:color.maroon)
        label.new(leftBody,array.get(mPivot,a),text='M '+array.get(mPivotName,a),style= label.style_none,textcolor=close>array.get(mPivot,a)?color.lime:color.maroon)


for a=0 to array.size(qPivot)-1
    if array.get(qPivotDisplay,a)
        box.new(leftBody,array.get(qPivot,a),rightBody,array.get(qPivot,a),border_color=close>array.get(qPivot,a)?color.lime:color.maroon,bgcolor=close>array.get(qPivot,a)?color.lime:color.maroon)
        label.new(leftBody,array.get(qPivot,a),text='Q '+array.get(qPivotName,a),style= label.style_none,textcolor=close>array.get(qPivot,a)?color.lime:color.maroon)
    
for a=0 to array.size(yPivot)-1
    if array.get(yPivotDisplay,a)
        box.new(leftBody,array.get(yPivot,a),rightBody,array.get(yPivot,a),border_color=close>array.get(yPivot,a)?color.lime:color.maroon,bgcolor=close>array.get(yPivot,a)?color.lime:color.maroon)
        label.new(leftBody,array.get(yPivot,a),text='Y '+array.get(yPivotName,a),style= label.style_none,textcolor=close>array.get(yPivot,a)?color.lime:color.maroon)

