How to Find Errors in the Programming Logic with Meta Editor and MQL4

Sometimes, you might ask yourself if you are insane. Errors happen and some of these errors are hard to find. I will show you a good example. I have removed the dynamic lot size calculation and decided to set a stop loss and a take profit value. The lot size is fixed again for buy and sell trades. Now, I will compile this EA and it works fine. Let’s run it and it produces results. Okay, this is how it should always work.

Now, here comes one of the most common mistakes. Let’s make this an int and compile it and it will still work, but in the back test, it will not trade and it will throw lots of order errors. You know, the reason is a wrong variable type because we set it up this way now. But if the first thing you see is a lot of errors, you can spend hours and hours to find the error. Now, here are a few tips to find errors a little bit faster.

The first thing is to use Google and to type in MQL4 followed by the term you are looking for. Most of the time, it will sent you right to the MQL4 documentation. It says, in the case of error, erroneous or unnormalized stop levels the error 130 error invalid stops will be generated. Okay, so the first step is we know the error 130 has something to do with invalid stops.

So the next thing I do is I comment out the erroneous line. Afterwards, I compile the EA again and do a new back test. We are going to check the journal and the errors are gone. So obviously, the error must be in one of these lines. Now, I’m going to copy and paste the first line and look up the OrderSend() function and here you can see each and every data type that is needed.

Now, I replace values that have worked in other expert advisors before and recompile the expert advisor. Okay, it opened a trade. Next, I try to use fixed values instead of the variable. Recompile the expert advisor. Okay and it starts to trade and the errors are gone.
The only thing that is left now is the data type over here. So if I’m right, it should work if I uncomment the line with the first buy statement. Compilation worked and we have found the error. Often, I ask myself if my code is executed correctly and I will use a print statement like this one because it will show up right in the journal. Now, you should be able to find errors in your code much faster.