NSE Sector Performance
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator("Sector Performance v0.2", overlay = false)

if not timeframe.isdaily
    runtime.error("Please switch timeframe to daily")
    
i_sector = input.string("Sectors", "Sector", ["Sectors", "NIFTY", "BANKNIFTY", "CNXAUTO", "CNXENERGY", 
                         "CNXFMCG", "CNXINFRA", "CNXIT", "CNXMEDIA", "CNXMETAL", "CNXPHARMA", "CNXREALTY",
                         "CNXPSUBANK","CNXFINANCE"])
i_time_1_txt = input.string("Year",                        title="Period", inline="1")
i_time_1     = input.time(timestamp("2022-01-01T00:00:00"),title="Date",   inline="1")
i_time_2_txt = input.string("Quarter",                     title="Period", inline="2")
i_time_2     = input.time(timestamp("2022-10-01T00:00:00"),title="Date",   inline="2")
i_time_3_txt = input.string("Month",                       title="Period", inline="3")
i_time_3     = input.time(timestamp("2022-11-01T00:00:00"),title="Date",   inline="3")
i_time_4_txt = input.string("Week",                        title="Period", inline="4")
i_time_4     = input.time(timestamp("2022-11-06T00:00:00"),title="Date",   inline="4")
i_text_size  = input.string(size.small, "Text Size", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge])

int ROWS = 30
int COLS = 10
int CLOSE_1 = 0, CLOSE_2 = 1, CLOSE_3 = 2, CLOSE_4 = 3
int CLOSE_PCT_1 = 4, CLOSE_PCT_2 = 5, CLOSE_PCT_3 = 6, CLOSE_PCT_4 = 7
var matrix<float> data_ = matrix.new<float>(ROWS, COLS, 0.0)

var table table_ = na
var text_color_ = color.white
var bg_color_ = color.gray

f_color(val_) => val_ > 0 ? color.green : val_ < 0 ? color.red : color.gray
f_bg_color(val_) => val_ > 0 ? color.new(color.green,80) : val_ < 0 ? color.new(color.red,80) : color.new(color.black,80)

f_bg_color_price(val_) => 
    fg_color_ = f_color(val_)
    abs_val_ = math.abs(val_)
    transp_ = switch 
        abs_val_ > .15 => 40 
        abs_val_ > .10 => 50 
        abs_val_ > .05 => 60 
        => 80
    color.new(fg_color_, transp_)

f_bg_color_highlow(val_) => 
    fg_color_ = f_color(val_)
    abs_val_ = math.abs(val_)
    transp_ = switch 
        abs_val_ < .02 => 40 
        abs_val_ < .05 => 60 
        abs_val_ < .07 => 80 
        => 100
    color.new(fg_color_, transp_)    

f_symbol(sector_, idx_) =>
    switch sector_
        "Sectors" =>
            switch idx_
                1 => "NIFTY"
                2 => "BANKNIFTY"
                3 => "CNXAUTO"
                4 => "CNXENERGY"
                5 => "CNXFMCG"
                6 => "CNXINFRA"
                7 => "CNXIT"
                8 => "CNXMEDIA"
                9 => "CNXMETAL"
                10 => "CNXPHARMA"
                11 => "CNXREALTY"
                12 => "CNXPSUBANK"
                13 => "CNXFINANCE"
                14 => "CNXMIDCAP"
                15 => "CNXSMALLCAP"
                => ""
        "NIFTY" =>
            switch idx_
                1 => "NIFTY"
                2 => "RELIANCE"
                3 => "INFY"
                4 => "HDFC"
                5 => "TCS"
                6 => "ITC"
                7 => "HINDUNILVR"
                8 => "LT"
                9 => "BHARTIARTL"
                10 => "BAJFINANCE"
                11 => "BHARTIARTL"
                12 => "ASIANPAINT"
                13 => "M_M"
                14 => "MARUTI"
                15 => "TITAN"    
                => ""
        "BANKNIFTY" =>
            switch idx_
                1 => "BANKNIFTY"
                2 => "HDFCBANK"
                3 => "ICICIBANK"
                4 => "SBIN"
                5 => "KOTAKBANK"
                6 => "AXISBANK"
                7 => "INDUSINDBK"
                8 => "AUBANK"
                9 => "BANDHANBNK"
                10 => "FEDERALBNK"
                11 => "IDFCFIRSTB"
                12 => "PNB"
                13 => "RBLBANK"
                => ""
        "CNXAUTO" =>
            switch idx_
                1 => "CNXAUTO"
                2 => "TATAMOTORS"
                3 => "BAJAJ_AUTO"
                4 => "M_M"
                5 => "MARUTI"
                6 => "ASHOKLEY"
                7 => "TVSMOTOR"
                8 => "HEROMOTOCO"
                9 => "EICHERMOT"
                10 => "EXIDEIND"
                11 => "AMARAJABAT"
                12 => "BOSCHLTD"
                13 => "MRF"
                14 => "BHARATFORG"
                15 => "BALKRISIND"
                => ""
        "CNXENERGY" =>
            switch idx_
                1 => "CNXENERGY"
                2 => "ADANIGREEN"
                3 => "BPCL"
                4 => "GAIL"
                5 => "HINDPETRO"
                6 => "IOC"
                7 => "NTPC"
                8 => "ONGC"
                9 => "POWERGRID"
                10 => "RELIANCE"
                11 => "TATAPOWER"
                12 => "GUJGASLTD"
                13 => "IGL"
                14 => "PETRONET"
                => ""
        "CNXFMCG" =>
            switch idx_
                1 => "CNXFMCG"
                2 => "BRITANNIA"
                3 => "COLPAL"
                4 => "DABUR"
                5 => "UBL"
                6 => "GODREJCP"
                7 => "HINDUNILVR"
                8 => "ITC"
                9 => "JUBLFOOD"
                10 => "MARICO"
                11 => "MCDOWELL_N"
                12 => "NESTLEIND"
                13 => "PGHH"
                14 => "TATACONSUM"
                => ""
        "CNXINFRA" =>
            switch idx_
                1 => "CNXINFRA"
                2 => "RELIANCE"
                3 => "TATAPOWER"
                4 => "ADANIPORTS"
                5 => "BHARTIARTL"
                6 => "AMBUJACEM"
                7 => "NTPC"
                8 => "LT"
                9 => "ONGC"
                10 => "POWERGRID"
                11 => "SIEMENS"
                12 => "CONCOR"
                13 => "DLF"
                14 => "INDIGO"
                => ""
        "CNXIT" =>
            switch idx_
                1 => "CNXIT"
                2 => "COFORGE"
                3 => "HCLTECH"
                4 => "INFY"
                5 => "LTI"
                6 => "MINDTREE"
                7 => "MPHASIS"
                8 => "OFSS"
                9 => "TCS"
                10 => "TECHM"
                11 => "WIPRO"
                12 => "PERSISTENT"
                13 => "NAUKRI"
                14 => "TATAELXSI"              
                15 => "JUSTDIAL"              
                => ""
        "CNXMEDIA" =>
            switch idx_
                1 => "CNXMEDIA"
                2 => "DBCORP"
                3 => "DISHTV"
                4 => "INOXLEISUR"
                5 => "JAGRAN"
                6 => "NETWORK18"
                7 => "PVR"
                8 => "SUNTV"
                9 => "TV18BRDCST"
                10 => "TVTODAY"
                11 => "ZEEL"
                12 => "SAREGAMA"
                13 => "PFOCUS"
                14 => "TIPSINDLTD"           
                => ""
        "CNXMETAL" =>
            switch idx_
                1 => "CNXMETAL"
                2 => "JSWSTEEL"
                3 => "TATASTEEL"
                4 => "SAIL"
                5 => "JINDALSTEL"
                6 => "APLAPOLLO"
                7 => "COALINDIA"
                8 => "RATNAMANI"
                9 => "NMDC"
                10 => "NATIONALUM"
                11 => "VEDL"
                12 => "HINDALCO"
                13 => "ADANIENT"
                14 => "WELCORP"       
                => ""
        "CNXPHARMA" =>
            switch idx_
                1 => "CNXPHARMA"
                2 => "SUNPHARMA"
                3 => "DIVISLAB"
                4 => "CIPLA"
                5 => "DRREDDY"
                6 => "BIOCON"
                7 => "TORNTPHARM"
                8 => "ALKEM"
                9 => "ZYDUSLIFE"
                10 => "AUROPHARMA"
                11 => "LUPIN"
                12 => "PEL"
                13 => "GLAND"
                14 => "LAURUSLABS"     
                => ""
        "CNXREALTY" =>
            switch idx_
                1 => "CNXREALTY"
                2 => "DLF"
                3 => "LODHA"
                4 => "GODREJPROP"
                5 => "OBEROIRLTY"
                6 => "PRESTIGE"
                7 => "PHOENIXLTD"
                8 => "BRIGADE"
                9 => "NBCC"
                10 => "SOBHA"
                11 => "SUNTECK"
                12 => "IBREALEST"
                13 => "MAHLIFE"
                14 => "HEMIPROP"
                => ""
        "CNXPSUBANK" =>
            switch idx_
                1 => "CNXPSUBANK"
                2 => "BANKBARODA"
                3 => "BANKINDIA"
                4 => "CANBK"
                5 => "CENTRALBK"
                6 => "INDIANB"
                7 => "IOB"
                8 => "J_KBANK"
                9 => "MAHABANK"
                10 => "PNB"
                11 => "PSB"
                12 => "SBIN"
                13 => "UCOBANK"
                14 => "UNIONBANK"
                => ""
        "CNXFINANCE" =>
            switch idx_
                1 => "CNXFINANCE"
                2 => "HDFCBANK"
                3 => "ICICIBANK"
                4 => "HDFC"
                5 => "KOTAKBANK"
                6 => "SBIN"
                7 => "BAJFINANCE"
                8 => "AXISBANK"
                9 => "BAJAJFINSV"
                10 => "SBILIFE"
                11 => "HDFCLIFE"
                12 => "ICICIGI"
                13 => "CHOLAFIN"
                14 => "SBICARD"
                15 => "SRTRANSFIN"
                => ""

f_update_data(idx_, period_, start_time_, bar_time_, close_) =>
    var time_array_ = array.new<int>(4,0)

    time_ = array.get(time_array_, period_)
    if time_ == 0 and bar_time_ >= start_time_
        time_ := start_time_
        array.set(time_array_, period_, start_time_)
    if time_ == start_time_ and matrix.get(data_, idx_, period_) == 0.0
        matrix.set(data_, idx_, period_, close_[1])

    if barstate.islast
        close_start_ = matrix.get(data_, idx_, period_)
        close_pct_ = close_/close_start_ -1
        matrix.set(data_, idx_, 4+period_, close_pct_)

        row_ = idx_+1
        col_ = 1 + period_
        table.cell(table_, col_, row_, str.format("{0,number,0.00%}", close_pct_), text_color = f_color(close_pct_), bgcolor = f_bg_color_price(close_pct_), text_size = i_text_size), col_ += 1


f_security(symbol_, idx_) =>

    if barstate.isfirst and symbol_ != ""
        row_ = idx_+1, col_ = 0
        table.cell(table_, col_, row_, symbol_, text_color = color.new(color.white,50), bgcolor = color.new(bg_color_,80), text_size = i_text_size, text_halign = text.align_left), col_ += 1

    [close_, bar_time_, high_250d_, low_250d_] = request.security("NSE:" + symbol_, timeframe.period, [close, time, ta.highest(high, 250), ta.lowest(low,250)], ignore_invalid_symbol = true)
    if symbol_ != ""
        f_update_data(idx_, CLOSE_1, i_time_1, bar_time_, close_)
        f_update_data(idx_, CLOSE_2, i_time_2, bar_time_, close_)
        f_update_data(idx_, CLOSE_3, i_time_3, bar_time_, close_)
        f_update_data(idx_, CLOSE_4, i_time_4, bar_time_, close_)

    if barstate.islast and symbol_ != ""
        row_ = idx_+1
        col_ = 5
        table.cell(table_, col_, row_, str.format("{0,number,#.00}", close_), text_color = color.new(color.white,50), bgcolor = color.new(bg_color_,80), text_size = i_text_size, text_halign = text.align_right), col_ += 1
        table.cell(table_, col_, row_, str.format("{0,number,#.00}", high_250d_), text_color = color.new(color.white,50), bgcolor = color.new(bg_color_,80), text_size = i_text_size, text_halign = text.align_right), col_ += 1
        val_ = 1 - close_/high_250d_
        table.cell(table_, col_, row_, str.format("{0,number,0.00%}", val_), text_color = color.new(color.white,50), bgcolor = f_bg_color_highlow(val_), text_size = i_text_size, text_halign = text.align_right), col_ += 1
        table.cell(table_, col_, row_, str.format("{0,number,#.00}", low_250d_), text_color = color.new(color.white,50), bgcolor = color.new(bg_color_,80), text_size = i_text_size, text_halign = text.align_right), col_ += 1
        val_ := close_/low_250d_-1
        table.cell(table_, col_, row_, str.format("{0,number,0.00%}", val_), text_color = color.new(color.white,50), bgcolor = f_bg_color_highlow(-val_), text_size = i_text_size, text_halign = text.align_right), col_ += 1

if barstate.isfirst
    table_ := table.new(position.middle_center, 20, 30, border_width = 1)
    row_ = 0, col_ = 0
    table.cell(table_, col_, row_, i_sector, text_color = text_color_, bgcolor = color.blue, text_size = i_text_size)
    table.merge_cells(table_, 0,0,9,0)
    row_ += 1    
    table.cell(table_, col_, row_, "Symbol", text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, str.format("{0}\n{1,date,yyyy-MM-dd}", i_time_1_txt, i_time_1), text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, str.format("{0}\n{1,date,yyyy-MM-dd}", i_time_2_txt, i_time_2), text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, str.format("{0}\n{1,date,yyyy-MM-dd}", i_time_3_txt, i_time_3), text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, str.format("{0}\n{1,date,yyyy-MM-dd}", i_time_4_txt, i_time_4), text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, "Close", text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, "250d High", text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, "% from High", text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, "250d Low", text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1
    table.cell(table_, col_, row_, "% from Low", text_color = text_color_, bgcolor = bg_color_, text_size = i_text_size), col_ += 1

idx_ = 1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
f_security(f_symbol(i_sector, idx_), idx_), idx_ +=1
