Interval Scheduling Optimiser
Given a list of scored candidate time windows - proposed trading sessions, upcoming news releases worth watching, maintenance slots, or any other set of "pick some, but they cannot overlap" choices - the obvious approach of sorting by score and greedily keeping each window that does not clash with an already-kept one is wrong. A single very high-scoring but long window can block two or more shorter windows that, added together, are worth more than it is; the greedy pass never gets to reconsider a decision once it has made it. This header implements the standard O(n log n) weighted interval scheduling dynamic program instead: sort the candidates by end time, then for each one in turn compute the best achievable total assuming it either is or is not included, using a binary search to find the latest-ending compatible candidate before it. The result is the true optimum, not a locally-greedy approximation - the demo includes a worked case (windows A, B, C) where A alone scores less than B and C combined, and the optimiser correctly rejects A in favour of the pair. Two windows are treated as compatible - non-overlapping - when one ends at or before the other starts; touching endpoints back to back is allowed, not counted as a clash. Notes and limitations - The sort step is a plain insertion sort, O(n^2). This is a deliberate simplicity choice for the tens-to-low-hundreds of candidate windows a session realistically produces; a genuinely large candidate set (thousands of windows) would want a faster sort before the O(n log n) DP proper. - "Non-overlapping" is the only constraint modelled - there is no minimum gap requirement between chosen windows, no maximum count of windows to select, and no per-day/per-category grouping. Callers needing any of those must filter the candidate list, or the result, themselves. - Scores are a plain double and can be negative; a negative-scoring window is never beneficial to include and the DP will naturally exclude it, but the header does not pre-filter negative scores out for you. - Ties (two different selections achieving the exact same total score) are broken deterministically by the fixed "prefer inclusion when include >= exclude" rule in the DP, not left arbitrary - but a different equally-optimal selection can exist for the same input if that matters to your use case.
Script · MT4 · v1.0 · Free
About Interval Scheduling Optimiser
Interval Scheduling Optimiser is a script for MetaTrader 4. MQL source code included.
Files in the download: IntervalSchedulingOptimiser.mqh, IntervalSchedulingOptimiser.txt, IntervalSchedulingOptimiserDemo.mq4.
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: Trading Sessions · News Trading · MT4 Script · MQL4 Source Code