|
|
|
|
|
|
|
|
People believe you can make tons of money with little work. They think you can make 100 percent a year doing a little bit of research on the weekends. That's ridiculous.
Monroe Trout. Chapter The Best Return That Low Risk Can Buy (Jack D. Schwager The New Market Wizards)
After an Expert has been tested on history data the tester in MT4 allows to save the Expert testing results in the html format, including the Balance and Equity chart and the list of performed trades. The list contains the date and time of opening and closing of each position, the volume, the type (long or short) and the result (profit or loss) of each transaction. Besides, various calculated parameters for estimation of the Expert testing results are shown in the report. In order to understand better how Expert Advisors work it is recommended to read the following articles on the website of the developers :
- Features of Experts Advisors
- Strategy Tester: Modes of Modeling during Testing
- Testing Features and Limits in MetaTrader 4
- What the Numbers in the Expert Testing Report Mean
It is possible to copy the list of transactions from the tab Results into Microsoft Excel and make an additional analysis (to make independent calculation of your results). But even in such case the statistics won’t be full, besides it is impossible to foresee in the tester variants for all occasions. For example we need to receive information about the field Commentary in orders, the lifetime of a position (between opening and closing) in minutes.
Let’s also output profit in points and in the deposit currency on the basis of 0.1 lot (to avoid Money Management influence). Just to be safe we will output the hour and the day of the week of opening/closing of orders. Additionally we will distinguish orders closed by stop, by take profit and by market. As you see the list is rather big.
Where shall we perform processing of the history of orders? Of course in the procedure that is executed last, i.e. in the deinit() function. After we have run a backtest of the Expert Advisor we have the history of transactions which we can process in a loop. We will use as a testing ground our Expert Advisor RandomTrade4.
We will create two arrays double trades[] and cancells[] (for cancelled orders), in which we will write attributes of each order. We will write line values in the array
string commentsTrades[] .
|
|
|
|
I didn’t fill the array of cancelled orders; everything is done in a similar manner for them:
|
|
|
|
Explanations are required only for the usage of the StringSubstr() function. The point is that the trading server or tester adds «[sl]» to the comment of an order at closing by stop. If you had an order with the comment «test», at closing by stop the comment will turn into «test[sl]». Thus the initial comment is written in the commentsTrades[cnt][0] , in index [1] we write «[sl]», «[tp]» or «»(empty line), if the order was closed by market. In index [2] we write the type of the order – «buy» or «sell». We will call the modified Expert Advisor RandomTrade+Statistika.mq4. Indexes 9, 10 and 12I are not processed in the array trades[]. They may be necessary for us in future.
Now we only have to decide what we will analyze. Though our Expert doesn’t convey any specific sense, but we will try to extract from it everything we can. Usually an Expert Advisor is written on the basis of some indicator signals and then attempts are made to improve it somehow ( by means of optimization) in order to increase profit at trading on history. We will try the reverse way – we’ll to see the regularity in the indicator readings through the array of random transactions. We’ll take the classic indicator MACD(12,26,9) and we will write in the order’s comment one, if the value of the main line is above zero, or minus one if it is below zero.
|
|
|
|
Then we compile the code of the Expert, launch it on EURUSD H1 with the parameter StDev=50 and open a csv-file in Excel. We will try to find the regularity – connection between the value of MACD and the trade direction. After sorting by the type «buy» and field «Comment»=-1 we see that these trades gave us a profit of $1266.
|
|
|
|
Now let’s check the reversed situation, we set «Comment»=1 – and get loss of $200.
|
|
|
|
We change the order type for «sell» and «Comment» for 1. Again we get loss, though it is small - $250.
|
|
|
|
It is interesting that over the last year (on this interval testing was performed) the EURUSD has been declining, while long positions under conditions that StopLoss=50 and TakeProfit=150 points are profitable. Is it possible that we by chance have revealed some regularity in the behaviour of this pair? To check this we will modify slightly the algorithm of our expert. The time of opening positions will be still random, while the direction of opening will be set rigidly. If MACD is above zero we sell, if it is below zero we buy. We will call this variant RandomTtade+Statistika2.mq4 .
|
|
|
|
To check our supposition we will make an optimization, if it shows that random transactions with such parameters are profitable in average – it means we are on the right way. The optimization parameters:
- Balk – from 1 to 100 with an increment equal 1 (100 runs for each combination of value parameters)
- TP_K – from 1 to 3.
- StDev – from 10 to 100 with an increment equal 5.
The optimization took a little less than two hours, I copied the Optimization results and saved them in an Excel file.
|
|
|
|
But the most important thing is that on the optimization chart we see that there is a whole series of profitable trades at StDev=65 and TP_K=2. The best results we achieved not when StDev=50, but not far from this value.
|
|
|
|
The most interesting thing is that if TakeProfit is three times bigger than stop (TP_K=3) – profit becomes less. This is one more surprise. Let’s set the optimal parameters and run a backtest. And then look at the chart:
|
|
|
|
One more run and again we look at the chart:
|
|
|
|
One more and again we see profit:
|
|
|
|
The testing report for one of the runs in the form of an html-file is attached.
|
|
|
|
Can it be so simple? It is hard to believe. We look thoroughly through the trades and notice that as soon as one transaction is the next one is opened. Somehow at such modification of the code we lost the main advantage of our expert – the randomness of trades. Or not? Let’s perform a simple check; at first we will test our expert in the tester with optimal parameters in the LongOnly mode,
|
|
|
|
And then in the ShortOnly mode:
|
|
|
|
We were taught: the order in which the terms are added does not matter. But here we don’t have profit moreover the number of transactions doesn’t correspond – separately we had approximately 300 long and 300 short positions, in all approximately 600 transactions, but in the Long&Short we had approximately 400 transactions. We may state with confidence a hidden fit of the expert advisor was performed. To disapprove this you need as a minimum to change the code so that a random pause will appear between transactions and as a maximum to teach the expert to distinguish the direction of the opened position.
Perhaps it will be necessary to make the same analysis for other expert advisors. In such case we can insert a part of the code of this expert in the deinit() function of a new expert. But there is another way – to use the #include directives. The #include directive (include, add) is valid only when the code is compiled. When the compiler meets this command it tries to find the indicated file in the folder "C:Program FilesMetaTrader 4expertsinclude" and insert the whole code from this file into the expert’s code.
We will demonstrate this with an example. At first we will take out the whole code in the deinit() block in a separate function HistoryCalculate(). Now in the block deinit() there is only the call of this function:
|
|
|
|
We save this variant as RandonTrade+Statistika3.mq4. If the compilation is successful we proceed to the creation of the header file Statistika.mqh. We launch Expert Advisor Creation Wizard; choose the type «Header file»:
|
|
|
|
We set the name of the file, the name of the author and link:
|
|
|
|
We have got the template of a header file which contains models of calling the dynamic library dll, the compiled file mq4 – ex4 and simply definitions of the constant (#define).
|
|
|
|
We copy the definition of our function HistoryCalculate(), at that we don’t forget to transfer the declarations of variables StatBars and stat_FileHandle, and of the statistics file name TradesFileName:
|
|
|
|
In our expert we delete the HistoryCalculate() function and variables StatBars, stat_FileHandle, TradesFileName , and add the line #include :
|
|
|
|
We save the expert with the name RandonTrade+Statistika4.mq4 and the header file. The convenience of this approach lies in the fact that if you have debugged some code, which you would like to use repeatedly in you developments, then you can easily insert in in your new code at the compilation stage. That’s why if later you require to change the code of the header file (*.mqh), you mustn’t forget of the necessity of recompiling all the programs that use this file.
Ultimately we will insert the definition of the unnecessary function Placebo(), which we won’t call, into the file Statistika.mqh.
|
|
|
|
We save the file and compile the expert over again. We receive the following message:
|
|
|
|
The compiler has determined that this function isn’t used and has deleted it from the executed file. That’s why one header file may contain a big library of various functions for all occasions, it doesn’t influence the size of the final executed file, only necessary functions will be used. The files of the Expert Advisors are here .
Go to article «Trailing Stop Technique».
|
|