Durable Webhook Dispatcher
A bare WebRequest() call inside an EA's OnTrade or OnTick is the naive version of "notify me when something happens", and it is wrong the moment the receiving endpoint is briefly down, rate-limiting, or the terminal itself loses network for a minute. That one failed call is gone forever - there is no record it was even attempted, and no second chance. This library turns "send a webhook" into "durably enqueue a webhook". WhEnqueue appends the URL and payload to a plain text queue file on disk and returns immediately - the caller's trade logic is never blocked waiting on a network call. A separate WhProcessQueue call (typically from OnTimer, once every few seconds) reads whatever is due, attempts to send it, and for each entry either drops it (2xx response), reschedules it with exponential backoff (WhBackoffSeconds: base delay doubling each attempt, capped at a maximum), or - once it has failed maxRetries times - moves it to an append-only dead-letter file carrying the reason for the final failure, so nothing is ever silently discarded. Because every entry's state lives on disk between calls, the whole queue survives an MT4 restart, a terminal crash, or a VPS reboot mid-outage; an in-memory retry queue would lose everything the instant the process restarted. The queue file is plain, hand-escaped text (one line per entry, '|' delimited, with '\' used to escape a literal '|' or newline inside the payload) rather than FILE_CSV, because a JSON payload routinely contains commas that FILE_CSV's own quoting rules would have to fight rather than help with. Notes and limitations - WhProcessQueue rewrites the whole queue file at the end of every pass. This is not an atomic rename-swap - MQL4 has no file rename function - so a crash during that specific write can in principle leave a truncated queue file. Acceptable for an alerting queue, where the worst realistic outcome is re-sending or losing one in-flight update; not intended as a durability guarantee for a financial ledger. - Enqueued ids are generated from the local time plus a per-run counter - unique enough for this queue's own bookkeeping, not a cryptographic or globally unique identifier. - The dead-letter file is append-only by design; it is a forensic log of everything that ultimately failed, not something WhProcessQueue ever rewrites or prunes. Clear it out manually if it grows large. - WhSendOnce always sends the payload as an "application/json" POST body. A webhook that expects a different content type or method needs a small edit to that one function. - The demo intentionally targets an unreachable URL so it needs no real webhook endpoint and no WebRequest whitelist entry to run and prove the retry/backoff/dead-letter behaviour end to end.
Script · MT4 · v1.0 · Free
About Durable Webhook Dispatcher
Durable Webhook Dispatcher is a script for MetaTrader 4. It has 3 settings, such as Max retries, Base delay sec and Max delay sec. MQL source code included.
Files in the download: DurableWebhookDispatcher.mqh, DurableWebhookDispatcher.txt, DurableWebhookDispatcherDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max retries | 3 | attempts allowed before dead-lettering |
| Base delay sec | 1 | starting backoff delay (kept small for the demo) |
| Max delay sec | 30 | backoff cap |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · MT4 Script · MQL4 Source Code