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

The Dynamic Time Warping Similarity implementation below can become a backtested trading strategy — describe your rules and Quant writes the code.

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

Turn Dynamic Time Warping Similarity into a trading strategy.

Take the implementation from this page into Quant, then build on it, backtest it on real data, and keep refining it in conversation.