To be a good programmer it is essential to know the basics of the language you are going to write in and platform you are going to write for. Don’t worry though, it won’t be a technical post at all. I will focus on the big picture of MetaTrader environment – the only thing you should really know to start programming.
There are three main components of MetaTrader that you should be aware of before digging in MQL4. These are server, MetaTrader terminal and programs written in MQL4.
Server can be understood as a broker dealing center or a marketplace. This is the place “connected” to the market. Place from which market data is transferred to your MetaTrader terminal.
MetaTrader terminal is a platform connected to the server from which new prices are received. Every new piece of data updates the informational environment of the terminal. This new piece of data is called the tick. Ticks are not received regularly – they arrived at the terminal once the new quote was announced in the market. Ticks are very important from the MQL4 perspective – every new tick triggers expert advisor or custom indicator.
MetaTrader terminal has many build-in tools that trader can use mainly for technical analysis of charts. These are well-known indicators, scripts and functions.
MQL4 programs are third main component of MetaTrader environment. They highly extend the potential of MetaTrader platform and allow traders to implement their trading ideas, automate their trading routine and in the end make their trading better.
There are three types of applications you can write in MQL4.
- Custom indicators – program that graphically illustrates your algorithm on the chart. These might be any idea that you can think of i.e. line presenting the average of highs of the previous five bars from given chart.
- Expert advisors – programs that can fully or partially automate trading strategy. They are also used for tools that assist and support traders i.e. position management tools and information management tools.
- Scripts – Programs that are run only once. They are usually designed to do a single specific task i.e. add a stop loss to an order, close a position, send push notification.
MQL4 programs interact with the terminal. Using MQL4 language you can access the informational environment of the terminal and use it in many ways.
It is crucial to understand the syntax of a language you are writting in. Syntax is basically a set of rules and principles that govern the structure of a language. In terms of MQL4, this is what you need to remember:
- Each line of the code needs to end with a semicolon
This is correct line of code;
This is incorrect line of code
- There is one exception to above rule. For blocks of code like functions or loops semicolon is not needed
Line of code;
- To write a comment inside the code you need to use two slashes or slash and a star symbol
//This is a comment. It will be excluded from the code
/* This is a multi-line comment, it will be excluded from the code
Comments continues here */
Variable names – name of the variable can consist of digits, letters and underscore, although it must start with a letter. MQL4 language is case-sensitive.
myVariable is different than MyVariable.