Loading…

Chaos Injection Toolkit

An EA's retry and error-handling logic is normally the least-tested part of the whole program, because it only runs when something has already gone wrong - and on a demo account during development, that almost never happens. Code paths that are never exercised are exactly the ones that break silently the first time a broker really does requote, time out or go off quotes during a busy session. This toolkit provides ChaosMaybeSleep() and ChaosMaybeFail() as small, independent probability rolls: configure a failure rate and a delay rate (both clamped to [0, 1] so a stray value like 50 cannot make every call fail by accident), enable chaos, and every guarded call site now has a configurable chance of sleeping for a random duration inside a set window, or being told to report failure with one of six representative broker error codes (requote, price changed, off quotes, trade timeout, server busy, broker busy) rather than always the same one - so a switch statement handling different error codes differently gets all its branches exercised, not just the first one written. The toolkit never calls OrderSend(), OrderClose() or any other trading function itself. ChaosGuard() only returns a decision - inject a failure here, with this error code - and it is entirely the caller's own trading call that acts on that decision. A "chaos testing" library that silently placed real trades as a side effect of testing error handling would be a considerably worse hazard than the gap it claims to close, so this one deliberately stops at the decision boundary. Notes and limitations - This is a development/testing aid, not a production feature - leave ChaosEnable(false) (the default) on anything that reaches a live account, and treat the demo's MockSendOrder() as a template, not something to wire directly to real trading. - The random source is plain MathRand(); it is not seeded for reproducible chaos runs by itself. Pair it with DeterministicReplayRecorder.mqh's ReplayNextRandom() if a specific injected sequence needs to be replayed exactly. - ChaosSimulatedTimeMs() only changes what the helper itself returns - it cannot and does not alter TimeCurrent(), TimeLocal() or the terminal's actual clock, so it is only useful for code that explicitly reads this value rather than the real clock. - The six-error pool is representative, not exhaustive - add to g_ChaosErrorPool and CHAOS_ERROR_POOL_SIZE in the header if a specific test needs a broker error code outside that set.

Script · MT4 · v1.0 · Free

About Chaos Injection Toolkit

Chaos Injection Toolkit is a script for MetaTrader 4. It has 5 settings, such as Failure rate, Delay rate and Min delay ms. MQL source code included.

Files in the download: ChaosInjectionToolkit.mqh, ChaosInjectionToolkit.txt, ChaosInjectionToolkitDemo.mq4.

Settings

SettingDefaultWhat it does
Failure rate0.30Probability [0,1] a mock call is reported as failed
Delay rate0.50Probability [0,1] a mock call is reported as delayed
Min delay ms10Lower bound of the injected delay window
Max delay ms40Upper bound of the injected delay window
Trials200Number of mock calls to run through the guard

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: Trading Sessions · Arbitrage · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria