In this article, we will talk about MQL5 and what it means. We will also look at how you can use MQL5 in your Forex Trading and how to start creating your own expert advisor/script.
What is MQL5?
MQL5 (MetaQuotes Language 5) is a programming language based on the popular programming language C++ and it is developed by MetaQuotes. MQL5 is the programming language that is compatible with the Metatrader 5 trading platform provided by MetaQuotes. This programming language is used for coding/creating expert advisors (algorithmic trading robots), custom indicators, and custom scripts for trading forex.
Why Use MQL5?
Hands down, MQL5 is the best programming language for creating automated trading robots for trading forex. There are other programming languages such as python and pine script. I have used all of these programming languages, and my favorite programming language for creating algorithmic trading robots is MQL5.
How to Use MQL5
To use MQL5 you first need to download Meta Editor 5 IDE. The Meta editor 5 IDE comes with MetaTrader 5 platform. You can download this platform through a broker or the MetaQuotes website. If you download through a broker you can deposit real money and test your expert advisor in the live market. You can download it through one of our recommended brokers, which is AvaTrade.
MQL5 Basics for Forex Trading
After you download and install MetaTrader 5 platform then you can open the meta editor by clicking on the icon. You can also open the meta editor through the MetaTrader 5 platform as shown below. Also, you can access the IDE by clicking on Tools -> MetaQuotes Language Editor.
After you click on the Icon called IDE as shown in the above figure, it will open the MetaEditor development platform. This is shown below.
Then go to file -> New Project. Here you can select what kind of project you want to build. Usually, it will be an expert advisor, a simple script, or a custom indicator. This is shown in the following figure.
Things You Need to Know to Use MQL5 in Forex Trading
If you are an experienced programmer you will find it easier to learn MQL5 but if you are new to programming it will take quite some time to learn the MQL5 programming language. First, you need to learn basic programming terms such as variables, data types, and functions. There are a lot of prebuilt functions that you can use when coding in MQL5. You can refer to these functions here.
How to Create an Expert Advisor
Now let’s look at how to create an expert advisor. First, go to file -> new project -> choose expert advisor template. Give it a name, “simpleea”. Keep clicking next and don’t tick anything. Then click finish. Your IDE should look like the following. If you look at the figure below there are multiple lines that start with “//”. This is called a comment. Comments will not be executed when you run your program.
As you can see in the above figure. There are functions called OnInit(), On Deinit(), and OnTick(). What each of these functions does is described as follows.
- OnInit() Function – this function is used for initialization. Here you write the code on what happens when the expert advisor is initialized.
- OnDeinit() Function – Here you write the code for what happens when the expert advisor is uninitialized.
- OnTick() Function – Here you write the code for what happens when a new tick is generated. A tick is an event that is characterized by a new price for a particular symbol at a moment in time.
Let’s create a simple alert in the expert advisor. An alert is a message that will be shown on your Metatrader 5 platform screen. To create an alert you have to use the predefined alert function. The alert function takes in a string.
Now, let’s define the string. Write the string as follows, string customAlert = “Hello Traders” in the onInit() function. Here the word “string” is called a datatype to store names. There are many other data types such as int (integer), double, float and etc.
Now enter Alert underneath the custom alert string. While you are typing it, you will see an autocomplete suggestion. Click on it. Also, make sure to put a semi-colon after the alert function. (This is the C++ programming syntax)
The alert function takes in a custom string. This is shown below, the argument (first value) is the string. We can pass our custom string into the alert function as shown in Figure 9.
Figure 8 shows what goes into the Alert function. You can have look at the other predefined functions here.
Then press the compile button as shown below. This complies the code and checks for errors & warnings. If there is an error, it will show this error in the description box which is at the bottom of the meta editor screen.
Then you can click the green start button to see the alert (expert advisor) in action. This is shown below.
Another way to run the expert advisor is to go to the MT5 platform and drag the expert advisor (“simpleea” in this case) with the mouse and drop it on the chart you want the expert advisor to run it. You can find your expert advisor in the “Navigator” section on the left side of the MT5 screen as shown in the above figure. (Figure 12)
Conclusion
MQL5 is a great programming language for creating expert advisors for trading forex. There are quite a few things you need to learn in order to code with MQL5.
I recommend that you learn MQL5 if you want to automate your trading strategy. Not only can you automate your trading strategy with MQL5, but you also can create your own expert advisors or custom indicators and sell them in the MQL5 market.