File Change Watcher With Debounce
MQL4 has no operating-system file-change notification API and no function pointers to register a callback with even if it did. This library covers both gaps: Poll() re-reads each watched file's FILE_MODIFY_DATE and FILE_SIZE on every call, and instead of invoking a stored callback it returns the ids and event types that just fired, for the caller to switch() on - the ENUM_FCW_EVENT dispatch pattern stands in for a callback the language cannot express. Watching for change is not the hard part; deciding WHEN a change is finished is. A file being written to slowly (a large export, a log file mid-flush) changes size and modify time on almost every poll until the writer closes it. Reporting the first observed difference would fire on a half-written file. This library only fires once the observed state has been identical across at least InpDebounceMs of consecutive polls - the change is reported exactly once, after it has actually settled, not once per intermediate write. Debounce state is tracked per watched path independently, so one slow file being rewritten repeatedly does not block or delay events firing for any other watched path. Notes and limitations - Polling only - there is no push notification, so Poll() must be called regularly (e.g. from OnTimer) for changes to be noticed promptly. A change happening between two Poll() calls is only detected on the next call. - Detection relies on FILE_MODIFY_DATE and FILE_SIZE. A rewrite that happens to produce the exact same size within the same whole-second modify timestamp is not detectable - this matches the resolution the terminal itself exposes for these properties. - Paths are sandboxed to MQL4\Files\ (or MQL5\Files\ on the native MQL5 build) (or MQL4\Files\ (or MQL5\Files\ on the native MQL5 build)Common\ with useCommon=true), the same restriction as all MQL4 file I/O. - No callbacks are invoked directly - Poll() returns parallel arrays of ids and events for the caller to dispatch itself.
Script · MT4 · v1.0 · Free
About File Change Watcher With Debounce
File Change Watcher With Debounce is a script for MetaTrader 4. It has 1 setting, such as Debounce ms. MQL source code included.
Files in the download: FileChangeWatcher.mqh, FileChangeWatcher.txt, FileChangeWatcherDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Debounce ms | 300 | debounce window in milliseconds |
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