Created
: 2022.10.11
On MetaTrader4 (MT4) / MetaTrader5 (MT5), you use a programming tool called MetaEditor and a dedicated language called MQL4 or MQL5 to create an Expert Advisor (EA).
Here we will look at how to create an Expert Advisor (EA) on MT4/MT5.
Switch between MT4/MT5 tabs to check the steps for each.
Click "Tools" in the menu and select "MetaQuotes Language Editor".
MetaEditor will be launched. Click the "New" button in the menu.
Select "Expert Advisor" and click "Next".
Enter the Expert Advisor (EA) name, author, and link to display. Configure the parameters as necessary and click "Next".
Number |
Item name |
Descriptions |
---|---|---|
1 |
Name |
Enter the name of the Expert Advisor (EA) that you create. |
2 |
Author |
Enter the author of the Expert Advisor (EA). |
3 |
Link |
Enter the link to display. |
The registered name, author, and link information will be displayed on the Expert Advisor (EA) settings window.
Set event handlers, which are the functions that are called if certain conditions are met.
Number |
Item name |
Descriptions |
---|---|---|
1 |
OnTrade |
OnTrade is a function that handles changes to orders, executions, positions, and trade history when a trade is executed. |
2 |
OnTradeTransaction |
OnTradeTransaction is called by the Expert Advisor (EA) when a TradeTranscation event occurs to process the execution result of a trade request. |
3 |
OnTimer |
OnTimer is a function that runs at a specified time interval. |
4 |
OnChartEvent |
OnChartEvent is a function that handles events on a chart. It is executed when an event occurs on a chart, such as creating, deleting, moving, or editing an object. |
5 |
OnBookEvent |
OnBookEvent is a function that handles the changes in the depth of market data. |
Set tester event handlers, which are the functions that are called if certain conditions are met.
Number |
Item name |
Descriptions |
---|---|---|
1 |
OnTester |
OnTester is a function used for formulas to calculate optimization results in the Strategy Tester. |
2 |
OnTesterInit |
OnTesterInit is a function that executes the initialization of the Expert Advisor (EA) when starting optimization in the Strategy Tester. |
3 |
OnTesterPass |
OnTesterPass is a function that executes data processing during optimization by the Strategy Tester. |
4 |
OnTesterDeinit |
OnTesterDeinit is a function that executes data processing after optimization by the Strategy Tester. |
Write an MQL4 program.
MQL4 and MQL5 have some similarities, but they aren't compatible with one another. MQL4 is similar to C, while MQL5 leans more towards C++. Please note that you won't be able to use Expert Advisor (EA) for MT5 in MT4.
After writing the MQL4 program, click the "Compile" button on the menu.
You will get a compile error as shown below if, for example, the syntax of your code is incorrect.
Missing semicolons and using full-width space are the two most common mistakes made when coding. If you get a compile error, please check these first.
After the compilation, the Expert Advisor (EA) created will appear in the designated folder in the Navigator.
Close the MetaEditor and go back to MT4. Check that the Expert Advisor (EA) created is in the Navigator on MT4.
Click "Tools" in the menu and select "MetaQuotes Language Editor".
MetaEditor will be launched. Click the "New" button in the menu.
Select "Expert Advisor" and click "Next".
If you select "Expert Advisor (template)", only the minimum fields will be filled out, such as the name of the Expert Advisor (EA), copyright, parameters, and functions. If you select "Expert Adviser (generate)", it will contain the items mentioned above, as well as the source code of indicators that come with MT5 by default.
Enter the Expert Advisor (EA) name, author, and link to display. Configure the parameters as necessary and click "Next".
Number |
Item name |
Descriptions |
---|---|---|
1 |
Name |
Enter the name of the Expert Advisor (EA) that you create. |
2 |
Author |
Enter the author of the Expert Advisor (EA). |
3 |
Link |
Enter the link of the Expert Advisor (EA) to display. |
The registered name, author, and link information will be displayed on the Expert Advisor (EA) settings window.
Set event handlers, which are the functions that are called if certain conditions are met.
Number |
Item name |
Descriptions |
---|---|---|
1 |
OnTrade |
OnTrade is a function that handles changes to orders, executions, positions, and trade history when a trade is executed. |
2 |
OnTradeTransaction |
OnTradeTransaction is called by the Expert Advisor (EA) when an event occurs to process the execution result of a trade request. |
3 |
OnTimer |
OnTimer is a function that runs at a specified time interval. |
4 |
OnChartEvent |
OnChartEvent is a function that handles events on a chart. It is executed when an event occurs on a chart, such as creating, deleting, moving, or editing an object. |
5 |
OnBookEvent |
OnBookEvent is a function that handles events on the Depth of Market. It is executed when the Depth of Market is updated. |
Set tester event handlers, which are the functions that are called if certain conditions are met.
Number |
Item name |
Descriptions |
---|---|---|
1 |
OnTester |
OnTester is a function used for formulas to calculate optimization results in the Strategy Tester. |
2 |
OnTesterlnit |
OnTesterInit is a function that executes the initialization of the Expert Advisor (EA) when starting optimization in the Strategy Tester. |
3 |
OnTesterPass |
OnTesterPass is a function that executes data processing during optimization by the Strategy Tester. |
4 |
OnTesterDeinit |
OnTesterDeinit is a function that executes data processing after optimization by the Strategy Tester. |
Write MQL5 program.
MQL4 and MQL5 have some similarities, but they aren't compatible with one another. MQL4 is similar to C, while MQL5 leans more towards C++. Please note that you won't be able to use Expert Advisor (EA) for MT4 in MT5.
After writing the MQL5 program, click the "Compile" button on the menu.
You will get a compile error as shown below if, for example, the syntax of your code is incorrect.
Missing semicolons and using full-width space are the two most common mistakes made when coding. If you get a compile error, please check these first.
After the compilation, the Expert Advisor (EA) created will appear in the designated folder in the Navigator.
Close the MetaEditor and go back to MT5. Check that the Expert Advisor (EA) created is in the Navigator on MT5.
Created
: 2022.10.11
Last updated
: 2024.08.28
Nakamaru is a manual production consultant at FINTECS, a company that specializes in creating manuals for their clients.
With a wide range of experience from fintech to entertainment, he presents what user-friendly manuals should be like.
He works with numerous large corporations as an external manual production project manager.
【Business information】
http://www.fintecs.co.jp/profile/
Akira Takagi
Systems engineer, MetaTrader administrator
After graduating from Computer Science at the Uninove, Brazil, in 2014, he has worked on various systems development projects.
He participated as a developer in the launch of forex services in 2019. Since then, he has also been involved in the development of MetaTrader plugins and APIs. He is certified by MetaQuotes as a MetaTrader5 Administrator and active in consulting and advisory as well.
We hope you find this article useful. Any comments or suggestions will be greatly appreciated.
We are also looking for writers with extensive experience in forex and crypto to join us.
please contact us at [email protected].
Disclaimer:
All information and content provided on this website is provided for informational purposes only and is not intended to solicit any investment. Although all efforts are made in order to ensure that the information is correct, no guarantee is provided for the accuracy of any content on this website. Any decision made shall be the responsibility of the investor and Myforex does not take any responsibility whatsoever regarding the use of any information provided herein.
The content provided on this website belongs to Myforex and, where stated, the relevant licensors. All rights are reserved by Myforex and the relevant licensors, and no content of this website, whether in full or in part, shall be copied or displayed elsewhere without the explicit written permission of the relevant copyright holder. If you wish to use any part of the content provided on this website, please ensure that you contact Myforex.
Myforex uses cookies to improve the convenience and functionality of this website. This website may include cookies not only by us but also by third parties (advertisers, log analysts, etc.) for the purpose of tracking the activities of users. Cookie policy