Sei sulla pagina 1di 3

Introduction to MT4 Programming continued - Patrick Nouvion

Page 1 of 3

PRO 1020 | Intro to MT4 Programming - Patrick Nouvion

Intro to MT4 Programming

Notice - If the concepts of variables, variable type, loops, IF-THEN, declarations and
sound familiar it is highly recommended for you to take the PRO1010 class before goi
this material. I will not go over the basic concepts covered in the previous material and I w
you have the prior knowledge.

Introduction

IMT4 has a powerful programming language called MQL4. It is similar to C and in my opini
satisfy every need, especially since you can write and use custom DLLs. Obviously we will
the basics, MetaQuotes did a great job integrating trading functions so that one can
trade, close a trade, access any data available on the platform and more. There are hundr
functions/items available and I doubt we will cover half of them by the end of all the MT4 c
make sure to use the search and help often. We will cover basics to get started writing cod
only a quick platform and editor overview and this class should help you get ready for the
platform.

The Platform:

So by now Im guessing you have IBFX Trader 4 installed on your machine and that you ha
a demo or a live account. If not then the first step is to download the software

Dont worry you can download the software for free and open a demo account which does
in any ways to IBFX. So go ahead and fill out the form, press submit and download the sof
Double click on the exe and run the install.

When the program starts for the first time - (From Start, programs, IBFX Trader 4, IBFX T
will be prompted with a form to open an account. Again this is free, so fill out the form (Sa
the newsletter, IBFX does not have one anyways) and click on ok. One last thing,
that are left opened and maybe open a new one (File New Chart).

If you are not familiar with the platform, make sure to have a look around and click aroun
live account of course). A long time ago when I taught people how to use Windows 95 I al
noticed how people were so scared of breaking things that they could not use a pc or lea
use one. Just to put you at ease in IBFX Trader 4 there is nothing you can do that a
wont fix. So click away and explore the program for a few minutes. This is the trading pla
what we need is the MQL4 editor, to access it you can either go through the Start, program
Trader 4 and select MetaEditor or from the trading platform click on Tools | MetaQuotes La
Editor.

Meta-Editor

This program is really similar to a text editor, in fact it is one. Can I use WordPad?
WordPad instead but here are a few reasons why you should not. First of all if you look at
click on the fifth icon from the left which looks like a dictionary. This is a great tool. You ca
access it from the menu by clicking on view and navigator.

The navigator by default is docked on the right side of the window, and has 3 tabs at the b
Files, Dictionary and search. You really need to use this resource has the definition
and items can be found there. I myself use it all the time, since I do not remember every
name or syntax.

The first Tab (Files) is a simple browser of your MT4 tools folder. You would use it for
open an indicator you programmed in the past that needs an update. Instead of clicking on
and then try to browse through your many folders, you can quickly open an indicator, scrip
from this Files tab.

The Second Tab (Dictionary) is an index of built in functions, and best of all it is categorize
example Im looking for available string functions; I can easily open the String functions
at the list and see what is available. This is sometimes faster than a search, especially if y
function is there but you kind of forgot the name.

http://www.ibfx.com/ibfxu/catalog/programming/pro1020.aspx

30-08-2009

Introduction to MT4 Programming continued - Patrick Nouvion

Page 2 of 3

The third tab is the search, my favorite of course. Types in a keyword press enter and the
will look for matching functions or items Go ahead and a search for Account Number and
enter.

Whether, you found a function in the dictionary tab or in the search, you can then double
item to get more info. Go ahead and double on the search result item called AccountNum

This should bring (If I is not already there) the Tool Box. Go ahead and increase it size so
a good look at it. On the left you will a list of related functions which is really useful. At the
have a toolbar, which is very much like a browser toolbar. At the bottom you will see
never use and you are welcome to look at them if you want.
Now the content of that page should read:
int AccountNumber()
Returns the number of the current account.
Sample:
Print ("account number ", AccountNumber());

First thing for you to notice is int, this tells you the type of the value the function returns
would not be very smart to write
string MyAccount = AccountNumber();

Although I think this specific example may work, it will not always be so. So I now know th
returns an int value and therefore if I were to store this data in a variable I would need to
this:
int MyAccount = AccountNumber();
Also it always gives you an example on how to use the function which I think is great and
understand how a function is supposed to be used.

Another reason to use this editor is that you can compile your code and see if there are er
were to use WordPad, save your document and copy it to the right spot. All this sounds alr
tedious; chances are it will have many mistakes you could have easily caught int the edito
MetaEditor if you try to compile a piece code and there are errors your will get in the error
tools box a list of problems found. If you double click on one them it usually takes you to
code where the problem is and the error description is usually easy to understand.

And finally from MetaEditor I can click on Terminal to quickly open the platform and test m
compiled program. Use the editor, use the search, use its resources.

Basic Rules:

If you have read the PRO3010 class, you will remember that we used pseudo code. The re
that is because every programming language has its own set of weird rules and
can start writing any code we need to make a list and go through some of those specific ru
apply for MQL4. These rules may not be true for MQLII
1) MQL4 is case sensitive

Int MyAccount = AccountNumber();


This won't work it is nothing else.
No INT, inT, or any other variation.
Same rule applies for double, string and bool (for Boolean variable). intMyAccount = accou
();
This won't work MQL4 functions are also case sensitive so it is AccountNumber() not
etc...
Same rule applies for if, for, while, else and others.
I think you got the drill but let me confirm one last thing:

MyAccount is not equal or not the same thing as myaccount. So I could easily declare 2 va
called MyAccount and one called myaccount. ( not a good idea of course )
2) Statement Termination
Each action/statement is terminated by a semicolon.
For example:
if( MyAccount != 0 )

http://www.ibfx.com/ibfxu/catalog/programming/pro1020.aspx

30-08-2009

Introduction to MT4 Programming continued - Patrick Nouvion

Page 3 of 3

{
Print(Not Connected);
MyAccount = AccountNumber();
Count +=1;
Etc
}
3) IF-THEN, IF THEN ELSE setup
You need to use French brackets for these functions. Remember when we wrote IF
Well the specific syntax for MQL4 is:
If( condition)
{
code;
}
For example:
if( A > B)
{
X = A;
}
else
{
X = B;
}

Many people do not use the brackets, but I would rather you try to use them as it will help
making serious mistakes. Here is an example of a nested if.
if(A>B)
{
if(X>Z)
{
L =1;
}
} else if(C>D)
{
if(W>S)
{
L =2;
} else { L =3;
}
}

Also please notice how everything is lined up, try to also keep your code clean and
fashion. It will be easier for you to spot mistakes. Simply insert a tab space for each nes
statement ( whether if an if or a loop ).
3) Loops setup
Again we need to use the French brackets here. I will list a few examples but it should be
straightforward.
The While loop:
while( Done == False )
{
Print(not done);
Done = . ;
}
The For loop:
for( int i= 0; i < 1000 ; i++)
{
Count++; Print( Count );
}

http://www.ibfx.com/ibfxu/catalog/programming/pro1020.aspx

30-08-2009

Potrebbero piacerti anche