Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Expert Advisor | BGC Grid EA | MQL5 Release

New free code from MQL5: indicators, EAs, and scripts for traders.

Overview

The EA features three distinct grid algorithms that activate automatically based on detected market regime:

  • BGT (Bi-Directional Grid Trading) — places both buy and sell orders at each level. Designed for ranging markets where volatility dominates drift. This is the default mode.
  • TGT (Trending Grid Trading) — places stop orders in the trend direction only, with no per-trade take profit and basket-level exit. Activates when a trend is detected via the sigma/mu ratio or a CUSUM structural break.
  • MGT (Mean-Reversion Grid Trading) — places counter-trend limit orders only, with take profit toward the anchor. Activates after a failed TGT cycle to capture the post-trend reversion.

Mode selection happens automatically at each cycle restart based on the VolatilityGate (ATR/drift ratio with cooldown) and CPDEngine (frozen-baseline CUSUM structural break detector).


Experience adaptive trading with the Fluid Expert Advisor for MT4/MT5. Dynamic money management and trend detection. Click for info.

Recommended Setup

Setting
Recommended value
Notes
Symbol
XAUUSD (Gold)
Primary development and testing instrument. The EA works on any symbol — ATR-dynamic spacing adapts automatically.
Chart timeframe
M1 (1-minute)
The chart timeframe is for tick-level execution and sync. Regime detection and ATR spacing use configurable higher timeframes (default H4 and D1).
Account type
Hedged Required. The EA places simultaneous long and short positions and uses CloseBy for efficient shutdown.

Important: Always test on a demo account first. Grid trading involves significant financial risk. The mathematical research demonstrates both the potential and the structural limitations of constrained grid systems. Past backtest performance does not guarantee future results.


Key Features

Regime Detection and Mode Switching

  • VolatilityGate computes the sigma_t / mu_t ratio (ATR vs. drift magnitude) on a configurable regime timeframe (default H4) with cooldown-based state transitions.
  • CPDEngine runs a frozen-baseline CUSUM structural break detector to identify abrupt regime changes that the continuous ratio filter may detect too gradually.
  • Three start gates prevent cycle deployment into hostile conditions: CPD block, price-level filter near recent extremes, and optional BGT disable mode.

Adaptive Grid Infrastructure

  • ATR-dynamic spacing recalculates grid width from daily ATR at each cycle restart, so spacing tracks the instrument's current volatility rather than using a fixed pip value.
  • Equity-based dynamic lot sizing (Remark 42 from the thesis) compounds base lot upward during profitable operation and scales down during drawdowns.
  • Mild multiplier or stress-responsive dynamic lot mode for per-level sizing within a cycle.

Cycle Management

  • Equity-based drawdown kill switch (runs every tick) with optional tighter threshold for MGT mode.
  • Profit-target basket close with automatic restart — the variance reset mechanism from the research.
  • Unconditional age-based restart (default 72 hours) — justified by the exponential variance growth proof.
  • Weekend flat close on Friday with delayed restart on Monday — protection against gap risk.

Diagnostics Pipeline

  • CSV logging of per-bar snapshots (30 fields), per-cycle summaries (28 fields), and event records (7 fields).
  • L_t formula computation, snapshot RPR, path-aware L_t, and Theorem 52/53 ruin probability diagnostics.
  • Two companion Python scripts for post-run analysis and cross-run parameter calibration.


Backtest Results

Image for BGC Grid EA

Image for BGC Grid EA

Image for BGC Grid EA


File Structure

The EA consists of one main file, nine header modules, and two companion Python scripts:

MQL5 Source Files

File
Role
Description
GridEA.mq5
Orchestrator
Main EA file — all inputs, OnInit/OnTick/OnDeinit, cycle start/restart policy, weekend rules, block flags, and glue code between modules.
Defines.mqh
Shared types
Enums (ENUM_GRID_STATE, ENUM_GRID_MODE, ENUM_LOT_MODE), structs (GridLevel, GridSession), and constants.
GridCore.mqh
Execution engine
Order placement for all three modes (PlaceLevelBGT/TGT/MGT), sync and re-arm, CloseAllPositions with CloseBy hedging, position recovery.
LotEngine.mqh
Lot sizing
Per-level lot calculation with mild multiplier mode and dynamic shrink-under-stress mode.
LtMonitor.mqh
Risk diagnostics
Theoretical L_t, weighted dollar loss, snapshot RPR, path-aware L_t, Theorem 52/53 ruin probability, expected absorption steps.
ProfitMonitor.mqh
Profit logic
Cycle profit calculation (equity minus cycleStartEquity), target check (percent or dollar), position counting.
VolatilityGate.mqh
Regime filter
ATR and drift computation on regime timeframe, sigma_t/mu_t ratio, cooldown-based favorable/unfavorable state, signed drift for trend direction.
CPDEngine.mqh
Break detector
Frozen-baseline CUSUM — collects baseline window, freezes it, accumulates S_high/S_low, fires on threshold breach, resets accumulators.
StateManager.mqh
Persistence
Saves and loads cycle state via MQL5 global variables. Supports spacing mismatch tolerance for ATR-dynamic mode.
DiagnosticLog.mqh
CSV logging
Buffers BarSnapshot, DiagCycleSummary, and DiagEventRecord rows in memory. Flushes to FILE_COMMON on OnDeinit or OnTester.

Python Companion Scripts

File
Purpose
Description
analyze_backtest.py
Post-run analysis
Loads latest CSVs from MetaTrader common files. Prints 13 text-analysis sections, runs 10 automated bug checks, generates 8 chart PNGs. Supports --date and --no-charts flags.
calibrate_parameters.py
Cross-run calibration
Loads all CSVs across all dates. Runs 6 calibration passes: variance age curve, Theorem 53 sanity check, sigma/mu threshold derivation, profitability zone analysis, expected-steps validation, and multi-timeframe regime detection. Supports --no-mt5 flag.


Input Parameters Overview

The EA exposes 12 input groups. Key parameters are listed below with their defaults. All parameters are documented in the source code and explained in the accompanying article series.

Group
Key parameters
Defaults
Grid structure
InpGridSpacing, InpGridLevels, InpBaseLot
500 pts, 8 levels/side, 0.01 lot
Dynamic spacing
InpUseATRSpacing, InpATRSpacingTF, InpATRSpacingMultiplier
true, D1, 0.12
Dynamic lot sizing
InpUseDynamicBaseLot, InpLotFraction
true, 0.000003
Exit conditions
InpRuinThreshold, InpProfitTargetPercent, InpMaxCycleHours
0.25 (25%), 3.0%, 72 hours
Regime detection
InpRegimeTF, InpMinSigmaMuRatio, InpATRPeriod, InpMAPeriod
H4, 5.0, 14, 20
Mode switching
InpEnableTGT, InpEnableMGT, InpTGTSigmaMuThreshold
true, true, 5.0
CPD
InpUseCPD, InpCPDWindow, InpCPDDelta, InpCPDThreshold
true, 50, 2.0, 15.0
Weekend
InpEnableWeekendClose, InpFridayCloseHour, InpMondayResumeHour
true, 22, 1

Note: Disable free-margin closures if need not be.

Research Basis

The EA is based on the following academic work by Aldo Taranto and Manzur Khan at the University of Southern Queensland:

  • Gambler's Ruin Problem and Bi-Directional Grid Constrained Trading and Investment Strategies — IMFI, 2020
  • Drawdown and Drawup of Bi-Directional Grid Constrained Stochastic Processes — JMS, 2020
  • Application of Bi-Directional Grid Constrained Stochastic Processes to Algorithmic Trading — JMS, 2021
  • Iterated Logarithm Bounds of Bi-Directional Grid Constrained Stochastic Processes — GSA, 2021
  • Bi-Directional Grid Constrained Stochastic Processes and Their Applications in Mathematical Finance — PhD Thesis, 2022

A detailed companion article series explains the mathematical foundations and walks through the implementation of each module which will be published soon.

Build better strategies with RobotFX professional tools – check them out.

70764

Best MetaTrader Indicators + Profitable Expert Advisors