Concept

Dynamic Time Warping Similarity

Dynamic Time Warping Similarity is a Machine Learning concept. The Library holds 1 implementation — a working definition you can pull into Quant.

Top Dynamic Time Warping Similarity indicator

The top custom implementation, built on the original standard Dynamic Time Warping Similarity formula.

1 total

What is Dynamic Time Warping Similarity?

Dynamic time warping (DTW) measures how similar two sequences are when they may unfold at different speeds. A plain Euclidean comparison lines up bar 1 with bar 1, bar 2 with bar 2, and so on, so two price moves with the same shape but different pacing score as dissimilar. DTW instead searches for a warping path that stretches or compresses the time axis of one sequence against the other, then reports the smallest cumulative distance achievable under that alignment. The technique was developed for speech recognition in the 1970s, where the same word spoken quickly or slowly needed to match the same template.

Traders care because chart history rarely repeats at a fixed tempo. A three-week basing pattern and a five-week basing pattern can be the same behavior on different clocks, and DTW is one of the few distance measures that treats them that way. That makes it a natural fit for analog searching: retrieving past windows whose shape resembles the current one, either for visual study or as the distance function inside KNN analog forecasting.

The method has sharp edges. Raw prices must be normalized first, typically by z-scoring each window, or absolute price level dominates the distance. Unconstrained warping can align almost anything with almost anything, so most implementations restrict the path to a band around the diagonal. And a low DTW distance says only that two shapes rhyme; it says nothing by itself about whether the continuation after the historical analog will repeat.

How it's calculated

DTW fills a cumulative cost matrix by dynamic programming over two sequences A = a_1..a_n and B = b_1..b_m:

d(i, j) = (a_i - b_j)^2
D(i, j) = d(i, j) + min(D(i-1, j), D(i, j-1), D(i-1, j-1))
DTW(A, B) = sqrt(D(n, m))
a_i, b_j: values of the two (usually z-normalized) sequences at positions i and j
d(i, j): local cost of matching point i of A with point j of B
D(i, j): minimal cumulative cost of aligning the first i points of A with the first j points of B
n, m: lengths of the two sequences

A Sakoe-Chiba band of width w often constrains |i - j| <= w to prevent degenerate warping and cut the O(n*m) cost.

Some variants use absolute difference for d(i, j) or normalize the final distance by path length; there is no single universal convention.

How traders use it

  • As an analog finder: the most recent k bars are z-normalized and compared by DTW against every historical window, and the closest matches are inspected for what happened next. The output is a study aid, not a signal by itself.
  • As the distance metric inside nearest-neighbor forecasting, replacing Euclidean distance so that analogs with similar shape but different tempo are not discarded.
  • As a grouping tool: windows are grouped by mutual DTW distance to build a library of recurring shapes, sometimes as a preprocessing step before other models.
  • With realistic expectations: DTW similarity is descriptive. Backtests of DTW-based analog systems are prone to overfitting because window length, band width, and normalization all add degrees of freedom.

Dynamic Time Warping vs Related Concepts

Matrix profile: The matrix profile exhaustively computes nearest-neighbor distances for every subsequence of a series, usually under z-normalized Euclidean distance. DTW is a distance function; the matrix profile is a data structure that could in principle use it but rarely does, for speed reasons.

KNN analog forecasting: KNN forecasting is the prediction framework; DTW is one choice of distance inside it. Euclidean KNN is faster but misses tempo-shifted analogs.

Correlation: Correlation compares two series point by point over a fixed alignment and measures linear co-movement. DTW allows the alignment itself to flex, so it detects shape similarity that correlation misses when timing differs.

Concept family

Machine Learning

32 concepts mapped · 32 in the Library

Dynamic Time Warping Similarity FAQ

Why not just use correlation or Euclidean distance to find similar patterns?

Those measures compare bar i with bar i, so a pattern that took 30 bars will not match the same pattern spread over 45 bars. DTW warps the time axis to align the shapes first, which is exactly what chart analogs need.

Does a strong DTW match mean the market will repeat the analog's outcome?

No. It means the shapes were similar, nothing more. Whether outcomes after similar shapes are better than chance is an empirical question that has to be tested out of sample, and results tend to be modest and regime-dependent.

Is DTW too slow for chart tools?

Naive DTW is O(n*m) per pair, which adds up when scanning years of history. Band constraints, lower-bounding tricks, and downsampling make it workable, but for large-scale all-pairs scans most tools switch to Euclidean methods.

Do I need to normalize price windows before applying DTW?

Almost always yes. Without z-normalization or a similar rescaling, windows from different price levels or volatility regimes are dominated by scale rather than shape.

Build Dynamic Time Warping Similarity your way.

Quant writes, tests, and refines it with you — then it runs on LuxAlgo charting or ports to TradingView.