Loading…

Retention Policy Engine

A long-running EA or export tool that writes its own trade log, snapshot, or report files into MQL4\Files eventually accumulates an unbounded pile of them unless something prunes it. Doing that pruning inline, ad hoc, wherever a file happens to be written is exactly how a project ends up with a bug that silently deletes files nobody meant to touch, or one that never deletes anything until the disk fills up. This header treats a group of related files - matched by one FileFindFirst- style wildcard pattern, e.g. "TradeLog_*.csv" - as a "family" governed by a single policy: a maximum age, a maximum combined size, and a maximum file count, any of which can be disabled by setting it to zero. RpeScanFamily enumerates the family with its real size and modify time from the sandbox. RpePlanSweep sorts oldest-first and decides, purely as data, which files would be removed and why - it touches nothing on disk. Only RpeExecuteSweep actually deletes, and only the files named in a plan the caller already has in hand. That plan/execute split means a caller can log, Alert(), or even show a confirmation dialog with the exact file list before anything is removed, rather than trusting a single combined "clean up now" call. Deletions always work oldest-first and each one relieves ALL three running totals (age, count, size) together, not independently - a file removed for being past its age limit also counts toward satisfying a count or size limit that was checked afterward, so the three limits cooperate rather than each scanning the family from scratch. Notes and limitations Everything operates inside MQL4's file sandbox (MQL4\Files, or MQL4\Files\ (or MQL5\Files\ on the native MQL5 build)Common with useCommonFolder=true) - it has no visibility outside that sandbox and cannot sweep, for instance, the terminal's own history folder. File modify-time resolution on most filesystems is one second, so several files written in the same second sort by scan order (their FileFindFirst enumeration order) rather than a meaningfully finer age ordering between them - this only matters when comparing files created within the same second of each other, which the demo's age-based section works around with a deliberate pause. FileDelete can fail for a file that is currently open elsewhere (another running program, or the same file still open in this one); RpeExecuteSweep counts only the deletions that actually succeeded and silently skips the rest rather than treating a lock as a fatal error, so check its return value against the plan's size if a caller needs to know about a partial sweep.

Script · MT5 · v1.0 · Free

About Retention Policy Engine

Retention Policy Engine is a script for MetaTrader 5. MQL source code included.

Files in the download: RetentionPolicyEngine.mq5.mqh, RetentionPolicyEngine.txt, RetentionPolicyEngineDemo.ex5, RetentionPolicyEngineDemo.mq5.

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: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria