Sideways Martingale
This professional-grade solution for MetaTrader 5 helps traders achieve greater efficiency in their daily workflow. This Expert Advisor serves as automated trading software. It is utilized to monitor financial markets and execute trades based on predefined algorithmic rules, enabling precise position management without the need for constant manual oversight.
How to Setup and Use Sideways Martingale
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.
Description & Settings
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. 2. 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]; 3. 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
4. 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 5. 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.
6. 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) 7. 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
8. 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
.
9. 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
10. Strategy Summary