How to Debug Expert Advisors (MetaTrader 4)

Debug Expert Advisors Using VTS

The MetaTrader platform does not have any built-in functionality for debugging. So, we do what we programmers have always done: add lots of print statements to figure out how the program is executing.

This is easy to do in VTS. Just use the Message tab of an Element to write information to the MetaTrader Platform’s Experts tab, the Price Chart or both. (Here is a link to the VTS help file on the message tab.)

All Elements offer a Message tab, but the Variable and Function Elements offer a few extras:

  • On the Variable Message tab you can choose to write the value of the variable.
  • On the Function Message tab you can write the value of function and also the value of the last error.  The MQL function GetLastError is used to get the numerical value of the last error that occurred. This can be useful information when debugging.

VTS Variable Message Tab


If you are trying to determine the flow of execution of your EA, then ‘Write to Experts Tab’ is your best option.  This will send the message to the Experts tab on your MT platform. The messages are written when the EA is attached to a live chart  or run on the Strategy Tester.


The messages are written in order as they occur. Actually the newer messages are added at the top of the list in the Experts tab.

In a busy market the messages will be written often.  If you right-click in the Experts tab window, you can uncheck  the Auto Scroll option. This will prevent the messages from scrolling away out of view.


 How to Track the Flow of Execution in your Expert Advisor

  • Use the Message tab to write out the value of all Variables and Indicators.
  • Use the Message tab to write out messages from Elements connected to the true and false tabs of all Logic Elements. For example, on the VTS-built drawings (OpenBuyLogic, OpenSellLogic, CloseBuyLogic, CloseSellLogic) there is an End Element connected to the true and false outputs of the Logic Elements. The End Elements are a good place to write messages from because they indicate how the Logic was evaluated (true or false).
  • Don’t make any assumptions. Look at the value of everything!
  • Run your EA on a live chart or the Strategy Tester and inspect the Experts Tab.

You should see the messages in order. It might be too much information and you’ll need to disable some of the messages.

  • Look for zero or negative values for indicators
  • Look at the values of the operands for your Logic elements. Do they agree with what you are seeing on the chart?

There is a lot of data, and it’s a little messy, but this is not rocket science. Just find out the value of every variable/function/indicator that is used by a Logic element and determine what should be happening. During the course of this determination you will discover what is wrong.  By the way, the computer is always right – trust me on that one.

Sometimes it is useful to monitor a value on the chart in real time. In this case use the ‘Write on chart‘ option on the Message tab.

Note: If you want to write the value of one of the MetaTrader built-in variables, like Bid, Ask, Close[0], Close[1], Open[0], etc.,

  1. Drag, drop and connect  a new Variable Element.
  2. In the assignment tab, enter the exact name of the MQL variable.
  3. Configure the Message tab accordingly.


Once you have insight into the exact values that your Expert Advisor is seeing, finding the actual problem is usually pretty easy. Good luck!

One Comment

  1. Pingback: Top 6 Reasons Your Expert Advisor Does Not Trade - MetaTrader Expert Advisor Builders

Leave a Reply

Your email address will not be published. Required fields are marked *


35 − = 30