Trabajo finalizado
Tarea técnica
EA Concept: Grid Martingale with Hedge
This EA for MT5 (MQL5) runs a continuous price-level grid system with independent buy and sell cycles.
It uses manual martingale lot sizes, a hidden profit target, and a Full hedge when price moves too far from the anchor or reaches max negative equity.
No indicators or signals — it starts trading immediately on launch.
🎯 Core Behavior
-
Rule # 1: Always has one buy and one sell base trade active on each grid.
-
Rule # 2: As price moves in one direction, the with-trend side keeps taking hidden TPs at next grid and reopening.
- Rule # 3: The against-trend side builds a martingale basket using predefined lot steps.
- Rule # 4: Once the basket’s total floating profit reaches a set cash or pip target, it closes all trades on that side together.
- Rule # 5: If price keeps trending far enough (past a defined number of grids or equity), the EA opens a full hedge to flatten exposure, then resets the whole cycle.
⚙️ Key Inputs
| Input | Description |
|---|---|
| GridSizePips = 30 | Distance between grid levels |
| LotSizes = "0.01;0.02;0.03;0.06;0.09;0.18;0.33;0.57;0.75" | Manual list of lot sizes per leg; last one repeats if needed |
| Profit$ = 10 | Hidden profit target in account currency (for martingale baskets) |
| ProfitPips = 5 | Hidden pip target from weighted average price |
| HedgeLevel = 10 | Number of grids from anchor before hedge trigger |
| Max Equity = 1000 | Maximum Negative Equity from anchor will trigger hedge |
| MaxSpreadPips = 1 | Spread filter – skip new trades if exceeded |
| MagicNumber = 1995 | Default magic number, user can change |
| SlippagePips = 1 | Max slippage allowed |
| AllowHedgeReset = true | Enables/disables the hedge reset logic |
| AllowNewCycle = true | Master on/off switch for opening new cycles after hedge |
| CommentTag = "GridMartingaleEA" | Order comment prefix |
🧩 Definitions
-
Pip: 0.0001 for standard pairs, 0.01 for JPY pairs.
-
Convert grids to points automatically.
-
Spread check: If spread > MaxSpreadPips, skip new entries but continue managing open trades.
🔁 State Model (per symbol)
The EA tracks:
-
anchor_price : Starting point for the current grid cycle.
-
grid_index : Current grid level relative to anchor.
-
Lists of open buy and sell trades (tickets, lots, prices, grid levels).
-
lot_step_index_buy/sell : Which lot size to use next.
-
hedge_triggered : Flag for when hedge is active.
-
cycle_id : Used in comments for order tracking and recovery after restart.
🧠 Trading Logic Summary
-
On startup (Grid 0)
-
Open one Buy and one Sell (first lot each).
-
No visible SL/TP — handled internally.
-
-
When price moves one grid up:
-
The Buy side (with trend) closes its hidden TP and reopens a new base Buy at the new level.
-
The Sell side adds a new leg (next lot size) at the new grid level.
-
-
When price moves one grid down:
-
Mirror behavior — Sell side takes profit and reopens; Buy side adds new leg.
-
-
Take Profit rules:
-
Base trades close individually on hidden TP (small cash or pip target).
-
Martingale baskets close all legs together when combined profit hits the target (cash or pips).
-
-
Hedge Reset:
-
If price moves HedgeLevel grids away from anchor, EA opens a hedge position equal to total opposite exposure.
-
After that, all orders stay open for manual closure.
-
EA resets cycle (new anchor = current price, clear arrays, start over).
-
-
Limits:
-
Max one buy and one sell per grid level.
-
If spread too high, skip entries.
-
Continue managing existing trades even when new entries are paused.
-
🧾 Technical Notes for Implementation
-
Use SymbolInfoInteger to get pip size/digits.
-
Parse LotSizes string into a numeric array; repeat last value if more legs are needed.
-
Respect broker min/max lot and margin.
-
Handle ECN brokers (no SL/TP on order send).
-
Retry on REQUOTE or PRICE_CHANGED .
-
Identify each order by magic number + cycle_id + grid + side + leg in comment.
-
Manage logic on every tick (or timer for quiet markets).
🧱 Example Flow
-
Grid 0: Buy 0.01, Sell 0.01
-
Price up 1 grid → Buy hits hidden TP → new Buy 0.01; Sell adds 0.02 leg
-
Price up another grid → Buy TP again → new Buy 0.01; Sell adds 0.03 leg
-
Price drops back → Buy side now accumulates legs; Sell side resumes its base TP behavior.
-
If Sell basket profit ≥ $10 (or 5 net pips), close all Sell legs; reset side.
🧩 Summary in One Line
Always one buy + one sell active, build martingale legs only on the losing side, close baskets at hidden TP, and if price drifts too far — hedge Full and restart EA.
On-Chart Summary Display
Add an on-chart status summary that updates automatically while the EA is running.
The summary should display key live metrics for both Buy and Sell sides in a compact, readable format at the top-left corner of the chart.
Display format example:
Behavior requirements:
-
Refresh automatically on every tick (or once per second).
-
Always show the current EA state clearly — even if no trades are open.
-
If either side (Buy or Sell) is running a martingale sequence, label it as “Martingale”; otherwise, “Neutral”.
-
Floating profit/loss should show the combined unrealized P/L of the active martingale basket in account currency.
-
Use consistent decimal precision (2 for lots, 1 for pips, 2 for USD).
-
Optional: color-code values for clarity (e.g., green for profit, red for loss).
Placement and style:
-
Top-Right or Left corner of the chart.
-
Clear, easy-to-read font.
-
Simple layout, no external UI libraries needed.
-
Should not interfere with order execution or EA logic.