Loading…

Behaviour Conformance Suite

"Does this EA actually obey its own risk rules" is normally answered by reading logs by eye after a backtest or a week of demo trading, which misses anything that did not happen to scroll past while someone was watching. This header replays OrdersHistoryTotal() and the current open book against a ConformancePolicy struct instead, so an entire run can be checked mechanically in a few function calls: a lot-size cap, a per-trade risk percentage cap (from stop-loss distance and current balance), a mandatory stop loss on every market order, a cap on simultaneously open positions, and an explicit ban on averaging down. The averaging-down check is the one a naive per-order scan gets wrong, because it cannot be answered from any single order's fields. It needs the OPEN INTERVAL of an earlier same-direction order on the same symbol - from its open time to its close time, or to "now" if it is still open - to overlap a later order's open time, with that later order priced worse than the earlier one. ConformanceCheckAveragingDown() reconstructs those intervals across the whole matching order set first (history plus the current book, unified so a position that is still open when the scan runs is treated correctly as "still open"), then does a pairwise comparison rather than trying to answer the question order-by-order. The risk-percentage check is deliberately honest about its own limitation: MQL4 keeps no record of account balance AT THE TIME an order was opened, only the current balance, so ConformanceCheckOneOrder() computes risk against today's balance and says so explicitly in the violation text rather than presenting an approximation as an exact historical figure. Notes and limitations - The risk-percentage check uses TODAY's AccountBalance(), not the balance at the time each historical order was opened - MQL4 keeps no such record. Treat it as an approximation for a roughly steady account size, not an exact historical figure; every violation message says so explicitly. - The averaging-down scan is a pairwise O(n^2) comparison over the matching order set. This is fine for the order counts a single account normally accumulates (hundreds to low thousands); an account with an extremely long history and a very permissive magic filter will take proportionally longer. - "Averaging down" here means a same-direction, worse-priced order opened while an earlier same-symbol, same-direction order was still open - it does not attempt to distinguish deliberate scale-in strategies from unintended re-entries; set ForbidAveragingDown = false for any EA whose strategy legitimately pyramids or scales into a position. - All checks operate on OP_BUY/OP_SELL market orders only; pending orders that never filled carry no realised risk and are skipped.

Script · MT4 · v1.0 · Free

About Behaviour Conformance Suite

Behaviour Conformance Suite is a script for MetaTrader 4. It has 6 settings, such as Max lot size, Max risk percent and Require stop loss. MQL source code included.

Files in the download: BehaviourConformanceSuite.mqh, BehaviourConformanceSuite.txt, BehaviourConformanceSuiteDemo.mq4.

Settings

SettingDefaultWhat it does
Max lot size1.0Reject any order larger than this, 0 = no cap
Max risk percent5.0Reject any order risking more than this % of current balance, 0 = no cap
Require stop losstrueEvery market order must carry a non-zero stop loss
Forbid averaging downtrueNo same-direction order at a worse price while an earlier one is open
Max open positions5Cap on simultaneously open positions matching the magic filter, 0 = no cap
Magic filter00 = check every magic number

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria