Rule Violation Detector
A trading plan with rules nobody checks is a trading plan that exists only on paper. This script reads back through closed history and tests every trade against four rule types at once: a risk ceiling in money, a session window, an instrument allow or block list, and a cap on trades opened in one calendar day. A trade can break more than one rule, and each broken rule gets its own row in the report rather than the first match hiding the rest. The cost column is always that trade's own net result, shown whether the breach happened to make money or lose it - filtering out the wins would let a trader believe the rule only matters when it goes wrong, which is exactly the belief that keeps a rule from being followed. The risk check is the one place a naive version of this script usually goes wrong. Risk in money is computed from MODE_TICKVALUE and MODE_TICKSIZE against the recorded stop distance, never a flat pip value, so the ceiling means the same thing on a gold or index trade that it means on a standard forex lot. History only stores the stop loss that was in place when the trade closed - if it was trailed, this is the trailed distance, and a trade with no stop loss at all is left out of the risk check entirely rather than scored against a risk of zero, which would flag every stopless trade as a breach of even the loosest ceiling. The daily trade cap counts trades in the order they were actually opened, not the order MT4's history pool happens to store them in, which is close time order and can put a short trade opened later ahead of a longer one opened earlier that day. History is sorted by open time first so the count reflects what really happened through the trading day. Notes and limitations - Market trades only. Pending orders and balance operations are not checked - there is no risk or session for either to violate. - The risk check uses the stop loss recorded at close time. A trade closed after its stop was removed is skipped by this rule, not scored as zero-risk. - Session and daily-count checks use server time, which the trader's own local time is not the same as. Confirm your terminal's server time before setting the window. - Only what MT4's Account History tab has loaded is tested. Choose "All History" first if a known bad trade is missing from the report. - History is sorted with a simple insertion sort, appropriate for the hundreds to low thousands of rows a typical account accumulates; a multi-decade history with tens of thousands of trades would notice the extra time.
Script · MT4 · v1.0 · Free
About Rule Violation Detector
Rule Violation Detector is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 10 settings, such as From date, To date and Max risk money. MQL source code included.
Files in the download: RuleViolationDetector.ex4, RuleViolationDetector.mq4, RuleViolationDetector.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| From date | 0 | From close time (1970.01.01 = no lower bound) |
| To date | 0 | To close time (1970.01.01 = up to now) |
| Max risk money | 0.0 | Risk ceiling per trade, account currency (0 = do not check) |
| Session start hour | 7 | Allowed session start, server time hour (0-23) |
| Session end hour | 21 | Allowed session end, server time hour (0-23) |
| Check session | true | Enable the session rule |
| Allowed symbols | Comma-separated whitelist (empty = every symbol allowed) | |
| Blocked symbols | Comma-separated blacklist (empty = none blocked) | |
| Max trades per day | 0 | Trades beyond this count on one calendar day are flagged (0 = do not check) |
| Delimiter | DelimiterComma | CSV column separator |
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: Trading Sessions · Alerts · MT4 Script · MQL4 Source Code