Post Tagged with: "expert advisor"

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!

Top 6 Reasons Your Expert Advisor Does Not Trade

Help! My Expert Advisor Does Not Trade

1. Did you enable trading on your MetaTrader platform?

The icon should be green, not red. This shows Expert Advisors as disabled:

This is what it looks like when Expert Advisors are enabled:

Expert Advisors enabled

 

 

 

2. Have you enabled Live Trading on the Expert Advisor input tab? 

If you don’t see a smiley face in the top right corner of the chart, no trades will be placed.

Check the Live Trading box when you attach your EA to a price chart.

EA input tab

 

3. Are there any error messages in the MetaTrader platform Experts tab:

MetaTrader Platform experts tab

Some error messages you may see are:

  • Error code 130 (ERR_INVALID_STOPS): This means your StopLoss or TakeProfit value is not valid. Check them and make sure they are large enough. The actual minimum or maximum value varies from broker to broker.
  • Error code 131 (ERR_INVALID_TRADE_VOLUME): This means your Lot value is not valid. The allowable values vary according to account type (micro, mini or standard).
  • Error code 133 (ERR_TRADE_DISABLED): Use the instructions above to learn how to enable trading on your MT platform.

If you see an error number that you do not understand, use the Navigator in the MetaEditor to search for the error number. See this post for using the MetaEditor for help.

 

4. Is one of the VTS System Managers preventing a trade from opening?

  • Check the Trade Time Manager and the Trade Signal Manager and make sure they are not configured to prevent trades from opening.
  • Check the values of the Input Variables MaxTrade, MaxBuys and MaxSells and make sure they are not set to 0 (zero).

 

5. Have you ever successfully open a trade using an Expert Advisor on this MetaTrader account?

Use the Test Wizard to create a simple EA that opens a trade. (The Test Wizard only allows EAs to run on a DEMO account.)

VTS Test Wizard

Make sure you can open a trade using a Test Wiizard EA before attempting to open trades using a more complex EA.

6. Is your Expert Advisor generating a Trade Signal? Is the EA not even trying to open a trade because the trade Logic is never true?

If you think your EA should be generating a valid signal to open a trade, and it appears that it is not, then see this post for debugging an Expert Advisor.