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.
The ONNX model returns:
A predicted label (not directly used)
A probability vector with three probabilities:
| Variable | Meaning |
|---|---|
| prob_side | Probability that the market is sideways / ranging |
| prob_bull | Probability that the market is bullish (uptrend) |
| prob_bear | Probability that the market is bearish (downtrend) |
These probabilities are extracted as:
The EA feeds 9 engineered features into the ONNX model:
| Feature Index | Description |
|---|---|
| f[0] | EMA200 slope (trend direction & strength) |
| f[1] | Price distance from EMA200 |
| f[2] | ATR value (volatility) |
| f[3] | Candle range normalized by ATR |
| f[4] | Breakout pressure vs previous high |
| f[5] | Candle body dominance |
| f[6] | Day of week |
| f[7] | Hour of day |
| f[8] | Previous candle direction |
These features allow the AI model to detect:
Market volatility
Trend strength
Time-based behavioral patterns
Price structure behavior
A market is considered sideways when:
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
The EA uses Envelopes to detect range extremes.
Price touches or breaks the lower envelope
AI confirms a sideways market
Opens a BUY position
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.
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 )
This is a critical risk control mechanism.
Before adding a new martingale position, the EA checks:
🔒 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
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.
If equity drawdown exceeds a defined percentage:
All positions are immediately closed
Prevents margin call scenarios
| Component | Purpose |
|---|---|
| AI Trend Detector | Classifies market regime |
| prob_side | Allows trading only in ranges |
| prob_bull / prob_bear | Blocks martingale during strong trends |
| Envelopes | Defines range extremes |
| Martingale | Recovery in sideways markets |
| Basket TP | Fast exit after mean reversion |
| Margin SL | Account-level protection |
Market Structure Onnx
Market Structure Expert Advisor use LightGBM (Light Gradient Boosting Machine)
TrendMomentumEA
Automated trend-following EA using EMA, RSI, and Stochastic signals to open trades on the last closed candle with Stop Loss and Take Profit.
Project Template Generator
This script serves as a practical example of how developers can programmatically work with files using MQL5. One of its key objectives is to demonstrate effective project file organization, which is essential for developers working on large-scale systems or aiming to create portable, self-contained projects. The concept can be expanded further and refined with additional ideas to support more advanced development workflows.
EA Duplicate Detector
Allow the EA to determine whether there are duplicate EAs on the chart based on conditions.