Loading…

Matrix Profile Motif Finder

The matrix profile is a time-series analysis technique from modern data mining research (Yeh et al.) that has almost no presence in retail trading platforms, despite being exactly suited to the question traders actually ask: "has this shape happened before, and how unusual is what is happening right now?" For every subsequence of InpMotifLength bars, it finds the distance to the single most similar other subsequence anywhere in a trailing search window, using z-normalised Euclidean distance so shape is compared, not absolute price level. A brute-force implementation, not the STOMP or MASS fast algorithms the original papers use, but the same underlying definition and the same signal. A LOW profile value at a bar means the shape ending there has a close historical twin somewhere in the search window - a motif, a genuinely repeating pattern rather than a subjective one. A HIGH value that is also a local peak means nothing nearby resembles this shape at all - a discord, the matrix profile's formal definition of a statistical anomaly. Both are flagged only once a one-bar confirmation shows the extreme actually held, against adaptive thresholds built from the profile's own rolling mean and standard deviation, so what counts as "unusual" adjusts to the current volatility regime instead of using a fixed distance cutoff that means different things in calm and violent markets. Notes and limitations - This is brute-force nearest-neighbour search, not the fast matrix profile algorithms from the research literature - correct by the same definition, but O(window x length) per bar rather than near-linear. InpWindowBars and InpMaxHistoryBars both trade compute time for coverage. - The trivial-match exclusion zone stops a subsequence from matching itself or its immediate overlapping neighbours; it does not stop two subsequences from genuinely different but visually similar trends from being called a motif. - Motif and discord flags land one bar after the extreme actually forms, the cost of a genuine, non-repainting confirmation. - This flags shape similarity and shape novelty, not a trade signal. It does not size a position, place a stop, or account for spread and slippage.

Indicator · MT4 · v1.0 · Free

About Matrix Profile Motif Finder

Matrix Profile Motif Finder is an indicator for MetaTrader 4. It draws buy and sell arrows on the chart and can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Motif length, Window bars and Max history bars. MQL source code included.

Files in the download: MatrixProfileMotifFinder.ex4, MatrixProfileMotifFinder.mq4, MatrixProfileMotifFinder.txt.

Settings

SettingDefaultWhat it does
Motif length16Subsequence length (bars) the profile is built from
Window bars300Trailing search window each subsequence is compared against
Max history bars2000Hard cap on bars given a profile value on first load
Avg lookback100EMA length used to build the adaptive motif/discord thresholds
Motif z1.0Profile must be at least this many std devs BELOW its own average to flag a motif
Discord z1.5Profile must be at least this many std devs ABOVE its own average to flag a discord
AlertstrueAlert once per closed bar on a new motif or discord

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Indicators.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the indicator from the Navigator onto a chart and adjust the settings.

Download the PDF guide

Tags: Trailing Stop & Break-even · Buy/Sell Arrows · Alerts · MT4 Indicator · MQL4 Source Code

MetaTrader 4 & 5 indicators · PolyMaria