Loading…

Trade Lifecycle Hooks

An EA's logging, alerting and journaling code usually ends up interleaved directly with its trading logic - a Print() call sitting right next to the OrderSend() it describes, an Alert() sitting next to an OrderClose(). TradeLifecycleHooks separates the two: the trading logic only ever calls FireHook(HOOK_ON_ENTRY/EXIT/MODIFY, context) at the moments it already knows those events happened, and every concern that should react to that moment (write a log line, pop an alert, bump a counter, append a journal row) is registered separately at OnInit and runs through one shared dispatch call. MQL4 has no function pointers, so a "hook" cannot literally be a caller- supplied function the way it would be in most languages. Every hook here is instead one row of (event, action, parameter) where action is itself an enum, and FireHook() switches on it - the same enum-dispatch shape EntryTriggerLibrary.mqh and ExitPolicyLibrary.mqh in this catalogue use for the identical underlying reason. Four actions ship out of the box: a formatted Print() log line, an Alert() popup, an incrementing terminal GlobalVariable (useful for a simple entry counter that survives an EA restart), and an appended CSV row in a sandboxed file under MQL4\Files\ (or MQL5\Files\ on the native MQL5 build). The registry is a fixed MAX_LIFECYCLE_HOOKS-slot array, not a dynamic one, because a strategy's hook set is a small, deliberately-authored list decided once at OnInit - not something that should grow unbounded at runtime. RegisterHook() fails loudly (returns -1) once the registry is full rather than silently degrading, and hooks are disabled rather than removed via SetHookEnabled() so a caller's previously-returned index stays valid for the life of the program. Notes and limitations - HOOKACTION_FILE_APPEND opens, seeks to end, writes, and closes the file on every single fire - safe (no handle leak) but not optimised for a hook that fires very frequently; batching writes is left to the caller if that ever becomes a bottleneck. - HOOKACTION_GLOBAL_VAR_INCREMENT uses the terminal's own GlobalVariable store, which is shared across every chart and program on this terminal - pick a name specific enough not to collide with an unrelated program's own global variable of the same name. - The demo disables its own Alert hook (SetHookEnabled) so an unattended test run does not pop a real dialog - re-enable it to see the alert fire in interactive use. - FireHook() runs registered hooks in registration order and does not stop on a hook that fails silently (e.g. FileOpen returning INVALID_HANDLE) - a failed file-append hook simply does nothing that cycle rather than blocking the other hooks registered for the same event.

Script · MT5 · v1.0 · Free

About Trade Lifecycle Hooks

Trade Lifecycle Hooks is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Synthetic events, Global var name and Journal file name. MQL source code included.

Files in the download: TradeLifecycleHooks.mq5.mqh, TradeLifecycleHooks.txt, TradeLifecycleHooksDemo.ex5, TradeLifecycleHooksDemo.mq5.

Settings

SettingDefaultWhat it does
Synthetic events3Synthetic entry/exit pairs to fire
Global var nameTLH_Demo_EntryCountGlobal variable incremented on each entry
Journal file nameTradeLifecycleHooksDemo.csvFile under MQL5\Files\ appended on each entry

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria