Home > Software design >  How can we toggle between two tables in Pinescript using a single boolean value?
How can we toggle between two tables in Pinescript using a single boolean value?

Time:07-02

I'm trying to toggle between two tables like this in Pinescript v5:

// live table
var table LEV_Display_mini = table.new(position.bottom_right, 14, 3)
var table LEV_Display = table.new(position.bottom_right, 14, 5)

color op_val_col = lev_sustain == max_lev ? color.green : lev_sustain >= lev_sustain[1] ? color.yellow : color.red

if barstate.islast
    if not miniOn
    
        table.cell(LEV_Display, 0, 0, 'New Positions:', text_color=color.white, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display, 1, 0, str.format('{0}x', max_lev), text_color=color.white, text_size=size.auto, bgcolor=color.black)
        
        table.cell(LEV_Display, 0, 1, 'Valid For:', text_color=color.white, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display, 1, 1, str.format('{0} bars', tradelength), text_color=color.white, text_size=size.auto, bgcolor=color.black)
        
        table.cell(LEV_Display, 0, 2, 'Est. Liq. Price:', text_color=color.white, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display, 1, 2, str.format('{0} L | {1} S', math.round(long_liq,decim),math.round(short_liq,decim)), text_color=color.white, text_size=size.auto, bgcolor=color.black)
        
        table.cell(LEV_Display, 0, 3, 'Open Positions:', text_color=color.yellow, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display, 1, 3, str.format('{0}x', lev_sustain), text_color = op_val_col, text_size=size.auto, bgcolor=color.black)
        
        table.cell(LEV_Display, 0, 4, 'Est. Liq. Price:', text_color=color.yellow, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display, 1, 4, str.format('{0} L | {1} S', math.round(op_long_liq,decim),math.round(op_short_liq,decim)), text_color=color.white, text_size=size.auto, bgcolor=color.black)
    
    else if miniOn
        
        table.cell(LEV_Display_mini, 0, 0, 'New Positions:', text_color=color.white, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display_mini, 1, 0, str.format('{0}x', max_lev), text_color=color.white, text_size=size.auto, bgcolor=color.black)
        
        table.cell(LEV_Display_mini, 0, 1, 'Est. Liq. Price:', text_color=color.white, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display_mini, 1, 1, str.format('{0} L | {1} S', math.round(long_liq,decim),math.round(short_liq,decim)), text_color=color.white, text_size=size.auto, bgcolor=color.black)
        
        table.cell(LEV_Display_mini, 0, 2, 'Open Positions:', text_color=color.yellow, text_size=size.auto, bgcolor=color.black)
        table.cell(LEV_Display_mini, 1, 2, str.format('{0}x', lev_sustain), text_color = op_val_col, text_size=size.auto, bgcolor=color.black)

Currently the result of this is the table object LEV_Display is showing when miniOn is false, and not showing when miniOn is true. This is fine.

The bug is that the table object LEV_Display_mini never shows regardless of that boolean value. At first I thought this may be some kind of overlapping graphics, but alas changing LEV_Display_mini's position elsewhere made no difference. I even tried deleting and clearing the table opposite of the one that should show and that also did not help.

Essentially I need a single boolean value that will result in table-A hiding and table-B showing and vice-versa.

CodePudding user response:

You can only have one table in one chart position, so when you create the LEV_Display table with table.new(), it overwrites the LEV_Display_mini created before it. Attempts to fill cells in the LEV_Display_mini table lead to nothing because it has been replaced and thus does not exist.

The easy fix is to just get rid of the LEV_Display_mini table. You can create a single table and fill it with either full info or short info depending on your bool input. The cells that are not filled are not displayed, so even though your full table created with 5 rows, it will only have 3 rows visually as long as you will not fill the remaining rows.

  • Related