Search code examples
arrayscolorspine-scriptpine-script-v5tradingview-api

Array color to multiple values


Is there any way to shorten this using only two colors but having the second color fill the remaining values? I have a sorted 21 value table consisting of 1 variable color and 20 fixed colors.

color[] cell_color=array.from(color.rgb(0, 0, 0), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107))

color[] text_color=array.from(color.rgb(255, 255, 255), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0))

I was trying to use array.fill & array.new_color to the remaining values but would just result in the array out of bound error message.

EDIT: Here's the complete code (Screener for 40+ instruments by QuantNomad copy)

//@version=5
indicator('Screener for 40+ instruments', overlay=true)

////////////
// INPUTS //

// SMA
rsi_len = input.int(  14, title = "RSI Length",     group = "Indicators")
rsi_os  = input.float(30, title = "RSI Overbought", group = "Indicators")
rsi_ob  = input.float(70, title = "RSI Oversold",   group = "Indicators")

col_width = input.float(5, title = "Column Width (%)")
scr_numb  = input.int(1, title = "Screen #", tooltip = '1 - rightmost screener', minval = 1)
 
/////////////
// SYMBOLS // 

u01 = input.bool(true,  title = "", group = 'Symbols', inline = 's01')
u02 = input.bool(true,  title = "", group = 'Symbols', inline = 's02')
u03 = input.bool(true,  title = "", group = 'Symbols', inline = 's03')
u04 = input.bool(true,  title = "", group = 'Symbols', inline = 's04')
u05 = input.bool(true,  title = "", group = 'Symbols', inline = 's05')
u06 = input.bool(true,  title = "", group = 'Symbols', inline = 's06')
u07 = input.bool(true,  title = "", group = 'Symbols', inline = 's07')
u08 = input.bool(true,  title = "", group = 'Symbols', inline = 's08')
u09 = input.bool(true,  title = "", group = 'Symbols', inline = 's09')
u10 = input.bool(true,  title = "", group = 'Symbols', inline = 's10')
u11 = input.bool(true,  title = "", group = 'Symbols', inline = 's11')
u12 = input.bool(true,  title = "", group = 'Symbols', inline = 's12')
u13 = input.bool(true,  title = "", group = 'Symbols', inline = 's13')
u14 = input.bool(true,  title = "", group = 'Symbols', inline = 's14')
u15 = input.bool(true,  title = "", group = 'Symbols', inline = 's15')
u16 = input.bool(true,  title = "", group = 'Symbols', inline = 's16')
u17 = input.bool(true,  title = "", group = 'Symbols', inline = 's17')
u18 = input.bool(true,  title = "", group = 'Symbols', inline = 's18')
u19 = input.bool(true,  title = "", group = 'Symbols', inline = 's19')
u20 = input.bool(true,  title = "", group = 'Symbols', inline = 's20')
u21 = input.bool(true,  title = "", group = 'Symbols', inline = 's21')

s01 = input.symbol('BTCUSDT',  group = 'Symbols', inline = 's01')
s02 = input.symbol('XRPUSDT',  group = 'Symbols', inline = 's02')
s03 = input.symbol('DOGEUSDT', group = 'Symbols', inline = 's03')
s04 = input.symbol('BNBUSDT',  group = 'Symbols', inline = 's04')
s05 = input.symbol('ETHUSDT',  group = 'Symbols', inline = 's05')
s06 = input.symbol('ADAUSDT',  group = 'Symbols', inline = 's06')
s07 = input.symbol('XRPBTC',   group = 'Symbols', inline = 's07')
s08 = input.symbol('DOGEBTC',  group = 'Symbols', inline = 's08')
s09 = input.symbol('TRXUSDT',  group = 'Symbols', inline = 's09')
s10 = input.symbol('BTCBUSD',  group = 'Symbols', inline = 's10')
s11 = input.symbol('ETHBUSD',  group = 'Symbols', inline = 's11')
s12 = input.symbol('BNBBUSD',  group = 'Symbols', inline = 's12')
s13 = input.symbol('VETUSDT',  group = 'Symbols', inline = 's13')
s14 = input.symbol('ETHBTC',   group = 'Symbols', inline = 's14')
s15 = input.symbol('BNBBTC',   group = 'Symbols', inline = 's15')
s16 = input.symbol('EOSUSDT',  group = 'Symbols', inline = 's16')
s17 = input.symbol('XLMUSDT',  group = 'Symbols', inline = 's17')
s18 = input.symbol('LTCUSDT',  group = 'Symbols', inline = 's18')
s19 = input.symbol('XRPBUSD',  group = 'Symbols', inline = 's19')
s20 = input.symbol('WINUSDT',  group = 'Symbols', inline = 's20')
s21 = input.symbol('DOTUSDT',  group = 'Symbols', inline = 's21')

//////////////////
// CALCULATIONS //

// Get only symbol
only_symbol(s) => 
    array.get(str.split(s, ":"), 1)

screener_func() =>
    
    // RSI
    rsi = ta.rsi(close, rsi_len)
    
    rsi

// Security call
rsi01 = request.security(s01, timeframe.period, screener_func())
rsi02 = request.security(s02, timeframe.period, screener_func())
rsi03 = request.security(s03, timeframe.period, screener_func())
rsi04 = request.security(s04, timeframe.period, screener_func())
rsi05 = request.security(s05, timeframe.period, screener_func())
rsi06 = request.security(s06, timeframe.period, screener_func())
rsi07 = request.security(s07, timeframe.period, screener_func())
rsi08 = request.security(s08, timeframe.period, screener_func())
rsi09 = request.security(s09, timeframe.period, screener_func())
rsi10 = request.security(s10, timeframe.period, screener_func())
rsi11 = request.security(s11, timeframe.period, screener_func())
rsi12 = request.security(s12, timeframe.period, screener_func())
rsi13 = request.security(s13, timeframe.period, screener_func())
rsi14 = request.security(s14, timeframe.period, screener_func())
rsi15 = request.security(s15, timeframe.period, screener_func())
rsi16 = request.security(s16, timeframe.period, screener_func())
rsi17 = request.security(s17, timeframe.period, screener_func())
rsi18 = request.security(s18, timeframe.period, screener_func())
rsi19 = request.security(s19, timeframe.period, screener_func())
rsi20 = request.security(s20, timeframe.period, screener_func())
rsi21 = request.security(s21, timeframe.period, screener_func())

////////////
// ARRAYS //

s_arr   = array.new_string(0)
u_arr   = array.new_bool(0)
rsi_arr = array.new_float(0)

// Add Symbols 
array.push(s_arr, only_symbol(s01))
array.push(s_arr, only_symbol(s02))
array.push(s_arr, only_symbol(s03))
array.push(s_arr, only_symbol(s04))
array.push(s_arr, only_symbol(s05))
array.push(s_arr, only_symbol(s06))
array.push(s_arr, only_symbol(s07))
array.push(s_arr, only_symbol(s08))
array.push(s_arr, only_symbol(s09))
array.push(s_arr, only_symbol(s10))
array.push(s_arr, only_symbol(s11))
array.push(s_arr, only_symbol(s12))
array.push(s_arr, only_symbol(s13))
array.push(s_arr, only_symbol(s14))
array.push(s_arr, only_symbol(s15))
array.push(s_arr, only_symbol(s16))
array.push(s_arr, only_symbol(s17))
array.push(s_arr, only_symbol(s18))
array.push(s_arr, only_symbol(s19))
array.push(s_arr, only_symbol(s20))
array.push(s_arr, only_symbol(s21))

// FLAGS 
array.push(u_arr, u01)
array.push(u_arr, u02)
array.push(u_arr, u03)
array.push(u_arr, u04)
array.push(u_arr, u05)
array.push(u_arr, u06)
array.push(u_arr, u07)
array.push(u_arr, u08)
array.push(u_arr, u09)
array.push(u_arr, u10)
array.push(u_arr, u11)
array.push(u_arr, u12)
array.push(u_arr, u13)
array.push(u_arr, u14)
array.push(u_arr, u15)
array.push(u_arr, u16)
array.push(u_arr, u17)
array.push(u_arr, u18)
array.push(u_arr, u19)
array.push(u_arr, u20)
array.push(u_arr, u21)

// RSI 
array.push(rsi_arr, rsi01)
array.push(rsi_arr, rsi02)
array.push(rsi_arr, rsi03)
array.push(rsi_arr, rsi04)
array.push(rsi_arr, rsi05)
array.push(rsi_arr, rsi06)
array.push(rsi_arr, rsi07)
array.push(rsi_arr, rsi08)
array.push(rsi_arr, rsi09)
array.push(rsi_arr, rsi10)
array.push(rsi_arr, rsi11)
array.push(rsi_arr, rsi12)
array.push(rsi_arr, rsi13)
array.push(rsi_arr, rsi14)
array.push(rsi_arr, rsi15)
array.push(rsi_arr, rsi16)
array.push(rsi_arr, rsi17)
array.push(rsi_arr, rsi18)
array.push(rsi_arr, rsi19)
array.push(rsi_arr, rsi20)
array.push(rsi_arr, rsi21)

color[] cell_color=array.from(color.rgb(0, 0, 0), 
  color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), 
  color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), 
  color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), 
  color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), 
  color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107), color.rgb(93, 96, 107))

color[] text_color=array.from(color.rgb(255, 255, 255), 
  color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), 
  color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), 
  color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), 
  color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), 
  color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0), color.rgb(0, 0, 0))

///////////
// PLOTS //
sort=array.sort_indices(rsi_arr, order.descending)
var tbl = table.new(position.top_right, 3, 22, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.white, 100))

if barstate.islast

    table.cell(tbl, 0, 0, 'Symbol',  width = col_width,     text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.small)
    table.cell(tbl, 1, 0, 'RSI',     width = col_width,     text_halign = text.align_center, bgcolor = color.gray, text_color = color.white, text_size = size.small)
    if (scr_numb > 1)
        table.cell(tbl, 2, 0,  '',       width = col_width * 2 * (scr_numb - 1), text_halign = text.align_center, bgcolor = color.new(color.gray, 100), text_size = size.small)

    for i = 0 to 20
        S_ARR = array.get(s_arr, array.get(sort, i))
        U_ARR = array.get(u_arr, array.get(sort, i))
        RSI = array.get(rsi_arr, array.get(sort, i))
        RSI_C = array.get(cell_color, array.get(sort, i))
        RSI_TC = array.get(text_color, array.get(sort, i))
        rsi_col  = RSI > rsi_ob    ? color.red   : RSI < rsi_os ? color.green : #aaaaaa 
    
        table.cell(tbl, 0, i + 1, S_ARR,                             text_halign = text.align_center, bgcolor = RSI_C, text_color = RSI_TC, text_size = size.small)
        table.cell(tbl, 1, i + 1, str.tostring(RSI, "#.##"),         text_halign = text.align_center, bgcolor = rsi_col,    text_color = color.white, text_size = size.small)

Solution

  •     for i = 0 to 20
            S_ARR = array.get(s_arr, array.get(sort, i))
            U_ARR = array.get(u_arr, array.get(sort, i))
            RSI = array.get(rsi_arr, array.get(sort, i))
            // RSI_C = array.get(cell_color, array.get(sort, i))
            // RSI_TC = array.get(text_color, array.get(sort, i))
            rsi_c = S_ARR == 'BTCUSDT' ? color.rgb(0, 0, 0) : color.rgb(93, 96, 107)
            rsi_tc = S_ARR == 'BTCUSDT' ? color.rgb(255, 255, 255) : color.rgb(0, 0, 0)
            rsi_col  = RSI > rsi_ob    ? color.red   : RSI < rsi_os ? color.green : #aaaaaa 
        
            table.cell(tbl, 0, i + 1, S_ARR,                             text_halign = text.align_center, bgcolor = rsi_c, text_color = rsi_tc, text_size = size.small)
            table.cell(tbl, 1, i + 1, str.tostring(RSI, "#.##"),         text_halign = text.align_center, bgcolor = rsi_col,    text_color = color.white, text_size = size.small)
    

    Never mind, I figured it out!

    I switched RSI_C and RSI_TC from using array.get, which required 21 colors as this table uses 21 values to simply using ?: conditions.

    The way I had it set up previously, the table would need a total of 42 colors (21 text colors and 21 table colors).

    Out of those 42 colors I only needed 2 variable colors so by using S_ARR == 'BTCUSDT' ? color.rgb(0, 0, 0) for my variable colors and : color.rgb(93, 96, 107) for my fixed colors I was able to single out the only values that required a variable color while having the rest as fixed colors.