Event Handling Functions

MQL4 event handling functions are automatically recognized by the client terminal based on their signature (name, return type, parameter types). These functions must precisely match the predefined event handler specifications.

Core Event Handlers

  • **OnStart()**: Handles the Start event. Automatically generated for scripts. Signature: void OnStart();. An int return type is also permissible.
  • **OnInit()**: Handles the Init event for Expert Advisors (EAs) and indicators. Used for initialization. Signature: void OnInit(); or int OnInit();. An int return type other than INIT_SUCCEEDED indicates initialization failure, triggering OnDeinit with REASON_INITFAILED. Requires #property strict for runtime subsystem analysis.
  • **OnDeinit(const int reason)**: Handles the Deinit event. Called before reinitialization or program unload. Signature: void OnDeinit(const int reason);. The reason parameter indicates the deinitialization cause.
  • **OnTick()**: Handles the NewTick event for EAs only. Triggered by new ticks for the attached symbol. Signature: void OnTick();. Not applicable to indicators or scripts.
  • **OnTimer()**: Handles the Timer event for EAs and indicators. Frequency is set by EventSetTimer(). Signature: void OnTimer();. Call EventSetTimer() in OnInit() and EventKillTimer() in OnDeinit().
  • **OnTester()**: Handles the Tester event after EA history testing. Used for optimization criteria. Signature: double OnTester();. Applicable only to EAs in the tester.
  • **OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam)**: Handles various chart events for EAs and indicators. Signature: void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam);. Events include key presses, mouse movements/clicks, object creation/modification/deletion, chart changes, and custom events (CHARTEVENT_CUSTOM+n). Parameter values vary by event type.
  • **OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spread[])**: Handles the Calculate event for custom indicators. Called when new data is available. Signature: int OnCalculate(...). Returns the number of calculated bars. prev_calculated helps optimize calculations by indicating previously processed bars. Use ArraySetAsSeries() for array indexing.
  • Initialization Return Codes (ENUM_INIT_RETCODE)

  • INIT_SUCCEEDED: Successful initialization.
  • INIT_FAILED: Initialization failed.
  • INIT_PARAMETERS_INCORRECT: Incorrect input parameters.
  • Deinitialization Reasons (Partial List)

  • REASON_INITFAILED: Initialization failed.
  • Chart Event IDs (Partial List)

  • CHARTEVENT_KEYDOWN
  • CHARTEVENT_MOUSE_MOVE
  • CHARTEVENT_OBJECT_CREATE
  • CHARTEVENT_OBJECT_CHANGE
  • CHARTEVENT_OBJECT_DELETE
  • CHARTEVENT_CLICK
  • CHARTEVENT_OBJECT_CLICK
  • CHARTEVENT_OBJECT_DRAG
  • CHARTEVENT_OBJECT_ENDEDIT
  • CHARTEVENT_CHART_CHANGE
  • CHARTEVENT_CUSTOM+n (n: 0-65535)
  • CHARTEVENT_CUSTOM_LAST
  • OnCalculate Parameters

  • rates_total: Total bars available for calculation.
  • prev_calculated: Bars calculated in the previous call.
  • time[]: Array of bar open times.
  • open[], high[], low[], close[]: Arrays of price data.
  • tick_volume[], volume[]: Arrays of volume data.
  • spread[]: Array of spread history.
  • Use ArrayGetAsSeries() and ArraySetAsSeries() to manage array indexing direction.