Loading…

Nearest Neighbour Pattern Forecast

"This looks like a pattern I have seen before" is a real trading instinct, but doing it rigorously by eye across years of charts is not realistic. This indicator automates the comparison: at every closed bar it takes the last InpPatternLength bars of returns, z-normalises them into a shape, then searches back through InpSearchBars of history for the K nearest shapes by Euclidean distance - a straightforward k-nearest-neighbours search, not a black box. For each of the K matches found, it looks up what the price actually did over the following InpHorizon bars after that historical match ended, and averages those outcomes into a forecast return, plotted with a dispersion band built from the spread between the individual matches. A tight band means the K best historical analogues agreed with each other; a wide one means the "nearest" matches were not very similar and the forecast should be weighted accordingly. The result is non-repainting by construction, not by patching: every neighbour used to build a forecast must have its own outcome already fully resolved further in the past than the bar being forecast, so nothing here can ever depend on data that had not happened yet when a value was plotted. A minimum bar separation between accepted matches also stops the K nearest neighbours from collapsing into K overlapping copies of the same one move. Notes and limitations - This is a statistical analogue search over the SAME symbol and timeframe it is attached to; it finds shapes similar to recent price action in this market's own history, not a general market-prediction model. - A tight dispersion band means the K matches agreed with each other, not that the forecast is guaranteed correct - shape similarity is not causation. - InpSearchBars and InpMaxHistoryBars both trade compute time for match quality; raising either increases attach time roughly proportionally. - This outputs a forecast return, not a trade signal. It does not size a position, place a stop, or account for spread and slippage.

Indicator · MT5 · v1.0 · Free

About Nearest Neighbour Pattern Forecast

Nearest Neighbour Pattern Forecast is an indicator for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Pattern length, Horizon and Neighbors. MQL source code included.

Files in the download: KnnPatternForecast.ex5, KnnPatternForecast.mq5, KnnPatternForecast.txt.

Settings

SettingDefaultWhat it does
Pattern length8Bars in the shape being matched (as returns)
Horizon5Bars ahead the matched outcome is measured over
Neighbors8Number of nearest historical matches averaged (K)
Search bars300How far back each forecast searches for candidate matches
Max history bars2000Hard cap on how many closed bars get a forecast computed on first load
Min separation4Minimum bar gap enforced between accepted matches, avoids near-duplicates
AlertstrueAlert once per closed bar when the forecast changes sign

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Indicators.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Alerts · MT5 Indicator · MQL5 Source Code

MetaTrader 4 & 5 indicators · PolyMaria