Skip to content
All library documents

Computing Matrix Profiles to Find Repeated and Anomalous Price Patterns

Article MQL5 articles

Summary

This article builds an MQL5 library for the Matrix Profile, a time-series method that measures each fixed-length window against its nearest non-overlapping neighbor. Low distances identify recurring shapes called motifs; high distances identify unusual windows called discords. Z-normalization makes comparisons insensitive to price level and scale, and an exclusion zone prevents adjacent, heavily overlapping windows from becoming trivial matches. Rolling means and standard deviations are computed from prefix sums, while FFT-based sliding dot products and a STOMP self-join provide the distance calculations.

The implementation is checked against direct calculations and the independent stumpy library, and the article describes an indicator that plots the profile and marks discords, plus a demonstration Expert Advisor that gates entries on anomaly scores. An EURUSD example reports profile values and runtime for a selected window and sample. These checks support computational correctness and feasibility, not trading profitability. The author presents the EA as a demonstration and notes the need to interpret anomaly scores carefully; the profile identifies shape novelty or repetition but does not itself specify whether to buy, sell, fade, or follow a move.

Key ideas

  • A Matrix Profile records each window’s nearest-neighbor distance, exposing both repeated motifs and unusual discords.
  • Z-normalization compares shape independently of price level, and an exclusion zone removes trivial matches between nearby windows.
  • Prefix sums provide rolling statistics, while FFT-based MASS and STOMP calculate the required distances efficiently.
  • The implementation is cross-checked against brute-force calculations and stumpy, supporting numerical validation.
  • An indicator and example EA show possible uses, but anomaly detection alone does not establish profitable trade direction.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.