The MetaTrader 5 terminal continues to improve and acquire new features. The new MetaTrader 5 build 439 contains the updated MQL5 Wizard, which allows creating Expert Advisors based on more flexible trade rules. In this article we describe new possibilities of the MQL5 Wizard and explain the changes in the architecture of Expert Advisors generated using this application.
Originally, the MQL5 Wizard was designed as a handy tool to help traders easily create programs in MQL5. Already in the first version of the MQL5 Wizard, you could quickly create a ready to use Expert Advisor as a set of simple modules, obtaining its source code in MQL5. What's important, you do not need to know the MQL5 language - all you need is read the article Create Your Own Expert Advisor in MQL5 Wizard.
An Expert Advisor generated by the Wizard contains three modules:
Each EA module is based on a certain class and provides plenty of opportunities for modifying the behavior of your Expert Advisor.
The Standard Library includes a set of functionally complete, ready-to-use classes to implement various mechanisms of money management, open position maintenance and generation of trading signals.
This approach allows creating trading robots based on proven and well-functioning modules. You need only to go through a few steps and specify the necessary parameters. But experience shows that even good examples, explaining the principles of writing custom classes, are not always able to help novice MQL5 programmers.
Another drawback of the previous version of the MQL5 Wizard is that to implement a new slightly modified trading algorithm, you need to write a new class.
With this experience and comments received from users, MetaQuotes decided to create an advanced version of the MQL5 Wizard with even more functions.
A New Concept of Generating Trading Signals
The main stumbling block in the previous version of the MQL5 Wizard was connected with the problems that could occur when creating one's own trading signals. Therefore, when developing the new version of the Wizard, the emphasis was placed on simplifying the creation of combined trading signals based on simple principles.
For this purpose, the following changes have been made to the base class of the trading signals generator (hereinafter referred to as the Signal):
- Any Signal (derived from class CExpertSignal) is based on forecasts of "advisors" that give their opinion about the possible price movement. These advisors, in turn, are also derived from the CExpertSignal class, and therefore can have their own advisors. The number of advisors in each Signal can be up to 64.
- New methods have been added to the CExpertSignal class, which implement "voting" on the direction of price movement. In addition, it contains a new mechanism for participating in such a "vote".
- To increase the flexibility of making a decision, each Signal involved in the decision-making (do not forget that the "advisors" are also signals), now can receive information from its own (different from the current) market instruments and/or from its own timeframe.
Thus, the decision about entering the market in one direction or another, can now be taken "collectively", i.e. taking into account the "considered opinion" of every advisor.
The Signal "votes" on the direction of price movement on the basis of embedded algorithms of market patterns detection (specific to this Signal). For example, a stochastic based Signal is able to identify a reversal, divergence with the price, etc.
The Standard Library already includes twenty ready-to-use Signals based on various indicators.
A New Mechanism for Making Trading Decisions
The mechanism for making trading decisions by the Signal can be presented in the form of the following key provisions:
- Each Signal has its own set of market patterns (some combination of price and indicator values).
- Each market pattern has a weight value from 1 to 100. The higher the value, the stronger the pattern.
- Each pattern generates a forecast of price movement in a certain direction.
- By comparing the forecasts received from the applied patterns, the strongest one is selected, and a forecast is given in the range from -100 to +100, where the sign determines the direction of the predicted movement (negative — the price will fall, positive — the price will rise). An absolute value corresponds to the strength of the found best pattern. This result is sent to the general vote.
- The forecast of each advisor within the module goes to the vote with the weight factor from 0 to 1.0, which is specified in its settings ("Weight").
- The outcome of the vote is a number from -100 to +100, where the sign determines the direction of the predicted movement, and the absolute value characterizes the strength of the signal. It is calculated as the arithmetic mean of weighted forecasts of all the advisors of the module. This outcome is a forecast of the Signal, and the absolute value is an indication of how strong the Signal is.
The settings of each generated Expert Advisor contains two parameters — the threshold for making a decision to open or close a position (ThresholdOpen and ThresholdClose) — which may have a value from 0 to 100. If the strength of a Signal overcomes the threshold value, a decision is made to conduct a trade operation.
The EA created using the MQL5 Wizard consists of three parts the most important of which is the head that contains the module for generating trading signals. Then the processes in the module of signals can be represented as follows:
The figure shows that the Signal module can contain two blocks - a block of market patterns and a block of advisors. The block of patterns produces forecasts from each pattern, after which the strongest one of them is chosen. The forecasts of advisors (other Signals) are multiplied by the corresponding weight and the resulting values are summed up during voting to produce the arithmetical mean forecast. In this case, the Signal module has produced -36.6 = (-100+60-54)/3 for the current situation. This means the Sell forecast with the absolute strength of 36,6.
Consider the decision-making mechanism on two simple examples.
Suppose there is an Expert Advisor with ThresholdOpen=20 and ThresholdClose=90. Signal modules based on MA with a weight of 0.4 and Stochastic with a weight of 0.8 are involved in making a trade decision. Consider two options of obtained trading signals:
- Option 1.
The price has crossed upwards a rising MA indicator. This corresponds to one of market patterns implying price growth, which is embedded in the MA module. Its significance is 100. At the same time, Stochastic oscillator has turned down and formed a divergence with the price. This is a pattern implying price fall, which is implemented in the Stochastic module . The significance of this pattern is 80.
Calculate the result of the final vote. The weighted forecast, obtained from the MA module, is calculated as 0.4 * 100 = 40. The weighted forecast of Stochastic module is calculated as 0.8 * (-80) = -64. The final forecast is calculated as the arithmetic mean of the two weighted predictions: (40 - 64)/2 = -12. This is a signal to sell with the conditional power of 12. The threshold value of 20 has not been reached. Accordingly, the trade operation will not be conducted.
- Option 2.
The price has crossed downwards a rising MA indicator. This corresponds to one of market patterns implying price growth, which is embedded in the MA module. Its significance is 10. At the same time, Stochastic oscillator has turned down and formed a divergence with the price. This is a pattern implying price fall, which is implemented in the Stochastic module . The significance of this pattern is 80.
Calculate the result of the final vote. The weighted forecast, obtained from the MA module, is calculated as 0.4 * 10 = 4. The weighted forecast of the Stochastic module is calculated as 0.8 * (-80) = -64. The final forecast is calculated as the arithmetic mean of the two weighted predictions: (4 - 64)/2 = -30. This is a signal to sell with the conditional power of 30. The threshold value of 20 has been reached. Accordingly, the result is a signal to open a short position.
Figure 1. An example of market patterns in the chart
The figure shows the market patterns described in the examples:
a) The divergence of the price and Stochastic oscillator is used in options 1 and 2.
b) The price has crossed the MA indicator from the bottom up - is used in option 1.
c) The price has crossed the MA from up downwards - is used in option 2.
Combining Signals in the EA
To create our own Expert Advisor, it is necessary first of all to decide on what instrument and in what timeframe we will trade. Second, we should select Signals on the basis of indicators that we will use for trading.
We will have the following conditions for our example:
- The traded symbol is EURUSD. Our timeframe is M10.
- The Signals used:
- EMA('EURUSD',M10,31) - signals based on an exponential moving average;
- Stochastic('EURUSD',M10,8,3,3) - signals based on the stochastic oscillator;
- EMA('EURUSD',H1,24) - signals based on an exponential moving average from a different timeframe;
- Stochastic('EURJPY',H4,8,3,3) - signals based on the stochastic oscillator from a different symbol and timeframe;
IntradayTimeFilter - a filter by time of the day and the number of the day of the week.
Creating a Trading Robot in the New MQL5 Wizard
Expert Advisor's source code is generated using MQL5 Wizard in MetaEditor.
Basic classes of trading strategies are located in terminal_data_folder\MQL5\Include\Expert\. Ready-to-use algorithms of trade signals classes, classes of trailing open positions and classes of money and risk management are located in the Signal, Trailing and Money sub-folders. MQL5 Wizard parses files in these folders and uses them to generate Expert Advisor's code.
Figure 2. Base Classes for MQL5 Wizard
To launch MQL5 Wizard you need to click "New" on the toolbar or select "New" from the "File" menu (or press Ctrl+N):
Figure 3. Launching MQL5 Wizard
Then select the type of the program you want to create. In our case select the "Expert Advisor (generate)" option:
Figure 4. Selecting the type of program
Step 1. General Properties of Expert Advisor
Next opens the dialog box, where you can set the general properties of Expert Advisor:
Figure 5. General properties of an Expert Advisor
Specify the name of your Expert Advisor, the author's name and the link to your web-site in the "Name", "Author" and "Link" fields (respectively).
Expert Advisor also has the following input parameters:
- Symbol (the string type) - the symbol the EA is running on;
- TimeFrame (the ENUM_TIMEFRAMES type) - the timeframe on which the EA is running.
By default, both options are set to "current" (i.e. the Expert Advisor will be tested and will trade on any symbol/timeframe).
But is it always so good? For example, you accidentally run an Expert Advisor set up for intraday trading on EURUSD (moreover, it uses confirmation from a cross pair), on a weekly chart of AUDNZD. It seems nothing good.
Therefore, it would be better to set the necessary parameters. In the next step select the type of trade signals, on which the EA will trade. To go to symbol selection, click "Next".
Step 2. Select the Modules of Trade Signals
Algorithm of opening and closing positions is determined by the modules of trade signals. Trade signals modules contain rules of opening/closing/reversing positions.
An Expert Advisor can simultaneously use up to 64 different (or the same, but with different settings) modules of trading signals.
Figure 6. Parameters of EA signals (beginning).
The Standard Library has ready-to-use Modules of trading signals:
- Signals of indicator 'Accelerator Oscillator';
- Signals of indicator 'Adaptive Moving Average';
- Signals of indicator 'Awesome Oscillator';
- Signals of oscillator 'Bears Power';
- Signals of oscillator 'Bulls Power';
- Signals of oscillator 'Commodity Channel Index';
- Signals of oscillator 'DeMarker';
- Signals of indicator 'Double Exponential Moving Average';
- Signals of indicator 'Envelopes';
- Signals of indicator 'Fractal Adaptive Moving Average';
- Signals of intraday time filter;
- Signals of oscillator 'Moving Average Convergence/Divergence';
- Signals of indicator 'Moving Average';
- Signals of indicator 'Parabolic SAR';
- Signals of oscillator 'Relative Strength Index';
- Signals of oscillator 'Relative Vigor Index';
- Signals of oscillator 'Stochastic';
- Signals of oscillator 'Triple Exponential Average';
- Signals of indicator 'Triple Exponential Moving Average';
- Signals of oscillator 'Williams Percent Range'.
Figure 7. Selecting signals
According to our "Terms of Reference", first we choose "Signals of indicator 'Moving Average''.
Configure the necessary parameters (PeriodMA and Method). Double-click on the "Name" field to exclude from the optimization (by default all the parameters of the signal are marked as input-parameters) all parameters except Weight.
Here we need to mention the fields of "Symbol" and "TimeFrame". These fields should be filled in if the appropriate settings of the signal are different from the current parameters. For a signal, the current settings are those of the "superior intelligence" (in this case, of the Expert Advisor).
Figure 8. Configuration of the signal parameters
Add the remaining signals and configure the necessary parameters. For the optimization, leave only the Weight parameter for all signals.
Configuration of signals is completed. Click "Next" to continue.
Figure 9. Parameters of the EA signals (end)
Step 3. Select the Module of Trailing Open Positions
The next step is to select the algorithm of trailing open positions (Trailing Stop). Using the trailing allows you to save earned profit.
Configuration of this module is described in detail in the article MQL5 Wizard: Creating Expert Advisors without Programming. In our case we will not use trailing.
Figure 10. Select the position trailing algorithm
Step 4. Select the Module of Money and Risk Management
On the last step you need to select system of money and risk management, which will be used in your Expert Advisor. Configuration of this module is described in detail in the article MQL5 Wizard: Creating Expert Advisors without Programming. In our case we will use a fixed lot.
Figure 11. Select the money management algorithm
That's it. Click "Finish".
Checking the Created Expert Advisor in MetaTrader 5 Strategy Tester
If all components of Standard Library are present, the code of the generated Expert Advisor compiles successfully. The best set of Expert Advisor's parameters can be found after optimization in MetaTrader 5 Strategy Tester.
For the optimization of the EA, select period from 01.01.10 to 01.01.11, run it in two stages.
The first stage: "Selection of signal weights, thresholds for triggering of entry/exit conditions and trade levels."
Figure 12. Parameters of the Expert Advisor optimization
Here is the result of the best optimization run.
Figure 13. Results of the Expert Advisor optimization
We have already got good results (in terms of profit and maximum drawdown). We must not forget that we were "trading" with a fixed lot.
But let's try to further improve the results. For this purpose, we have another signal (we have not yet used its settings). The values found by optimizing the parameters describe a market pattern recognized by the Expert Advisor.
But let's suggest that the resulting market pattern does not always work equally well. Let's try to apply the time filter to the optimization results.
For this purpose, let's optimize the Expert Advisor according to the "Bad hours" parameter of the "IntradayTimeFilter" Signals. The "Bad hours" parameter is a bitmap of 24 bits (the number of hours per day). Every bit of the bitmask uniquely matches one of the hours (number of bits in the mask and the number of hours in a day match). The value of any bit equal to 1 will override any result of a vote if the number of the current hour is equal to the number of this bit (i.e., the hour is considered bad for trading).
To go through all possible combinations of "good" and "bad" hours, we will optimize the parameter from 0 to 2^24-1 (in the natural form 16 777 215) at a step equal to 1.
Figure 14. Parameters of the Expert Advisor optimization
As a result of the optimization we obtain 11 665 415. To understand what this means, let's express it in a binary form:
- 101100100000000000000111 - i.e. the numbers of "bad" hours are 0, 1, 2, 17, 20, 21 and 23.
Here are the parameters of the Expert Advisor after all optimizations.
Figure 15. Properties of the Expert Advisor
The report of a single test with the best parameters is attached. The profit is higher, and the drawdown is lower. It can be concluded that after time filtering, the results of the trading system at the historic period has improved.
Using the classes of trading strategies significantly eases creation of robots for testing trading ideas right in MetaEditor using the MQL5 Wizard. Now the entire source code of Expert Advisor can be constructed based on the combination of ready-to-use modules of the Standard Library or using your own modules.
Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/275