Futures All List Candle Analysis - FALCA
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gokhanpirnal

//@version=5
indicator("Futures All List Candle Analysis", shorttitle = "FALCA",  overlay = false)

// |                                   INPUT                                    |
// Sorting
sortTable = input.string(title="Sort Table by" , defval="High-Low C/H", 
     options=[ "Alphabet", "High-Low C/H", "Low-High C/H", "High-Low C/O", "Low-High C/O", "High-Low C/L", "Low-High C/L"])

// Set Type
set       = input.string(title="Sector Set Type" , defval="1.LIST", 
     options = ["1.LIST", "2.LIST", "3.LIST", "4.LIST", "5.LIST", "6.LIST", "7.LIST", "8.LIST", "9.LIST", "10.LIST", "11.LIST", "12.LIST", "FAVORITE LIST"])

// Table Position
in_table_pos = input.string(title="Table Location", defval= "Top Right", 
     options = [ "Top Right" , "Middle Right" , "Bottom Right" , 
                 "Top Center", "Middle Center", "Bottom Center", 
                 "Top Left"  , "Middle Left"  , "Bottom Left" ], 
              group= "Table Styling")

// Table Size
in_table_size = input.string(title="Table Size", defval="Tiny", 
     options=["Auto",  "Huge",  "Large", "Normal", "Small", "Tiny"], 
     group= "Table Styling")

// Table Colors
cell_lowColor  = input.color(color.red, title= "Low Cell", 
                     inline="1", group="TableColor")
cell_midColor  = input.color(color.yellow, title= "Mid Cell", 
                     inline="2", group="TableColor")
cell_highColor = input.color(color.green, title= "High Cell", 
                     inline="3", group="TableColor")

background_col = input.color(color.gray, title= "Background Color", 
                     inline="1", group="TableColor")
title_textCol  = input.color(color.white, title="Title Text Color", 
                     inline="2", group="TableColor")
cell_textCol   = input.color(color.black, title="Cell Text Color", 
                     inline="3", group="TableColor")

// |                                  SYMBOLS                                   |
sym_short(s) => 
    str.substring(s, str.pos(s, ":") + 1)

symbol(num, req) =>
    if req == "symbols"
        if  set == "1.LIST"
            symbols = num == 0   ? "BINANCE:1000LUNCUSDTPERP" : num == 1   ? "BINANCE:1000SHIBUSDTPERP" :
                      num == 2   ? "BINANCE:1000XECUSDTPERP"  : num == 3   ? "BINANCE:1INCHUSDTPERP"    :
                      num == 4   ? "BINANCE:AAVEUSDTPERP"     : num == 5   ? "BINANCE:ADAUSDTPERP"      :
                      num == 6   ? "BINANCE:ALGOUSDTPERP"     : num == 7   ? "BINANCE:ALICEUSDTPERP"    :
                      num == 8   ? "BINANCE:ALPHAUSDTPERP"    : num == 9   ? "BINANCE:ANKRUSDTPERP"     :
                      num == 10  ? "BINANCE:ANTUSDTPERP"      : num == 11  ? "BINANCE:APEUSDTPERP"      :
                      num == 12  ? "BINANCE:API3USDTPERP"     : na
        else if set == "2.LIST"
            symbols = num == 0   ? "BINANCE:APTUSDTPERP"      : num == 1   ? "BINANCE:ARPAUSDTPERP"     : 
                      num == 2   ? "BINANCE:ARUSDTPERP"       : num == 3   ? "BINANCE:ATAUSDTPERP"      : 
                      num == 4   ? "BINANCE:ATOMUSDTPERP"     : num == 5   ? "BINANCE:AUDIOUSDTPERP"    : 
                      num == 6   ? "BINANCE:AVAXUSDTPERP"     : num == 7   ? "BINANCE:AXSUSDTPERP"      : 
                      num == 8   ? "BINANCE:BAKEUSDTPERP"     : num == 9   ? "BINANCE:BALUSDTPERP"      : 
                      num == 10  ? "BINANCE:BANDUSDTPERP"     : num == 11  ? "BINANCE:BATUSDTPERP"      : 
                      num == 12  ? "BINANCE:BCHUSDTPERP"      : na
        else if set == "3.LIST"
            symbols = num == 0   ? "BINANCE:BELUSDTPERP"      : num == 1   ? "BINANCE:BLUEBIRDUSDTPERP" :
                      num == 2   ? "BINANCE:BLZUSDTPERP"      : num == 3   ? "BINANCE:BNBUSDTPERP"      :
                      num == 4   ? "BINANCE:BNXUSDTPERP"      : num == 5   ? "BINANCE:BTCDOMUSDTPERP"   :
                      num == 6   ? "BINANCE:BTCUSDTPERP"      : num == 7   ? "BINANCE:C98USDTPERP"      :
                      num == 8   ? "BINANCE:CELOUSDTPERP"     : num == 9   ? "BINANCE:CELRUSDTPERP"     :
                      num == 10  ? "BINANCE:CHRUSDTPERP"      : num == 11  ? "BINANCE:CHZUSDTPERP"      :
                      num == 12  ? "BINANCE:COMPUSDTPERP"     : na
        else if set == "4.LIST"
            symbols = num == 0   ? "BINANCE:COTIUSDTPERP"     : num == 1   ? "BINANCE:CRVUSDTPERP"      : 
                      num == 2   ? "BINANCE:CTKUSDTPERP"      : num == 3   ? "BINANCE:CTSIUSDTPERP"     : 
                      num == 4   ? "BINANCE:CVXUSDTPERP"      : num == 5   ? "BINANCE:DARUSDTPERP"      : 
                      num == 6   ? "BINANCE:DASHUSDTPERP"     : num == 7   ? "BINANCE:DEFIUSDTPERP"     : 
                      num == 8   ? "BINANCE:DENTUSDTPERP"     : num == 9   ? "BINANCE:DGBUSDTPERP"      : 
                      num == 10  ? "BINANCE:DOGEUSDTPERP"     : num == 11  ? "BINANCE:DOTUSDTPERP"      : 
                      num == 12  ? "BINANCE:DUSKUSDTPERP"     : na
        else if set == "5.LIST"
            symbols = num == 0   ? "BINANCE:DYDXUSDTPERP"     : num == 1   ? "BINANCE:EGLDUSDTPERP"     :
                      num == 2   ? "BINANCE:ENJUSDTPERP"      : num == 3   ? "BINANCE:ENSUSDTPERP"      :
                      num == 4   ? "BINANCE:EOSUSDTPERP"      : num == 5   ? "BINANCE:ETCUSDTPERP"      :
                      num == 6   ? "BINANCE:ETHUSDTPERP"      : num == 7   ? "BINANCE:FILUSDTPERP"      :
                      num == 8   ? "BINANCE:FLMUSDTPERP"      : num == 9   ? "BINANCE:FLOWUSDTPERP"     :
                      num == 10  ? "BINANCE:FOOTBALLUSDTPERP" : num == 11  ? "BINANCE:FTMUSDTPERP"      :
                      num == 12  ? "BINANCE:GALAUSDTPERP"     : na 
        else if set == "6.LIST"
            symbols = num == 0   ? "BINANCE:GMTUSDTPERP"      : num == 1   ? "BINANCE:GRTUSDTPERP"      : 
                      num == 2   ? "BINANCE:GTCUSDTPERP"      : num == 3   ? "BINANCE:HBARUSDTPERP"     : 
                      num == 4   ? "BINANCE:HNTUSDTPERP"      : num == 5   ? "BINANCE:HOTUSDTPERP"      : 
                      num == 6   ? "BINANCE:ICPUSDTPERP"      : num == 7   ? "BINANCE:ICXUSDTPERP"      : 
                      num == 8   ? "BINANCE:IMXUSDTPERP"      : num == 9   ? "BINANCE:INJUSDTPERP"      : 
                      num == 10  ? "BINANCE:IOSTUSDTPERP"     : num == 11  ? "BINANCE:IOTAUSDTPERP"     : 
                      num == 12  ? "BINANCE:IOTXUSDTPERP"     : na
        else if set == "7.LIST"
            symbols = num == 0   ? "BINANCE:JASMYUSDTPERP"    : num == 1   ? "BINANCE:KAVAUSDTPERP"     :
                      num == 2   ? "BINANCE:KLAYUSDTPERP"     : num == 3   ? "BINANCE:KNCUSDTPERP"      :
                      num == 4   ? "BINANCE:KSMUSDTPERP"      : num == 5   ? "BINANCE:LDOUSDTPERP"      :
                      num == 6   ? "BINANCE:LINAUSDTPERP"     : num == 7   ? "BINANCE:LINKUSDTPERP"     :
                      num == 8   ? "BINANCE:LITUSDTPERP"      : num == 9   ? "BINANCE:LPTUSDTPERP"      :
                      num == 10  ? "BINANCE:LRCUSDTPERP"      : num == 11  ? "BINANCE:LTCUSDTPERP"      :
                      num == 12  ? "BINANCE:LUNA2USDTPERP"    : na 
        else if set == "8.LIST"
            symbols = num == 0   ? "BINANCE:MANAUSDTPERP"     : num == 1   ? "BINANCE:MASKUSDTPERP"     : 
                      num == 2   ? "BINANCE:MATICUSDTPERP"    : num == 3   ? "BINANCE:MKRUSDTPERP"      : 
                      num == 4   ? "BINANCE:MTLUSDTPERP"      : num == 5   ? "BINANCE:NEARUSDTPERP"     : 
                      num == 6   ? "BINANCE:NEOUSDTPERP"      : num == 7   ? "BINANCE:NKNUSDTPERP"      : 
                      num == 8   ? "BINANCE:OCEANUSDTPERP"    : num == 9   ? "BINANCE:OGNUSDTPERP"      : 
                      num == 10  ? "BINANCE:OMGUSDTPERP"      : num == 11  ? "BINANCE:ONEUSDTPERP"      : 
                      num == 12  ? "BINANCE:ONTUSDTPERP"      : na
        else if set == "9.LIST"
            symbols = num == 0   ? "BINANCE:OPUSDTPERP"       : num == 1   ? "BINANCE:PEOPLEUSDTPERP"   :
                      num == 2   ? "BINANCE:QNTUSDTPERP"      : num == 3   ? "BINANCE:QTUMUSDTPERP"     :
                      num == 4   ? "BINANCE:REEFUSDTPERP"     : num == 5   ? "BINANCE:RENUSDTPERP"      :
                      num == 6   ? "BINANCE:RLCUSDTPERP"      : num == 7   ? "BINANCE:ROSEUSDTPERP"     :
                      num == 8   ? "BINANCE:RSRUSDTPERP"      : num == 9   ? "BINANCE:RUNEUSDTPERP"     :
                      num == 10  ? "BINANCE:RVNUSDTPERP"      : num == 11  ? "BINANCE:SANDUSDTPERP"     :
                      num == 12  ? "BINANCE:SFPUSDTPERP"      : na
        else if set == "10.LIST"
            symbols = num == 0   ? "BINANCE:SKLUSDTPERP"      : num == 1   ? "BINANCE:SNXUSDTPERP"      : 
                      num == 2   ? "BINANCE:SOLUSDTPERP"      : num == 3   ? "BINANCE:SPELLUSDTPERP"    : 
                      num == 4   ? "BINANCE:STGUSDTPERP"      : num == 5   ? "BINANCE:STMXUSDTPERP"     : 
                      num == 6   ? "BINANCE:STORJUSDTPERP"    : num == 7   ? "BINANCE:SUSHIUSDTPERP"    : 
                      num == 8   ? "BINANCE:SXPUSDTPERP"      : num == 9   ? "BINANCE:THETAUSDTPERP"    : 
                      num == 10  ? "BINANCE:TOMOUSDTPERP"     : num == 11  ? "BINANCE:TRBUSDTPERP"      : 
                      num == 12  ? "BINANCE:TRXUSDTPERP"      : na
        else if set == "11.LIST"
            symbols = num == 0   ? "BINANCE:UNFIUSDTPERP"     : num == 1   ? "BINANCE:UNIUSDTPERP"      :
                      num == 2   ? "BINANCE:VETUSDTPERP"      : num == 3   ? "BINANCE:WAVESUSDTPERP"    :
                      num == 4   ? "BINANCE:WOOUSDTPERP"      : num == 5   ? "BINANCE:XEMUSDTPERP"      :
                      num == 6   ? "BINANCE:XLMUSDTPERP"      : num == 7   ? "BINANCE:XMRUSDTPERP"      :
                      num == 8   ? "BINANCE:XRPUSDTPERP"      : num == 9   ? "BINANCE:XTZUSDTPERP"      :
                      num == 10  ? "BINANCE:YFIUSDTPERP"      : num == 11  ? "BINANCE:ZECUSDTPERP"      :
                      num == 12  ? "BINANCE:ZENUSDTPERP"      : na
        else if set == "12.LIST"
            symbols = num == 0   ? "BINANCE:ZILUSDTPERP"      : num == 1  ? "BINANCE:ZRXUSDTPERP"       : 
                      num == 2   ? "CRYPTOCAP:TOTAL"          : na
        else if set == "FAVORITE LIST"
            symbols = num == 0   ? "CRYPTOCAP:TOTAL"             : num == 1   ? "BINANCE:BTCUSDTPERP"         :
                      num == 2   ? "BINANCE:BTCDOMUSDTPERP"      : num == 3   ? input.symbol(defval="BINANCE"):
                      num == 4   ? input.symbol(defval="BINANCE"): num == 5   ? input.symbol(defval="BINANCE"):
                      num == 6   ? input.symbol(defval="BINANCE"): num == 7   ? input.symbol(defval="BINANCE"):
                      num == 8   ? input.symbol(defval="BINANCE"): num == 9   ? input.symbol(defval="BINANCE"):
                      num == 10  ? input.symbol(defval="BINANCE"): num == 11  ? input.symbol(defval="BINANCE"):
                      num == 12  ? input.symbol(defval="BINANCE"): na

// |                                 CALCULATION                                |
//Order
[colMtx, order] = switch sortTable
    "High-Low C/H" => [1, order.descending]
    "Low-High C/H" => [1, order.ascending ]
    "High-Low C/O" => [2, order.descending]
    "Low-High C/O" => [2, order.ascending ]
    "High-Low C/L" => [3, order.descending]
    "Low-High C/L" => [3, order.ascending ]
    => [0, order.ascending]

// Matrix 
sectors = matrix.new<float>(13, 4, na)

// Fill Matrix Function
fun_matrix(mtxName, row, adj) =>
    
    // Symbol Name
    string sym= symbol(row, "symbols")
    s = ticker.modify(sym, syminfo.session)
    
    // Symbol code
    matrix.set(mtxName, row + adj, 0, row)
    
    // C/H
    ch = request.security(s, timeframe.period, (close[0]/high[0]))
    matrix.set(mtxName, row + adj, 1, ch)
    
    // C/O
    co = request.security(s, timeframe.period, (close[0]/open[0]))
    matrix.set(mtxName, row + adj, 2, co)
    
    // C/L
    cl = request.security(s, timeframe.period, (close[0]/low[0]))
    matrix.set(mtxName, row + adj, 3, cl)

//++++++++++ Sector Matrix
fun_matrix(sectors,  0, 0)
fun_matrix(sectors,  1, 0)
fun_matrix(sectors,  2, 0)
fun_matrix(sectors,  3, 0)
fun_matrix(sectors,  4, 0)
fun_matrix(sectors,  5, 0)
fun_matrix(sectors,  6, 0)
fun_matrix(sectors,  7, 0)
fun_matrix(sectors,  8, 0)
fun_matrix(sectors,  9, 0)
fun_matrix(sectors,  10, 0)
fun_matrix(sectors,  11, 0)
fun_matrix(sectors,  12, 0)

matrix.sort(sectors, colMtx, order)

// |                                   Table                                    |
// Get Table Position
table_pos(p) => 
    switch p
        "Top Right"     => position.top_right     
        "Middle Right"  => position.middle_right   
        "Bottom Right"  => position.bottom_right  
        "Top Center"    => position.top_center    
        "Middle Center" => position.middle_center
        "Bottom Center" => position.bottom_center
        "Top Left"      => position.top_left      
        "Middle Left"   => position.middle_left 
        => position.bottom_left

// Get Table Size
table_size(s) =>
    switch s
        "Auto"   => size.auto   
        "Huge"   => size.huge   
        "Large"  => size.large  
        "Normal" => size.normal 
        "Small"  => size.small
        => size.tiny
tz = table_size(in_table_size)

// Get Title Column
fun_titlCol(tbl, col, txtCol) =>
    table.cell(tbl, col, 0, text_halign =text.align_left, 
     text       = txtCol, 
     text_color = title_textCol, 
     text_size  = tz, 
     bgcolor    = background_col)
   
// Get Cell Values
fun_cell(tbl, row, mtxName, mtxRow) =>

    changePerc = matrix.get(mtxName, mtxRow, 2)
    calll = (changePerc-1)
    bgColor    = (calll < 0 ? color.rgb(255,106,106,0):color.rgb(106,255,106,0))

    table.cell(tbl, 0, row, text_halign=text.align_right, text_valign=text.align_center,
     text       = sym_short(symbol(matrix.get(mtxName, mtxRow, 0), "symbols")),
     text_color = title_textCol, text_size = tz, bgcolor= background_col)
        
    table.cell(tbl, 1, row, text_halign=text.align_right, text_valign=text.align_center,
     text       = str.tostring(matrix.get(mtxName,mtxRow,1),"#.####"),
     text_color = cell_textCol, text_size = tz, bgcolor = bgColor)
    
    table.cell(tbl, 2, row, text_halign=text.align_right, text_valign=text.align_center,
     text       = str.tostring(matrix.get(mtxName,mtxRow,2),"#.####"),
     text_color = cell_textCol, text_size = tz, bgcolor = bgColor)
    
    table.cell(tbl, 3, row, text_halign=text.align_right, text_valign=text.align_center,
     text       = str.tostring(matrix.get(mtxName,mtxRow,3),"#.####"),
     text_color = cell_textCol, text_size = tz, bgcolor = bgColor)
        
// Create Table
var tbl = table.new(table_pos(in_table_pos), 4, 14, frame_width = 5, 
         border_width = 1, border_color = color.new(title_textCol, 100))

// Fill up Cells.
if barstate.islast
    table.clear(tbl, 0, 0, 3, 13)
    
    // columns
    fun_titlCol(tbl, 0, "Symbol")
    fun_titlCol(tbl, 1, "C/H=1 $:LONG \nC/H<1 $:UNSTABLE \n.")
    fun_titlCol(tbl, 2, "C/O>1 $:LONG \nC/O=1 $:STABLE \nC/O<1 $:SHORT")
    fun_titlCol(tbl, 3, ". \nC/L>1 $:UNSTABLE \nC/L=1 $:SHORT")

    for i = 1 to 13
        fun_cell(tbl, i, sectors, i - 1)