How to set up and launch EAs (trading robots) on MT4/MT5?
Setting up EAs on MT4 and MT5. Learn how to create, edit, launch and complete the operation.
How to setup Expert Advisers on MT4 and MT5
Expert Advisors are created using the MetaQuotes Language 4 (MQL4) or MetaQuotes Language 5 (MQL5) trading strategy programming language and the MetaEditor editor.
To launch the Expert Advisor editor, you need to execute the “New” command of the context menu of the “Navigator — Expert Advisors” window, the “Tools — MetaQuotes Language Editor” menu command.
When creating an Expert Advisor, the Expert Advisor Creation Wizard will open automatically, allowing you to quickly start working with a new MQL4/MQL5 program.
In there, as the type of the created object, you must specify “Expert Advisor” and fill in the fields with the required data:
- Name – the name of the adviser;
- Author – the name of the author;
- Link — address of the developer’s site;
- Parameters — list of input parameters of the Expert Advisor. To add a new parameter, you must click the “Add” button; to remove it, click the “Delete” button.
After that, a new Expert Advisor window with the specified parameters opens in the editor.
The file with the source text (*.MQ4 or *.MQ5) of the Expert Advisor is automatically placed in the /EXPERTS folder of the client terminal.
From this moment, you can start writing the Expert Advisor code.
Complete the edit by compiling the program
Upon completion of development, it is necessary to compile the adviser.
To do this, in the Expert Advisor editor, you need to execute the “File — Compile” menu command, press the F5 key or the toolbar button.
As a result of a successful compilation, an executable program file with the *.EX4 or *.EX5 extension is created, which is automatically placed in the /EXPERTS folder.
The list of compiled Expert Advisors can be viewed in the “Navigator — Expert Advisors” window of the client terminal.
If the compilation was not completed successfully, the expert icon will be greyed out.
This means that it cannot be used.
To start editing an existing Expert Advisor from the terminal, you must execute the “Edit” command from the context menu of the “Navigator — Expert Advisors” window.
This will open the EA editor, which will have the source text of the selected Expert Advisor loaded into it.
After changing the source code of the Expert Advisor, you need to recompile it and get a new executable EX4 or EX5 file.
Otherwise, the terminal will use the unchanged, previous version of the Expert Advisor.
How to edit/update Expert Advisors
Before you start using Expert Advisors, you must first configure them.
The operation parameters common for all Expert Advisors are set in the settings window of the client terminal.
This window can be opened by the menu command “Tools — Settings” or by shortcut keys Ctrl+O.
To configure the parameters of Expert Advisors, select the “Expert Advisors” tab.
- Allow automatic trading
- This option allows you to enable or disable trading operations using expert advisors and scripts. If it is disabled, then Expert Advisors and scripts will work, but they will not be able to perform trading operations. This limitation is useful when testing the analytical abilities of programs in real-time (not to be confused with testing on historical data). You can also enable and disable automatic trading using the button on the terminal toolbar.
- Disable automatic trading on account change
- This option is a protective mechanism that disables trading of Expert Advisors and scripts when changing a trading account. It can be useful, for example, when switching from a demo account to a real one.
- Disable automatic trading when changing profile
- Profiles store a large amount of information about the current settings of all charts in the workspace. In particular, profiles store information about attached Expert Advisors. The Expert Advisors included in the profile will start working with the arrival of a new tick. By enabling this option (checking the box), you can prevent Expert Advisors from making trades when changing profiles.
- Disable automatic trading when changing the symbol or period of the chart
- If this option is enabled, then when the symbol or period of the chart to which the Expert Advisor is attached changes, this Expert Advisor will be automatically prohibited from trading.
- Allow DLL import
- Expert Advisors can use DLL libraries to extend their functionality. By checking this box, you can use such libraries without any restrictions. If this option is disabled, no Expert Advisor will be able to use external DLLs. It is recommended to disable import when working with unknown experts.
- Allow WebRequest for the listed URLs
- In the MQL4/MQL5 language, the WebRequest() function allows you to receive and send information from websites using GET and POST requests. To allow an MQL4/MQL5 program to make such requests, you must enable the “Allow WebRequest for the following URLs” option and explicitly manually specify the URLs of trusted sites. For security reasons, this option is disabled by default.
How to launch Expert Advisers
After configuring the general parameters, you can launch Expert Advisors.
To do this, you just need to attach the expert adviser to the chart.
The “Attach to Chart” command of the context menu of the “Navigator — Expert Advisors” window or double-clicking the selected Expert Advisor in the same window with the left mouse button allows you to overlay it on the active chart.
Moreover, using the “Drag’n’Drop” method, you can apply an Expert Advisor to any chart.
At the same time, a window for the individual settings of the adviser will appear on the screen.
On the General tab, you can change the following settings:
- Positions — select the direction of opening positions:
- Long & Short – in both directions;
- Only Long – only for the buy;
- Only Short – only for sell.
- Allow signals – allow/prohibit the adviser to give signals;
- Allow EA to trade — allow/prohibit real-time trading operations. Keep in mind that even if this option is enabled, the EA may be prohibited from trading by general terminal settings.
- Allow DLL import — allow/prohibit the import of functions from DLL files;
- Allow import of external experts — allow/prohibit calling functions from external experts.
- Allow changing signal settings — this option allows/prohibits an MQL4/MQL5 program from subscribing to and unsubscribing from Signals, as well as changing signal settings. Functions for working with the signal database from MQL4/MQL5 programs make it possible to independently analyze the quality of signals, dynamically manage subscriptions, and manage risks. Detailed information about signal management functions can be found in the MQL4/MQL4 Reference.
In the “Inputs” tab, you can change the external variables of the expert.
These include extern class variables.
To change a parameter, double-click on its value with the left mouse button and write a new one.
In this case, you can change the value of each variable or load an already saved set of input parameters (“Load” button).
You can save the current set of external variables using the button of the same name.
The “Reset” button returns all default settings.
In the “General” tab, the parameters specified in the terminal settings are set.
And the parameters specified in the source code of the program are set as input variables.
To confirm the attachment of an Expert Advisor with the specified parameters, you must click the “OK” button. And you can cancel the overlay of an expert by clicking the button of the same name.
After setting up the Expert Advisor, it will be initialized and with the advent of a new tick, the Expert Advisor will start to execute.
Evidence of the attachment of an expert adviser is the appearance in the upper right corner of the chart of the name of the expert and the icon – a smiley.
If trading is disabled in the Expert Advisor settings, then L will appear instead of a smiley. A cross means that all Expert Advisors are disabled.
How to stop running Expert Advisers
To stop the work of an Expert Advisor, it is necessary to remove it from the chart.
To remove an Expert Advisor from a chart, execute the chart context menu command “Expert Advisors — Delete” or attach another Expert Advisor to the chart.
In addition, the adviser can be removed from the chart when changing the profile or template.
- When the client terminal is turned off, the work of all Expert Advisors is terminated;
- When the chart is closed, the Expert Advisor attached to it ends;
- When applying a second adviser, the first one is deleted after confirmation;
- Deleting an Expert Advisor from the “Navigator” window does not terminate the work of the Expert Advisor of the same name superimposed on the chart;