Articles by: David Williams

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.

 

What is the MetaTrader SHIFT parameter?

What is the MetaTrader SHIFT parameter?

The SHIFT parameter is used to identify a candle (or bar) on a MetaTrader price chart.

  • The currently forming candle has a shift value of  0.
  • The last fully formed candle has a shift value of 1.
  • The candle formed before that one has a shift value of 2, and so and so on

So when you look at a  MetaTrader price chart, the candle-shift-values read from 0 to N, from right to left. Where the candle with a shift of 0 (zero) is the candle at the far right edge of the chart.

This concept of a candle being identified by its shift-value runs deep in the MQL language.

Any of the 4 price values  (High, Low, Open, Close) can be obtained using a shift value as an index to the built-in price series that are available in MQL.

For example:

  • High[0] : This is the high value of the currently forming candle.
  • Close[1]: This is the close value of the last fully formed candle.
  • Open[5]: This is the open price from 5 candles ago

The price series arrays only offer  the price of a candle on the current chart (defined by its Symbol and Time-frame).  MQL also offers functions to get the price value for a candle from any chart.  These functions are named iHigh, iLow, iOpen and iClose.

The parameters for these functions are the symbol, timeframe and yes, of course, the shift:

double iHigh( string symbol, int timeframe, int shift)

This will get you the high value of the last fully formed candle on a EURUSD 1 minute chart:

iHigh( “EURUSD”, PERIOD_H1, 1)

We’re almost done. Finally, every technical indicator provides a shift value as well, for the same reason.

For example, here is the RSI function:

double iRSI( string symbol, int timeframe, int period, int applied_price, int shift)

To get the RSI value for the last candle, set the shift parameter to 1.

In the VTS-MetaTrader EA builder, every shift parameter offers a visual selection menu. To find it, just scroll up in the parameter pull-down menu and select the first item “choose…“.

IMPORTANT note about shift of zero!

When you use a shift of zero,  understand that the value can change on each incoming change in price (tick).

For example, for a candle, the only value that will not change for a shift of zero is the open price.  The high and low values may change on each tick; and the close value does change on each tick.

Since nearly all indicators use the basic price values for their calculations, you can count on an indicator value changing on each tick if its shift parameter is zero.

WARNING: If you have logic that tests if an indicator value is greater or less than some value, and the shift is 0, the logic may be true for exactly 1 tick and then become false again.  Generally, it is safer to use a shift of 1 to avoid this “bouncing”, but it can inject a delay in your EA.  Look into the “Power Tab” on the Logic element to address these advanced situations.

Here is a forum post with more info:

MQL Shift Parameter