Post Tagged with: "EA"

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!

A Nail Gun, a Jack Hammer and Herding Cats

Herding Cats

Yup, it’s true.  I put a 4 inch nail through my left thumb with the ole’ DuoFast pneumatic framing gun.

Here’s a pic.  Don’t worry, it’s not too graphic:

Thumbnail

Ever since I was a kid, I wanted to build my own house. Not sure why.

I’d go through the whole process in my mind.  From the foundation to the roof, step-by-step, building an imaginary house.  Fell asleep many a times with those visions in my head.

My main problem in realizing this dream was money. A bank won’t even consider giving you a mortgage unless you own the land or you’re a licensed builder.

Well, I didn’t own any land and I wasn’t a builder.  

But after a lot of searching, I managed to find a company in California that was willing to lend money to do-it-yourself home builders. Yeah, they charged a big chunk of money for the service, but this was really the only way.

It wouldn’t be easy. I had worked a few construction jobs before I joined the military and again throughout college so I had some experience. But this was different. This was a big project.

It turned out to be a long, tough year. Yup, per the terms of the mortgage, it had to be complete in 1 year!

For the most part, it went pretty well. Still, with a big project like a house, there’s so much detail.

And what I learned was that it’s very difficult to get something exactly right the first time you do it.  

There is a list of things I wish I had done differently. But, unfortunately, when it comes to house-building, it’s not too easy to go back and make fundamental changes.

So why am I writing about this? Well, aside from getting a chance to show off a really cool picture of a nail through my thumb, I’ve found this to be true in a lot of different areas.

It’s hard to get something right the first time.

As a matter of fact, this is especially true with software programming. Software is usually complex, with a lot of details. It’s very difficult to get all those details right the first time.

But the big difference is that it is easy to go back and change software. You don’t have to jack-hammer a foundation.  Just re-write and re-compile.

And when it comes to building an EA, you should expect the same thing. It won’t be right the first time.

I think this fact is actually the best argument for either:

(1) learning MQL, or

(2) using a tool like VTS.

It’s just so much easier to be able to tweak and tune your EA yourself – without having to work with a programmer (like me!).

You know what they say: “Managing programmers is like herding cats”.

Whatever path you choose for your EA development, don’t worry, I doubt you’ll put a nail through your thumb!

NOTE:  I took that picture with one of those disposable cameras while I was driving myself to the emergency room.  I was back to work about 1 hour later.  I’m kinda proud of that!  The truth is I got extremely lucky and did not hit any bone.

Truly, it was merely a flesh wound.