Life cycle of MQL4 program
Hi folks,
Today we are going to discuss a flustering issue for the most of new MQL4 comers; the life cycle of the MQL4 program.
What the first block of code is executed first and what the last block of code is executed, what's the phases of the MQL4 program. What's the different between the life cycle of the expert advisors and script? and a lot of questions about the MQL4 program life cycle.
Let's know first what happen when the MQL4 starts?
When the MQL4 program starts:
The first time you attach the MQL4 to chart the first thing MetaTrader will do is showing the input dialog depending on the type of the program:
Expert Advisors:
In this case you'll get the expert advisor input window (Figure 1) which enable you to set the parameters of the expert advisor if there was any (i.e. take profit and stop loss levels).
Each variable you declare using "extern" keyword will appear in the expert advisor input windows with its default value.
Indicators:
The same as expert advisors, the first time you attach an indicator to chart you'll get the parameters window (Figure 2) which enable you to set the parameters of the indicator, the colors of the indicator lines (Figure 3) and the levels of the sub-window chart (Figure 4).
Each variable you declare using "extern" keyword will appear in the indicator parameters windows with its default value.
The colors are defined using the preprocessor keyword "#property indicator_colorN", for example:
#property indicator_color1 Silver
#property indicator_color2 Red
and the preset levels are defined using the preprocessor keywords "#property indicator_levelN", "#property indicator_levelcolor", "#property indicator_levelwidth" and "#property indicator_levelstyle".


Scripts:
If there were any inputs coded in the script using the extern keyword, they will appear in the script input window (Figure 5) only if you used the preprocessor keyword "#property show_inputs".
If you used "#property show_confirm" preprocessor keyword a confirmation message box will appear before the script input window.
This is the first step MetaTrader will take in the journey of the MQL4 program life. Let's see what will happen nex?
Initialization phase?
The next phase in the MQL4 program cycle is the initialization phase.
Every line of code you wrote inside the block of init() function will be executed in this phase. This phase executed if one of these triggers occurred:
1- The first time you attach the program to a new chart (After the input window appearance).
2- When you start MetaTrader and there were opened chart(s) that hosts expert advisors or indicators. The script is executed only once (to the end of its life cycle) then de-initialized so MetaTrader wouldn't host opened scripts.
3- When you change the period of the chart and the program was hosted on one or more charts (except the scripts).
4- When you change the symbol of the chart and the program was hosted on one or more charts (except the scripts).
5- When you recompile the program in MetaEditor and the program was hosted on one or more charts (except the scripts).
6- When you change one of the program inputs (hitting F7 hot key to open expert advisor input window and right click the indicator then choose indicator properties from the context menu - Figure 6).
7- When you change the account the expert advisors will be initialized.
Unlike the above case, the initialization code is executed only one time during the life cycle of the program and will not be called again till the end of the program.
Execution phase:
After the initialization of the program it will wait for new quotation arrival from the server, every time MetaTrader receives new price quotation from the server it calls start() function and execute all the lines of the code in its block.
Note: The scripts is an exception because its start() function is executed once you attach it to the chart and immediately after the init() function then the script is detached from the chart.
The start() function have to finish its job first before receiving/reacting to new quotations, that means the code of start() function is executed line by line and only when MetaTrader find the return keyword it will be ready to call start() function again with the most new quotation.
If there were any quotations that arrived while the execution of start() function it would be ignored by the program,
Besides the new quotation arrival trigger, the start() function is executed in these cases:
1- For the indicators the start() function is recalled in the case of changing the chart symbol.
2- For the indicators the start() function is recalled in the case of changing the chart period
Note: The start() function will not be run when you open the expert advisor input window, you have to close it to enable MetaTrader to execute start() function again.
The execution phase is the longest phase in the program cycle life and in the most of programs the start() function block code is the most important code.
De-initialization phase?
Every program attached has its end, when the program ends its job the block of deinit() function will be executed.
These are the triggers of the deinit() function:
1- When you shut down MetaTader and the program was hosted on one or more charts.
2- When you close the chart that hosts the program.
3- Before the changing of the period of the chart that hosts the program.
4- Before the changing of the symbol of the chart that hosts the program.
5- When you recompile the program and the program was hosted on one or more charts.
6- When you change the inputs of the program.
7- When you change the account the expert advisors will be de-initialized.
These was the life cycle of the MQL4 program and it's the time to de-initialize the lesson.
Hope you enjoyed it,
Coders Guru

