거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Twitter에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
Experts

Sideways Martingale - MetaTrader 5용 expert

조회수:
1139
평가:
(2)
게시됨:
\MQL5\Files\
trend_detector.onnx (6444.66 KB)
MQL5 프리랜스 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

Backtest EURUSD 01/03/2025 - 20/01/2026 Timeframe M5 (ONNX AI training specifically for M5-M15)

Backtest GBPUSD 01/03/2025 - 20/01/2026 Timeframe M5 (ONNX AI training specifically for M5-M15)


1. 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.


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:

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:

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:

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


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

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 Onnx

Market Structure Expert Advisor use LightGBM (Light Gradient Boosting Machine)

TrendMomentumEA 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 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 EA Duplicate Detector

Allow the EA to determine whether there are duplicate EAs on the chart based on conditions.