Running MQL

Here is the (simple version) life cycle of MQL5.

All EA is attached to a chart, which is uploaded to the client terminal memory.  Global variables get initialized.  If global variable of the class type has a constructor, then called during initialization of global variables.

The program waits for an event from client terminal, and then one following event-handler is called.


int OnInit()

Init event handler for EA and Indicators. Handles initialization event.


void onDeinit(const init reason)

Deiniti event handler for EA and Indicators.


void onStart()

Start event handler for Scripts.


int OnCalculate(const int rates_total, const int prev_calculated, cost datetime & TIME[], const double &Open[], const double &High[], const double &Low[], const double &Close[], cost long &TickVolume[], const long &Volume[], const int &Spread[])

Calculate event handler for all prices for Indicators.

int OnCalculate (const int rates_total, const int prev_calculated, const int begin, const double &price[])


void OnTick()

NewTick event handler for EA.


void onTimer()

Timer event handler for EA and Indicators.


void onTrade()

Trade event handler for EA.


double OnTester()

Tester event handler for EA.


void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)

Chart event handler for EA.


void onBookEvent(const string &symbol_name)

BookEvent event handler for EA and Indicators.