How interested are you in learning MQL4 programming ?
This opens up great prospects for any Forex trader. After all, you can create a trading advisor of almost any complexity. EAs can be a reflection of almost any trading strategy, which means diversification takes place. The very concept of automated trading implies many benefits - more free time, less emotional stress, optimization of trading and diversification of risks.
Introduction to writing trading robots in mql4. Creation of an Expert Advisor, advice from programmers on how to solve the problems of debugging an Expert Advisor, process optimization.
To create, not to order from a professional programmer. In fact, the task is not that difficult. And the advantages are obvious: savings on freelance services and the ability to further create your own trading advisors. The skill won't go anywhere!
MQL4 programming language - what and how?
Perhaps you know that the MT4 trading terminal has its own programming language - the so-called. MetaQuotes Language (abbreviated as MQL). The most popular version is 4, although some also practice writing Expert Advisors in MQL5 - for MT5 .
This language allows you to both write your own indicator, script or trading advisor (expert), and come up with various analytical functions to analyze the market situation.
An indicator is a program that graphically displays certain calculations on a chart. Any number of indicators can be located on one chart at the same time.
A script is a small program that performs a specific action one-time. For example, when you press a certain key for an open order, a fixed stop loss and take profit are set.
A trading advisor is a program that performs a variety of actions: displays any information on the chart, opens and closes trades, sets, cancels and changes stop orders, and much more. In fact, an advisor is a programmatic representation of a specific trading system (strategy). EAs are sometimes called Mechanical Trading Systems (MTS). The main feature of MTS is complete autonomy.
MQL4 programming language - let's start!
The syntax of the MQL4 language is very similar to the well-known C (the one from which C + and C ++ came from). Therefore, if you know how to program in C, then learn MQL4 without problems. The only differences are that the latter is a simplified version of C, and is also “sharpened” for writing indicators and MTS.
In this section you will find all the necessary practical information about MQL4 programming. This programming language is built into one of the most popular trading terminals MetaTrader4. And all this thanks to the automation of Forex strategies, which you can do completely by yourself.
Below you will find detailed practical MQL4 lessons, which cover everything "inside and out". The training material is designed for both the very beginners and those who are already "in the subject."
Complete beginners will feel strange at first, but start with simple basics, and then gradually move on to complex functions - and you will succeed! You will find useful both a book-guide on the language
Benefits of MQL4 Programming
● Implementation of your own ideas for profitable trading.
● A new direction for self-development.
● Ability to write paid advisors to order.
● Deeper understanding of trading operations.
● New ideas for trading.
● Diversification of risks.
● Automation of trade.
● Ability to carry out any statistical analysis.
Lazy or busy traders can always turn to a professional programmer for help, who will write you a turnkey trading advisor for a relatively small fee (from $ 50 and more). Still, we advise you to study at least the basics of the MQL4 programming language - this will help you more accurately explain to the programmer what trading algorithm you want to get at the output.
An immediate answer to the question: why an advisor and not a script ? I am interested in opening on a new bar (candle). The script will start working immediately after being transferred to the trading chart. The Expert Advisor will wait until a new bar and only then will it open positions.
Moreover, in the event of a failure, the EA, remaining working on the chart, will check whether all orders for the specified currency pairs are open. If an order for a certain currency pair is not found, the EA will open it immediately.
I would like to note that the creation of these advisors and indicators will not provide profitable and stable Forex trading. These are just assistants that will facilitate the process of analyzing the trading situation and operations with orders.
To avoid setting unnecessary external parameters and speed up the work, there will be 2 different advisors. 1st for Buy, 2nd for Sell. This will reduce the level of possible errors from carelessness.
For those who are just starting to get acquainted with programming in MQL 4, for a better understanding of the basics of the MQL language, you can first study the blog entry on how to write an indicator that displays the spread for the current trading instrument.
For beginners, many programming issues should be cleared up after reading this post.
Let's define the required external settable variables.
• number of lots;
• deviation from the opening price;
• opening series;
Now open MetaEditor. On the left in the Navigator, select the "Files" tab and open the Moving Averages Expert Advisor. After opening, select and delete all content, then click File - Save As and save it under the name "Buy_X".
Setting external variables for setindexstyle
The first lines are to set the required external variables.
For beginner MQL4 programmers - the external parameter indicates that the variable can be changed externally, i.e. in MetaTrader.
The workLimit variable is the limiter. If the price moves away from the opening price of a new bar by more than the specified level, the order will not be opened.
The openSet variable is, in principle, a small addition. Although I have never used add-ons in Forex trading before, I decided to provide for this parameter for the future.
The value of the openSet variable will be indicated in the Comments in the opened order. With the help of this, we will be able to determine which series of orders we are currently working with.
To make it clear, I will explain with an example: on a new bar, we opened 5 orders of the 1st series. After 10 minutes, we decide that we need to top up. We again transfer the EA to the trading chart, specify the series number 2 in the EA parameters and open the EA for 5 more orders on a new bar.
Now we declare the global variables of the setindexstyle
In the pairs  array, we set the required currency pairs. The size of the array of 14 elements is specified arbitrarily.
The pairsOpen  array will store the number of open orders for each currency pair. Those. after the 1st opening, all elements in this array should have a value of 1. A value of 2 will mean that a refill has been made.
The openPrice  array contains the open prices of the current bar for each currency pair.
Init () function
Let's move on to the contents of the init () function . This function is launched when attaching an advisor to a trading chart, changing the advisor's settings, or changing a symbol or chart period.
First, we find out if the system uses 5 and 3 decimal places or 4 and 2.
Getting Started Creating an MQL4 Expert Advisor
You can disassemble the finished program - what commands are used, how they work, etc. Or you can start by writing a simple trading algorithm , so that in the future you can impose new commands on the ready-made "backbone" or change the ready-made ones. Those. from simple to more complex.
Also, when creating a trading robot, the advice and recommendations of those traders (programmers) who already have some experience in this business will certainly come in handy. A lot of useful information can be found on the forums of traders using MQL4 or MQL5. There you can get an answer to almost any question related to writing expert advisors and not only.
MQL4 Expert Advisor - Tips from Experienced Programmers
As soon as it becomes possible to test a finished piece of code or function, do it immediately. Later, when the program grows to a large size, it will be more difficult to verify that it is working correctly.
Pieces of code that are often repeated are best served with separate functions.
You don't have to strive to write optimal code . First, create something that works correctly - even if the code is ugly or unwieldy. This is subsequently corrected by code optimization.
There is no need to hesitate to use your own algorithms, despite the availability of ready-made ones or those that seem to you to be more perfect. The priority is on the correctness of the program, which is impossible without understanding how this or that algorithm works. The algorithms you create are more accessible to you and easier to optimize.
It is quite easy to write an indicator or an Expert Advisor knowing the MQL4 language and built-in functions. Another thing is whether everything will work correctly? Debugging a finished program takes a lot of time, because MT4 has neither a debugger nor a program debugger.
The debugging process is reduced to an uninteresting manual review of the EA trade results, viewing the logs and subsequent code correction. If an MQL4 Expert Advisor is simple and has several commands, then all right. But there are also complex MTS (Mechanical Trading Systems) with a huge multi-thousand-line program code.
Therefore, trading programs that actually work should be simple - first of all, for understanding. The higher the chances that the EA will trade correctly according to the given algorithm.
Also, creating an expert is a creative process. It all starts in the head, and only then the lines of the program code get into the file on the computer. Then there is debugging, performance check and program optimization.
Creating an MQL4 Expert Advisor - simplifying the process
There are two ways to simplify your task. The first is to use ready-made blocks (function libraries) created by you or another trader (programmer). The second is the use of special programs.
For the first method, parts of the program code that can be found on the above forums or created by you personally are well suited. The main thing is that you must understand how this piece of the program works. And then it's a matter of technology.
The second method uses alternative programs that simplify the process of creating an MQL4 Expert Advisor. This is something like the Configuration Wizard in Windows applications - each step is described and understandable even for those who are not MQL4 programmers.
As you can see, it is not so difficult. The main thing is to learn the basics of the MQL4 language, and the rest will follow. With a little practice, you will create your own MQL4 Expert Advisor. Then another one, and another ... And now you have new perspectives!