A normal Expert Advisor used in our practical work favorably compares to its simple analogs, because it provides a trader with high-quality information support.
During trading, the situation changes all the time, various events take place. To make proper decisions, a trader must be fully informed. For this purpose, various functions are used in Expert Advisors. These functions are aimed at informing the user about a certain set of facts and processes.
In simple Expert Advisors, as a rule, this task is performed by the standard function Comment()
that displays the preset text in the upper left corner of the symbol window.
This information output method is not quite comfortable, since the text can often be superimposed onto the price chart. So this method can only be used in a limited amount of cases, to display short messages.
We will consider here a conceptually different method of displaying information - the entire message is shown in a separate window, whereas graphical objects are used to form message texts. The use of graphical objects produces a tangible advantage, since you can move objects
(unlike texts shown using Comment()) creating, in this manner, your message history.
A separate subwindow for displaying of information is created using a custom indicator adjusted correspondingly. The only purpose of this indicator is
the creation of that subwindow, so no calculations are performed in the indicator, nor indicating lines are built in it. The code of the indicator Inform.mq4
may look as follows:
Generally, a programmer may add into the indicator his or her desired code and contribute to its properties. For example, you can display indicating lines in a certain part of the indicator subwindow. In the above example, a simple code of the indicator is given, in the window of which graphical objects will be shown.
User-Defined Function Inform()
int Inform(int Mess_Number,int Number=0,double Value=0.0)
The function displays in the subwindow of the indicator
Inform.mq4 messages created using graphical objects. The function controls the position of graphical objects in the indicator subwindow:
every new message is displayed in the lower part of the window (lower line) and colored in the desired color, the previously shown messages being moved into the upper part of the window (one line upward). If no new messages are displayed within 15 seconds,
all previously displayed messages in the window will be colored in gray (in order not to create diversions for the user) when the program refers to the function.
Mess_Number - message number that can take the following values:
- (zero) 0 - no message is displayed, this mode is used to reset the timer;
- (minus one) -1 - all graphical objects created by the function will be deleted;
- (one or more) - the number of the message to be displayed in the indicator subwindow;
Number - integer used in some messages;
Value - real number used in some messages.
The function Inform() that creates graphical objects, like other functions in a normal EA,is formed as an include file,
int Inform(int Mess_Number, int Number=0, double Value=0.0)
static int Time_Mess;
static int Nom_Mess_Graf;
static string Name_Grf_Txt;
if (Time_Mess==0) return;
for(int i=0;i<=29; i++)
ObjectSet( Name_Grf_Txt[i], OBJPROP_COLOR, Gray);
for(i=0; i<=29; i++)
Graf_Text="Closed order Buy "+ Number;
Graf_Text="Closed order Sell "+ Number;
Graf_Text="Deleted pending order "+ Number;
Graf_Text="Opened order Buy "+ Number;
Graf_Text="Opened order Sell "+ Number;
Graf_Text="Placed pending order "+ Number;
Graf_Text="Order "+Number+" modified into the market one";
Graf_Text="Reopened order "+ Number; break;
Graf_Text="Partly closed order "+ Number;
Graf_Text="New minimum distance: "+ Number;
Graf_Text=" Not enough money for "+
DoubleToStr(Value,2) + " lots";
Graf_Text="Trying to close order "+ Number;
Graf_Text="Trying to open order Sell..";
Graf_Text="Trying to open order Buy..";
Graf_Text="Invalid password. EA doesn't function.";
case 2: Graf_Text="Common error."; break;
case 129: Graf_Text="Wrong price. "; break;
case 135: Graf_Text="Price changed. "; break;
case 136: Graf_Text="No prices. Awaiting a new tick.."; break;
case 146: Graf_Text="Trading subsystem is busy"; break;
case 5 : Graf_Text="Old version of the terminal."; break;
case 64: Graf_Text="Account is blocked."; break;
case 133: Graf_Text="Trading is prohibited"; break;
default: Graf_Text="Occurred error " + Number;
Graf_Text="Expert Advisor works only for EURUSD";
Graf_Text="default "+ Mess_Number;
for(i=29; i>=1; i--)
ObjectSet( Name_Grf_Txt[i], OBJPROP_YDISTANCE, 2+15*i);
ObjectCreate (Name_Grf_Txt,OBJ_LABEL, Win_ind,0,0);
ObjectSet (Name_Grf_Txt,OBJPROP_CORNER, 3 );
ObjectSet (Name_Grf_Txt,OBJPROP_XDISTANCE, 450);
ObjectSet (Name_Grf_Txt,OBJPROP_YDISTANCE, 2);
In block 2-3, the variables used in the function are described. To store the names of graphical objects, the array Name_Grf_Txt is used. According to the method accepted in the function, the program creates a new graphical object for each new message. The total amount of objects is 30, each object represents a text entry located in one line. In case of large screen resolution, the amount of lines to be shown can be increased by increasing of the amount of objects to be created.
In block 3-4, the number of the subwindow of the indicator "Inform" is calculated for messages to be shown in. If the indicator is not attached, the function stops its operations. If there is no window, the messages cannot be displayed, but this does not affect the workability of the EA - all other functions will work in their normal modes, trades will be performed, as well.
In block 4-5, the message colors are analyzed. The function with the parameter
Mess_Number=0 is called in the Expert Advisor at every tick (see the function start() in Expert Advisor
usualexpert.mq4). If all objects are gray in the preceding history, the function ends its operations. However, if the value of the variable Time_Mess is nonzero, the properties of all objects are changed, namely, all objects will be colored in gray.
If (block 5-6) the parameter Mess_Number=-1 is specified in the function call,
all objects previously created at the execution of the function are deleted. This may be necessary when the EA is detached from the symbol window.
In this case, according to the common rule, each application program
must delete all objects it created during execution (see the function
deinit() in the Expert Advisor usualexpert.mq4).
If the control in the program is passed to block 6-7, this means that it is necessary to create a new graphical object with required properties and place it in the lower part of the indicator subwindow (in the lower line; here, the term of "line" is conditional; in fact, the location of graphical objects is determined by the preset coordinates). Each newly created object has its unique name. To create object names, we use the historical number of the message, this is why the messages counter is placed in block 6-7 (at a later stage, the value of the variable Nom_Mess_Graf is used to form a unique name, block 8-9). It is here where the last publication time is accounted and the basic color is established for new messages (green).
Block 7-8 consists of the operator 'switch', inside which the control is passed according to the value of the parameter Mess_Number specified in the function call. In each 'case' of this operator of the variable Graf_Text, a new value is assigned that is the content of the message to be displayed. A special color is set for some messages, for example, red for important messages. All messages are accompanied with sounds that are the execution of the standard function PlaySound() (see Wave Files).
The creation of a new graphical object and replacing of the existing ones are performed in block 8-9. The amount of objects is limited, so one object (the oldest one) is deleted every time when a new message is published. All other existing objects are moved one line upwards. Objects are moved by changing their properties - vertical coordinates. The horizontal coordinates of objects remain unchanged.
After all necessary preparations have been made (all objects are moved one line upwards), a new object with new unique name and with properties partially determined in block 7-8 is created. The type of the graphical object is Text Label.
The objects of this type are positioned relative to the symbol window, which allows the user to move price chart arbitrarily, without affecting the positions of messages.
The function Inform() can be called from any location in the program where, implicitly, a text message should be displayed. As a result of long processing, messages are accumulated in the window. The user can view the messages history by resizing the indicator subwindow (for example, by dragging up its upper edge). Optionally, you can also set the window height in such a way that the visible space displays the desired amount of message lines (three-four lines are usually recommended).
Fig. 156. Symbol window. Message in the indicator Subwindow.
It is easy to see that the diversity of messages displayed by the function can be increased.
If you are going to expand the program, it is sufficient just to add the new version of
'case' in the operator 'switch' (block 7-8).