Intx13's MQL4 Beginner Tutorial

[U][B]Introduction to MQL4: Part 1[/B][/U]

[I]MQL4[/I] (MetaQuotes Language 4) is the programming language/environment used in MetaTrader 4. It is a very powerful, yet fairly simple, environment in which you can write scripts, custom indicators, or expert advisors. We will focus on expert advisors, but much of the same material applies to custom indicators as well. In fact, all of the custom indicators available in MetaTrader are themselves written in MQL.

If you have no programming experience, writing software to implement your chosen strategy may seem like a daunting task. Luckily, MQL4 is easy to learn and most straightforward strategies can be implemented in an afternoon. Of course if you’ve never written a program in your life, you’ll need to learn a bit before you’re ready to write the money-maker that will let you retire at 40! And that’s what this tutorial is all about (the learning, not the retiring).

[B]Capabilities and Limitations[/B]

MQL4 is very powerful. In essence it can do anything that you can, sitting in front of MetaTrader, plus a lot more. One way to think about it is that MetaTrader is this big complex machine that can do many things, and that there are two ways to interface with this machine. There is the graphical user interface (GUI), which lets a human click buttons and type in numbers to analyze the market and place orders, and there is MQL4, which lets a computer program do the same thing.

In MQL4 you can use indicators, analyze the history of the market, open and close positions based on your analysis, and do many complex tasks at once.

There aren’t really any true limitations (a limitation being something that a human can do that MQL4 cannot). The only limitation that one might think of is that the expert advisor is not human (duh!). Your EA is going to make cold, logical decisions and follow them blindly. A human following a strategy can still make high level judgements like “the market looks unstable, I’ll not trade for a bit, even though my strategy says I should buy right now”. An EA will simply buy (unless you have programmed it to analyze the market stability, of course).

[B]The strategy tester[/B]

In “real life”, the idea is to load up your trustworthy expert advisor, attach it to your favorite market, and hey presto! you’re rich. We will not be discussing real-time trading (even on demo accounts) in this tutorial, because it takes forever to see if your system is really working. Instead we will test all of our designs on the [I]strategy tester (back-tester)[/I]. Until you’re comfortable writing MQL programs there’s no real reason to be running EAs on a live account anyway.

The back-tester is a component in MetaTrader 4 that allows you to specify a fake initial deposit size, pick a market and timeframe, and execute an expert advisor against the historical data of that market very quickly, to see how your design would have performed. This lets us examine the long term behavior of an EA without having to wait a long time. So long as the market behaves similarly in the future as it did in the near past (which is a good assumption for the lengths of time used in the back-tester) then we’re in business.

Let’s open the strategy tester.
[ul]
[li]Start MetaTrader 4.
[/li][li]Click “Strategy Tester” under the “View” menu.
[/li][li]The following window should appear at the bottom of the screen.
[/li][/ul]

Let’s test out a built-in expert advisor just to hang of things.
[ul]
[li]Select “MACD Sample” from the “Expert Advisor” menu.
[/li][li]Select your favorite market from the “Symbol” menu.
[/li][li]Select “Every tick” from the “Model” menu.
[/li][li]Select “H1” from the “Period” menu.
[/li][li]Click “Start”
[/li][li]Wait until it completes (may take a while!).
[/li][li]Click “Graph” at the bottom of the strategy tester window.
[/li][/ul]

The expert advisor in progress:

The performance graph after it finishes:

Back-testing an EA is important because we don’t want to get hung up on short-term profits or losses. By testing our code over long periods of time we can analyze its performance in the “big picture”.