Last time, we confirmed that MQL5 is a "programming language for making MetaTrader 5 execute processes you have designed yourself."
So, where exactly do you write this MQL5 code?
That is where MetaEditor comes in. We won't be creating any difficult programs this time. We will proceed as far as opening MetaEditor, writing code, and compiling it.
MetaEditor is the development environment for writing MQL5
MetaEditor is a development environment for creating and editing MQL5 programs.
If MetaTrader 5 is the place where you view charts and trade, MetaEditor is the place where you create the programs that run behind the scenes.
It is easier to understand if you separate their roles.
MetaTrader 5
→ The place where you actually use EAs and indicators
MetaEditor
→ The place where you write MQL5 code
You do not enter MQL5 directly into the MetaTrader 5 screen where you normally view charts.
When you write code, you move to MetaEditor.
Trying to open MetaEditor
There are several ways to open MetaEditor from MetaTrader 5.
The most common way is to launch MetaEditor from the MetaTrader 5 menu.
You can also launch it using the F4 key on your keyboard.
When MetaEditor opens, the atmosphere is quite different from your usual chart screen.
It may look like there are many items at first, but you do not need to memorize them all.
The things you will use first are mainly:
creating files
writing code
Compile
View errors
These are the four.
With just this, you can start learning MQL5.
Creating your first EA file
This time, we will create one practice EA.
Start creating a new file in MetaEditor and select Expert Advisor (template).
For the name, for example, do the following.
HelloMQL5
Once created, MetaEditor will prepare the basic code for the EA.
Although there are some differences in the generated content depending on the environment, you do not need to understand every line from the beginning.
This time, we will simplify the code to a form that is easy for you to check yourself.
First, use OnInit to display text
Let's try changing the created EA to the following code.
int OnInit()
Print("Hello MQL5");
return(INIT_SUCCEEDED);
Several new terms appear here.
However, you only need to remember two points for now.
OnInit() is a process called when the EA is initialized.
And,
Print("Hello MQL5");is a process that outputs text to the log.
In other words, in this program, when the EA is initialized,
Hello MQL5
it is trying to display.
You can look at the detailed mechanism of OnInit() again when we cover event handlers in the future.
Compile after writing code
In MQL5, writing the code is not the end of the process.
Next, compile it.
When you execute the compile operation at the top of MetaEditor, it checks if there are any problems with the MQL5 code you wrote.
What is important here is that
compilation is a separate task from "saving"
.
If it were a document, you could just save it and read it as is, but in MQL5, you need to convert the code into a format that MetaTrader 5 can handle.
That process is compilation.
The image is
MQL5を書く ↓ コンパイルする ↓ MetaTrader 5で使える状態になる
the following flow.
What is displayed if there are no errors?
If there are no syntax problems in the code, it will show that there are no errors in the results at the bottom of MetaEditor.
What you should be careful about here is that
no compilation errors does not mean the program's logic is also correct
.
For example,
even a program that says "I wrote the conditions to sell when I intended to buy"
might pass compilation itself if it is correct as MQL5 syntax.
What I mainly check during compilation is
"whether it is valid as MQL5 code"
.
Whether the program behaves as intended is something I check separately by actually running it.
This distinction will be quite important when you start creating EAs from now on.
Trying to intentionally cause an error
Since we are at it, let's practice looking at errors.
In the code from earlier,
Print("Hello MQL5");try deleting the semicolon at the end of it.
Print("Hello MQL5")Please try compiling in this state.
Since it is no longer valid MQL5 code, an error will be displayed at the bottom of MetaEditor.
You might feel anxious when an error appears, but in programming, the error display itself is a clue.
Rather than having no information at all,
"the way this part is written is incorrect"
being told this makes it easier to find the cause.
Once you have confirmed it, put the semicolon back.
Print("Hello MQL5");Then compile it again.
By doing this,
change → compile → view error → fix
getting used to this flow will be very helpful in the future.
Try running it in MetaTrader 5
Once you have compiled it, return to MetaTrader 5.
When you apply the created EA to a chart, OnInit() is executed during the EA's initialization.
In this code, since we have written Print() inside it, you can check the output from the logs.
Hello MQL5
You can confirm the output like this.
Once you have done this,
Write code yourself
→ Compile
→ Run in MetaTrader 5
→ Check the results
you have experienced the basic cycle of MQL5 development once.
The code is only a few lines long, but this flow will not change even when you start creating larger EAs in the future.
Think of the place where you write code and the place where you run it separately
At first, you might get confused about whether you are operating MetaTrader 5 or MetaEditor.
In such cases, please think of the roles as divided into two.
In MetaEditor, you
create.
In MetaTrader 5, you
run and check.
Of course, it gets a little more complicated in actual development, but this is enough for the beginning.
For example, when a problem occurs while creating an EA,
Is the code itself wrong?
Is the result of the execution wrong?
makes it easier to think about them separately.
For now, it is enough if it works this far.
The code created this time does not perform automated trading.
It does not even retrieve prices.
Even so, it is a big step in learning MQL5.
int OnInit()
Print("Hello MQL5");
return(INIT_SUCCEEDED);
Enter this code yourself, compile it, and confirm 'Hello MQL5' on the MetaTrader 5 side.
Please try this much first.
After that,
Print("Hello MQL5");to,
Print("My first EA");changing it like this is also good practice.
Even just changing the string,
you can get a feel for how changing the code changes the execution result.
you can get a feel for it.
MQL5 is not something you can easily master just by looking at the code.
Write a little in MetaEditor, compile it, and see the results in MetaTrader 5.
True MQL5 learning begins by repeating this small back-and-forth process.