Education

How to Resolve an MQL Syntax Error

How to Resolve an MQL Syntax Error

Inspect the Error message, then locate & fix the problem

The VTS output tab will show the exact MQL syntax error:

Although this shows the exact location of the error in the MQL file, this information may not be very helpful, especially with a large system.

To find out more, click the Editor button to launch the MetaEditor. Then press F7 in the MeteEditor – this will build the EA.  The same error message is shown in the MetaEditor’s Error tab:

When you double-click the error, it will bring you to the exact location of the error in the MQL file.

In this case the error is associated with the MQL Variable named Variable1

Now you can inspect that Element on your VTS drawing:

The assignment field of the Variable1 Element contains a semi-colon (;). This is not valid MQL syntax, so it causes the MQL build to fail.  Changing the semi-colon to 0 (zero), or some other value will fix the error.

Every error is a little different , but the objective is the same: find the general location of the error and then make changes to see if the error is resolved.

 

Important Note: The first time you click the Editor button in VTS, the MetaEditor is launched and the Expert Advisor for the current VTS system is  loaded into the MetaEditor.  But, and this is significant, if you make changes to your VTS drawings and rebuild by clicking the Build button, the MQL file in the MetaEditor will not be refreshed. You need to close the file in the MetaEditor and then reopen it to see the latest changes. There is simply no facility that VTS can use to instruct the MetaEditor to reload a file.  We had a sloppy work-around in a previous version of VTS, but changes were made to the MT platform (around build 400) that caused the work-around to break.

Final points:

  • This is a  handy trick:  If an Element is not connected (using a link) on the VTS drawing pad, it will not be included in the MQL code and therefore will not be built. So you can temporarily remove the links to/from an Element and re-build.  If the error is gone, then the problem was caused by the excluded Element.  Also, you can remove all links – just connect the Start Element to an End Element, and rebuild as you add each Element back to the drawing one at a time.
  •  You can get more information about the MetaEditor in this post.
  • We offer a free email MQL course. Through a series of emails, it teaches you how to program MetaTrader4 Expert Advisors. No strings attached, just a bunch of emails spaced out over a few weeks.
  • I’ve written an ebook on how to learn MQL: MQL For Traders

 

Common reasons your Expert Advisor does not build

The most common reasons your Expert Advisor does not build

First: Did your Expert Advisor really not build? 

VTS will output both Errors and WarningsWarnings are informational only. They may indicate there is a problem, but they will not prevent an Expert Advisor from being built. For example, if a Logic Element is not fully defined, you will see a warning, but the EA will still build.

How and Why does VTS allow syntax errors to occur?

First, Why?  VTS allows some free-form MQL to be added to a VTS system. This keeps you from being restricted from using advanced trading techniques. However, this can also cause errors.

How? There are 4 basic ways that syntax errors sneak into a VTS system

1. Variable Assignment

The Variable assignment field allows you to enter any valid MQL, not just a numerical or alpha-numerical value.   For example, you may want to determine the spread, so you create a variable named “spread” and in the assignment field enter:

Ask – Bid

Or you may want the average price:

(Ask – Bid)/2

It’s pretty useful to be able to enter simple MQL like this.

2. MQL Element

This is similar to entering MQL code in the variable assignment field, except that you can enter multiple lines of MQL code, or even an entire MQL function.  You should not use the MQL Element unless you have some experience with MQL or are willing to learn.

3. Parenthesis

On the Logic Element configuration window, there are parenthesis buttons to add open or close parenthesis  to any logical condition.

The  open and close parenthesis allow you to create complex logic. But if the parenthesis don’t match up, you will get an MQL syntax error. Again, you should not use this functionality unless you have clear understanding of the order of logical precedence in MQL (or you are willing to learn).

Best practice: When using any of the above 3 methods, press the Build button often (after making any changes) and check for errors. This way, if you inject an error, you’ll have a better idea of where it is located.

4. Non-ASCII characters or Spaces

  •  Most of our testing is done on Windows PCs where US-English is the primary locale. If your PC is not using an English locale, please be very careful with your element names and values and be sure they are simple ASCII values.
  • MQL variables and function names can not have spaces (or start with numbers). The VTS entry screens should prevent these from being entered, but it does happen.
  • If you are used to using commas for fractional numbers and decimals as a thousand separator (backwards to us English users) this will cause problems. The MQL syntax expects fractional numbers as 1.23, not 1,23.

See this post on how to resolve an MQL Syntax error.