pine-scriptbackground-color

Color conditional formating with two parameters in Pine Script


Tried to create bgcolor formating with two parameters, but getting error. Would someone fix the problem. Further, wish to know How to do color formating when table.cell_set_text() is used.

//Delta Table

Table_Def = table.new(position = position.bottom_right, columns = 30, rows = 1, frame_color = color.black, frame_width = 1, border_color = color.black, border_width = 1)

table.cell(Table_Def, column = 0, row = 2, text = "CumDelta", text_color = color.black, text_size = size.auto, text_halign = text.align_left)

table.cell(Table_Def, column = 1, row = 2, text = str.tostring(cumdelta\[29\]), text_color = color.black, bgcolor = if cumdelta\[29\]\>0 and cumdelta\[29\]\>cvdAverage\[29\] \* 1.05 ? color.new(color.lime, 0) : if cumdelta\[29\]\>0 and cumdelta\[29\]\>cvdAverage\[29\] \* 1.025 ? color.new(color.lime, 50) : if cumdelta\[29\]\<0 and cumdelta\[29\]\<cvdAverage\[29\]\*1.05) ? color.new(color.fuchsia, 0) : if cumdelta\[29\]\<0 and cumdelta\[29\]\<cvdAverage\[29\]\*1.05) ? color.new(color.fuchsia, 50) : color.new(color.gray, 50), text_size = size.auto, text_halign = text.align_right)

Solution

  • the ternary does not need "if"

    try this:

    //Delta Table
    
    Table_Def = table.new(position = position.bottom_right, columns = 30, rows = 1, frame_color = color.black, frame_width = 1, border_color = color.black, border_width = 1)
    
    table.cell(Table_Def, column = 0, row = 2, text = "CumDelta", text_color = color.black, text_size = size.auto, text_halign = text.align_left)
    
    cumdeltaCol = cumdelta > 0 and cumdelta > cvdAverage * 1.05  ? color.new(color.lime, 0)     :
                  cumdelta > 0 and cumdelta > cvdAverage * 1.025 ? color.new(color.lime, 50)    :
                  cumdelta < 0 and cumdelta < cvdAverage * 1.05  ? color.new(color.fuchsia, 0)  :
                  cumdelta < 0 and cumdelta < cvdAverage * 1.05  ? color.new(color.fuchsia, 50) :
                  color.new(color.gray, 50)
    
    table.cell(Table_Def, column = 1, row = 2, text = str.tostring(cumdelta), text_color = color.black, bgcolor = cumdeltaCol, text_size = size.auto, text_halign = text.align_right)