Dynamic Time Warping
A plain Euclidean or point-by-point distance between two price shapes only works when they are exactly the same length and perfectly time-aligned. A pattern that plays out a few bars faster or slower than a stored template scores as completely different from it even though a human chart reader would call it the same shape. Dynamic Time Warping fixes this by finding the lowest-cost elastic alignment between the two sequences instead of a fixed one-to-one comparison: a standard dynamic-programming recurrence where each cell is the pointwise cost plus the cheapest of the three neighbouring paths that could have reached it (match, insertion, deletion). Left completely unconstrained, DTW can degenerate into pathological warping - a single point in one series stretching to absorb a long run in the other - which is both slow (nothing to prune the search) and a poor similarity measure for two shapes that are supposed to have roughly proportional timing, such as two candle-pattern templates. The Sakoe-Chiba band bounds the recurrence to |i-j| <= window, which both restricts the matrix to a diagonal strip (a real speedup for longer series) and forbids exactly that runaway-alignment failure mode. This header implements the real banded recurrence, not a shortcut approximation - the matrix is genuinely pruned to the band, not computed in full and then filtered. A z-normalisation helper is included because comparing shape rather than raw level is usually what a trader actually wants: two candle patterns with the same shape but different price levels or amplitude otherwise score as dissimilar under a raw absolute-difference cost, which defeats the point of shape matching. Notes and limitations - O(n*m) time and memory in the unconstrained case, O(n*window) with a band set - still quadratic in the worst case, so this is meant for short-to-medium comparison windows (candle patterns, tens of bars), not whole-history matching. - The matrix is stored as one flat 1D array with manual row-major indexing, since MQL4 has no native 2D dynamic array - documented in the source, not a hidden implementation detail. - Distance() returns absolute cost, not a normalised similarity score in [0,1] - use DistanceNormalized() or z-normalise both series first for comparisons across pairs of different scale or length. - No money is at risk in the demo. It compares synthetic numeric arrays only and never sends an order.
Script · MT4 · v1.0 · Free
About Dynamic Time Warping
Dynamic Time Warping is a script for MetaTrader 4. It has 1 setting, such as Band width. MQL source code included.
Files in the download: DynamicTimeWarping.mqh, DynamicTimeWarping.txt, DynamicTimeWarpingDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Band width | 2 | Sakoe-Chiba window for the constrained comparisons |
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