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
Start event. Automatically generated for scripts. Signature: void OnStart();. An int return type is also permissible.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.Deinit event. Called before reinitialization or program unload. Signature: void OnDeinit(const int reason);. The reason parameter indicates the deinitialization cause.NewTick event for EAs only. Triggered by new ticks for the attached symbol. Signature: void OnTick();. Not applicable to indicators or scripts.Timer event for EAs and indicators. Frequency is set by EventSetTimer(). Signature: void OnTimer();. Call EventSetTimer() in OnInit() and EventKillTimer() in OnDeinit().Tester event after EA history testing. Used for optimization criteria. Signature: double OnTester();. Applicable only to EAs in the tester.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.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_KEYDOWNCHARTEVENT_MOUSE_MOVECHARTEVENT_OBJECT_CREATECHARTEVENT_OBJECT_CHANGECHARTEVENT_OBJECT_DELETECHARTEVENT_CLICKCHARTEVENT_OBJECT_CLICKCHARTEVENT_OBJECT_DRAGCHARTEVENT_OBJECT_ENDEDITCHARTEVENT_CHART_CHANGECHARTEVENT_CUSTOM+n (n: 0-65535)CHARTEVENT_CUSTOM_LASTOnCalculate 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.
Previous
arrow_back
Exporting functions