A moving-average crossover EA is the canonical "starter strategy" — cheap to backtest, easy to understand, and famously mediocre on its own because it generates many false signals in ranging markets. This article adds a LightGBM trend filter that the EA consults before acting on a crossover signal: only take the trade if the filter predicts the trend will persist.
The base EA (without filter)
Standard: buy when fast MA crosses above slow MA, sell when it crosses below. We'll keep this exact signal generation — we just wrap it with a yes/no decision from the ONNX model.
Training the trend filter
For each historical MA crossover signal, label whether it would have been profitable (1) or not (0). Use a fixed-horizon exit rule, e.g., 50 bars after entry. Features:
- Slope of slow MA over last 20 bars (proxy for prevailing direction).
- Distance from price to fast MA, normalized by ATR.
- Realized volatility over last 50 bars.
- Time since last crossover (sometimes crossovers cluster — usually bad).
- Higher-timeframe trend direction (H4/D1 MA slope).
Using the filter in MQL5
What to expect from the filter
- Trade count drops 40–60%.
- Win rate improves by 5–15 percentage points.
- Maximum drawdown improves materially — often the biggest practical win.
- Total profit may go up or down depending on whether the filter is removing more losers than winners.
Running this live?
See which prop firms allow ONNX-driven EAs, or compare MT5 brokers for running an ML EA.