Tester Environment Detector
IsTesting(), IsOptimization() and IsVisualMode() overlap in ways that are easy to get backwards from memory: optimisation always implies testing, but testing does not imply optimisation; visual mode is a testing sub-state, not a separate mode; and a plain live or demo chart returns false from all three. Code that special-cases "am I in the tester" by checking only one of the three quietly mishandles the other two - a common bug is skipping expensive per-tick work only when IsOptimization() is true, which still lets thousands of silent single passes run the same expensive work needlessly. DetectTesterEnvironment() reads all three real MQL4 calls once and classifies the result into exactly one of four states via ENUM_RUN_ENVIRONMENT, checking optimisation first since it implies testing by MQL4's own contract. ShouldSkipUiWork() turns that classification into the one decision most programs actually need: skip chart objects, file writes and sound alerts during an optimisation pass or a silent tester pass, since nobody is watching either one, but keep them for a live/demo chart or a visual tester run, where a human is plausibly looking. Notes and limitations - This only classifies the CURRENT running program's own environment - it cannot tell an EA anything about whether some other program on the same chart is being tested or optimised. - ShouldSkipUiWork() is a judgment call about when a viewer is plausibly present, not a guarantee - a visual tester run left unattended still reports "keep UI work on", since MQL4 has no way to detect whether a human is actually looking at the screen right now. - All three underlying calls (IsTesting, IsOptimization, IsVisualMode) are genuine MQL4 built-ins, not approximations - this header only adds the classification and the mutually-exclusive framing on top.
Script · MT4 · v1.0 · Free
About Tester Environment Detector
Tester Environment Detector is a script for MetaTrader 4. It has 1 setting, such as Simulate ui work. MQL source code included.
Files in the download: TesterEnvironmentDetector.mqh, TesterEnvironmentDetector.txt, TesterEnvironmentDetectorDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Simulate ui work | true | Print a line showing what UI work would be skipped here |
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: MT4 Script · MQL4 Source Code