$ £ ¥
¥ £ $

How to Structure Expert Advisor

As was already mentioned in our guide on MQL4 program types, an expert advisor is a program, a robot, that executes trades automatically without the need of your intervention. In the majority of the cases, expert advisors are created to trade for you, so that you can open, close, and manage your trades even when you are sleeping or not at your computer. What is the logic behind an expert advisor? How does it work?

In our guide on MQL4 Code Flow in Expert Advisors, Indicators, and Scripts, you could already see how the code is executed for an EA attached to a chart in MetaTrader 4, but it is important to see what the logic behind an expert advisor is in more detail. Here you are going to see what the main steps driving an expert advisor that can trade for you are.


Structure of MT4 EA

An expert advisor can be divided into a few main modules, each one with its specific goal. The main modules are:

  • Initialization
  • Check of the prerequisites
  • Order management
  • Technical analysis with optional order submission/closure
  • Termination
MQL4 Expert Advisor Structure - Main Modules

It is also beneficial to remember how the code flows in a trading robot, so that you can associate the module to a portion of the EA's code.

MQL4 Expert Advisor Structure - Code Functions

Initialization

During the initialization phase, it is necessary to define the variables, functions, and initial data. This part of code is usually contained inside the OnInit() function for the data that isn't changing during the execution of the trading robot. However, there may be cases where some of the initialization is performed inside the OnTick() function.

Initialization can include many tasks:

  • Initialization of variables
  • Initialization of files/logs
  • Issuing alerts/notifications about the start

Check Prerequisites

When you run an expert advisor, it is good practice to have prerequisites to check and to stop or disable the execution in case they aren't met. Adding such checks to the OnTick() function lets the EA execute predefined modules if at some point during the program's runtime, the prerequisites are met. Some examples of prerequisite checks could be:

  • Check the day of the week or time of day if you want to limit your trading to specific periods.
  • Check the spread to limit trading if it is too high.
  • Check your account/margin if it is enough to trade
  • Check how many orders are already open — perhaps, you don't want to have more than X trades on simultaneously.

Some checks may disable the trading functions only while some others may disable both order management and trading.


Order Management

Order management is the module that takes care of open orders. In almost any expert advisor, you want to check the orders that are currently open and assess their status; for example, whether they are in profit or in loss. At this stage you may want your trading robot to modify these orders if some rules are met. The order management module can be run either inside the OnTick() function or inside the OnTimer() function. Some examples of order management are:

  • Trailing stop if you want to move the stop-loss of trades that are moving in favorable direction.
  • Move the take-profit price.
  • Close orders where the condition to stay open no longer subsists.
  • Partially close orders.

Order management may sometimes include some technical analysis too.


Technical Analysis

During this phase, the trading robot studies the market conditions, indicators, price action, chart information, and all other rules you have given it to understand if it is time to open a trade or not. Technical analysis can also trigger a closing of orders when their criteria to stay open aren't not met. Technical analysis is usually performed inside the OnTick() function. Some examples of technical analysis can be:

Technical analysis is, basically, where you define the rules to trade or not to trade. If these rules are met, the EA then proceeds to opening or closing of orders — i.e., it simply opens new orders or closes existing ones.


Termination

Termination is the final module and it usually doesn't do much. Here, you can remove and clean up variables and sometimes include some final task as, for example:

  • Create a report.
  • Send a notification.
  • Close a file.

Termination is executed in the DeInit() function.


Conclusion

Remember that MT4 trading robots work in a loop: check prerequisites, then order management and technical analysis. When this is completed it restarts from the check of the prerequisites again and again until the program is stopped and the termination is executed.

You should also consider that although the modules seem separated, in many cases, they may be partially overlap.

Every time you are going to create an expert advisor try to think about these main modules and what you want them to do. The design and planning of an EA is one of the most important part of the process because a poorly designed trading robot will not perform well.

If you want to save hours of research and coding and if you want to see some professional code, you can have a look at our MT4 Expert Advisor Template. You can even use it to build your own EA!

If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.