MQL4 TUTORIAL – HOW TO GET THE LAST ERROR

video
play-sharp-fill

 

In this video we are going to talk about common backtesting errors and how to find them. This is an Expert Advisor, but it doesn’t show anything and it doesn’t trade. So the first thing you need to check is the journal tab here. And here you might see an error message like this one: order send error 131. Now, what does that actually mean? Let’s pause the strategy tester here.

 

Right click on one of the error messages and select Copy. Now go to Google and paste the text here. I will remove the currency pair and the time and date stamps here. So now it says MQL4 order send error 131. Now you can press Enter. And you should see this one: error codes appendixes MQL4 tutorial.

 

And when you click on the link, you will see what each and every code of these errors mean. In our case it was 131. That’s an indicator for an invalid trade volume. Another very common error is error 138 for broker requotes. Let’s change the slippage for our trade from three to zero.

 

Recompile the code for a simple buy trade. I have called this Expert Advisor MQL4 errors because I wanted to show you how to create errors. Because that might help you to find them. Right now we don’t get any errors here. So let’s set this one to minus one. Now click on compile. And you can compile it without any errors and any warnings because syntactically everything is correct.

 

Now let’s start a new test here. And this time it says order sent error 4051. Let’s see if we can find this one: 4051 means invalid function parameter value. So let’s get back to three pips for the slippage. Let’s try a value of Ask plus 100 points for the stop loss, compile the code.

 

And once again you don’t see any errors here. But when you restart your Expert Advisor test, you see order send error 130. And 130 stands for invalid stops. Let’s fix this error and set the take profit value to Ask minus ten times point. Once again you can compile everything, but when you start a new test you will see other send error 130.

 

Well, we intentionally created all the errors here, but you might even experience order errors just because you change a setting. Everything is all right here. Let’s compile it, restart a test and you should see that it works. But when you stop the test and change the spread value here to 100, you will see, after you have clicked on start, that the order send error 130 remains. From time to time you may ask yourself: what is going wrong here?

 

So there is a way to output the last error directly here on the chart. Let’s stop the test here and change our little test expert advisor here. We are going to use the comment function to output the text “the last error was” and we use the function get last error, because that will return the contents of the system variable last error. And in this last error, you will find all the error codes here. So let’s check it out.

 

Just recompile the code, click the little button here, or press F4 on your keyboard. And when I start the expert advisor, it says the last error was 130. If I readjust the spread here and start another test, the output is the last error was zero. Okay, now, you know how to find errors in MQL4, and you know how to create an output directly on your chart. And I would suggest to use this simple expert advisor here and change a few things.

 

And when you restart your expert advisor, you should get the output of the error code directly on the chart.