Loading…

Interval Tree For Time Windows

A trading session filter, a news blackout guard and a holiday calendar all reduce to the same question: given a timestamp, does it fall inside any of a known set of intervals? The naive answer is a loop over every interval on every tick. For a handful of sessions that is fine. For a calendar carrying a year of scheduled news releases it stops being fine. This is not a textbook node-pointer interval tree with rotations. A real self-balancing augmented BST needs a rotation to rewrite a child's parent pointer from a call operating on the child - a pointer-to-pointer update - and neither MQL4 nor MQL5 has a reference-to-pointer parameter to express that with. Chasing the textbook shape here produces an unbalanced tree with none of the guarantees, for more code than the alternative below. The alternative: sort the intervals by start once, then keep a running prefix maximum of their end times. A point can only be covered by an interval whose start is at or before it, and among those, the largest end time is exactly the prefix maximum at the binary-search cutoff. If that maximum is still short of the point, nothing covers it - not one interval needs to be checked individually. This is the standard technique for STATIC stabbing queries: build once, query many times. That is exactly the shape of a session or holiday calendar - assembled once at OnInit, then asked thousands of times as ticks arrive. Adding an interval after the tree has been built simply marks it dirty; the next query rebuilds lazily, so callers never have to remember to call Build() themselves. Sorting uses insertion sort, which is the wrong choice for a general-purpose sort and the right one here: calendars run to tens or low hundreds of entries, the sort runs once per change rather than once per tick, and insertion sort is a dozen lines with no recursion and nothing subtle to get wrong. Contains() answers the yes/no question in guaranteed logarithmic time. FindBlocking() answers which interval, which needs one extra backward scan bounded by how many intervals happen to overlap that exact point - small for any calendar that is not itself pathological. Notes and limitations - This is a STATIC structure. It is built for "assemble a calendar, then query it many times", not for a stream of intervals that changes every tick. Adding intervals is cheap; each Add() just marks the tree dirty, and the sort cost is paid once at the next query, not once per Add(). - FindBlocking() returns exactly one covering interval when several overlap the same point. Which one comes back depends on scan order, not on any notion of priority. Keep blackout calendars and session calendars in separate CIntervalTree instances if overlap priority actually matters to the strategy - the demo shows this exact situation at 14:30, where a probe sits inside two sessions and a blackout simultaneously. - Intervals are treated as closed (start and end both inclusive). - Sorting is O(n^2) insertion sort. Fine for the tens to low hundreds of entries a real calendar holds; not a choice for a general-purpose sort. - No removal by value - call Clear() and rebuild the calendar if entries need to change, which matches how session and holiday calendars are actually maintained in practice (reloaded, not edited in place).

Script · MT4 · v1.0 · Free

About Interval Tree For Time Windows

Interval Tree For Time Windows is a script for MetaTrader 4. It has 2 settings, such as Blackout minutes before and Blackout minutes after. MQL source code included.

Files in the download: IntervalTree.mqh, IntervalTree.txt, IntervalTreeDemo.mq4.

Settings

SettingDefaultWhat it does
Blackout minutes before15News blackout: minutes before the event
Blackout minutes after15News blackout: minutes after the event

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

MQL4 & MQL5 scripts · PolyMaria