Discussing the article: "Local Outlier Factor: Density-Based Anomaly Detection in Native MQL5"

Check out the new article: Local Outlier Factor: Density-Based Anomaly Detection in Native MQL5.

This article builds a native MQL5 LOF engine without DLLs, ALGLIB or ONNX. Bars are mapped to five standardized features; k‑NN reachability and density produce a scale‑free LOF score, and the alarm level is a moving quantile of the reference cloud. The score gates only new entries in an EMA‑cross EA, with an indicator for charting and a script for independent checks on XAUUSD, EURUSD and GBPUSD.

Most anomaly filters in trading code start with a single threshold on a single number: skip the bar if its range is greater than three ATRs, stand aside if volume is above the 95th percentile. Rules like that work until the market changes gear. A range of two ATRs at the London open is routine. The same range at three in the morning is something else. A global cut-off cannot tell the difference, because it measures how far a bar sits from the whole crowd, not from the bars that actually resemble it. Session-aware thresholds, volatility regimes and other local methods address the same gap too; LOF is one route among several, chosen here for how directly it turns "unfamiliar neighbourhood" into a single number.

Local Outlier Factor (LOF) is a general-purpose way to spot points that sit in a thinner neighbourhood than their neighbours do, and it fits this problem well even though nothing in it was built with trading sessions or market regimes specifically in mind. It scores a point by comparing how densely packed its own neighbourhood is with how densely packed the neighbourhoods of its neighbours are. A point in a thin region next to thick ones scores well above 1. A point in a busy region scores about 1, wherever that region happens to sit in the feature space, so a quiet regime and a wild regime can both be "normal" at the same time.

In this article we build LOF from scratch in native MQL5 using the built-in matrix and vector types. No DLLs, ALGLIB, or ONNX are used. Every closed bar becomes a five-dimensional point. The last 500 bars form a reference cloud, and the newest bar is scored against that cloud without ever being added to it. The alarm level is not a hand-picked number: it is a quantile of the reference cloud's own scores, so it moves with the symbol and the timeframe.

Author: Adedayo David Gbadebo