table.new() in MQL5: a grid of OBJ_LABEL objects per corner

Short answer

MetaTrader has no table object. Build each cell from an OBJ_LABEL, plus an OBJ_RECTANGLE_LABEL for its background, positioned with OBJPROP_CORNER, OBJPROP_XDISTANCE and OBJPROP_YDISTANCE on a fixed grid; the position.* constant becomes the corner.

table.new() creates a grid pinned to a corner of the chart pane, and table.cell() fills each cell with text, a colour and a background. MetaTrader has no table object. What it has is OBJ_LABEL, text at a pixel offset from a chart corner, and OBJ_RECTANGLE_LABEL, a filled rectangle at a pixel offset, both positioned with OBJPROP_CORNER, OBJPROP_XDISTANCE and OBJPROP_YDISTANCE. A Pine table becomes one or two such objects per cell laid out on a fixed grid, and the position.* constant becomes the corner they are measured from.

What table.new() computes

table.new(position, columns, rows, bgcolor, frame_color, frame_width, border_color, border_width) declares a grid at one of nine positions (position.top_left to position.bottom_right, including the centre variants) and returns a table id; a script may have only one table per position. table.cell(table_id, column, row, text, width, height, text_color, text_halign, text_valign, text_size, bgcolor, tooltip) sets a cell, with columns and rows zero-based, and table.cell_set_text and friends change one property later. Cells size themselves to their content unless width and height (percent of the pane) are given. Because a table does not move with the bars, scripts normally create it once with var and update the cells only when barstate.islast is true, which keeps the work to one bar per tick.

The MQL5 equivalent

The Pine version shows RSI and the close in a 2 by 2 table at the top right.

//@version=6
indicator("Table demo", overlay=true)
var table t = table.new(position.top_right, 2, 2, border_width=1)
r = ta.rsi(close, 14)
if barstate.islast
    table.cell(t, 0, 0, "RSI", text_color=color.white, bgcolor=color.gray)
    table.cell(t, 1, 0, str.tostring(r, "#.##"), text_color=color.white, bgcolor=color.blue)
    table.cell(t, 0, 1, "Close")
    table.cell(t, 1, 1, str.tostring(close))

In MQL5 each cell is an OBJ_RECTANGLE_LABEL for the background and an OBJ_LABEL for the text, both anchored to CORNER_RIGHT_UPPER. Distances are measured from the anchor corner, so for a right-hand corner Pine's column 0 is the one farthest from the edge. The cell size is fixed in pixels; ChartRedraw() makes the update visible between ticks.

#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input int InpLength = 14;   // RSI length

int hRsi;
#define TBL_X0 10     // pixels from the anchor corner
#define TBL_Y0 20
#define TBL_CW 70     // cell width
#define TBL_CH 20     // cell height
#define TBL_COLS 2
#define TBL_CORNER CORNER_RIGHT_UPPER   // position.top_right

// table.cell(t, col, row, text, text_color, bgcolor): a background rectangle and a text label,
// both anchored to the same chart corner
void Cell(const int col, const int row, const string text, const color txt, const color bg)
{
   string base = "tbl_" + IntegerToString(col) + "_" + IntegerToString(row);
   string bgNm = base + "_bg", txNm = base + "_tx";
   // distances are measured from the anchor corner, so for a right-hand corner column 0
   // (the leftmost in Pine) is the farthest from the edge
   int x = TBL_X0 + (TBL_COLS - 1 - col) * TBL_CW, y = TBL_Y0 + row * TBL_CH;
   if(ObjectFind(0, bgNm) < 0)
     {
      ObjectCreate(0, bgNm, OBJ_RECTANGLE_LABEL, 0, 0, 0);
      ObjectSetInteger(0, bgNm, OBJPROP_CORNER, TBL_CORNER);
      ObjectSetInteger(0, bgNm, OBJPROP_XSIZE, TBL_CW);
      ObjectSetInteger(0, bgNm, OBJPROP_YSIZE, TBL_CH);
      ObjectSetInteger(0, bgNm, OBJPROP_BORDER_TYPE, BORDER_FLAT);
      ObjectSetInteger(0, bgNm, OBJPROP_COLOR, clrSilver);    // frame_color
      ObjectSetInteger(0, bgNm, OBJPROP_SELECTABLE, false);
     }
   ObjectSetInteger(0, bgNm, OBJPROP_XDISTANCE, x);      // the rectangle grows towards the chart centre
   ObjectSetInteger(0, bgNm, OBJPROP_YDISTANCE, y);
   ObjectSetInteger(0, bgNm, OBJPROP_BGCOLOR, bg);
   if(ObjectFind(0, txNm) < 0)
     {
      ObjectCreate(0, txNm, OBJ_LABEL, 0, 0, 0);
      ObjectSetInteger(0, txNm, OBJPROP_CORNER, TBL_CORNER);
      ObjectSetInteger(0, txNm, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER);
      ObjectSetInteger(0, txNm, OBJPROP_FONTSIZE, 9);
      ObjectSetInteger(0, txNm, OBJPROP_SELECTABLE, false);
     }
   ObjectSetInteger(0, txNm, OBJPROP_XDISTANCE, x + 4);
   ObjectSetInteger(0, txNm, OBJPROP_YDISTANCE, y + 3);
   ObjectSetInteger(0, txNm, OBJPROP_COLOR, txt);
   ObjectSetString (0, txNm, OBJPROP_TEXT, text);
}

int OnInit()
{
   hRsi = iRSI(_Symbol, _Period, InpLength, PRICE_CLOSE);
   return hRsi == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED;
}

int OnCalculate(const int rates_total, const int prev_calculated,
                const datetime &time[], const double &open[], const double &high[],
                const double &low[], const double &close[], const long &tick_volume[],
                const long &volume[], const int &spread[])
{
   double r[1];
   if(CopyBuffer(hRsi, 0, 0, 1, r) != 1)      // newest value only: `if barstate.islast`
      return 0;
   Cell(0, 0, "RSI",   clrWhite, clrGray);
   Cell(1, 0, DoubleToString(r[0], 2), clrWhite, clrDodgerBlue);
   Cell(0, 1, "Close", clrBlack, clrWhiteSmoke);
   Cell(1, 1, DoubleToString(close[rates_total - 1], _Digits), clrBlack, clrWhiteSmoke);
   ChartRedraw();
   return rates_total;
}

void OnDeinit(const int reason) { ObjectsDeleteAll(0, "tbl_"); }

The position constants map onto the four corners: top_left to CORNER_LEFT_UPPER, top_right to CORNER_RIGHT_UPPER, bottom_left to CORNER_LEFT_LOWER, bottom_right to CORNER_RIGHT_LOWER. The centre positions have no corner; compute the offset from ChartGetInteger(0, CHART_WIDTH_IN_PIXELS) and CHART_HEIGHT_IN_PIXELS and re-place the cells in OnChartEvent on CHARTEVENT_CHART_CHANGE. For a one-column status table the quickest substitute is Comment(), which prints several lines of text at the top left of the chart with no objects at all.

Where the two differ

TopicPine ScriptMQL5
Object modelOne table, cells addressed by (column, row)One or two chart objects per cell, each with a unique name
PositionsNine, including centresFour corners; centres computed from the chart size
Cell sizeFits the text, or percent of the paneFixed pixels you choose; long text overflows
Frame and bordersframe_color, border_widthOBJPROP_BORDER_TYPE and colour per background rectangle
Text alignmenttext_halign, text_valignOBJPROP_ANCHOR of the label plus a hand-tuned offset
Transparencybgcolor with alphaNone; opaque backgrounds
Update costCheap; done on the last barProperty writes are cheap too, but do them once per tick, not per historical bar
Strategy TesterVisible in backtestsVisible only in visual mode

What the Stratlate converter emits

table.new(position, columns, rows, ...) becomes pine_table_new(columns, rows, CORNER_*), where the corner is chosen from the position constant (the right-hand and top positions default to CORNER_RIGHT_UPPER, bottom_left and bottom_center to CORNER_LEFT_LOWER, bottom_right to CORNER_RIGHT_LOWER, the left and centre-top positions to CORNER_LEFT_UPPER); a position held in a variable goes through pine_position_corner at run time. table.cell(t, col, row, text, ...) becomes pine_table_cell(t, col, row, text): one OBJ_LABEL per cell, named from the table id, column and row, placed on a fixed grid of 60 by 18 pixels from the corner, carrying the text only. The text_color, bgcolor and text_size arguments of table.cell itself are dropped, while the separate table.cell_set_text_color, table.cell_set_bgcolor and table.cell_set_text_size calls are mapped to matching helpers; frame, borders and alignment are not drawn, and table.merge_cells keeps the text in the top-left cell and is noted as cosmetic (PM283). The example converts CLEAN, and the numbers in the cells are what matters; expect to adjust cell sizes by hand after compiling.

Pitfalls

  • Updating the cells inside the historical loop. Thousands of property writes per load for a table that only shows the last value; update once after the loop, on the newest bar.
  • Anchoring the text label with ANCHOR_LEFT_UPPER in a right-hand corner. The text grows away from the edge in the wrong direction and leaves its cell; use ANCHOR_RIGHT_UPPER for right corners.
  • Counting columns from the edge in a right-hand corner. Pine's column 0 is the leftmost cell, so its distance from the right edge is the largest.
  • Forgetting ChartRedraw(). Object changes made without a new tick are not painted until the chart next repaints.
  • Reusing a name for two tables. Prefix the names with the table position so two converted scripts on one chart do not overwrite each other's cells.
  • Leaving the objects after removal. Add ObjectsDeleteAll(0, prefix) to OnDeinit.

FAQ

Is there really no table in MQL5?

Not as a chart object. The standard library's panel classes (CAppDialog and the controls under Controls) build dialogs from the same label and rectangle objects, and Comment() prints plain text at the top left. For a converted Pine table a grid of labels is the smallest faithful solution.

How do I centre a table like position.middle_center?

Read the pane size with ChartGetInteger(0, CHART_WIDTH_IN_PIXELS) and CHART_HEIGHT_IN_PIXELS, anchor to CORNER_LEFT_UPPER, and set each cell's distance to half the pane minus half the table. Re-run that placement on CHARTEVENT_CHART_CHANGE so a resized window keeps it centred.

Can I show a coloured background with transparency?

No. OBJPROP_BGCOLOR is opaque. Choose a colour close to the chart background for a subtle cell, and give important cells a strong solid colour as the example does.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.

Open the converter

Related