Loading…

Week Anchored Date Math

Two families of date question keep coming up around contract and event scheduling, and both are easy to get subtly wrong by hand: "which calendar date is the nth Tuesday/Friday/whatever of this month", and "what week number does this date fall in". This library answers both properly rather than approximately. WadmNthWeekdayOfMonth finds the nth occurrence of a weekday in a month by computing that weekday's FIRST occurrence from the month's actual starting weekday (read via MQL4's own TimeToStruct/StructToTime rather than a hand- rolled Zeller's congruence, since the platform's calendar handling is already correct), then stepping by whole weeks. nth=-1 asks for the LAST occurrence instead, the common case for month-end rebalancing dates. WadmExpiryFriday is a named wrapper for the specific nth=3 case, the standard convention for equity index option and futures expiry. WadmIsoWeekOfYear/WadmIsoWeekYear follow ISO 8601 throughout: week 1 of a year is the week containing that year's first Thursday (equivalently, the week containing January 4th), weeks run Monday to Sunday, and the ISO week-NUMBERING year can differ from the plain calendar year for a few days at the very start of January or end of December - December 29-31 can belong to week 1 of the FOLLOWING year, and January 1-3 can belong to the LAST week of the PREVIOUS year. Most home-grown "week of year" code assumes the calendar-year convention instead and gets these boundary dates wrong; this library computes the genuine ISO answer and exposes the week-numbering year separately so the distinction stays visible at every call site rather than being silently assumed away. Notes and limitations - WadmNthWeekdayOfMonth returns 0 for an impossible request (e.g. the 5th occurrence of a weekday in a month that only has four) - always check the return value against 0 before using it. - All calculations are pure calendar arithmetic in server-time terms; this library does not consult a holiday calendar, so an nth-weekday or expiry date it computes can itself fall on a market holiday - combine with a holiday-aware library (e.g. TradingCalendarEngine elsewhere in this catalogue) when that matters. - ISO week 53 only exists in years whose January 1st is a Thursday, or in leap years whose January 1st is a Wednesday - most years only go up to week 52. WadmIsoWeekOfYear returns whatever the calculation produces without special-casing this; it is a natural consequence of the definition, not an error case to guard against.

Script · MT4 · v1.0 · Free

About Week Anchored Date Math

Week Anchored Date Math is a script for MetaTrader 4. It has 1 setting, such as Year. MQL source code included.

Files in the download: WeekAnchoredDateMath.mqh, WeekAnchoredDateMath.txt, WeekAnchoredDateMathDemo.mq4.

Settings

SettingDefaultWhat it does
Year2026Year the monthly expiry/last-Friday table is built for

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