Advertisements
$ £ ¥
¥ £ $

MQL4 Code Flow in Expert Advisors, Indicators, and Scripts


Code Flow — MQL4 Programs

Before you start writing any MQL4 program, you must first understand what happens when your program is attached to a chart. This is why it is necessary to have a look at the flow of code and some native functions called by MetaTrader.

MQL4 Code Flow for Indicators and Expert Advisors
Generic Code Flow for Indicators and Expert Advisors

Code Flow in Indicators and Expert Advisors

First, let's have a look at the flow for indicators and expert advisors as they have a very similar one. To understand this process you need to understand the concept of a tick. Generally speaking, when you run or attach an indicator or an expert advisor to a chart, MetaTrader starts the initialization of the code by calling the function OnInit(), the code included in the function is run. Normally, it is initialization of parameters, settings, variables, and initial checks. After the initialization, MetaTrader waits for a tick to run what is usually the core of the program, the task that is executed. For indicators, the function called is the OnCalculate(), while for expert advisors, the function is OnTick().

The code in the respective function is run and the task is executed. When the process is completed, MetaTrader will wait for the next tick to call the function again, entering a loop until the program is stopped or detached from the chart. It is important to note here that when the tick is received, it triggers the call of the function only if the function is not already running. So, if, for example, the code in the OnTick() function is running, and a new tick is received, it will not trigger another call of OnTick().

When the program is stopped or the indicator (or expert advisor) is detached from the chart, then MetaTrader will close the program by calling the DeInit() function, which usually contains a cleanup of variables and chart objects, closing of files, creation of reports, or notifications, and so forth.

Remember that while the program is running, received ticks will trigger the call of OnCalculate()  or OnTick() functions, so the task will be repeated until the platform, the chart, or the indicator/EA are closed.

The following images will help you understand the concept.

MetaTrader Indicators - Code Flow
Code Flow and Functions Called for Indicators
MetaTrader Expert Advisors - Code Flow
Code Flow and Functions Called for Expert Advisors

Expert Advisor — Code Flow Demonstration

It is worth to see an example of a simple expert advisor to see how the code flow works.

After the code below is attached to a chart, for each received tick it will simply print the current Ask price for the chart's currency pair.

#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

// An empty OnInit() function is automatically generated by MetaEditor when you choose to create
// a new expert advisor. Here you can initialize your variables or perform other checks. It is run
// only once when the expert advisor is attached to the chart or is re-initialized (platform restart,
// timeframe switch, chart symbol change, input parameters' change).
int OnInit()
{
   return(INIT_SUCCEEDED);
}

// An empty OnDeinit() function is automatically generated by MetaEdtior when you choose to create
// a new expert advisor. Here you can delete your objects, close files, send notifications, or create
// reports, and so on. It is run only once when the expert advisor is removed from the chart or is
// re-initialized (platform restart, timeframe switch, chart symbol change, input parameters' change).
void OnDeinit(const int reason)
{

}

// This is the core of the expert advisor's code. It will be repeated on every received tick.
// This function is used only in expert advisors. Indicators use their equivalent - OnCalculate().
void OnTick()
{
   // Print the ask price each tick.
   Print(Ask); // Ask (mind the uppercase A) is the predefined variable for current ask price in MT4.
}

The expert advisor was left running for only a few seconds and a few ticks were received during that time:

Demonstration of Expert Advisors' OnTick Function in MQL4 Code Flow
Result of Demo-1 Expert Advisor Being Run and Then Stopped After Few Seconds

Code Flow in Scripts

With MetaTrader scripts, the flow is much simpler as they only run the code once when they are executed and then they just stop. When a script is attached to a chart, MetaTrader calls the function OnStart(), executing the code inside. You can see an example in How to Create a Basic Script to Test Your Code.

MetaTrader Scripts - Code Flow
Code Flow and Functions Called for Scripts

Conclusion

It is important to make sure that you know the flow of the code running as this is a fundamental concept when designing your programs for MetaTrader platform.

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.