Loading…

Matched Filter Template Detector

A raw dot-product or sum-of-squared-differences comparison between a template and a candidate window is dominated by the window's AMPLITUDE - a large, loosely similar move can outscore a small, precisely shaped match, which makes those simpler scores unreliable for genuine shape detection. This header implements the real fix: normalised cross- correlation (NCC), the same formula behind Pearson's correlation coefficient. MatchedFilterSetTemplate stores a template shape once and precomputes its mean and sum of squared deviations, so repeated scoring does not redo the template's own statistics on every call. MatchedFilterScoreAt then demeans BOTH the template and the current signal window (so overall price level cancels out) and divides by both sides' own spread (so overall move size cancels out too) - what remains is a pure shape- similarity score in [-1, +1]: +1 is a perfect scaled-and-shifted match of the template's shape, 0 is no linear relationship, -1 is a perfect match of the template's mirror image. A window or template with zero variance returns 0.0 rather than dividing by zero, since correlation is mathematically undefined there. MatchedFilterScan runs this across every valid position in a signal in one call, and MatchedFilterFindBest and MatchedFilterCountAboveThreshold turn the resulting score array into a single best match and a simple detection-event count respectively. Notes and limitations - MATCHED_FILTER_MAX_TEMPLATE_LEN (128) is a compile-time constant at the top of the header - raise it there if a longer template is needed. - NCC measures LINEAR shape similarity only - a template and a window related by a non-linear transform (e.g. one is a squared version of the other) will not score highly even if visually related. - A near-flat template or window (variance close to zero) produces a score of exactly 0.0 by design, not a spuriously high or low value. - This is a pure pattern-scoring tool, not a trading signal generator - what threshold counts as "a real match" and what action (if any) to take on one is entirely up to the caller.

Script · MT4 · v1.0 · Free

About Matched Filter Template Detector

Matched Filter Template Detector is a script for MetaTrader 4. It has 5 settings, such as Signal length, Embed position and Embed scale. MQL source code included.

Files in the download: MatchedFilterDetector.mqh, MatchedFilterDetector.txt, MatchedFilterDetectorDemo.mq4.

Settings

SettingDefaultWhat it does
Signal length300Length of the synthetic test signal
Embed position180Index where the known template copy is embedded
Embed scale2.5Amplitude scale applied to the embedded copy (NCC should ignore this)
Embed offset50.0Level offset applied to the embedded copy (NCC should ignore this too)
Random seed99Seed so the synthetic signal is repeatable

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: Buy/Sell Arrows · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria