Loading…

Injectable Clock

Code that calls TimeCurrent() directly cannot be tested for "what happens at rollover" without either sitting a live account open until midnight or running the full Strategy Tester and hoping the case you care about shows up in the loaded history. Neither is a fast way to check one specific boundary condition. The fix is the oldest trick in dependency injection: stop reading the clock directly, read it through one function, and make that function's source swappable. ClockNow() returns TimeCurrent() in the default, live mode - so existing code needs zero changes to start using it - but after calling ClockUseSimulated(), the same ClockNow() returns an internal clock instead, one that only moves when ClockSetTime or one of the ClockAdvance* functions tells it to. That turns "does my session-close logic fire at exactly 23:59:59 and not a second before" from an overnight wait into three lines: park the simulated clock at 23:58, advance three minutes, check the state. A weekly rollover, a pending order's expiry, a news-blackout window - all of them become a ClockSetTime call to the exact moment that matters, not a hope that the test run happens to cross it. Production code that adopts ClockNow() pays no cost in live mode - it is a direct call to TimeCurrent() with nothing in between - and gains the ability to be driven by a test script whenever that is useful. The header is self-contained. No dependencies, no DLLs, two module-level variables and a handful of plain functions. Notes and limitations - The simulated clock never advances on its own, ever - not per tick, not per OnTimer call. If a test needs it to move, it must call one of the Advance functions or ClockSetTime. That is the entire feature: nothing happens except what the test asks for. - Global state. Both the mode flag and the simulated time are module-level variables, so one terminal process shares one clock. Call ClockUseLive() or ClockUseSimulated() at the start of each test to avoid one case's clock state leaking into the next. - This header only supplies the time source. It has no opinion on what a session, a rollover or an expiry actually means for a given broker or symbol - that logic belongs in the code being tested, driven by whatever ClockNow() currently returns. - TimeLocal() is untouched. Only TimeCurrent()-based logic benefits from routing through ClockNow(); a program that reads TimeLocal() directly is reading the PC clock, which this header does not, and should not, intercept.

Script · MT4 · v1.0 · Free

About Injectable Clock

Injectable Clock is a script for MetaTrader 4. MQL source code included.

Files in the download: ClockAbstraction.mqh, ClockAbstraction.txt, ClockAbstractionDemo.mq4.

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 · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria