Expert Advisor featuring GUI: Creating the panel (part I)

Anatoli Kazharski | 31 July, 2018



Despite the active development of algorithmic trading, many traders still prefer manual trading. However, it is hardly possible to completely avoid the automation of routine operations.

The article shows the development of a multi-symbol signal Expert Advisor for manual trading. As an example, let's examine signals of Stochastic from the terminal standard delivery. You can use this code to develop your own EAs with graphical interface (GUI): any other indicator can be included into it, as well as results of certain calculations can be used to make decisions.

For those who execute other traders' orders, the article may be useful as an example of a technical task to demonstrate to customers. This example may save your time when preparing a comprehensible requirements specification for developing a program that features GUI.

Here are the issues to be discussed in detail in this article.

  • Creating a GUI.
  • Getting a list of symbols with specified properties. 
  • Trading operation controls. 
  • Fast switching of symbols and timeframes on charts without the EA re-initialization.
  • Managing chart properties via the user interface.
  • Receiving indicator signals from multiple symbols with color indication.
  • Working with open positions. 
  • EasyAndFast library updates.

The article is to be published in two parts. In this article, we consider developing the panel, while the next one describes filling it with functions.

GUI elements

We will start developing the EA with the construction of a GUI enabling users to interact with the program and allowing data visualization. It is possible to create a GUI using the capabilities of the standard library, but in my example, it is implemented based on the EasyAndFast library. Its rich features make it possible to focus on the functionality of the program itself without being distracted by refining its graphical part.

First, let's outline the general GUI structure. The diagram below shows the GUI window consisting of two tabs. The lists display the functions to be placed on them. This is a simplified example. A customer and a developer may elaborate on it in more detail during the discussion.

Fig. 1. General GUI view with notes

Fig. 1. General GUI view with notes

There may be a lot of GUI controls. Therefore, let's list them in a hierarchical form at first:

  • Form for controls
    • Status bar for displaying additional summary information
    • Group of tabs:
      • Trade:
        • Input field with a checkbox for sorting out the symbol list
        • Request button to start forming the list of symbols
        • Button for SELL
        • Button for BUY
        • Trade volume input field
        • Button for closing all open positions
        • Input field for setting a sell signal level
        • Input field for setting a buy signal level
        • Table of symbols for trading
        • Chart for visualizing symbol data. For more convenience, let managing some chart properties using the following group of elements:
          • Combo box with a drop-down list for selecting timeframe switching 
          • Checkbox for enabling/disabling the time scale 
          • Checkbox for enabling/disabling the price scale 
          • Scale management input field 
          • Button for enabling an indent 
          • Checkbox for displaying the indicator 
      • Positions:
        • Table of positions
    • Indicator for replaying frames

In the main program class (CProgram), declare methods and class instances of the elements listed above. The code of methods for creating elements is provided in a separate file and included into the file with an MQL program class:

//| Application development class                                    |
class CProgram : public CWndEvents
   //--- Window
   CWindow           m_window1;
   //--- Status bar
   CStatusBar        m_status_bar;
   //--- Tabs
   CTabs             m_tabs1;
   //--- Input fields
   CTextEdit         m_symb_filter;
   CTextEdit         m_lot;
   CTextEdit         m_up_level;
   CTextEdit         m_down_level;
   CTextEdit         m_chart_scale;
   //--- Buttons
   CButton           m_request;
   CButton           m_chart_shift;
   CButton           m_buy;
   CButton           m_sell;
   CButton           m_close_all;
   //--- Combo boxes
   CComboBox         m_timeframes;
   //--- Checkboxes
   CCheckBox         m_date_scale;
   CCheckBox         m_price_scale;
   CCheckBox         m_show_indicator;
   //--- Tables
   CTable            m_table_positions;
   CTable            m_table_symb;
   //--- Standard chart
   CStandardChart    m_sub_chart1;
   //--- Progress bar
   CProgressBar      m_progress_bar;
   //--- Create the GUI
   bool              CreateGUI(void);
   //--- Form
   bool              CreateWindow(const string text);
   //--- Status bar
   bool              CreateStatusBar(const int x_gap,const int y_gap);
   //--- Tabs
   bool              CreateTabs1(const int x_gap,const int y_gap);
   //--- Input fields
   bool              CreateSymbFilter(const int x_gap,const int y_gap,const string text);
   bool              CreateLot(const int x_gap,const int y_gap,const string text);
   bool              CreateUpLevel(const int x_gap,const int y_gap,const string text);
   bool              CreateDownLevel(const int x_gap,const int y_gap,const string text);
   bool              CreateChartScale(const int x_gap,const int y_gap,const string text);
   //--- Buttons
   bool              CreateRequest(const int x_gap,const int y_gap,const string text);
   bool              CreateChartShift(const int x_gap,const int y_gap,const string text);
   bool              CreateBuy(const int x_gap,const int y_gap,const string text);
   bool              CreateSell(const int x_gap,const int y_gap,const string text);
   bool              CreateCloseAll(const int x_gap,const int y_gap,const string text);
   //--- Combo box
   bool              CreateComboBoxTF(const int x_gap,const int y_gap,const string text);
   //--- Checkboxes
   bool              CreateDateScale(const int x_gap,const int y_gap,const string text);
   bool              CreatePriceScale(const int x_gap,const int y_gap,const string text);
   bool              CreateShowIndicator(const int x_gap,const int y_gap,const string text);
   //--- Tables
   bool              CreatePositionsTable(const int x_gap,const int y_gap);
   bool              CreateSymbolsTable(const int x_gap,const int y_gap);
   //--- Standard chart
   bool              CreateSubChart1(const int x_gap,const int y_gap);
   //--- Progress bar
   bool              CreateProgressBar(const int x_gap,const int y_gap,const string text);
//| Methods for creating controls                                    |
#include "CreateGUI.mqh"

Next, let's focus on assembling the GUI, methods for creating its elements and their properties.

Assembling the GUI

We will use the elements of ten types in the GUI of the developed application.

  • Form for controls (CWindow)
  • Status bar (CStatusBar)
  • Group of tabs (CTabs)
  • Input field (CTextEdit)
  • Button (CButton)
  • Combo box with a drop-down list (CComboBox)
  • Checkbox (CCheckBox)
  • Table (CTable)
  • Standard chart (CStandardChart)
  • Progress bar (CProgressBar)

We will need more than one element in some categories from this list, so we will consider only one element per each group. Let's consider the methods of their creation in the same sequence. 

Form for controls

Below is the code of the method for creating a form all other elements are to be located on. First, we need to add the form into the list of the program GUI elements. To do this, call the CWndContainer::AddWindow() method by passing the CWindow type element object to it. Then, set its properties before creating the form. We set the following properties (in the same order as in the listing below):

  • Form size (width and height)
  • Header font size
  • Form moving mode (within the chart)
  • Mode of manually re-sizing the form (by dragging the borders)
  • Form buttons. Visibility of each button is customizable. In this case, the following ones are used:
    • Closing the form. When clicking the button, the program closure confirmation window appears in the program main form.
    • Maximizing/minimizing the form. 
    • Element tooltips. This button also has two states. If enabled, specified tooltips are displayed in the controls.
    • Expanding the form for the entire chart area. This can be undone by clicking the button again.
  • A tooltip can be set for each button.

After setting the properties, call the form creation method — CWindow::CreateWindow() and pass to it:

  • chart ID,
  • chart subwindow index,
  • header text,
  • form initial coordinates.
//| Create the controls form                                         |
bool CProgram::CreateWindow(const string caption_text)
//--- Add the window pointer to the windows array
//--- Properties
//--- Set tooltips
//--- Create the form

It is recommended to compile the program and check the result each time you add a new GUI element: 

Fig. 2. Form for controls

Fig. 2. Form for controls

Screenshots of all the intermediate results are shown below.

Status bar

The code of the method for creating a status bar starts with specifying the main element, which is used to calculate the elements bound to it and align their size. This saves time when developing an application: a whole group of related elements can be moved by changing the coordinates only of the main one. To bind the element, its pointer is passed to the CElement::MainPointer() method. In this example, we bind the status bar to the form, therefore, the form object is passed to the method.

Then, set the status bar properties. It is to contain three sections displaying information for users. 

  • In order not to specify dimensions relative to the form, make sure the width changes automatically when the form width is changed.
  • The indent of the element's right edge is 1 pixel.
  • Bind the status bar to the bottom of the form, so that it is automatically adjusted by the bottom edge when the form height changes.
  • Then, when adding points, set the width for each of them.

After the properties are set, create the element. Now, it is ready for work, and we can change the text in its sections during the runtime. In our example, set the text "For Help, press F1" in the first section

At the end of the method, make sure to save the pointer of the created element in the general list of the GUI elements. To do this, call the CWndContainer::AddToElementsArray() method and pass the form index and element object to it. Since we have only a single form, its index will be 0.

//| Create the status bar                                            |
bool CProgram::CreateStatusBar(const int x_gap,const int y_gap)
//--- Save the pointer to the window
//--- Properties
//--- Set the number of parts and their properties
   int width[STATUS_LABELS_TOTAL]={0,200,110};
   for(int i=0; i<STATUS_LABELS_TOTAL; i++)
//--- Create a control
//--- Set a text in the status bar sections
   m_status_bar.SetValue(0,"For Help, press F1");
//--- Add the object to the general array of object groups

The remaining elements of the EasyAndFast library are created based on the same principle. Therefore, we will only consider the customizable properties to be used in our EA.

Fig. 3. Adding the status bar

Fig. 3. Adding the status bar

Group of tabs

Let's set the following element properties in the method for creating tab groups:

  • Center the texts in the tab.
  • Place the tabs in the upper part of the working area.
  • The size is automatically adjusted to the main element (form) area. In this case, it is not necessary to specify the size of the tab group area.
  • Set the indents of the right and bottom edges of the main element. If the form changes its size, these indents are left intact.
  • When adding the following tabs, the tab name and width are also transferred to the method.

Below is the method code:

//| Create tab group 1                                               |
bool CProgram::CreateTabs1(const int x_gap,const int y_gap)
#define TABS1_TOTAL 2
//--- Save the pointer to the main element
//--- Properties
//--- Add tabs with specified properties
   string tabs_names[TABS1_TOTAL]={"Trade","Positions"};
   for(int i=0; i<TABS1_TOTAL; i++)
//--- Create the control
//--- Add the object to the general array of object groups

Fig. 4. Adding the tab group

Fig. 4. Adding the tab group

Input field

For example, let's consider an input field, in which a user can specify currencies and/or currency pairs to form a list of symbols in a table. Its main element is a group of tabs. Here we need to specify the tab the input field should be displayed on. To do this, call the CTabs::AddToElementsArray() method and pass the tab index and the attached element object to it.

Now, let's consider the properties for this input field.

  • By default, "USD" is entered in the input field: the program will collect symbols having USD in the table. Currencies and/or symbols in this input field are comma-separated. Below, I will show the method of forming a list of symbols by comma-separated lines in this input field.
  • The input field will feature a checkbox. After disabling the checkbox, a text in the input field is ignored. All detected currency pairs are included into the symbol list.
  • The input field width is equal to the entire width of the main element and is adjusted when the tabs area width changes.
  • The Request button is placed to the right of the input field. While the program is running, you can specify other currencies and/or symbols in the input field. Click this button to generate the list. Since the input field width is to be changed automatically, while the Request button should always be located to the right of it, make sure that the right side of the input field has an indent from the right edge of the main element. 

The element of CTextEdit type consists of several other elements. Therefore, if you need to change their properties, you can get pointers to them. We had to change some properties of the text input field (CTextBox). Let's consider them in the same order as implemented in the code listing below.

  • Input field indent from the left edge of the main element (CTextEdit). 
  • Automatic change of the width relative to the main element.
  • When activating the input field (by left-clicking the input field), the text is highlighted fully automatically for a quick replacement.
  • If there is no text in the input field at all, the following prompt is displayed: "Example: EURUSD, GBP, NOK".  

The checkbox of the input field is enabled by default. To do this, activate it immediately after creating the element.

//| Create a checkbox with the "Symbols filter" input field          |
bool CProgram::CreateSymbolsFilter(const int x_gap,const int y_gap,const string text)
//--- Save the pointer to the main element
//--- Reserve for the tab
//--- Properties
   m_symb_filter.GetTextBoxPointer().DefaultText("Example: EURUSD,GBP,NOK");
//--- Create a control
//--- Enable the check box
//--- Add the object to the common array of object groups

In addition to the text input field, there will be numeric ones in the GUI. For example, Lot input field (volume for opening positions). Other properties of this type should be specified for input fields.

  • Total width of the element.
  • Maximum value to enter.
  • Minimum value to enter.
  • Step used when switching using the increment and decrement buttons.
  • Number of digits after the decimal point.
  • In order for the input field to become numeric, we should specify this using the CTextEdit::SpinEditMode() method.
  • Default value after downloading the program to the terminal chart.
  • Input field width.
  • Automatic text highlighting in the input field when clicking on it.
  • Anchoring the input field to the right edge of the element.

Here is the code for the method:

//| Create the "Lot" input field                                     |
bool CProgram::CreateLot(const int x_gap,const int y_gap,const string text)
//--- Save the pointer to the main element
//--- Reserve for the tab
//--- Properties
//--- Create a control
//--- Add the object to the common array of object groups

Fig. 5. Adding the input fields

Fig. 5. Adding the input fields

The image does not look very logical, but when you add other elements, everything turns out to be in place.


Let's add a few buttons to the EA's GUI. We will consider the one with the most properties: the button for opening SELL positions.

  • Button width.
  • The button text is accurately centered both vertically and horizontally.
  • Button background color.
  • Background color when hovering the cursor.
  • Background color when left-clicking.
  • Button text color.
  • Text color when hovering the cursor.
  • Text color when left-clicking.
  • Button frame color.
  • Frame color when hovering the cursor.
  • Frame color when left-clicking.

The same properties are changed for the BUY button except for specified background colors.

//| Create the 'Sell' button                                         |
bool CProgram::CreateSell(const int x_gap,const int y_gap,const string text)
//--- Save the pointer to the main element
//--- Reserve for the tab
//--- Properties
//--- Create a control
//--- Add the element pointer to the database

Fig. 6. Adding the buttons

Fig. 6. Adding the buttons

Combo box with a drop-down list

To change a timeframe, let's make a combo box with a drop-down list. Define the properties for its configuration.

  • Total width of the element. 
  • Number of list items (in our case, there are 21 of them corresponding to the number of timeframes in the terminal).
  • The element is to be bound to the right part of the tabs area.
  • Combo box button width.
  • The button is bound to the right part of the element.

Values are assigned to each list item, and some properties are set for the list via the pointer after that.

  • Highlighting items when hovering the mouse cursor.
  • Highlighted item. In our case, this is the point by index 18 (D1 timeframe).

Below is the code of the method for creating the combo box:

//| Create a combo box for creating timeframes                       |
bool CProgram::CreateComboBoxTF(const int x_gap,const int y_gap,const string text)
//--- Total amount of list items
#define ITEMS_TOTAL2 21
//--- Pass the panel object
//--- Anchor to the tab
//--- Properties
//--- Save item values to the combo box list
   string items_text[ITEMS_TOTAL2]={"M1","M2","M3","M4","M5","M6","M10","M12","M15","M20","M30","H1","H2","H3","H4","H6","H8","H12","D1","W1","MN"};
   for(int i=0; i<ITEMS_TOTAL2; i++)
//--- Get the list pointer
   CListView *lv=m_timeframes.GetListViewPointer();
//--- Set the list properties
//--- Create the control
//--- Add the element pointer to the database

Fig. 7. Adding the combo box

Fig. 7. Adding the combo box


A checkbox is the simplest element. Only two properties should be specified for it.

  • Width.
  • Location of the main element's right part.

After creating the element, we can enable the checkbox programmatically.

//| Create the "Date scale" checkbox                                 |
bool CProgram::CreateDateScale(const int x_gap,const int y_gap,const string text)
//--- Save the window pointer
//--- Reserve for the tab
//--- Properties
//--- Create a control
//--- Enable the checkbox
//--- Add the object to the common array of object groups

Fig. 8. Adding the checkboxes

Fig. 8. Adding the checkboxes


The GUI will feature two tables. Let's consider the one that visualizes the formed list of symbols and signals for opening positions. It is located on the first tab. First, declare and initialize the arrays for placing the table properties. Set the following properties.

  • Element width.
  • Table dimensions (number of columns and rows).
  • Column width (values are passed in the array).
  • Text alignment (values are passed in the array).
  • Indent of the text from the cell edges.
  • Displaying the headers.
  • Ability to select rows.
  • Ability to manually re-size columns by dragging a header border.
  • Displaying formatting in Zebra style.
  • Automatic change of the vertical size relative to the main element.
  • Indent from the bottom edge of the main element.

Texts for headers are to be specified after creating the table:

//| Create a symbol table                                            |
bool CProgram::CreateSymbolsTable(const int x_gap,const int y_gap)
#define COLUMNS1_TOTAL 2
#define ROWS1_TOTAL    1
//--- Save the pointer to the main element
//--- Reserve for the tab
//--- Column width array
   int width[COLUMNS1_TOTAL]={95,58};
//--- Text alignment array in columns
//--- Text indent array in columns by X axis
   int text_x_offset[COLUMNS1_TOTAL]={5,5};
//--- Properties
//--- Create a control
//--- Set the header names
//--- Add the object to the common array of object groups

The second table displays some properties of open positions. Ten columns display the following data.

  • Position symbol.
  • Number of positions.
  • Total volume of all open positions.
  • Volume of BUY positions.
  • Volume of SELL positions.
  • The current total result of all open positions.
  • The current result of all open BUY positions.
  • The current result of all open SELL positions.
  • Deposit load per each individual symbol.
  • Median price

The following properties should be additionally configured in the second table.

  • Image indents from the right and upper cell edges.
  • Ability to sort values.
  • Automatic change of the horizontal size relative to the main element.
  • Indent from the right edge of the main element.

Images in the cells of the first column will symbolize the buttons, by clicking which you can close a position or all of them if this is a hedge account on a specified symbol. 

//| Create a position table                                          |
bool CProgram::CreatePositionsTable(const int x_gap,const int y_gap)
//--- Properties

Fig. 9. Adding the table on the first tab

Fig. 9. Adding the table on the first tab

Fig. 10. Adding the table on the second tab

Fig. 10. Adding the table on the second tab

Details on working with the tables in the main program file (CProgram) are to be revealed in one of the following article sections.

Standard chart

The element of CStandardChart type is meant to visualize data by symbols. EURUSD D1 is displayed by default. It features the following properties.

  • Horizontal scrolling.
  • Width auto adjustment.
  • Height auto adjustment.
  • Indent from the right edge of the main element.
  • Indent from the bottom edge of the main element.

If necessary, it is possible to create an array of charts aligned in a horizontal row. To do this, use the CStandardChart::AddSubChart() method by passing a symbol and chart timeframe as arguments. However, in this case, we need a single chart, while symbols and timeframes are switched using other controls.

//| Create the standard chart 1                                      |
bool CProgram::CreateSubChart1(const int x_gap,const int y_gap)
//--- Save the pointer to the window
//--- Reserve for the 1st tab
//--- Properties
//--- Add charts
//--- Create a control
//--- Add object to the common array of object groups

Fig. 11. Adding the chart

Fig. 11. Adding the chart

Progress bar

The progress bar allows users to understand what the program is doing now. So, let's add it to the GUI. Below are the properties for our example (in the same order as in the code).

  • Total height of the element.
  • The indicator height (progress bar line).
  • Bar indent by X.
  • Bar indent by Y.
  • Indent of the main text label by X.
  • Indent of the main text label by Y.
  • Indent of the percentage text label by X.
  • Indent of the percentage text label by Y.
  • Indication of a drop-down control (for auto hiding). 
  • Font. 
  • Indicator frame color.
  • Indicator background color.
  • Indicator progress bar line.
  • Width auto adjustment.
  • Indent from the right edge of the main element.

Examples of applying the progress bar are to be displayed below.

//| Create the progress bar                                          |
bool CProgram::CreateProgressBar(const int x_gap,const int y_gap,const string text)
//--- Save the pointer to the main element
//--- Properties
//--- Create the element
//--- Add the element pointer to the database

We have described all the controls to be used in our EA's GUI. At the moment, this is just a graphical shell. Further on, we will develop all the necessary methods to make all this work in accordance with the original idea.

EasyAndFast library updates

In the EasyAndFast library, the CTable::SortData() public method has been revised in the CTable class. Now, as the second argument, you can specify the table sorting direction (optional parameter). Previously, the CTable::SortData() method call started sorting in the opposite direction from the current one. Also, the methods for receiving the current sorting direction and sorted column index have been added. If the table has been sorted by a user manually (by clicking on a header), and then the data in the table has not been updated in the same sequence, it is possible to restore it after finding the current sorting direction. 

//| Class for creating a drawn table                                 |
class CTable : public CElement
   //--- Sort data by a specified column
   void              SortData(const uint column_index=0,const int direction=WRONG_VALUE);
   //--- (1) Current sorting direction, (2) sorted array index
   int               IsSortDirection(void)             const { return(m_last_sort_direction);    }
   int               IsSortedColumnIndex(void)         const { return(m_is_sorted_column_index); }
//| Sort data by a specified column                                  |
void CTable::SortData(const uint column_index=0,const int direction=WRONG_VALUE)
//--- Exit if exceeding the table borders
//--- Index the sorting is to start from
   uint first_index=0;
//--- Last index
   uint last_index=m_rows_total-1;
//--- Direction is not managed by a user
      //--- First time, it is sorted in ascending order. Every other time it is sorted in the opposite direction
      if(m_is_sorted_column_index==WRONG_VALUE || column_index!=m_is_sorted_column_index || m_last_sort_direction==SORT_DESCEND)
//--- Remember the index of the last sorted data column
//--- Sorting

Another small addition has been made to the CKeys class of the CKeys::KeySymbol() method. The numeric keypad (a separate block of keys on the right side of the keyboard) has not been processed previously. Now you can enter numbers and special characters from this section of the keyboard as well.

//| Return pressed button symbol                                     |
string CKeys::KeySymbol(const long key_code)
   string key_symbol="";
//--- If a space is needed (Space key)
      key_symbol=" ";
//--- If (1) an alphabetic character or (2) numeric symbol or (3) a special symbol is required
   else if((key_code>=KEY_A && key_code<=KEY_Z) ||
           (key_code>=KEY_0 && key_code<=KEY_9) ||
           (key_code>=KEY_NUMLOCK_0 && key_code<=KEY_NUMLOCK_SLASH) ||
           (key_code>=KEY_SEMICOLON && key_code<=KEY_SINGLE_QUOTE))
//--- Return a symbol

New versions of the CTable and CKeys classes can be downloaded at the end of the article.


This was the first part of the article. We have discussed how to develop GUIs for programs of any complexity without excessive effort. You can continue to develop this program and use it for your own purposes. In the second part of the article I will show how to work with GUI, and most importantly — how to fill it with functionality. 

Below, you can download the files for testing and detailed study of the code provided in the article.

File name Comment
MQL5\Experts\TradePanel\TradePanel.mq5 The EA for manual trading with GUI
MQL5\Experts\TradePanel\Program.mqh File with the program class
MQL5\Experts\TradePanel\CreateGUI.mqh File implementing methods for developing GUI from the program class in Program.mqh
MQL5\Include\EasyAndFastGUI\Controls\Table.mqh Updated CTable class
MQL5\Include\EasyAndFastGUI\Keys.mqh Updated CKeys class