Thursday, November 4, 2010

Converting MQL 4 to MQL 5

7 Steps
1) First, download the following files and place them in your Metatrader 4 / experts / include folder
- mt4accountinfo.mqh
- mt4string.mqh
- mt4datetime.mqh
- mt4objects_1.mqh
- mt4timeseries_2.mqh

2) Now, download the latest version of the rewrite script and place it in your Metatrader 4 / experts / scripts folder:

3) After that you will need to place a mql4 indicator, script or ea which you want to convert to MQL 5 in your Metatrader 4 / experts / files folder.
4) Now open Metatrader 4 and run the script. As you can see you must first provide the name (without extension) of your mq4 file and select if it is an indicator (0), EA (2) or script (3) file.


In my case i'm rewritting a MQL4 indicator named ang_DCT:
5) Now hit the "OK" button and if everything is done, the following message will appear:

6) After that go to your Metatrader 4 / experts / files folder and you will find the converted mq5 file there:

7) Now open this file with Metaeditor 5 in compile it:


Wednesday, October 20, 2010

Metatrader 5

Metatrader 5 div>

· receiving quotes and news in real-time mode;
· viewing price charts of financial securities in one of 21 timeframes ;
· performing trade operations;
· viewing depth of market and trading stock symbols;
· controlling and managing open positions and pending orders;
· conducting technical analysis using 38 built-in indicators and 39 graphical objects;
· writing Expert Advisors, custom indicators, scripts and functions in MetaQuotes Language 5 (MQL 5); 
· working with different language interfaces.

For making a decision to trade, reliable on-line information is necessary. For that, quotes and news are delivered at the terminal in the real-time mode. On basis of on-line delivered quotes, it is possible to analyze markets using technical indicators and line studies. Expert Advisors allow to work off routine of observing markets and controlling of own positions. Moreover, to ensure more flexible control over positions, several order types are built into the terminal.


Articles about Metatrader 5


MetaQuotes Language 5 ( MQL 5)

The client terminal has its own built-in language for programming trading strategies - MetaQuotes Language 5. This is the fifth generation of MQL languages. It allows writing Expert Advisors that automate management of trade processes, as well as implementing one's own trading strategies. Besides, custom indicators, scripts and function libraries can be created in MQL5.

MQL5 Features:
· The language is object-oriented;
· MQL5 syntax is similar to that of C++;
· It contains a large number of functions  necessary for analyzing quotes, managing positions, calling technical indicators, etc.;
· The language is characterized by high performance;
· It has the integrated development environment MetaEditor that includes a debugger.

The detailed description of all language constructions and functions can be found in MQL5 Userguide.



MetaEditor is the MQL5 integrated development environment. It is a component part of the client terminal. MetaEditor allows to create, edit, compile and debug source program texts written in MQL5.

· Editor Opening
In order to open MetaEditor, one should execute " MetaQuotes Language Editor" command of the "Tools" menu or the "Standard" toolbar, or press F4. The editor can also be opened by executing the " Modify" command in the context menu of an Expert Advisor, indicator or script in the "Navigator" window.
· MQL5 Wizard
MetaEditor has a built-in "MQL5 Wizard" that helps to quickly create MQL5 programs. The file is automatically saved in the corresponding folder of the client terminal. Templates can be used for creating files of MQL programs.
· Compilation
After the program text has been written, it should be compiled. The successful compilation results in the creation of the program executable code that can be started or tested in the terminal.
· Debugging
If an MQL5 program has a source code, it can be debugged using MetaEditor.

More details about MetaEditor can be found in MetaEditor Userguide.

Thursday, October 14, 2010

Guide to Testing and Optimizing of Expert Advisors in MQL5


Most of the time when a developer has written an Expert Advisor, making sure that the Expert Advisor achieves its aim of good profitability is always a very tasking process. In this article, we will look at some of the major steps needed in testing and optimizing an Expert Advisor so that we will be able to achieve close to the desired goal of writing the Expert Advisor.

1. Identifying and Correcting Code Crrors

We will begin this article by looking at some common code errors normally encountered in the process of writing an Expert Advisor code. Most of the time beginners face some tough time identifying and correcting code errors when writing their codes or when modifying a code written by another developer. In this section we will look at how easy it is to use the MQL5 editor to identify and correct some of such errors.

You have just completed writing the code and it seems everything should just work because you are almost certain that the code is error free. Or, it was a code that was written by someone else and you made a few changes and, alas! when you hit the Compile button (or press F7), you were presented by series of errors in the code as shown in the Error tab of the MetaEditor Toolbox window.

Compilation errors in an Expert Advisor code

Figure 1. Compilation errors in an Expert Advisor code

Wow! 38 errors and 1 warning, your code may not have as much errors as shown here, all we want to look at are the various types of errors that are likely to show up when compiling our code and how we can resolve them. Let us describe the diagram above.

  • The Section marked 1 shows the description of the error in the code. This is what gives us the idea of what the error looks like.
  • The section marked 2 shows us in which file we have the error. This is very important if we have included files that have errors. With this we will be able to know which file we are to check for the error described.
  • The section marked 3 shows us which line and column (on the line) in our code the error is located. This enables us to know which particular line to check for the error described.
  • The section marked 4 shows the summary of the compilation errors and warnings.

Let us now begin to resolve the errors one after the other. Let us scroll up to the first line in the Error tab so that we can start from the beginning.

Identifying and resolving code errors-1

Figure 2. Identifying and resolving code errors

The first issue is described as : "truncation of constant value" and is discovered on line 16 column 20, to locate the exact line in our code, from the Edit menu of the MetaEditor, select Go to Line or press CTRL G on your keyboard.

Figure 3. Locating the error code line number

Figure 3. Locating the error code line number

A dialog box will be displayed.

Figure 4. Locating error line number dialog

Figure 4. Locating error line number dialog

The range of number as shown on the dialog box is the total number of lines in the code. In this case (1-354) shows that our code contains 354 lines of code.

Type the line number you want to check in the box and click the OK button. You will be taken straight to the line number in the code. You will see the mouse cursor blinking on that particular line.

Figure 5. Cursor showing the error line number

Figure 5. Cursor showing the error line number

The problem here is that we declare Lot as an integer (int) variable but initialize it with a double value (0.1). To correct this error we will change the int to double, save the file and then click COMPILE button again to see if that has been corrected.

Figure 6. Compile and save code after correction is made

Figure 6. Compile and save code after correction is made 

On compiling again, the first issue has been resolved, but we still have more issues as shown below:

More errors in the code to resolve

Figure 7. More errors shows up in code after compilation

We will now follow the same procedure as above and go to line 31. However, this time we will right-click on the error on the Errors tab and select Go to line

Another way of locating code error line

Figure 8. Another way of locating code error line 

Or simply select the error and hit the Enter button on your keyboard. Immediately, you will be taken to the code line number 31.

You will see the mouse cursor blinking and also a small round red button (error icon) on that particular code line 31.

Locating the code error line

Figure 9a. Locating the code error line

However, if it is a warning message like the first one on line 16 that we corrected earlier, it will show a triangular yellow button (warning icon):

Warning sign

Figure 9b. Locating the code error line

It is very obvious that we don’t have any problem on line 31, but the error description says: "'STP' - unexpected token" .

We then must check the previous code line (that is line 30) to see what may be wrong. On close examination, semicolon is missing after "double ccminb = -95.0000" on line 30, that is why we have that error on line 31. We will now fix this error by typing the semicolon after -95.0000 and compile the code again.

Now the line 31 errors are gone. Next is line 100 as shown below:

More errors still exist in code

Figure 10. More errors still exist in code

Hey Olowsam, must we be compiling after each correction, why don’t we just go to through all the lines at the same time and after we have done all the corrections then we compile the code once instead of compiling after each correction?

Did you just ask this question?

You may be right in a way, but I will not advise that. Problems are always resolved one after the other – Step by Step. Any attempt to lump problem together and solve them at once may lead to many headaches. You will soon understand why… just be patient with me.

Back to our problem, we are to check line 100 for the next error. The error states : "'if' - expressions are not allowed on a global scope" And I am sure that the if expression in line 100 is  not on a global scope, but why are we having this error. Please let us go to line 100.

Figure 11.  Locating the error in the code

Figure 11.  Locating the error in the code

We can't find any problem on line 100 and because we just finished correction line 31, we are sure that the problem now is between line 32 and line 99. So let us move upward to line 99 (we have a comment , so it can't be the error). If we also look upwards to the declarations (MqlTick, MqlTradeRequest and MqlTradeResult), they  are correctly declared and punctuated.

Next let us look at the code for the if expression before these declaration code lines and see if the expression is okay.  On very close study, we discover that the if expression has a closing brace, but no opening brace.

Figure 12. Looking above the error line number to identify error

Figure 12. Looking above the error line number to identify error

Add the opening brace and compile the code again.

//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
      Alert("We have less than 60 bars, EA will now exit!!");

Once the code was compiled; errors on line 100, 107, 121, 126, 129, etc were completely cleared and new ones show up. See why it is good to follow step by step? 

More errors show up in code

Figure 13. More errors still exist in code

Next we move to line 56 with two errors : "'cciVal1' - parameter conversion is not allowed" and "'cciVal1' - array is required"

On closer look at line 56, cciVal1 is supposed to have been declared as an array. Could it be that we did not declare it as an array but now trying to use it as an array? Let us check the declaration section to confirm this before we can know what next to do.

//--- Other parameters
int maHandle;               // handle for our Moving Average indicator
int cciHandle1,cciHandle2;  // handle for our CCI indicator
double maVal[];             // Dynamic array to hold the values of Moving Average for each bars
double cciVal1,cciVal2[];   // Dynamic array to hold the values of CCI for each bars
double p1_close,p2_close;   // Variable to store the close value of Bar 1 and Bar 2 respectively

From, here, we can see that we mistakenly declare cciVal1 as a double rather than as a dynamic array because we omitted the square brackets ([]). Let us add the square brackets (just as we have for cciVal2[]) and then compile the code.

//--- Other parameters
int maHandle;               // handle for our Moving Average indicator
int cciHandle1,cciHandle2;  // handle for our CCI indicator
double maVal[];             // Dynamic array to hold the values of Moving Average for each bars
double cciVal1[],cciVal2[]; // Dynamic array to hold the values of CCI for each bars
double p1_close,p2_close;   // Variable to store the close value of Bar 1 and Bar 2 respectively

Errors in code reduced considerably

Figure 14. Errors in code has been reduced considerably

What! So many errors have disappeared. We only corrected error reported on line 56 and some other errors were resolved automatically. This is because, the error reported on line 56 was responsible for those other errors. This is why it is good to follow a step by step process in resolving errors in your code.

We will now move to the next reported error on line 103 : "'GetLastError' - undeclared identifier"  Wait a minute, GetLastError is supposed to be a function… Let go to line 103 to see what the problem is.

//--- Get the last price quote using the MQL5 MqlTick Structure
      Alert("Error getting the latest price quote - error:",GetLastError,"!!");    // line 103

The problem is actually on line 103. GetLastError is a function and every function needs a pair of parenthesis for input parameters. Let us type an empty pair of parenthesis and then compile the code. The empty pair of parenthesis indicates that the function takes no arguments or parameters.

//--- Get the last price quote using the MQL5 MqlTick Structure
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");  // line 103

Next, we move to line 159 : "'=' - l-value required"  and  a warning : "expression is not Boolean" Let us go to line 159 and see what this error means. 

      else if(PositionGetInteger(POSITION_TYPE) = POSITION_TYPE_SELL) // line 159
            Sell_opened = true; // It is a Sell

It can be seen here that we assigned the value of POSITION_TYPE_SELL to  PositionGetInteger(POSITION_TYPE) in the if statement and this is not what we intend to do. We wanted to make comparison instead. We will now change the expression to use equal operator rather than using an assignment operator. (that is ‘==’ instead of ‘=’). Make the correction and compile the code.

      else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) // line 159
            Sell_opened = true; // It is a Sell

Good! Now we have one more to go. Let us go to line 292 to see why it says "'PositionsTotal' - undeclared identifier" . Wait a minute, can you remember that we have seen an error like this before? ‘GetlastError’ line 103. Possibly, we forget to add the pair of parenthesis for PositionsTotal  too, since it is a function. Let us go to line 292 to confirm.

bool CheckTrade(string otyp)
   bool marker = false;
   for (int i=1; i<=PositionsTotal;i++)  // line 292

Just like we suspected, it is because we forgot to add the pair of parenthesis for the function PositionsTotal. Now add the pair of parenthesis (PositionsTotal()) and compile the code. Let me also state that, it is possible to get this error if we actually use a variable which we did not declare anywhere in the code.

Figure 15. All compilation Errors has been completely resolved

Figure 15. All compilation Errors has been completely resolved

Wonderful! Now we have been able to correct all the compilation errors. It is now time to debug our code and see if there are run-time errors.  Here, we will not be going into the details of how to debug our code as it has already been explained in this article.

As the debug session begins, we notice another error :

Figure 16. Runtime error observed during debugging of code

Figure 16. Runtime error observed during debugging of code

Click the OK button and you will be taken to the line of code that generates the error.

Identifying the line of code that generates run-time error

Figure 17. Identifying the line of code that generates run-time error

The error is generated by this code on line 172 as you can see from the figure above. Since the error is an "Array out of range" error, it means that the value we intend to get from the array is out of the range of the array values available. So we will now go to the line where we copy the indicator buffers into arrays to see what the problem is.

//--- Copy the new values of our indicators to buffers (arrays) using the handle
      Alert("Error copying MA indicator Buffers - error:",GetLastError(),"!!");
   if(CopyBuffer(cciHandle1,0,0,3,cciVal1)<0 || CopyBuffer(cciHandle2,0,0,3,cciVal2)<0)
      Alert("Error copying CCI indicator buffer - error:",GetLastError());

We can observe from the CopyBuffer functions that we have only copied three values (Bar 0, 1, and 2) which means that we can only access array values of maVal[0],  maVal[1],  and maVal[2] and also cciVal1[0] , cciVal1[1]  and cciVal1[2], etc. But in our code on line 172, we were trying to get the array value for cciVal1[3]. This is why the error was generated.  Now, stop the debugger so that we can fix the error:

Figure 18. Stop debugger to correct error in code

Figure 18. Stop debugger to correct error in code

To fix this we need to increase the number of records to be copied from Indicator buffers to 5 so that we will be able to obtain array values of cciVal1[0], cciVal1[1], cciVal1[2], cciVal1[3], and cciVal1[4] if need be.

//--- Copy the new values of our indicators to buffers (arrays) using the handle
      Alert("Error copying MA indicator Buffers - error:",GetLastError(),"!!");
   if(CopyBuffer(cciHandle1,0,0,5,cciVal1)<0 || CopyBuffer(cciHandle2,0,0,5,cciVal2)<0)
      Alert("Error copying CCI indicator buffer - error:",GetLastError());

Correct the code as shown and then start the debugger again. This time, no more errors as we notice our Expert Advisor performing trade actions

Figure 19. All errors corrected, Expert Advisor performs trade during debugging

Figure 19. All errors corrected, Expert Advisor performs trade during debugging


2. Testing the Expert Advisor

Once we are sure that our code is error free, it is now time to test the Expert Advisor to be able to get the best settings that will give us the best results. In order to carry out the test, we will use the Strategy Tester, a program which is built into the MetaTrader  terminal. To launch the Strategy Tester, Go to View menu on the Terminal and select Strategy Tester.

Figure 20. Launching the Strategy Tester

Figure 20. Launching the Strategy Tester

2.1. Preliminary Testing of our Expert Advisor

At this point, we want to test our Expert using the available symbols in the Market Window. With this result we will be able to guess which currency pairs we can better optimize our Expert for. Make sure the Market Window contains most of the currencies you are targeting for the Expert.

Select the Expert on the Strategy Tester Settings Tab, select the period/timeframe you have in mind (and of course you can also test it for different timeframes) and then Select 'All Symbols Selected in MARKET Watch' in the optimization field. Directly in-front is the Optimization results parameter, select Balance + max Profit Factor.

Figure 34. Preliminary test of Expert Advisor with all symbols in the Market Watch window

Figure 21. Preliminary test of Expert Advisor with all symbols in the Market Watch window 

1. Select the tick generation mode –(Every Tick)

2. Select Optimization Type –(All Symbols Selected in MARKET Watch)

3. Select type of expected Result from optimization

You can get the details of the various optimization types from the terminal help documentation. We are not forward testing, so leave Forward as No.

For this test, the main values/parameters (highlighted in green) in the Inputs tab will be used.

Figure 35. Preliminary test input parameters

Figure 22. Preliminary test input parameters

Once you are done, switch over to the Settings tab and click the Start button. On completion of the test, you will see a message in the Journal Tab similar to the following:

Figure 36. Preliminary test completed

Figure 23. Preliminary test completed

Once the test is completed, go to the Optimization Results Tab to see the results.

Figure 37. Preliminary test optimization results

Figure 24. Preliminary test optimization results

Our interest is in the symbol that gives the highest Result based on our setting – (Balance + max Profit Factor). To get this, let us sort the result by clicking on the Result title such that the symbol with the highest result is listed at the top.

Figure 38. Preliminary optimization result analysis

Figure 25. Preliminary optimization result analysis

From this result, we can see that our Expert Advisor can be profitable for the following symbols (EURUSD, EURJPY, AUDUSD) in the timeframe we have selected.  You can further perform this test for another timeframe, say, 30mins and see what you have. This should be taken as an assignment and please share the result so that we can all learn too.

From the result of our preliminary test, we will now decide which symbol(s) and timeframe(s) we are going to optimize our Expert Advisor for.

In this example, we will optimize our Expert Advisor for the EURUSD and 1 Hour timeframe.  What are the things that motivate the choice we just made:

  • Profit Factor:

The Profit factor is the ratio of the total profit to that total loss for that test. The higher the Profit factor the more profitable your trading strategy is.

  • Drawdown %:

This refers to the relative drawdown of the equity or the largest loss (in percent) from the maximal value of equity. The lower the Drawdown (in percent), the better the strategy.

  • Recovery Factor:

This is the ratio of the profit to the maximal drawdown. It reflects the riskiness of the trading strategy.

Having decided on the symbol and timeframe to use, it is now time to optimize our Expert Advisor.

2.2. Optimizing the Expert Advisor

Optimization is simply a process of fine-tuning the performance of our EA by testing with various factors (parameters) that determines the effectiveness or profitability of our Strategy coded in the EA. It is a similar procedure to testing, but instead of testing the EA only once, it will be tested many times depending on the parameters selected in the Input tab.

To begin, we go to the settings tab and enable optimization and then select the type of result we want from our optimization.

Figure 39. Optimization settings for Expert Advisor

Figure 26. Optimization settings for Expert Advisor

1. Select the tick generation mode –(Every Tick)

2. Select Optimization Type –(Fast Genetic Based Algorithm)

3. Select type of expected Result from optimization (here we select Balance + Max Profit Factor)

You can get the details of the various optimization types from the terminal help documentation. We are not forward testing, so leave Forward as No. Having set the optimization properties, let us set the parameters to be used for the optimization in the Inputs tab.

Figure 40. Optimization Input parameters

Figure 27. Optimization Input parameters

Since we are optimizing, we will only concentrate on the areas highlighted in yellow. First of all, any parameter we do not want to use in the optimization must be unchecked. In order words, we will only check the parameters we want to use in the optimization of the EA. Here, I have checked five parameters, but you may decide to check only one or two depending on the parameters that the effectiveness of your strategy is based on. For example, you may check only the Moving Average and CCI periods such that the optimization result will let you know the best value for each of the Indicators that give your EA the best performance. This is the main essence of optimizing.

Also, the number of parameters checked will determine the total number of tests that your EA will go through. You will soon see what I am talking about.

Setting The Values


This is the starting value to be used for the selected variable for optimization.  Let us use the Stop Loss variable to explain how to set the values. For the Stop Loss, we have asked the tester to start with a value of 30. This will be the minimum value that will be used for Stop Loss during the optimization.


This is the incremental value for the Stop Loss. If we set an increment of 2; it means that, if in the first test, it uses 30 for Stop Loss it will use either 32, 36, 34 etc. in the second… It does not mean that it will use 30, then followed by 32, 34 etc. No, it selects the values at random but they will always be multiples of two (2) between the Start value and the Stop value.


This is the maximum or highest value that will be used for the optimization. Here we specified 38. This means that the values that will be used for the testing will be between 30 and 38 but will be values which are multiples of 2. It will not use 40 or any value greater.

The total number of tests that will be carried out depends on the settings of these three sections. In our example, the tester will combine a total of 5 possibilities alone for the Stop Loss as shown in the Steps column on the Inputs Tab, it will combine a total of 8 possibilities for the Take Profit, etc. By the time you consider all the other variables, it will be getting to hundreds or thousands of possibilities (tests/passes). If you don't want to wait for ages in order to optimize a single EA, make sure you don't include or check too many variables; maybe just two or three that the performance of your EA really depends on (most especially, the indicator periods, if you use them in your own code). Also you must make sure your step value will not result in having too many possibilities (tests). For example, if we use 1 as the step value, then we have increased the number of attempts for the Stop Loss alone to 10. Well, as said earlier, the total time required to complete an optimization session depends on the total number of available agents you have setup on your system.

I believe the explanation is sufficient.

Once we have finished setting the inputs, we now go back to the Settings tab and click the Start Button.

Once the optimization is completed, we can see the details on the journal tab.

Figure 43. Optimization completed as shown in the Journal tab

Figure 28. Optimization completed as shown in the Journal tab

To view the results as each test is passed or completed, we go to the Optimization Results tab. And it is always good to sort the output by the Results so that we can easily identify the settings that gives us the best result based on our optimization setting. Clicking on the Result heading within the optimization Results tab will arrange the results in either ascending or descending order

Figure 44. Optimization report

Figure 29. Optimization report

Switch over to the Optimization Graph tab to see how the graph looks like.

Figure 45. Optimization graph

Figure 30. Optimization graph

Don’t understand what you see? Don’t worry; the dots you see is a plot of the number of tests your EA passed against the optimization result based on the optimization result type you selected. In our case we selected Balance + max Profit factor.

2.3. Interpreting the result

To successfully interpret the optimization report, go to the Optimization Results tab. You will discover that you cannot see some fields like, Profit factor, Expected Payoff, Drawdown %, etc . To see them, right-click anywhere in the Optimization Results tab and select the additional information you wish to see as shown below:

Figure 46. Selecting Drawdown% in optimization result

Figure 31. Selecting Drawdown% in optimization result

 Figure 47. Selecting Profit Factor in optimization result

Figure 32. Selecting Profit Factor in optimization result

Having added these additional records, we will now analyze the Optimization result to decide the best settings for our Expert Advisor.

Optimization report Analysis

Figure 33. Analyzing the optimization result

From the above figure, the highlighted sections labeled A and B indicates the best results for our Expert Advisor.  Now the choice you make is completely yours, It all depends on what you are looking for. However, here we are interested not only in the settings that give the highest profit, but also have a lower drawdown%.

As you can see, the section A (highlighted in yellow) has the best result (Balance + max Profit Factor) of 22381.71 with a profit of 924.10 while the section B (highlighted in green) has the second best result of 22159.25 but with a higher profit of 936.55. Section A had a lower Drawdown% of 1.78 while B has a higher drawdown of 1.95.

The Strategy Tester saves the optimization results to the"<Client terminal data folder>\Tester\cache" folder. In your case all the optimization data will be saved to the cci_ma_ea.EURUSD.H1.0.xml file,

The file name has the following form: ExpertName.SYMBOL.PERIOD.GenerationMode.xml, where:

  • ExpertName - Expert Advisor Name;
  • Symbol - symbol;
  • Period - timeframe (M1,H1,...)
  • GenerationMode - tick generation mode (0-every tick, 1 - one minute OHLC, 2 - open prices only).

The XML files can be opened by MS Excel.

2.4. Choosing the Best Result

To finally obtain the best result, we need to look at the Optimization graph again. Switch back to the Optimization graph.  Right-click anywhere within the graph and select  1D Graph.

Select 1-D graph form optimization graph

Figure 34. Select 1-dimensional (1 D) graph for result analysis

With this we can easily see the values of each of the input parameters that give the best result.  You can now begin to choose each parameter to be able to see the best value. Right-click on the graph and select X-Axis and then select the parameter you want to check. It will look like below (for Stop loss)

Figure 50. Getting the best StopLoss value from the optimization result

Figure 35. Getting the best StopLoss value from the optimization result

Actually, from the optimization result, it is very clear that the best Stoploss is 34, the best TakeProfit is 78, and the best CCI_Period1 is 62. To obtain the best values for the MAPeriod and CCI_Period2, select each of them as above

Figure 51. Getting the best Moving Average Period value from the optimization result

Figure 36. Getting the best Moving Average Period value from the optimization result

This graph shows a value of 26 as the MA_Period with the best result.

Figure 52. Getting the best CCI_Period1 value from the optimization result

Figure 37. Getting the best CCI_Period1 value from the optimization result

This graph shows a value of 62 as the CCI_Period1 with the best result.

Figure 53. Getting the best CCI_Period2 value from the optimization result

Figure 38. Getting the best CCI_Period2 value from the optimization result

This graph shows values of 28 or 30 as the CCI_Period2 with the best results.

Having obtained the best values for each parameter, it is now time for the final testing of our Expert Advisor.

2.5. The Final Test

The final test involves putting together the best parameters for the testing of the Expert Advisor. In this case, we will use the best values we discovered in the INPUT section of the Strategy Tester as shown below.

Input values for the final test

Figure 39. The final test input parameters

In the SETTINGS tab of the Strategy Tester, we will disable Optimization as shown below

The settings for the final test

Figure 40. The final test settings

We will now click the START button to begin the test. Once the test is completed, we have the results on the RESULTS tab as shown below

Figure 56. The final test results

Figure 41. The final test results

And likewise, we have the graph for the test on the GRAPH tab

Figure 57. The final test graph result

Figure 42. The final test graph result


In this article, we have discussed the ways to identify and correct code errors and we have also discussed how to test and optimize an Expert Advisor for the best symbol from the market watch.

With this article, I believe checking code for errors using the editor and optimizing and testing of Expert Advisors using the Strategy Tester makes writing a better and profitable Expert Advisor possible.

Saturday, September 25, 2010

Better Linux compatibility with MetaTrader 5

I noticed that installing winetricks and then allfonts and gdiplus packages additional to wine 1.3.5 improves MetaTrader 5 look and feel.


1) Install winetricks

2) Install allfonts from winetricks

$ winetricks allfonts

3) Install gdiplus from winetricks

$ winetricks gdiplus

Tuesday, September 21, 2010

Improved access to historical quotes Metatrader 5

Those who like you use Metatrader trading platform you have tested dozens sure, maybe hundreds, of making the respective Expert Advisors backtesting the strategy tester. To perform a backtesting over a long period of time will inevitably need the quoted price of the currency pair and for the entire period during which we will test.

In Metatrader 4 is the problem that the number of candles on a chart is limited and therefore limits the price history with which we have to do the backtesting. Metatrader 4 The solution was to manually download the historical prices of candles M1 and then load this data in the Metatrader, although you can download the chart data is better than M1 M1 download and use the script to get a journalist-turned level of acceptable quality modeling for the backtesting is as reliable as possible. Thus we have the problem of gaining access to the information necessary to make our backtesting with good quality directly from the platform, a problem that must be added that not all brokers offer to download files with the historical contributions. Some will be thinking that the History Center of Metatrader 4 is the download function and that what I say is meaningless, so I recommend you read this article about how to make a backtesting in Metatrader 4 before proceeding.

Metatrader 5 on access to historical data M1 is much quicker and easier, indeed in Metatrader 5 only works with graphics data M1, to be handed to the superior temporal, but always using data from 1 minute candles. For example a sail of a 5-minute chart Metatrader 5 is built with data from the five 1-minute candles that compose it. This fact, together with that in Metatrader 5 can establish an unlimited number of candles on a chart (not Metatrader 4) by simply choosing makes monthly charts and back on the chart until the start of it is downloading all historical M1 automatically and that to build this chart are going to download only data M1. And it extends to improving the modeling of backtesting is of the highest quality without the need for additional operations before backtesting.

The History Center of Metatrader 5 or even allows you to view or modify the data as in Metatrader 4, but according MetaQuotes this functionality will be added to the final version of Metatrader 5.

Monday, September 20, 2010

MQL4 and MQL5: Differences at a glance

MQL5 is the new generation language for the new platform MetaQuotes Software Corp. , Metatrader 5 . The developers of the most popular trading platform in Forex are designed to maintain maximum compatibility and mql5 MQL4 but still indicators, expert advisors and scripts written in MQL4 will have to migrate to the new language mql5 or not work due to some important differences between them. In this article I’ll leave some of the most important and general differences to consider when you prepare to upgrade your mql5 programs.

The differences appear at first sight mql5 code are:

In mql5 not have to declare the functions start (), init () and deinit () (function start () was essential for in MQL4).

The number of buffers in the indicators are not limited (in MQL4 the maximum amount of buffers for an indicator was 8).

Mql5 provides automatic conversions between types of variables involved, even in a numeric string.

Local variables are initialized other than string.

Local Arrays are automatically deleted from the cache after use.
Operator precedence as in C + +.
Checking the logical conditions faster and shorter.
Qe speciales replace functions init, start and deinit

In MQL4 contained three features that could be using either a script, an indicator or an Expert Advisor. In mql5 there are many more features and can not all be used interchangeably in any program.

The function init () has an analogous MQL4 mql5 the function OnInit () function start () has its analogue in OnStart () and deinit () in OnDeinit ().

The OnInit and OnDeInit features have the same functions as init and deinit in MQL4, designating them the code that must be executed to initialize or a program written in fading mql5. Therefore these functions passed to mql5 MQL4 can simply rename or call the former functions as follows:

void OnInit () (
init ();
OnDeinit void ()
deinit ();

You can not do the same for the function start (), since no function mql5 OnStart () that can be used in all types of programs mql5 but there are several functions depending on the type, so for example OnStart () is analogous start () in scripts, OnCalculate () is the main function indicators could and replaced the start () and onTick () it is for the expert advisors. Regarding the above in this paragraph on the role OnStart () I should mention that if a script does not function contine OnStart () code will be compiled as an Expert Advisor. Likewise if an indicator does not contain OnCalculate () as an indicator compilation is impossible.

Predefined Variables MQL5

In MQL5 peredefinidas have Periodic variables such as Bid, Ask and Bars (these must be obtained through those corresponding predefined functions) and others such as Point and Digits have a slightly different expression. MQL5 predefined variables are:
_Digits: Returns the decimal number dei available in the price of contización the symbol of the current graphic.

Point: Pip stores the size of the symbol on the current chart.
LastError: Contains the code of the last error occurred during program execution mql5. Default is the only variable that can be changed through programming mql5 as it can be reset using ResetLastError ().

Period: Stores the current period of a graphic that is running the program.
Symbol: Contains the name of the symbol of the current graphic.
StopFlag: It contains the end point in the execution of a program mql5. When Metatrder five tries to stop a program mql5 _StopFlag changes to true.
UninitReason: Contains the reason code to stop the execution of a program mql5.

Variables exteranas

In MQL4 external variables are declared in the initial part of the program by the name “extern”. In MQL5 this has changed and also external variables are declared at the beginning of the program but with the name “input”.

Timeseries Access

A major change is that there is mql5 timeseries predefined as Open, High, Low, Close, Volume and Time. To access these data is necessary to establish the corresponding functions described in Access to Timeseries and Indicator Data .

Expert Advisors

In MQL4 EA was required Parq even contain the start () function is executed every tick received in Metatrader terminal. In mql5 new events have been made to call the execution of a certain code. This can be used in an EA:

OnTick: analogous to the start of MQL4 function is executed every time you get a new tick.

OnTimer: runs on the basis of a “timer” every so often a time established through EventSetTimer () that will be declared in advance for example in OnInit ().
OnChartEvent: generated by events occurring when the user interacts with the terminal as keystrokes, movements and mouse clicks, movements of objects in the chart with the mouse or creation / deletion of objects. OnChartEvent also run through your call EventChartCustom () function to be processed by OnChartEvent.
OnBookEvent I have not been able to understand that it is this event, I will continue studying for acutalizar this data.

OnTrade: event generated following completion of a transaction between the terminal and the server (eg submission, amendment or cancellation of an order).

OnTester: Event generated when we pass the final test of an EA in eStrategy tester.
You OnCalculate is only for OnStart only indicators and scripts.

Custom Indicators

The main novelty in the MQL5 indicators is that, unlike MQL4, the number of buffers is not limited. This feature will greatly increase the possibilities for developers, but with increasing use of buffers increases memory and the PC needed to run the program.

Another difference to note is the increase of available styles for displaying the indicator on the screen. While MQL4 offered six types, MQL5 offers up to 18. This adds to the great change that has taken on representation of the indicators and graphical objects.

Also changes the direction of indexing of the elements of the buffer indicator. Because the buffer is going to have the conduct of an array type variable, we will go closer in time with increasing the position of the indexed column systems. For example, in the zero position MQL4 return the current value of the indicator while MQL5 zero position is the value of the first calculation, the most ancient data. In MQL5 therefore must specify the type of data to be stored in an array and bind this array to a given buffer and use the access to timeseries described a few paragraphs above.

All these changes make the algorithm for calculating the indicdores in MQL5 recostruído have to be almost entirely if they come from a program written in MQL4.


In my opinion, the language has improved greatly and will greatly increase the chances of the indicators, and scripts Adviors Experts for Metatrader Metatrader 5 on 4 but for users in general without much or any programming knowledge will be very difficult to cope with new language, MQL5 is more sophisticated, more powerful and therefore more complicated.

Saturday, September 18, 2010

Here Comes the New MetaTrader 5 and MQL5

Here is MT5 in action! :

The basic change in MQL5 is the appearance of the object oriented programming. I won’t go deep into OOP – it’s just that experienced programmers get more possibilities. For those who liked MQL4 and don’t know about OOP developers left the possibility to write in MQL5 using the style of MQL4 without OOP. The difference is in the functionality that should be learned again.

Let’s take a simple example: the Ask and Bid variables do not exist anymore. In order to get the Bid values the below function should be called:


There are no frequently used Low[0] or iLow ( Symbol(), PERIOD_D1, 0 ), but you can easily re-construct them. The new functions working with history data give possibilities to read into memory the history data from one point till another one, from a certain bar till another certain bar or from the selected time till the other selected time. Earlier reading of a data series the whole visible range was loaded into memory. Whether you needed it or not, but it was read; and if you needed to read M1, it was read from 1999 (in case there was available history) till the current date, hour and minute.

Now only the necessary range can be read, which considerably saves time and memory.

   MqlRates rates_arrayG[];
   Int Bar=30; // read only 30 bars stating from the zero one

This feature saves both time and memory.

Such a change in functionality doesn’t frighten. We’ll simply need time to learn new functions-analogs.

Some functional innovations that I waited from MQL:

  • OnTimer() – function to process the timer events (now you don’t need to loop the Expert Advisor to make it work with a certain periodicity independent of the incoming tick);
  • OnTrade() – function to process trade events – trade position opening, closing or volume change;
  • OnChartEvent() – processing events by teh mouse or keyboard.

Let’s dwell a little on them.

The OnTimer() function is called if the timer is pre-initialized in the OnInit preset function (processor of EA initialization events).


//|                                                                  |
int OnInit()
   EventSetTimer(1); //each second we'll refer to OnTimer()
//|                                                                  |
void OnDeinit()
   EventKillTimer(); // canceling of timer reference must be called at exit
//|                                                                  |
void OnTimer()
   MqlDateTime str1;
   TimeGMT(str1); // new function to get GMT time

So, control can be obtained not only at tick receipt as it was earlier, but also from the timer which allows writing real-time manageable programs. With this possibility more elaborate systems can be created.

I liked the OnTrade() function. This function is called at the moment when any of the following trade events triggers: order placing, activation of StopLoss or TakeProfit, change of StopLoss or TakeProfit levels, placing/deletion of a pending order.

Now it’s much easier to monitor events connected with trade operations. Now there is no need in loops checking the state of orders at ticks or bars. Such loops are used in MQL4, which considerably reduces the program’s performance so important in optimization.

Let’s dwell on the OnChartEvent() function. The function call is performed for several events. I didn’t manage to test each of them, but the list is impressive:

  • CHARTEVENT_KEYDOWN — key pressing event;
  • CHARTEVENT_OBJECT_CLICK — event of a mouse click on a graphical object belonging to a chart;
  • CHARTEVENT_OBJECT_DRAG — event of a graphical object moving performed by a mouse;
  • CHARTEVENT_OBJECT_ENDEDIT — event of text editing end;
  • CHARTEVENT_CUSTOM+n — identifier of a custom event;
  • CHARTEVENT_CUSTOM_LAST — the last one.

The possibility to manage trading and graphics on a new functional level – this is what the developers have promised.

New graphical objects, buttons, entry field appeared. Chart management has become fantastic, one can even insert pictures from files – this option offers a lot of possibilities for those who like special design. This is not Photoshop, this is the result of MQL5 and MetaTrader 5 possibilities. Among new features is that you can create your own buttons and entry fields adding, for example, a button to close all the open orders, or the button of quick Buy and Sell with preset stop and take parameters.

There is one unpleasant fact: objects cannot be created from indicators. This was made intentionally to quicken the performance of indicators. The good news is that they understand it and, probably, will implement the possibility to start several Expert Advisors in one chart. Thus we’ll be able to create EA-indicators with objects and without trading. Such indicators can be created now – they will operate like indicators. Now the task is solved by starting a trading EA in one chart and the EA creating objects in the second one, and then implement the exchange between them.

For example, I managed to transform my breakthrough indicator from MQL4 to MQL5 in several hours. The most time was taken by the function learning and debugging. In MQL5 the code has become shorter.

As for the terminal itself, I was impressed by the number of timeframes. In my opinion there’s even the excess. Though, the abundance of minute timeframes can be useful for some traders. Well, now there is only one step to the creation of a freely set timeframe. All data are stored now only as a minute timeframe, so there are no problems with the synchronization of different timeframes – this is an important technological solution.

Now there are no files for different timeframes in the HISTORY catalog

Another pleasant introduction is that now we can clear logs.

This is just a brief review of MetaTrader 5. I can’t describe all the system’s new features for such a short time period – the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven’t managed to try all its features, but I am already impressed.

The magicians from METAQUOTES have created an amazing product. I am a developer with 25 years of experience, have seen the start of many projects and can state this for sure!

Best regards,

Yuriy Zaytsev