Creation of a Normal Program
As a rule, after having coded a number of simple application programs in MQL4, a programmer goes to a more complex project - to creation of a convenient program for practical use. Simple programs, in some cases, do not satisfy the needs of a trading programmer for at least two reasons:
1. The functional boundedness of simple programs does not allow them to provide a trader with all necessary information and trading management tools, which doesn't make the use of such programs efficient enough.
2. The code imperfection of simple programs makes difficult their further development aimed at expanded services.
In this section, we represent one of the possible alternatives of realizing a trading Expert Advisor
that can be considered as a basis for your own projects.
- Structure of a Normal Program
The availability of many user-defined functions in a program allows you to create powerful and flexible algorithms to process information. Compiler directive #include allows you to use your function (once written and debugged) in other programs. In this manner, you can create your own libraries or use open-source developments of other programmers.
- Order Accounting
The section considers an example of user-defined function Terminal() that is realized in a separate include file with the extension .mqh. Such files are connected to the program code during compilation using the directive
- Data Function
An example of one more user-defined function that helps to organize the output of text information about the current work of an EA. This function allows you to abandon the function Comment() to display the text in the chart window. The function is realized as an indicator in a separate subwindow of the chart window.
- Event Tracking Function
A trader cannot always notice all the events during trading. The program written in MQL4 allows you to detect the changes in any trading conditions or situations. user-defined function Events() connects to the EA using the directive #include and applies calls to another include function, Inform().
- Volume Defining Function
The calculation of the volume of a position to be opened is one of the tasks of equity/risk management. The user-defined function Lot() is a small example used for these purposes.
- Trading Criteria Defining Function
The most important part of any trading is detection of the times of entering the market and that of closing a position. The creation of trading rules or criteria is the core of any Expert Advisor. User-defined function Criterion()
is connected using the directive #include. It shows how an EA can decide on the basis of the indicator values about whether the current situation complies with one or another criterion.
- Trade Functions
The current situation has been analyzed with the function Criterion(), so now we have to make trades: open, close, modify or delete a pending order. All these operations can be put in separate user-defined functions: Trade(), Close_All() and
Open_Ord(). The protecting stop orders are moved using the user-defined function Tral_Stop().
- Error Processing Function
Error control is an integral part of an Expert Advisor. This is you who determines how to process the message about the busy trade context, about no prices for the requested symbol, etc. In some cases, it is sufficient to display a message about the error. In other cases, it would be reasonable to try and repeat the trade request after a certain amount of time.
It is necessary to determine how one or another error will be processed. The user-defined function Errors() shown in this section processes errors using the selection operator switch().