Sector Rotation
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vignesh_vish

//@version=5
indicator("Sector Rotation",max_boxes_count=500)
Highlight=input.string(defval="None",title="Highlite",options=["None","BNF","IT","PRMA","FMCG","AUTO","MTAL","MDIA","RLTY","IFRA","ENGY","PSU-B","PVT-B","F-SRV","CONSM","C-DUBL"],inline="Highlite",group="Sector to Highlight")
highliteColor=input.color(defval=color.orange,title="Highlite Color",inline="Highlite",group="Sector to Highlight")
IT_SorH=input.bool(true,title="IT (IT)",group="Sector to Plot")
BN_SorH=input.bool(true,title="BANKNIFTY (BNF)",group="Sector to Plot")
PHARMA_SorH=input.bool(true,title="CNX-PHARMA (PRMA)",group="Sector to Plot")
FMCG_SorH=input.bool(true,title="CNX-FMCG (FMCG)",group="Sector to Plot")
AUTO_SorH=input.bool(true,title="CNX-AUTO (AUTO)",group="Sector to Plot")
METAL_SorH=input.bool(true,title="CNX-METAL (MTAL)",group="Sector to Plot")
MEDIA_SorH=input.bool(true,title="CNX-MEDIA (MDIA)",group="Sector to Plot")
REALITY_SorH=input.bool(true,title="CNX-REALTY (RLTY)",group="Sector to Plot")
INFRA_SorH=input.bool(true,title="CNX-INFRA (IFRA)",group="Sector to Plot")
ENERGY_SorH=input.bool(true,title="CNX-ENERGY (ENGY)",group="Sector to Plot")
PSUBANK_SorH=input.bool(true,title="CNX-PSUBANK (PSU-B)",group="Sector to Plot")
PVTBANK_SorH=input.bool(true,title="NIFTY-PVTBANK (PVT-B)",group="Sector to Plot")
FINANCE_SorH=input.bool(true,title="CNX-FINANCE (F-SRV)",group="Sector to Plot")
CONSUMPTION_SorH=input.bool(true,title="CNXCONSUMPTION (CONSM)",group="Sector to Plot")
CONSUMERDURBL_SorH=input.bool(true,title="NIFTY_CONSR_DURBL (C-DUBL)",group="Sector to Plot")


ITClose=request.security("CNXIT",timeframe.period,close)
BNClose=request.security("BANKNIFTY",timeframe.period,close)
PHARMAClose=request.security("CNXPHARMA",timeframe.period,close)
FMCGClose=request.security("CNXFMCG",timeframe.period,close)
AUTOClose=request.security("CNXAUTO",timeframe.period,close)
METALClose=request.security("CNXMETAL",timeframe.period,close)
MEDIAClose=request.security("CNXMEDIA",timeframe.period,close)
REALITYClose=request.security("CNXREALTY",timeframe.period,close)
INFRAClose=request.security("CNXINFRA",timeframe.period,close)
ENERGYClose=request.security("CNXENERGY",timeframe.period,close)
PSUBANKClose=request.security("CNXPSUBANK",timeframe.period,close)
PVTBANKClose=request.security("NIFTYPVTBANK",timeframe.period,close)
FINANCEClose=request.security("CNXFINANCE",timeframe.period,close)
CONSUMPTIONClose=request.security("CNXCONSUMPTION",timeframe.period,close)
CONSUMERDURBLClose=request.security("NIFTY_CONSR_DURBL",timeframe.period,close)

bool[] SorH_Arr=array.from(BN_SorH,IT_SorH,PHARMA_SorH,FMCG_SorH,AUTO_SorH,METAL_SorH,MEDIA_SorH,REALITY_SorH,INFRA_SorH,ENERGY_SorH,PSUBANK_SorH,PVTBANK_SorH,FINANCE_SorH,CONSUMPTION_SorH,CONSUMERDURBL_SorH)
float[] secuCloseArr=array.from(BNClose,ITClose,PHARMAClose,FMCGClose,AUTOClose,METALClose,MEDIAClose,REALITYClose,INFRAClose,ENERGYClose,PSUBANKClose,PVTBANKClose,FINANCEClose,CONSUMPTIONClose,CONSUMERDURBLClose)
float[] secuPreCloseArr=array.from(BNClose[1],ITClose[1],PHARMAClose[1],FMCGClose[1],AUTOClose[1],METALClose[1],MEDIAClose[1],REALITYClose[1],INFRAClose[1],ENERGYClose[1],PSUBANKClose[1],PVTBANKClose[1],FINANCEClose[1],CONSUMPTIONClose[1],CONSUMERDURBLClose[1])
string[] inxName=array.from("BNF","IT","PRMA","FMCG","AUTO","MTAL","MDIA","RLTY","IFRA","ENGY","PSU-B","PVT-B","F-SRV","CONSM","C-DUBL")
indexName=array.new_string()
perChArr=array.new_float()

for i=0 to array.size(SorH_Arr)-1
    if(array.get(SorH_Arr,i))
        cls=math.round(((array.get(secuCloseArr,i)-array.get(secuPreCloseArr,i))/array.get(secuPreCloseArr,i))*100,2)
        array.push(perChArr,cls)
        array.push(indexName,array.get(inxName,i))

currentSymbolPerCh=math.round(((close-close[1])/close[1])*100,2)
perChArrGTZ_unSorted=array.new_float()
indexNameGTZ_unSorted=array.new_string()


perChArrLTZ_unSorted=array.new_float()
indexNameLTZ_unSorted=array.new_string()
var int LtPos=na
var int GtPos=na
//separating -ve and +ve array
for i=0 to array.size(perChArr)-1
    if(array.get(perChArr,i)>=0)
        array.push(perChArrGTZ_unSorted,array.get(perChArr,i))
        array.push(indexNameGTZ_unSorted,array.get(indexName,i))
    else
        array.push(perChArrLTZ_unSorted,array.get(perChArr,i))
        array.push(indexNameLTZ_unSorted,array.get(indexName,i))
//sorting +ve array
positiveBox=array.new_box()
negativeBox=array.new_box()
boxC(I_id,T_top,B_bottom,T_txt,BG_color,BC_color)=>
    array.push(I_id,box.new(bar_index,T_top,bar_index+1,B_bottom,text=T_txt,bgcolor=color.new(BG_color,80),border_color=color.new(BC_color,50),text_halign=text.align_left,text_size=size.small))


float boxSpace=0.0
float boxSpaceCluster=0.0
if (timeframe.period=="D")
    boxSpace:=0.07
    boxSpaceCluster:=0.06
else if(timeframe.period=="W")
    boxSpace:=0.14
    boxSpaceCluster:=0.12
else if(timeframe.period=="M")
    boxSpace:=0.23
    boxSpaceCluster:=0.21
else if(timeframe.isintraday)
    boxSpace:=0.05
    boxSpaceCluster:=0.04
    
if(time>=chart.left_visible_bar_time and time<=chart.right_visible_bar_time) 
    if(array.size(perChArrGTZ_unSorted)>0)
        for i=0 to array.size(perChArrGTZ_unSorted)-1
            lowinx=i
            for j=i to array.size(perChArrGTZ_unSorted)-1
                if(array.get(perChArrGTZ_unSorted,j)<array.get(perChArrGTZ_unSorted,lowinx))
                    lowinx:=j
            temp=array.get(perChArrGTZ_unSorted,lowinx)
            array.set(perChArrGTZ_unSorted,lowinx,array.get(perChArrGTZ_unSorted,i))
            array.set(perChArrGTZ_unSorted,i,temp)
            
            Ntemp=array.get(indexNameGTZ_unSorted,lowinx)
            array.set(indexNameGTZ_unSorted,lowinx,array.get(indexNameGTZ_unSorted,i))
            array.set(indexNameGTZ_unSorted,i,Ntemp)
            
        currentMaxTopValue=0.0
        currentMaxBottomValue=0.0
        //positiveBox=array.new_box()
        string txt=na
        for i=0 to array.size(perChArrGTZ_unSorted)-1
            HLC=array.get(indexNameGTZ_unSorted,i)==Highlight?highliteColor:color.green
            if(i==0)
                boxC(positiveBox,array.get(perChArrGTZ_unSorted,i)+boxSpace,array.get(perChArrGTZ_unSorted,i)," "+str.tostring(array.get(indexNameGTZ_unSorted,i))+" "+str.tostring(array.get(perChArrGTZ_unSorted,i))+"%",color.green,HLC)
                currentMaxTopValue:=array.get(perChArrGTZ_unSorted,i)+boxSpace
                currentMaxBottomValue:=array.get(perChArrGTZ_unSorted,i)
                txt:=str.tostring(array.get(indexNameGTZ_unSorted,i))+" "+str.tostring(array.get(perChArrGTZ_unSorted,i))+"%"
            else 
                if (array.get(perChArrGTZ_unSorted,i)>currentMaxTopValue)
                    boxC(positiveBox,array.get(perChArrGTZ_unSorted,i)+boxSpace,array.get(perChArrGTZ_unSorted,i)," "+str.tostring(array.get(indexNameGTZ_unSorted,i))+" "+str.tostring(array.get(perChArrGTZ_unSorted,i))+"%",color.green,HLC)
                    currentMaxTopValue:=array.get(perChArrGTZ_unSorted,i)+boxSpace
                    currentMaxBottomValue:=array.get(perChArrGTZ_unSorted,i)
                    txt:=str.tostring(array.get(indexNameGTZ_unSorted,i))+" "+str.tostring(array.get(perChArrGTZ_unSorted,i))+"%"
                else
                    txt:=str.tostring(array.get(indexNameGTZ_unSorted,i))+" "+str.tostring(array.get(perChArrGTZ_unSorted,i))+"%"+"\n"+txt
                    HLC:=str.contains(txt,Highlight)?highliteColor:color.blue
                    txtArr=str.split(txt,"\n")
                    wordCount=0
                    for s=0 to array.size(txtArr)-1
                        if str.length(array.get(txtArr,s))>1
                            wordCount:=wordCount+1
                    top=array.get(perChArrGTZ_unSorted,i)>(currentMaxBottomValue+(wordCount*boxSpaceCluster))?array.get(perChArrGTZ_unSorted,i):currentMaxBottomValue+(wordCount*boxSpaceCluster)
                    box.set_text(array.get(positiveBox,array.size(positiveBox)-1), txt)
                    box.set_top(array.get(positiveBox,array.size(positiveBox)-1),top)
                    box.set_bottom(array.get(positiveBox,array.size(positiveBox)-1),currentMaxBottomValue)
                    box.set_border_color(array.get(positiveBox,array.size(positiveBox)-1),HLC)
                    box.set_bgcolor(array.get(positiveBox,array.size(positiveBox)-1),color.new(color.green,80))
                    currentMaxTopValue:=top//array.get(perChArrGTZ_unSorted,i)+array.get(perChArrGTZ_unSorted,i)>(currentMaxBottomValue+(wordCount*0.04))?array.get(perChArrGTZ_unSorted,i):currentMaxBottomValue+(wordCount*0.04)
        if array.max(perChArr)>=0
            line.new(bar_index,currentMaxTopValue,bar_index+1,currentMaxTopValue,color=color.new(color.blue,0),width=2)
        if array.min(perChArr)>=0
            line.new(bar_index,array.min(perChArr),bar_index+1,array.min(perChArr),color=color.new(color.blue,0),width=2)
            
        if array.size(positiveBox)>0
            for i=0 to array.size(positiveBox)-1
                array.get(positiveBox,i)
//negative Array
    if(array.size(perChArrLTZ_unSorted)>0)
        for i=0 to array.size(perChArrLTZ_unSorted)-1
            highinx=i
            for j=i to array.size(perChArrLTZ_unSorted)-1
                if(array.get(perChArrLTZ_unSorted,j)>array.get(perChArrLTZ_unSorted,highinx))
                    highinx:=j
            temp=array.get(perChArrLTZ_unSorted,highinx)
            array.set(perChArrLTZ_unSorted,highinx,array.get(perChArrLTZ_unSorted,i))
            array.set(perChArrLTZ_unSorted,i,temp)
            
            Ntemp=array.get(indexNameLTZ_unSorted,highinx)
            array.set(indexNameLTZ_unSorted,highinx,array.get(indexNameLTZ_unSorted,i))
            array.set(indexNameLTZ_unSorted,i,Ntemp)
        currentMaxTopValue=0.0
        currentMaxBottomValue=0.0
        //positiveBox=array.new_box()
        string txt=na
        for i=0 to array.size(perChArrLTZ_unSorted)-1
            HLC=array.get(indexNameLTZ_unSorted,i)==Highlight?highliteColor:color.red
            if(i==0)
                boxC(negativeBox,array.get(perChArrLTZ_unSorted,i),array.get(perChArrLTZ_unSorted,i)-boxSpace," "+str.tostring(array.get(indexNameLTZ_unSorted,i))+" "+str.tostring(array.get(perChArrLTZ_unSorted,i))+"%",color.red,HLC)
                currentMaxTopValue:=array.get(perChArrLTZ_unSorted,i)
                currentMaxBottomValue:=array.get(perChArrLTZ_unSorted,i)-boxSpace
                txt:=str.tostring(array.get(indexNameLTZ_unSorted,i))+" "+str.tostring(array.get(perChArrLTZ_unSorted,i))+"%"
            else
                if(array.get(perChArrLTZ_unSorted,i)<currentMaxBottomValue)
                    boxC(negativeBox,array.get(perChArrLTZ_unSorted,i),array.get(perChArrLTZ_unSorted,i)-boxSpace," "+str.tostring(array.get(indexNameLTZ_unSorted,i))+" "+str.tostring(array.get(perChArrLTZ_unSorted,i))+"%",color.red,HLC)
                    currentMaxTopValue:=array.get(perChArrLTZ_unSorted,i)
                    currentMaxBottomValue:=array.get(perChArrLTZ_unSorted,i)-boxSpace
                    txt:=str.tostring(array.get(indexNameLTZ_unSorted,i))+" "+str.tostring(array.get(perChArrLTZ_unSorted,i))+"%"
                else
                    txt:=txt+"\n"+str.tostring(array.get(indexNameLTZ_unSorted,i))+" "+str.tostring(array.get(perChArrLTZ_unSorted,i))+"%"
                    HLC:=str.contains(txt,Highlight)?highliteColor:color.blue
                    txtArr=str.split(txt,"\n")
                    wordCount=0
                    for s=0 to array.size(txtArr)-1
                        if str.length(array.get(txtArr,s))>1
                            wordCount:=wordCount+1
                    bottom=array.get(perChArrLTZ_unSorted,i)<(currentMaxTopValue-(wordCount*boxSpaceCluster))?array.get(perChArrLTZ_unSorted,i):currentMaxTopValue-(wordCount*boxSpaceCluster)
                    box.set_text(array.get(negativeBox,array.size(negativeBox)-1), txt)
                    box.set_top(array.get(negativeBox,array.size(negativeBox)-1),currentMaxTopValue)
                    box.set_bottom(array.get(negativeBox,array.size(negativeBox)-1),bottom)
                    box.set_border_color(array.get(negativeBox,array.size(negativeBox)-1),HLC)
                    box.set_bgcolor(array.get(negativeBox,array.size(negativeBox)-1),color.new(color.red,80))
                    currentMaxBottomValue:=bottom
        if array.max(perChArr)<0
            line.new(bar_index,array.max(perChArr),bar_index+1,array.max(perChArr),color=color.new(color.blue,0),width=2)
        if array.min(perChArr)<0
            line.new(bar_index,currentMaxBottomValue,bar_index+1,currentMaxBottomValue,color=color.new(color.blue,0),width=2)
        if array.size(negativeBox)>0
            for i=0 to array.size(negativeBox)-1
                array.get(negativeBox,i)

hline(0,title="LineZero",color=color.blue,linewidth=2,linestyle=hline.style_dotted)
colr=currentSymbolPerCh>=0?color.green:color.red
vline= if ta.change(time("M"))and (timeframe.period=="D" or timeframe.period=="W")
    line.new(bar_index,-0.02,bar_index,0.02,extend=extend.both,color=color.new(color.gray,0),width=1)

plotshape(currentSymbolPerCh,title="Current Symbol Percentage Change",style=shape.square,location= location.absolute,color=colr,size=size.tiny,display=display.price_scale +  display.pane)