User interface panels

For setting up strategy parameters, triggering actions, or displaying something, the following elements are available:

The control panel can be created by script or with Excel™. The panel is a rectangular grid of multi-purpose cells, similar to a spreadsheet. Every cell can display text, numbers, or a color, and can serve as a numeric or text input field or as a push, toggle, radio, or spin button. With this concept, even complex control functions for commercial strategies can be realized in a simple way. Such a control panel does not look particularly pretty, but it's functional:

Panel

A panel can have a virtually unlimited number of rows and columns. If there are more than fit on the screen, the panel can be scrolled with scrollbars on the right and bottom side. Header rows or columns can be defined that stay fixed when the rest of the panel scrolls. This way, individual asset parameters can be set up or displayed for multi-asset strategies. An example of a scrolling panel with thousands of rows can be found in the History.c script.

The following functions can be used for defining and handling a control panel or for populating the [Action] scrollbox:

panel (int rows, int columns, int color, int size)

Opens an empty control panel with the given number of rows and columns, background color, and horizontal cell size in pixels. If a control panel was already open, it is replaced by the new one. The panel is opened next to the Zorro window, and can be freely moved by dragging it with the mouse. If the number of rows or columns exceed the screen size, scrollbars are added to the panel. panel(0,0,0) removes the panel.

panel (string filename, int color, int size)

As above, but loads a control panel from a .csv spreadsheet file. Every cell of the panel is set up from the corresponding spreadsheet cell. The cells in the file are separated with  ;  or  ,  delimiters. Every row must end with a new line character. The following control characters in the cell content have a special meaning:  !...  - cell is a button;  #...  - cell can be edited;  @  - merge the cell with the next not-empty cell to the left. The default colors are grey for text cells, red for numbers, white for editable cells, and blue for buttons; they can be changed with the ColorPanel variables.
  CSV delimiters and control characters must otherwise not appear in cell content. For still displaying them, the cell content can be set with panelSet() after loading the panel. Examples of spreadsheet-generated panels can be found in the Strategy folder.

panelSet (int row, int col, string text, int color, int style, int type)

Set properties or update content of an individual cell.
 
row - row number starting with 0, or -1 for the [Result] button, or -2 for the [Action] scrollbox.
col - column number starting with 0, or line number in the [Action] scrollbox. 
text - content to appear in the cell, button, or scrollbox line, or 0 for not changing the content. Numbers can be converted to text with strf.
color - background color of the cell, or 0 for not changing the color.
style - 1 normal text, 2 highlighted text, 3 greyed-out text, +4 right-aligned, +8 left-aligned, +12 centered, +16 bold, or 0 for not changing the style.
type - 1 normal cell, 2 editable cell, 4 button, 0 for not changing the type. If row is -2 and type is nonzero, the text is selected in the [Action] scrollbox.

Default colors are stored in ColorPanel[n], where n = 0 for text background, 1 for number background, 2 for an editable field, 3 for a button, 4 for highlighted text, 5 for greyed out text.

panelGet (int row, int col): string

Returns a temporary string with the current content of the cell. Numbers can be converted from the returned string with sscanf, atof, or atoi.

panelMerge(int Row, int Col, int NumRows, int NumCols)

Merges the given area of the panel to a single cell, with the content of the first cell. Row / Col is the upper left corner and NumRows / NumCols the size.

panelFix(int NumRows, int NumCols)

Gives the number of header rows and columns that stay fixed on a scrolling panel. Header cells cannot be merged.

panelSave (string Filename)

Saves the panel content in .csv format to the given FileName, or to "Data\*.pan" when the name is empty or zero.

panelLoad (string Filename)

Loads the saved panel content in .csv format from the given FileName, or from "Data\*.pan" when the name is empty or zero. Only editable or button cells are affected.
 

click(int Row, int Col)

User-supplied function that is triggered by the following GUI actions:
- mouse click on a button cell,
- entering a number or editing a cell,
- clicking the [Result] button (Row = -1),
- selecting a previously set action from the [Action] scrollbox (Row = -2).
- selecting an asset with the [Asset] scrollbox (Row = -3; asset name in AssetBox).
Dependent on the desired behavior of the button, the function can then change the cell color or content through panelSet, and/or save the panel content with panelSave().

Remarks:

Examples: (see also Download.c, History.c, PanelTest.c)

// click function example
function click(int row,int col)
{
  if(!is(RUNNING)) return; // prevent clicks after the end of the script
  if(row < 0) return; // prevent clicks by result button or scroll box
  sound("click.wav");
string Text = panelGet(row,col);
if(Text == "Buy") // toggle button between "Buy" and "Sell"
panelSet(row,col,"Sell",0,0,0);
else if(Text == "Sell")
panelSet(row,col,"Buy",0,0,0);
else if(Text == "Close All") // push button to close trades call(1,closeAllTrades,0,0); else if(Text == "Cancel") // back to last saved state panelLoad("Data\\mypanel.csv"); panelSave("Data\\mypanel.csv"); }
// Entering a code number
int Code = 0;

void click(int row,int col)
{
  sound("click.wav");
  Code = atoi(panelGet(0,0));
}

void enterNumber()
{
  Code = 0;
  panel(1,4,GREY,30);
  print(TO_PANEL,"Enter your code number");
  panelSet(0,0,"",0,1,2);
  panelMerge(0,0,1,3);
  panelSet(0,3,"Ok",ColorPanel[3],1,4);
  while(!Code) wait(0);
  panel(0,0,0);
  printf("\nCode: %i",Code);
}
// Start a program via Action scrollbox
void click(int row,int col)
{
  if(row == -2 && col == 0) // first action line
    exec("Editor","Strategy\\Z.ini",0);
}

function run()
{
  panelSet(-2,0,"Edit Z9.ini",0,0,0);
  ...
}

See also:

sliders, wait, user supplied functions, ColorPanel

 

► latest version online