Financial markets are inherently non-stationary, making traditional static rule-based systems prone to decay as regime shifts occur. This article explores an alternative algorithmic framework implemented in MetaTrader 5 (MQL5) using random graph theory. Instead of assuming a rigid market structure, the system models price dynamics as an evolving, adaptive graph whose transition probabilities are continuously re-estimated and validated against an Erdös–Rényi null model to filter out market noise.
State Discretization and K-Step Random Walk Forecasting
The core system discretizes market conditions into distinct vertices based on three normalized, dimensionless metrics: trend strength (EMA spread divided by ATR), volatility compression/expansion (ATR relative to its long-term average), and momentum (RSI). This discretization yields 15 to 45 potential market states. State transitions are captured in a dynamic transition matrix that incorporates exponential decay to discount past market behavior and Laplace smoothing to handle sparse data.
By raising the transition matrix to the k-th power, the Expert Advisor executes a k-step random walk to forecast directional bias over a specified bar horizon. Additionally, normalized Shannon entropy is calculated across the resulting probability distribution to assess forecast confidence, preventing trades when the random walk has mixed and lost informational value.
Structural Verification Using the Erdös–Rényi Null Model
To avoid overfitting to random fluctuations, the system introduces a statistical test using the Erdös–Rényi G(n, p) random graph model. The observed state-transition network is binarized by filtering edges that exceed a baseline density threshold, and its average clustering coefficient is calculated. Monte Carlo simulations then generate hundreds of random graphs of identical size and edge density.
The difference in clustering between the market graph and random graphs is expressed as a z-score. A high positive z-score indicates persistent, clustered market regimes, whereas a z-score near zero suggests that the observed structure is indistinguishable from random noise, serving as a gate to keep the trading system flat.
Position Management via Trade-Outcome Graphs
Complementing state forecasting, the EA constructs a separate trade-outcome graph that models active trades as random walks across R-multiple milestones (0.5R, 1.0R, 1.5R, 2.0R, and 3.0R). The algorithm tracks advancement and absorption (failure) rates at each milestone to determine real-time survival probabilities, triggering early exits when the probability of reaching the next milestone falls below a predefined threshold.
Backtest Findings and Practical Limitations
Backtesting on XAUUSD (M15 timeframe) comparing the adaptive outcome-graph exit against a fixed 3.9R take-profit demonstrated that the fixed target outperformed the graph-based exit across net profit ($2,292 vs. $1,451), profit factor (1.18 vs. 1.10), and maximum drawdown (11.00% vs. 18.93%). While the outcome graph produced higher win rates, it prematurely cut winning trades due to an aggressive probability threshold (0.875), acting effectively like an early exit and diminishing the average winner size.
Key Takeaways
The framework demonstrates the practical value of viewing financial time series through complex network theory. Transition matrices, entropy measurements, and graph clustering provide powerful diagnostics of market regimes and information decay. However, translating empirical survival probabilities into trade management requires a complete expected-value formulation—accounting for remaining payoff size and transaction costs—rather than raw transition probabilities alone.
Mentoring question
How could you reformulate the trade-outcome exit rule to incorporate an explicit expected-value calculation—factoring in risk-to-reward ratio and transaction costs—rather than relying solely on raw milestone survival probabilities?