Creating An Expert Advisor In MetaTrader

One can create their own automated trading strategy to trade the financial markets on their behalf. Due to the strong influence of typical human emotions, trading in manual mode is quite challenging. Automated trading platforms do not experience this drawback.

A variety of tools are provided by MetaTrader platform for the creation and application of mechanical trading systems (expert advisors). Expert advisors can be created, tested, and debugged using the development environment. Experts are able to completely control trading actions online in addition to being alerted to suggested trading signals.

The goal of this article is to teach newcomers how to create straightforward Expert Advisors in the MQL programming language.

What are expert advisors?

The MetaTrader trading platform’s built-in MetaQuotes Language programming language is used to create trading strategies. It enables you to execute your own trading methods as well as create Expert Advisors to automate trading procedures. Additionally, MQL enables the creation of unique scripts, function libraries, and indicators.

Expert Advisors (EA’s) are software programs created in the MetaQuotes Language (MQL) and used to automate trading strategies and analytical procedures. They make it possible to handle trading activities based on signals received and undertake quick technical analysis of price data. Technical analysis and trading routine tasks can be delegated to expert advisors in their entirety. Any symbol or period can be the subject of analytical or trading activities by a expert advisor, regardless of whether the relevant chart has been opened or not.

Utilizing expert advisors entails:

  • Establishment of an expert advisor: The built-in “MetaEditor” must be used to build and construct an expert advisor. It is a component of the client terminal and serves as an efficient setting for MQL program development.
  • Advanced setup: One must first build up experts before using them. In the client terminal settings, there are working parameters that are universal to all specialists. Additionally, every expert can have their own settings.
  • Launching an expert: One must impose an expert into the chart in order to launch it. When a new tick starts, the expert will begin to act.
  • EA disconnect: When an expert advisor is removed off the chart, it is terminated.

Create an expert advisor

MetaEditor and MetaQuotes Language (MQL) are required to construct experts. The “Create” command of the “Navigator – Expert Advisors” window context menu, the “Tools – MetaQuotes Language Editor” menu command, pressing F4 or the “MetaEditor” button of the “Standard” toolbar are all required to start the expert editing software.

Following that, the Expert Creation Wizard will instantly launch, enabling you to begin working on a new MQL software right away. The “Expert Advisor” must be marked as a certain kind of object to be produced, and the following fields must be completed:

Creating an expert advisor (EA)
Creating an expert advisor (EA)

Developer is the name of the developer; Name is the expert’s name; Link refers to the website of the developer; Inputs is a list of EA inputs. One must click the “Add” button to add a new parameter, and one must click the “Delete” button to remove a parameter.

Following that, the editor’s new expert window with the defined inputs will open. The client terminal’s /EXPERTS folder will immediately save the expert’s source file (*.MQ4). Now is the time to begin writing the expert code.

It is necessary to compile the expert after it has been developed. To do this, use the expert editor’s “File – Compile” command, or click F5 or the toolbar’s Compile button. If the compilation process is successful, an executable program file with the *.EX4 extension will be generated and saved automatically in the /EXPERTS folder. The “Navigator – Expert Advisors” pane in the client terminal allows users to view the list of assembled experts. The expert’s symbol will be gray if it was not successfully compiled. This essentially rules out using this expert.

Changing expert advisors

The “Modify” command from the “Navigator – Expert Advisors” window context menu must be used to begin altering an existing expert from the terminal. The selected expert’s source code will then be displayed in the experts editor. One must recompile the changed expert source code to obtain a fresh executable EX4 file. If not, the terminal will use the non-modified version of the expert from earlier.

Eexpert advisor setup

One must first build up experts before using them. In the settings box for the client terminal, working parameters that are common to all experts are defined. The “Tools – Options” menu command or the accelerating keys Ctrl+O can be used to open this window.

Expert advisor options
Expert advisor options

The “Expert Advisors” tab must be chosen in order to configure expert settings. It has the following parameters available:

  • Allow automated trading – this option enables or disables the use of scripts and expert advisors to execute trades. Scripts and Expert Advisors will function if it is disabled, but they won’t be able to execute trades. This restriction can be helpful for evaluating an Expert Advisor’s real-time analytical skills (not to be confused with testing of Expert Advisors on history data). Using the AutoTrading button on the toolbar, automated trading can also be enabled or disabled.
  • Disable Auto Trading after Account Change – This option acts as a safety measure by blocking trading by Expert Advisors and scripts after Account Change. It comes in handy, for instance, when switching from a demo account to a real one.
  • Disable Auto Trading after changing the profile: Since profiles contain a lot of data about the settings that are now in effect for all charts in the workspace. Particularly, profiles include details about the attached Expert Advisors. When a new tick is generated, the expert advisors that are listed in the profile will begin to work. By turning on this option, a user can prevent Expert Advisors from trading after a profile change.
  • Disable Auto Trading when the charts symbol or period has been changed: When this option is activated, the expert adviser that is associated to a chart will automatically be disallowed from doing trade operations whenever the period or symbol of the chart is changed. Disable Auto Trading When the Charts Symbol or Period Has Changed
  • Permit DLL imports: The use of DLLs by EA’s can increase their functionality (dynamic-links libraries). Such libraries can be used without any restrictions if it is enabled. No expert can use external DLLs if this option is deactivated. When collaborating with unidentified specialists, it is advised to turn off import.
  • Accept WebRequest for the specified URL: Using GET and POST requests, the WebRequest() method in MQL is used to deliver and receive data to websites. Enable this option and manually define the URLs of reputable websites to let a MQL application to send such queries. By default, the option is deactivated for security reasons.

Launching an expert advisor

Experts can be launched once general parameters have been established. Simply affixing the expert to the chart will suffice to achieve this. The “Attach to a chart” option in the context menu of the “Navigator – Expert Advisors” window or a left-click double-click on the chosen expert in the same window allow you to insert it into the currently active chart.

Expert advisor launch
Expert advisor launch

The “Drag’n’Drop” method enables the expert to be inserted into any chart. The expert special settings window will then display.

Expert advisor settings
Expert advisor settings

It is possible in the “Common” tab to:

  • Positions – Determine the position’s opening direction: In both directions, Long and Short; Only Long for buying; Only Short for selling.
  • Enable alerts – turn on/off expert alerts;
  • Disable alert after being hit – stop sending alerts after the initial one;
  • Allow live trading – Enable or disable live trading to allow trading. It should be noted that even if this option is selected, the terminal’s default settings may disable the Expert Advisor’s autotrading.
  • Allow DLL imports – Enabling or disabling the import of functions from DLL files;
  • Allow import of external experts – Enabling/disabling the summoning of functions from external experts; allowing the import of external experts;
  • Allow alteration of Signals settings – this option permits or disallows a MQL application to subscribe to and unsubscribe from Signals as well as modify the signal copying settings. Working with the basis of signals from a MQL application’s functions enables you to manage the subscription dynamically, make your own study of the signal quality, and adjust risks. The MQL Reference offers more thorough information about the functions for manipulating the signals.

The “Inputs” tab allows the expert to modify external variables.

Expert advisor inputs
Expert advisor inputs

These are extern class variables. One must double-click on an input value with the left mouse button and then type a new value to save it. At that point, one can alter the values of each variable or download the previously saved set of inputs (the “Load” button). Using the button with the same name, one can save the current set of external variables.

The “Reset” button resets everything to its default state. The “Common” tab contains the parameters defined in the terminal settings. The program source code’s parameters are also set as inputs. Pressing “OK” will attach the expert with the changed settings to the chart. One must touch the corresponding button to stop the expert imposing.

An expert will be initialized and executed as soon as a new tick is generated after it has been set up. If an expert’s name and a smiley can be seen in the chart’s top right corner, it is attached. The smiley will be replaced by a L if live trading is disabled in the expert settings. A dagger (û) denotes the disability of all specialists.

Expert advisor shutdown

An expert must be eliminated from the chart in order to be neutralized. Before the EA is shut down, it must have been deinitialized. One must use the “Expert Advisors – Delete” command from the chart context menu or add another expert to the same chart in order to remove an expert from it. The expert may also be taken out of the chart when the profile or template are changed.

  • When a client terminal shuts down, all experts do as well
  • When a chart closes, the expert associated with it is also shut down
  • When another expert is imposed, the prior one is afterwards withdrawn
  • The expert with the same name imposed in the chart is not disabled when the expert with that name is removed from the “Navigator” window
  • The client terminal settings that disable experts do not completely disable them. The init() method will still run even when the start() function of each expert is stopped by this option

Final thoughts

Most traders decide they want to automate their trading as much as possible as soon as they begin learning about Forex trading. Being swayed by emotion, being anxious about a losing position, and being greedy when it comes to holding onto a winning deal. Another issue is the requirement to regularly analyse charts and check the positions. A forex robot may be significantly more proficient than a human trader. Day and night, regardless of profit or loss, it abides by the rules.