Understand and Use MQL5 Strategy Tester Effectively

Mohamed Abdelmaaboud | 24 May, 2023

Introduction

As MQL5 programmers or developers, we find that we need to well understand and use the strategy tester to get effective results. By using this valuable tool we can get valuable insights in terms of the performance of our created MQL5 programs and this will affect in getting better trading results. So, we find that we need to well understand important topics before we do our testing like dealing with all error types as it is very normal as developers that we make mistakes that result in errors. Another topic that we need to understand well also is Debugging which let us execute our created programs in an interactive way. Then, we come to the most important and valuable tool which is the strategy tester to test and evaluate our created programs with the most interesting features than MetaTrader 4. So, it will be an amazing learning journey in this article to learn the most important aspects of using the MQL5 Strategy Tester we will cover it through the following topics:

We will try to cover the most popular points about these previous topics to well understand what we need to deal with as programmers or developers. What I need to mention here is that when we deal with this topic, it is normal that all readers already know the MQL5 programming language and they are know how to code their programs as this is a pre-requested topic that you need to be understandable to get the most benefits from reading this article. If you need to learn about the language first, you can find the MQL5 documentation. Also, you can read my other articles about learning the basics of mql5 programming and how to create trading systems based on the most popular technical indicators and I hope you find them useful.

Errors

In this topic, we will learn about errors that we can find when creating, executing, and running MQL5 programs. What makes this topic very important to understand is that the MQL5 will report these errors correctly but if we do not know what these errors mean or at which stage of our program we will take much more time to handle or resolve them than if we already know what they mean.

We will present this topic based on the stage of our working on the MQL5 program, while we write our code there are errors and warnings that can be faced which are Compilation errors and warnings. We may face also errors when executing the MQL5 program which are the runtime errors and we may face also other types of errors when our MQL5 program is trying to trade which are the trade server errors. So, we will cover the following types of errors:

Before covering these errors there is an important thing you need to know the place of errors appearing. In the MQL5 IDE, there is a toolbox window in the lowest part if you do find it by default, you can view it by one of the following methods:

1- Clicking View ==> Choosing Toolbox

Toolbox

2- Pressing Ctrl+T from keyboard

3- Clicking the Toolbox button from the main toolbox

Toolbox2

After that, we can find it the same as the following picture

Toolbox3

Compilation Errors and Warnings:

When we write our code for a specific program we may make a mistake and this is very normal by writing a wrong syntax or having typos for example which leads to errors when compiling the code. In this part, we will share the most popular and commonly faced this type of error. The most important thing also that you need to know is that the program cannot be compiled until the errors are eliminated or solved. I will present the most popular errors that we can face as per this type of errors:

semicolon expected error:

This error we will face when forgetting to write a semicolon at the end of the line, So we have a missing semicolon, or if we forgot a left bracket also. We have to use these types of symbols correctly to avoid this type of error. the following code is an example of this error.

Wrong code with error:

int a=(2+1)

The error will be the same as the following:

semicolon expected

Correct code without error:

After correcting the code by adding the semicolon at the end of the code line we can find the correct code the same as the following:

int a=(2+1);

After compiling the code we find the error is eliminated and the code is compiled without any errors the same as the following:

semicolon expected resolved

Unexpected token error:

This is another type of code that we commonly face and the reason for it is forgetting a right parenthesis in the last line of code or we may add extra left parenthesis in the present line of code. We can see an example of this type of error the same as the following:

Wrong code with error:

int a=(2+1;

We can see the error as we know in the Errors tab in the Toolbox the same as the following:

Unexpected token

Correct code without error:

int a=(2+1);

We can see the Error tab with any errors after correcting the code by adding the right parenthesis the same as the following:

 Unexpected token resolved

Undeclared identifier error:

This type of error occurs when we use a variable without declaring it first as we must declare any new variable before using it or assigning any value to it. Declaring it by choosing the right and suitable data type that we need to be returned to this variable like integer or string for example. The following is an example of this type of error by using a new variable without declaring it.

Wrong code with error:

a=5;

In the previous code, we used the variable (a) by assigning 5 to it without declaring it. So, we will find the  undeclared identifier error when we compile this code the same as the following:

Undeclared identifier

As we can see the accuracy of the error description can be very helpful as it not only reports the error but specified the variable 'a' that is the reason for this error.

Correct code without error:

int a=5;

After compiling this correct code we will not find any errors in the same picture that we mentioned before from the Errors tab of the Toolbox.

Unbalanced left parenthesis error:

This type of error occurs when we miss the right parenthesis or we use an extra right one. We can find this error through the following example.

Wrong code with error:

   bool a=7;
   if (a=5
   a=5;

In the previous code, we find that by forgetting the right parenthesis the error of  "unbalanced left parenthesis' in addition to the error of "some operator expected" the same as in the following picture

Unbalanced left parenthesis

Correct code without error:

   bool a=7;
   if (a=5)
   a=5;
After correcting the code we will find that it compiled without errors.


Unexpected end of program error:

Sometimes we miss a closing bracket in our code and this occurs unexpected end-of-program error we have to check our code to see what needs to add or we need to make sure that every opening bracket has a closing one to solve this error. The following is an example of this code.

Wrong code with error:

void OnStart()
  {
   bool a=7;
   if (a=5)
   a=5;

By writing the code the same as the previous block of code by forgetting the closing bracket after the last line (a=5) we can find the "unexpected end of program" error in addition to another error of "unbalanced parentheses" the same as the following picture

unexpected end of program

Correct code without error:

void OnStart()
  {
   bool a=7;
   if (a=5)
   a=5;
   }

After correcting the code by adding the closing bracket we will find that the code compiled without errors.

Expressions are not allowed on a global scope error:

This error occurs when we miss a left bracket in a compound operator or it may happen when we write a statement or expression outside the scope of a specific function as we have to use only expressions within the scope of the function. The following example is for this type of error.

Wrong code with error:

   int a=(7+5);
   if (a<7)
   
   a=7;
   }

In the previous code, we missed the (}) opening that changed the scope and reported the error "Expressions are not allowed on a global scope". The following is the error from the Errors tab.

Expressions are not allowed on a global scope


Correct code without error:

   int a=(7+5);
   if (a<7)
   {
   a=7;
   }

After correcting the code the same as the previous block of code we will find that the code compiled without any errors.

Wrong parameters count error:

We experience this type of code when we use a specific function with specific parameters and we do not specify these parameters properly by specifying too many or not enough parameters. This error will appear if we use the predefined function or our created functions. The following is an example of this error.

Code with error:

If we created a function of "myVal" to return an integer value of the result of the summation of two integer values what means that we have two parameters in this function? So, when we call this function by using parameters different than these two parameters we will get this type of error the same as the following code

void OnStart()
  {
   int example=myVal(10);
  }
//+------------------------------------------------------------------+
int myVal(int a, int b)
  {
   return a+b;
  }

By compiling the previous wring code we will get the error the same as the following:

Wrong parameters count

Code without error:

To write this code correctly we need to use the specified parameters of this created function the same as the following

void OnStart()
  {
   int example=myVal(10,20);
  }
//+------------------------------------------------------------------+
int myVal(int a, int b)
  {
   return a+b;
  }

After compiling this correct code we will not find any errors.

Some operator expected error:

This type of error that we find when we miss an operator in a specific location by missing it completely or misplacing it. The following is an example of this type of error.

Code with error:

int a= 7 10;

In the previous code, we missed an operator in between the two numbers and this is the reason that we will get an error of "some operator expected" the same as in the following picture:

Some operator expected

Code without error:

int a= 7+10;

After correcting the code by adding the (+) we will find the code will be compiled without errors.

The mentioned errors are the most popular errors in this type as compilation errors but we said that there are compilation warnings we need also to learn what they are, they are shown for informational objectives and they are not errors. The following is an example code that we find that we will get a warning in the Errors tab

int a;

By compiling the previous code we will get a warning of the variable 'a' not used as we declared a new 'a' variable but we do not use it or assign value to it. The following is the warning as a picture

 warning

You can check out all MQL5 The following link from documentation for more information about:

Some tips that can be very useful and helpful to deal with these errors when occurs:

Now we learned about the errors that we can get in the first stage when writing the code of the MQL5 program. We will move to the second stage which is errors that occur when we execute the program which are Runtime errors.

Runtime Errors:

As we mentioned before Runtime errors are the types of errors that occur during the execution of a MQL5 program. These errors will not prevent the code from compiling without errors but it will not be executed or operated as we need or expect. We can find these errors in the log as printed messages with the reason for the error and the line that has the error in the source code also.

Examples of these errors:

Code of error Reason of error
4007
  Not enough memory for the relocation of an array, or an attempt to change the size of a static array
4101   Wrong chart ID
4301   Unknown symbol as a market info
4754   Order not found
5120   Internal database error

If you noticed that we will have a code of error as MQL5 defined all Runtime errors by codes to be easy to identify and you can find these codes by using GetLastError() function as adding error handling method to your program, we can see these errors reporting in the Journal tab while testing or in the expert tab while attached the program to the chart to be executed in the current time, and you can see all of Runtime Errors with all codes in the MQL5 documentation through the following link:

https://www.mql5.com/en/docs/constants/errorswarnings/errorcodes

I need to mention here that the program will not end the execution after the runtime error except for a few critical errors that will end the execution like A array out-of-range error for example.

Trade Server Errors:

These types of errors occur in the stage of execution of a trade as a structure of a trade request MqlTradeRequest using the OrderSend() function, we can see also these errors reported in the Journal tab while testing or in the expert tab while attaching the program to the chart to be executed in the current time. If you need to see examples of these errors the following are for that:

Code of error
Reason of error
10006
  Request rejected
10032
  Operation is allowed only for live accounts
10033
  The number of pending orders has reached the limit
10034
  The volume of orders and positions for the symbol has reached the limit
10041
  The pending order activation request is rejected, the order is canceled

You can see all codes of this type of error you can find them in the MQL5 documentation through the following link:

https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes

Debugging

In this topic, we will learn how Debugging can be used through the MQL5 debugger tool as this can be used to execute our created program through historical or live data and we can do that by clicking the start/ resume debugging on real or history data the same as the following picture of Debugger buttons from the toolbox

 Debugger buttons

1- Start/Resume debugging on history data

2- Start/Resume debugging on real data

3- Pause debugging

4- Stop debugging

Once we start the start button to debug on history data we will find the program will be executed on the historical data by opening a chart to debug the program on the historical data or if we chose the debugging on real data the program will be attached the chart on real data and beside the program you will find the (Debugging).

The Strategy Tester

In this part or topic, we learn about the most important and valuable tool in the MetaTrader 5 which is the Strategy Tester with new features that are better than the tester of MetaTrader 4 like for example the multi-currency testing and others. How we can get this Strategy Tester we can do that through one of the following methods:

1- While opening the MetaTrader 5 trading terminal, press Ctrl+R from the Keyboard.

2- From the menu of View ==> choose Strategy Tester.

Tester

After that, we will find the Strategy Tester the same as the following of the Overview tab to choose what you need or the type of testing

Tester1

Simply, through the Overview tab, we will choose what we need to test, and once we do that the tab will be changed to the Settings tab with predefined settings based on what you chose to test.

You can also see previous tested results through the last option View previous results. You can also search for a specific previous test through the search below the options of testing.

If we move to the setting tab we will it the same as the following:

 Tester2


We have also the Inputs tab to check if the tested program has inputs that can be inserted or edited by the user.

After determining our setting and pressing the Start button, the program will be tested and we can monitor that testing through the chart that will appear if we do visualize testing and tabs in this chart which Trade to see executed trades if our program does that, History to monitor closed or canceled orders, Operations to see all Operations, and Journal to check printed messages of the program and others. We can also monitor it through the Graph tab in the Strategy Tester window to monitor the performance of the program.

After the test is finished we can check also the Backtest tab that will be appeared as it will have all testing statistics and insights the same as the following example

Tester3

Tester4

As per the program you will test the data in this Backtest tab will appear like or different than what you see in the previous example.

There is one more thing I need to mention here as it is very important in the testing process which is the Optimization that includes selecting a wide range of different parameters and inputs and checking every possible combination of these inputs or parameters to check and test which parameters will generate the best results. Here we will choose one of the Optimization models from the Overview tab and we will select the suitable setting of Optimization the same as the following pictures

Tester5

Tester6

The Most important elements that we need to check and evaluate after testing:


Conclusion

We learned some essential topics about one of the most important and valuable tools that we all need to master when developing any MQL5 program which is the Testing process through the Strategy Tester of the MetaTrader 5 trading terminal and we learned some related topics that will help to understand and use this tool effectively. We learned about Errors that we face when developing MQL5 programs as per the types of these errors, which are:

Then we learned about Debugging through learning how to use the debugger of the MQL5 IDE and we learned how to use the Strategy Tester of the MetaTrader 5 through learning its different model, we can set settings of the testing based on the Model, how we can monitor the testing process through a different tab that reports everything when testing any MQL5 program, and after testing how we can read the report of the test and identified the most important elements that we need to check and evaluate and how we can optimize our strategy through testing to get the best result from the test MQL5 program.

I hope that you find this article and its information useful for you to help in improving your results through making effective testing and making your MQL5 programs. If you find the article useful and you need to read more articles for me you can check my other articles like sharing how we can create trading systems based on the most popular technical indicators.