Trading Calendar Engine
The forex week does not close at Saturday 00:00 server time and reopen at Monday 00:00 - it closes Friday evening and reopens Sunday evening, at hours that vary by broker. A naive "is it Saturday or Sunday" weekend check is wrong on both ends: it treats several hours of genuinely closed Friday-evening market as open, and several hours of genuinely open Sunday-evening market as closed. TceIsWeeklyClosed models the gap explicitly as a configurable close-day/close-hour to open-day/open-hour window instead. Holidays and half (early-close) days come from a small documented CSV file rather than being compiled into the source - a holiday calendar goes stale the moment it is baked into code, and every market's holiday list differs. The format is deliberately minimal: date, H (full holiday) or S (half day), an optional close hour for half days, and a free-text note. TceIsTradingTime combines all three checks - weekly gap, full holiday, half-day early close - into one answer, and TceNextTradingOpen walks forward minute by minute (capped by maxDaysAhead so it always terminates, even against an empty or misconfigured calendar) to find the next moment trading actually resumes. Notes and limitations - All times are server time (the same clock TimeCurrent() and datetime literals use in MQL4) - convert first if a source calendar is published in a different time zone. - TceNextTradingOpen walks forward one minute at a time up to maxDaysAhead*1440 steps; it returns 0 if nothing is found in that window, which only happens against a badly misconfigured weekly gap or an unreasonably long unbroken holiday stretch. - The CSV loader tolerates '#' comment lines and blank lines but does not validate dates beyond a basic parse failure check - a malformed date field is silently skipped rather than raising an error, so log the returned count and compare it against the file's expected row count when debugging a calendar file. - This library answers "is the market open" for a generic weekly-gap market; it does not model intraday session boundaries for specific exchanges (e.g. equity market hours within a trading day) - that is a distinct concern from the holiday/weekend awareness this header covers.
Script · MT4 · v1.0 · Free
About Trading Calendar Engine
Trading Calendar Engine is a script for MetaTrader 4. It has 5 settings, such as Calendar file, Weekly close dow and Weekly close hour. MQL source code included.
Files in the download: TradingCalendarEngine.mqh, TradingCalendarEngine.txt, TradingCalendarEngineDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Calendar file | TradingCalendarEngineDemo.csv | Sandbox Files\ filename for the sample calendar |
| Weekly close dow | 5 | Weekly close day (0=Sun..6=Sat), default Friday |
| Weekly close hour | 22 | Weekly close hour, server time |
| Weekly open dow | 0 | Weekly open day (0=Sun..6=Sat), default Sunday |
| Weekly open hour | 22 | Weekly open hour, server time |
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