Loading…

Trading Time Duration Calculator

A plain (closeTime - openTime) subtraction on a trade held from Friday evening to Monday morning reports roughly 60 hours, when the market was only open for a few of them either side of the weekend. Every average- holding-time, trades-per-hour or time-in-market statistic built on that plain subtraction is silently inflated by however many weekends the sample happens to straddle - and because that inflation is not constant (a Monday-to-Tuesday trade has none, a Friday-to-Monday trade has two full days of it), it corrupts comparisons between trades, not just the absolute numbers. A strategy that trades mostly short intraday holds and one that occasionally holds over a weekend cannot be compared fairly on wall-clock duration alone. TradingSecondsBetween walks day-by-day between the two instants, clips each day to a configurable TradingCalendar (which days of the week are trading days, and what local session window each trading day allows), and sums only the seconds that actually overlap a trading session - correct for a span that starts or ends mid-session, not just for whole-day spans. The default calendar is Monday-Friday, 24-hour session, typical for FX; set a narrower sessionStart/sessionEnd for a market with fixed trading hours (e.g. a futures session), or mark additional days closed for a market that does not trade every weekday. TtdTradingFraction expresses the same idea as a ratio - what fraction of the wall-clock span was actually trading time - useful for a single number that quantifies how much a given holding period was inflated by non- trading time. Notes and limitations - Times are treated as local to whatever timezone the input datetimes are already expressed in (typically broker/server time). This library does no timezone conversion of its own - see TimezoneRulesLite.mqh, a separate submission in this catalogue, for converting between zones first if needed. - A single session window per trading day is supported. A market with a split session (e.g. a lunch break) needs two separate TradingSecondsBetween calls, one per sub-window, summed by the caller. - The day-by-day walk is capped at roughly 55 years as a safety guard against an unbounded loop on a corrupted or accidentally huge input range; no realistic holding-time query approaches that span. - toTime <= fromTime returns 0 rather than a negative duration or an error - callers passing arguments in the wrong order get a silent zero, not a crash; check the order if a report unexpectedly reads 0 throughout.

Script · MT5 · v1.0 · Free

About Trading Time Duration Calculator

Trading Time Duration Calculator is a script for MetaTrader 5. It has 1 setting, such as Scan history. MQL source code included.

Files in the download: TradingTimeDuration.mq5.mqh, TradingTimeDuration.txt, TradingTimeDurationDemo.ex5, TradingTimeDurationDemo.mq5.

Settings

SettingDefaultWhat it does
Scan historytrueAlso report trading-hours duration for closed trades

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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 · Time Filter · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria