Pro Trading Art - Top N Candle's Gainers/Losers(1-40)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © protradingart

//@version=5
indicator("Pro Trading Art - Top N Candle's Gainers/Losers(1-40)", "PTA - Top N Candle's Gainers/Losers(1-40)", overlay = true)

timeframe = input.timeframe("", "Timeframe")
period = input.int(5, "Period")

type = input.string("Gainers", "Top", options = ['Gainers', "Losers"])
location = input.string("Top", "Table Location", options = ['Top', "Middle", "Bottom"])

order =  type == "Gainers"? order.ascending : order.descending
tableLocation = location == "Top" ? position.top_right : location == "Middle" ? position.middle_right : position.bottom_right

topGainer(symbol, tf, period, symbolArray, changeArray)=>
    [current, prev] = request.security(symbol, tf, [close, close[period]])
    changePer = (current-prev)/prev*100

    array.push(symbolArray, symbol)
    array.push(changeArray, changePer)

gticker = '=============== Watchlist ==============='
symbol1 = input.symbol(defval = "NSE:ACC", title="Symbol1", group=gticker)
symbol2 = input.symbol(defval = "NSE:ADANIENT", title="Symbol2", group=gticker)
symbol3 = input.symbol(defval = "NSE:ADANIGREEN", title="Symbol3", group=gticker)
symbol4 = input.symbol(defval = "NSE:ADANIPORTS", title="Symbol4", group=gticker)
symbol5 = input.symbol(defval = "NSE:ADANITRANS", title="Symbol5", group=gticker)
symbol6 = input.symbol(defval = "NSE:AMBUJACEM", title="Symbol6", group=gticker)
symbol7 = input.symbol(defval = "NSE:APOLLOHOSP", title="Symbol7", group=gticker)
symbol8 = input.symbol(defval = "NSE:ASIANPAINT", title="Symbol8", group=gticker)
symbol9 = input.symbol(defval = "NSE:DMART", title="Symbol9", group=gticker)
symbol10 = input.symbol(defval = "NSE:AXISBANK", title="Symbol10", group=gticker)
symbol11 = input.symbol(defval = "NSE:BAJAJ_AUTO", title="Symbol11", group=gticker)
symbol12 = input.symbol(defval = "NSE:BAJFINANCE", title="Symbol12", group=gticker)
symbol13 = input.symbol(defval = "NSE:BAJAJFINSV", title="Symbol13", group=gticker)
symbol14 = input.symbol(defval = "NSE:BAJAJHLDNG", title="Symbol14", group=gticker)
symbol15 = input.symbol(defval = "NSE:BANDHANBNK", title="Symbol15", group=gticker)
symbol16 = input.symbol(defval = "NSE:BANKBARODA", title="Symbol16", group=gticker)
symbol17 = input.symbol(defval = "NSE:BERGEPAINT", title="Symbol17", group=gticker)
symbol18 = input.symbol(defval = "NSE:BPCL", title="Symbol18", group=gticker)
symbol19 = input.symbol(defval = "NSE:BHARTIARTL", title="Symbol19", group=gticker)
symbol20 = input.symbol(defval = "NSE:BIOCON", title="Symbol20", group=gticker)
symbol21 = input.symbol(defval = "NSE:BOSCHLTD", title="Symbol21", group=gticker)
symbol22 = input.symbol(defval = "NSE:BRITANNIA", title="Symbol22", group=gticker)
symbol23 = input.symbol(defval = "NSE:CHOLAFIN", title="Symbol23", group=gticker)
symbol24 = input.symbol(defval = "NSE:CIPLA", title="Symbol24", group=gticker)
symbol25 = input.symbol(defval = "NSE:COALINDIA", title="Symbol25", group=gticker)
symbol26 = input.symbol(defval = "NSE:COLPAL", title="Symbol26", group=gticker)
symbol27 = input.symbol(defval = "NSE:DLF", title="Symbol27", group=gticker)
symbol28 = input.symbol(defval = "NSE:DABUR", title="Symbol28", group=gticker)
symbol29 = input.symbol(defval = "NSE:DIVISLAB", title="Symbol29", group=gticker)
symbol30 = input.symbol(defval = "NSE:DRREDDY", title="Symbol30", group=gticker)
symbol31 = input.symbol(defval = "NSE:EICHERMOT", title="Symbol31", group=gticker)
symbol32 = input.symbol(defval = "NSE:NYKAA", title="Symbol32", group=gticker)
symbol33 = input.symbol(defval = "NSE:GAIL", title="Symbol33", group=gticker)
symbol34 = input.symbol(defval = "NSE:GLAND", title="Symbol34", group=gticker)
symbol35 = input.symbol(defval = "NSE:GODREJCP", title="Symbol35", group=gticker)
symbol36 = input.symbol(defval = "NSE:GRASIM", title="Symbol36", group=gticker)
symbol37 = input.symbol(defval = "NSE:HCLTECH", title="Symbol37", group=gticker)
symbol38 = input.symbol(defval = "NSE:HDFCAMC", title="Symbol38", group=gticker)
symbol39 = input.symbol(defval = "NSE:HDFCBANK", title="Symbol39", group=gticker)
symbol40 = input.symbol(defval = "NSE:HDFCLIFE", title="Symbol40", group=gticker)


symbol = array.new_string(na)
changePer =  array.new_float(na)


topGainer(symbol1, timeframe, period, symbol, changePer)
topGainer(symbol2, timeframe, period, symbol, changePer)
topGainer(symbol3, timeframe, period, symbol, changePer)
topGainer(symbol4, timeframe, period, symbol, changePer)
topGainer(symbol5, timeframe, period, symbol, changePer)
topGainer(symbol6, timeframe, period, symbol, changePer)
topGainer(symbol7, timeframe, period, symbol, changePer)
topGainer(symbol8, timeframe, period, symbol, changePer)
topGainer(symbol9, timeframe, period, symbol, changePer)
topGainer(symbol10, timeframe, period, symbol, changePer)
topGainer(symbol11, timeframe, period, symbol, changePer)
topGainer(symbol12, timeframe, period, symbol, changePer)
topGainer(symbol13, timeframe, period, symbol, changePer)
topGainer(symbol14, timeframe, period, symbol, changePer)
topGainer(symbol15, timeframe, period, symbol, changePer)
topGainer(symbol16, timeframe, period, symbol, changePer)
topGainer(symbol17, timeframe, period, symbol, changePer)
topGainer(symbol18, timeframe, period, symbol, changePer)
topGainer(symbol19, timeframe, period, symbol, changePer)
topGainer(symbol20, timeframe, period, symbol, changePer)
topGainer(symbol21, timeframe, period, symbol, changePer)
topGainer(symbol22, timeframe, period, symbol, changePer)
topGainer(symbol23, timeframe, period, symbol, changePer)
topGainer(symbol24, timeframe, period, symbol, changePer)
topGainer(symbol25, timeframe, period, symbol, changePer)
topGainer(symbol26, timeframe, period, symbol, changePer)
topGainer(symbol27, timeframe, period, symbol, changePer)
topGainer(symbol28, timeframe, period, symbol, changePer)
topGainer(symbol29, timeframe, period, symbol, changePer)
topGainer(symbol30, timeframe, period, symbol, changePer)
topGainer(symbol31, timeframe, period, symbol, changePer)
topGainer(symbol32, timeframe, period, symbol, changePer)
topGainer(symbol33, timeframe, period, symbol, changePer)
topGainer(symbol34, timeframe, period, symbol, changePer)
topGainer(symbol35, timeframe, period, symbol, changePer)
topGainer(symbol36, timeframe, period, symbol, changePer)
topGainer(symbol37, timeframe, period, symbol, changePer)
topGainer(symbol38, timeframe, period, symbol, changePer)
topGainer(symbol39, timeframe, period, symbol, changePer)
topGainer(symbol40, timeframe, period, symbol, changePer)

sortKeyValue(key, value, order=order.ascending)=>
    if array.size(key) == array.size(value)

        sortedKey= array.new_string(na)

        sortedIndices = array.sort_indices(value, order)
        sortedValue = array.new_float(na)
        for i = 0 to array.size(key) - 1
            index = array.get(sortedIndices, i)
            array.push(sortedKey, array.get(key, index))
            array.push(sortedValue, array.get(value, index))

        [sortedKey, sortedValue]

[sortedKey, sortedValue] = sortKeyValue(symbol, changePer, order)

if barstate.islast
    size = array.size(sortedKey)
    if size > 0
        var table = table.new(position = tableLocation, columns = size + 2, rows = 3, bgcolor = color.yellow, border_width = 1, border_color=color.black)
        if barstate.islast
            table.cell(table_id = table, column = 0, row = 0, text = "Size")
            table.cell(table_id = table, column = 1, row = 0, text = "Index")
            
            table.cell(table_id = table, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = table, column = 1, row = 1, text = "Value")
            
            for i = 0 to size - 1
                table.cell(table_id = table, column = i + 2, row = 0, text = str.tostring(size-i))
                table.cell(table_id = table, column = i + 2, row = 1, text = str.tostring(array.get(sortedKey, i)))
                table.cell(table_id = table, column = i + 2, row = 2, text = str.tostring(array.get(sortedValue, i), format.percent))
    else
        var table = table.new(position = tableLocation, columns = 2, rows = 3, bgcolor = color.yellow, border_width = 1, border_color=color.black)
        if barstate.islast
            table.cell(table_id = table, column = 0, row = 0, text = "Size")
            table.cell(table_id = table, column = 1, row = 0, text = "Message")
            
            table.cell(table_id = table, column = 0, row = 1, text = str.tostring(size))
            table.cell(table_id = table, column = 1, row = 1, text = "No element in your array", text_color=color.red)


