Sideways Martingale with AI Trend Detection

Sideways Martingale with AI Trend Detection
Download ALL MT5 experts (1558)
YouTube Video Thumbnail



Similar MetaTrader Tools

Sideways Martingale with AI Trend Detection

Info

The Sideways Martingale with AI Trend Detection is a Expert Advisor for MetaTrader 5 that backtest eurusd 01/03/2026 - 20/01/2026 timeframe m5backtest gbpusd 01/03/2026 - 20/01/2026 timeframe m5general overviewsidewaysmartingale is an expert advisor designed to trade sideways (range-bound) markets using a martingale recovery strategy, enhanced with an ai-based trend detector implemented via an onnx model. The EA combines:- AI trend classification (Sideway / Bullish / Bearish)- Envelopes indicator for range-based entries- Controlled martingale position scaling- Profit-based basket closing- Margin-based emergency stopThe core idea is: Trade only when the market is statistically classified as sideways, and avoid adding martingale positions when a strong trend is detected.

Usage

This tool is typically used for trend following strategies across multiple currency pairs.

Platform

This Expert Advisor works exclusively on MetaTrader 5 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL5/Experts folder via File ? Open Data Folder in MetaTrader 5.


How to Install and Use Sideways Martingale with AI Trend Detection

1. Installation: Open the "File" menu, select "Open Data Folder," navigate to MQL/Experts, paste your file, and restart the terminal.

2. Activation: Drag the EA from the Navigator onto a chart, ensure "Allow live trading" is checked in the Common tab, and verify the AutoTrading button is green.

3. Optimization: Right-click your chart, choose "Expert List," click "Properties" to adjust inputs, and save your preferred setup as a set file for future use.

4. Maintenance: Regularly check the "Experts" tab in the terminal window to monitor trade logs and potential execution errors.

Frequently Asked Questions

Q: Why is my EA not opening trades? A: Check the "AutoTrading" button, ensure "Allow live trading" is enabled, and verify your broker allows automated trading on your account type.

Q: Can I run multiple EAs on one chart? A: No, each chart can only host one active EA; however, you can open multiple charts for different currency pairs to run several EAs.

Q: What does the "smiley face" icon mean? A: A smiley face in the top-right corner of the chart indicates the EA is successfully running; a frowny face means it is disabled.

What this tool does

Backtest EURUSD 01/03/2026 - 20/01/2026 Timeframe M5Backtest GBPUSD 01/03/2026 - 20/01/2026 Timeframe M5General OverviewSidewaysMartingale is an Expert Advisor designed to trade sideways (range-bound) markets using a martingale recovery strategy, enhanced with an AI-based trend detector implemented via an ONNX model.

Typical Use Case

This Expert Advisor excels in trend following on MetaTrader 5.

Compatible Platform & Setup

This Expert Advisor works on MetaTrader 5. Place the file in the MQL5/Experts folder and restart the terminal.

Description & Settings

Related: Pro Mart: A Martingale-Based Expert Advisor - another powerful expert for MetaTrader 5 traders.

Backtest EURUSD 01/03/2026 - 20/01/2026 Timeframe M5

Also recommended: Martingale - similar expert with strong performance on MetaTrader 5.

Backtest GBPUSD 01/03/2026 - 20/01/2026 Timeframe M5


General Overview


SidewaysMartingale is an Expert Advisor designed to trade sideways (range-bound) markets using a martingale recovery strategy, enhanced with an AI-based trend detector implemented via an ONNX model.

The EA combines:

- AI trend classification (Sideway / Bullish / Bearish)
- Envelopes indicator for range-based entries
- Controlled martingale position scaling
- Profit-based basket closing
- Margin-based emergency stop

The core idea is: Trade only when the market is statistically classified as sideways, and avoid adding martingale positions when a strong trend is detected.


AI Trend Detector (ONNX Integration)


ONNX Model Output

The ONNX model returns:

- A predicted label (not directly used)
- A probability vector with three probabilities:

These probabilities are extracted as:
float prob_side = prob_data[0].values[0]; float prob_bull = prob_data[0].values[1]; float prob_bear = prob_data[0].values[2];


Feature Engineering (AI Inputs)


The EA feeds 9 engineered features into the ONNX model:

These features allow the AI model to detect:

- Market volatility
- Trend strength
- Time-based behavioral patterns
- Price structure behavior


Sideways Market Detection Logic


A market is considered sideways when:
bool is_sideway = (prob_side >= InpAISidewayThreshold);

Example:

If InpAISidewayThreshold = 0.70

Then at least 70% confidence is required to classify the market as sideways.

No new trades are opened unless this condition is met.


Entry Logic (Scalping in Range)


The EA uses Envelopes to detect range extremes.

Buy Entry if(price_close <= lower[0] && is_sideway)

- Price touches or breaks the lower envelope
- AI confirms a sideways market
- Opens a BUY position

Sell Entry else if(price_close >= upper[0] && is_sideway)

- Price touches or breaks the upper envelope
- AI confirms a sideways market
- Opens a SELL position

This ensures trades are taken only at range extremes during non-trending conditions.


Martingale Recovery Logic


When positions already exist, the EA applies a distance-based martingale:

- New position is opened only if price moves away by a defined pip distance.
- Lot size increases using a multiplier (LotMultiplier).
- Maximum number of trades is limited (MaxTradesInSeries).

Distance Check if(dist >= reqDist)


AI Safety Filter for Martingale


This is a critical risk control mechanism.

Before adding a new martingale position, the EA checks:

If current series is BUY if(s_seriesType == POSITION_TYPE_BUY && prob_bear >= InpAISafetyThreshold) return;

If current series is SELL if(s_seriesType == POSITION_TYPE_SELL && prob_bull >= InpAISafetyThreshold) return;

Meaning:

- If AI detects a strong opposite trend
- And confidence exceeds InpAISafetyThreshold
- Martingale expansion is stopped.

This prevents:

- Martingale during strong breakouts
- Deep drawdowns caused by trend continuation.


Profit Target & Basket Closing


The EA monitors total floating profit across all positions:
if(totalProfitUSD >= TakeProfitTargetUSD)

Once reached:

- All positions are closed.
- Martingale series is reset.
- EA waits for a new sideways setup.

This approach treats all positions as one basket trade.


Risk Management


Margin-Based Emergency Stop if(((bal - eq)/bal)*100.0 >= StopLossByMarginPercent)

If equity drawdown exceeds a defined percentage:

- All positions are immediately closed.
- Prevents margin call scenarios.


Strategy Summary

You may also like: Reversing Martingale EA - excellent alternative for expert users on MetaTrader 5.


Leave your opinion, ask a question, share some knowledge

Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.