MQL5 Expert Advisor Modification: Pyramid and Re-Entry Trade Manager

Spezifikation

General Idea of the Trading Strategy / EA Functionality

This order is for modifying and completing an existing MQL5 Expert Advisor (EA) that acts as a trade manager for user-opened "master" trades ( trades opened by another EA or user that has a stop loss as at trade open, it could be a stop or limit order or even a market execution). The EA does not generate trading signals or open/modify/close master trades itself—masters are always opened by the user or another EA. Instead, the EA automates pyramid pending orders (additional entries at stepped levels), manages their fills and closures, arms re-entries when pyramid positions close at a loss, and handles re-placement of re-entry pendings when price retraces. The goal is to create a "pyramiding with re-entry" structure that adds positions in the direction of the master trade while providing risk management through breakeven trailing and lot adjustments based on losses.

The underlying concept is a position management system for trend-following or momentum trades: after a master is opened, the EA builds a pyramid of pending stop orders at calculated levels beyond the master's entry (spaced by a multiple of the master's stop loss distance). If a pyramid position closes (e.g., at SL), and it's a loss, the EA "arms" a re-entry mechanism at that level. When price retraces to the prior level, it places a pending order to re-enter at the original pyramid level, with reduced lot size to account for the loss. This allows recovery from drawdowns while limiting risk through lot reduction and breakeven moves.

Key abstractions:

  • Master Trade: A user-opened position that the EA monitors but never touches.
  • Pyramid Levels: Stepped entry prices calculated from the master's SL distance, where pending stops are placed.
  • Re-Entry: An armed pending order mechanism triggered by price retrace after a pyramid loss, allowing limited re-attempts at the same level.

This is not a full trading robot but a management overlay. The existing code (provided as .mq5 source) already implements core features like master detection, pyramid placement, fill mapping, persistence via GlobalVariables, and fallbacks for broker issues (e.g., reticketing). However, specific bugs and incomplete parts (detailed in "What Needs to Be Done") prevent reliable re-entry arming and triggering, especially across restarts.

For visualization, refer to the following conceptual flow (text-based; developer can request charts if needed):

  1. User opens Master (e.g., BUY at 1.1000, SL at 1.0950 → slDist = 50 pips).
  2. EA places Pyramid Level 1: BUY_STOP at 1.1000 + (50 * PyramidingStepR), e.g., 1.1025 if StepR=0.5.
  3. On fill: Map position to master/level.
  4. If pyramid closes at loss: Arm re-entry with trigger at master entry (1.1000), entry at 1.1025, reduced lot.
  5. When price >= trigger (1.1000): Place pending BUY_STOP at 1.1025 (shifted if needed for stops level).

Recommended reading for context: MQL5 articles on OnTradeTransaction, GlobalVariables for persistence, and handling broker reticketing.

Terms and Definitions

To ensure clarity, here are key terms used in this specification. All prices are normalized using NormalizeDouble(price, _Digits) for storage and comparisons. Use bold for these terms in code comments.

  • Master Trade: A position opened externally (user/other EA) with MAGIC_TEST magic number that has a stop loss as at trade open, it could be a stop or limit order or even a market execution. Identified by ticket/position ID. EA monitors its open_price, sl, tp, lot, direction (is_long=true for BUY).
  • Pyramid Pending Order: A pending STOP order (BUY_STOP for long masters, SELL_STOP for short) placed at calculated level_price. SL/TP inherited from master distances.
  • Level (N): 1-based integer for pyramid steps. Formula: level_price(N) = master_open + sign * (slDist * PyramidingStepR * N), where sign=+1 for BUY, -1 for SELL; slDist = |master_open - master_sl|.
  • Re-Entry: A structure armed after a pyramid loss, containing triggerPrice (prior level or master open), entryPrice (closed level's price), lot (reduced), remaining attempts (up to MaxReentriesPerLevel).
  • Mapping: Association of pending/order/position to master and level. Primary: ticket-based; Fallback: price-nearest within FallbackTolerancePoints * _Point.
  • Persistence: Storage via GlobalVariables (e.g., EA_pos<symbol>&#x3C;POSITION_ID> = "masterTicket|level|entryPrice" as string) to survive terminal restarts.</symbol>
  • Reticketing: Broker assigns new ticket after OrderSend; detect by scanning OrdersTotal() for matching price/volume/time.
  • Shift-Outwards: Adjust pending price to satisfy SYMBOL_TRADE_STOPS_LEVEL (stop_gap = level * _Point): For BUY_STOP, max(entry_price, Ask + stop_gap); For SELL_STOP, min(entry_price, Bid - stop_gap).
  • Breakeven Buffer: spread * BreakevenBufferSpreads added to prior level for SL move (e.g., for BUY: prior_entry + buffer).
  • Loss Count: Per-master counter incremented on pyramid loss; resets on master close. Used in lot = master.lot * PyramidLotMultiplier * pow(LotSizeReductionFactor, loss_count + (profit<0?1:0)).
  • Unmatched: Queue for fills/closes without immediate mapping; persisted as EA_nomap<symbol>_&#x3C;POSITION_ID> = entryPrice; rescanned in ProcessUnmatched().</symbol>

Input parameters (exact names, types, defaults, ranges):

  • EnableTrading (bool) = true: Global enable/disable.
  • TestLots (double) = 0.01: Lot for optional test master.
  • TestSL_Pips (double) = 50: SL pips for test.
  • TestTP_Pips (double) = 150: TP pips for test.
  • PyramidingStepR (double) = 0.5: Step multiplier (0.1 to 3.0+).
  • PyramidLotMultiplier (double) = 0.5: Base lot multiplier.
  • LotSizeReductionFactor (double) = 0.9: Reduction per loss.
  • BreakevenBufferSpreads (double) = 1.5: Buffer multiplier.
  • UseStructureForTrailing (bool) = true: True=continuous trailing; False=one-time on next fill.
  • MaxReentriesPerLevel (int) = 10: Max re-attempts per level.
  • PatchMaxRetries (int) = 5: Order send retries.
  • PatchTestMode (bool) = false: Force triggers for testing.
  • MaxPyramidLevels (int) = 100: Max levels.
  • FallbackTolerancePoints (int) = 50: Price match tolerance (was 200; reduce for accuracy).
  • PersistenceEnabled (bool) = true: Enable globals.

Description of Setup Preceding Actions

The EA starts in OnInit by loading persisted states (pendings, positions, unmatched) via GlobalVariables. It scans PositionsTotal() for existing masters (magic == MAGIC_TEST). If none, optionally places a test master if PatchTestMode. For each master, it builds pyramid pendings if not already present (up to MaxPyramidLevels).

Events triggering actions:

  • New master detected: Place pyramid pendings at levels 1+.
  • Pending fill (TRADE_TRANSACTION_DEAL_ADD, ENTRY_IN): Map to master/level, persist.
  • Pyramid close (ENTRY_OUT): If loss, arm re-entry for that level.
  • Price retrace (OnTimer, 1s): Check armed re-entries; if triggered, place pending.
  • Any error/reticket: Retry with backoff; fallback match if needed.

Signal / Event Descriptions

No traditional "signals" as this is a manager, but events:

  • Pyramid Placement Event: On new master or level availability. Condition: Master exists, level N not placed/filled. Action: Compute level_price, place pending with SL/TP from master dists.
  • Fill Mapping Event: On DEAL_ENTRY_IN with MAGIC_PYRAMID. Condition: Match ticket to pendingPyramids[] (primary) or price/history (fallback). Action: Create positionMap entry, persist as string, add to master's pyramidPositions[].
  • Close Arming Event: On DEAL_ENTRY_OUT. Condition: Mapped position, profit < 0. Action: Arm re-entry with trigger = prior level (or master if level=1), entry = closed level, lot reduced, remaining = MaxReentriesPerLevel.
  • Re-Entry Trigger Event: OnTimer, price >= trigger (BUY) or <= (SELL). Condition: Armed, remaining >0, no pending. Action: Place pending at entryPrice (shifted), decrement remaining.
  • Trailing Event: On new pyramid fill. If UseStructureForTrailing=true: Move all prior SLs to prior levels + buffer. Else: Move immediate prior to its entry + buffer.

Lifetime: Re-entries persist until remaining=0 or master closes (remove all related). Pendings/mappings persist via globals indefinitely unless master closes.

Placing of Orders and Opening of Positions

  • Use OrderSendAsync for pendings (BUY_STOP/SELL_STOP).
  • SL/TP: slDist/tpDist from master.
  • Lot: For pyramids = master.lot * PyramidLotMultiplier; For re-entries = adjusted by loss_count.
  • On success: Persist ticket→level, detect reticket.
  • Retries: Exponential backoff (500ms base, up to PatchMaxRetries).
  • Abort if slDist < stop_gap: Log ERROR, skip.

Management of Trading Positions/Orders

  • Trailing: As above, enforce stop_gap, move SL outward only.
  • Breakeven: +buffer to prior entry.
  • Monitor floating P/L: No, closes by SL/TP or manual.
  • Partial fills: Map each positionID separately to same level.

Cancellation of Orders and Closing of Positions

  • Cancel: On master close (remove all pendings/re-entries for that master).
  • Close: Positions close by SL/TP (EA doesn't close them).
  • Re-entry cancel: If remaining=0 or master gone.

Order Lot Calculation

Fixed for initial pyramids; reduced for re-entries via formula. No MM yet (fixed lot base).

Processing Trading Errors and Environment State

  • Log all: Use PrintFormat with tags (DEBUG-PENDING, DEBUG-FILL, etc.).
  • Errors: Analyze return codes, log ERROR: with code/desc.
  • Restarts: Load globals in OnInit; keep on Deinit.
  • Notifications: None; rely on journal.
  • Bar vs Tick: Use OnTradeTransaction for events, OnTimer(1s) for checks (bar-opening friendly).

Difference Between Bar-Opening and In-Bar Trading

Operate on ticks (OnTradeTransaction) for fills/closes; timer for triggers (in-bar ok, not scalping).

Important Aspects

  • Defensive: Bounds checks, Select() verifies, backward array removes.
  • Testing: Provide README with tester scripts for acceptance (pyramid place, fill map, close arm, retrace place, restart recovery).
  • No grid/martingale: Pure pyramid with limited re-entries.
  • Developer: Provide full source/ex5, debugging guide (log tags), minimal license server if requested.

What is Contained in the Existing Code (Current Implemented Features)

  • Master load/reload in OnInit.
  • Pyramid placement with retries/reticketing.
  • Fill mapping (primary/fallback) in OnTradeTransaction.
  • Persistence load/save for pendings/positions/unmatched.
  • Re-entry arming on close (primary/fallback).
  • Re-entry placement in OnTimer with shift-outwards.
  • Trailing in PlacePyramid/TrailTo.
  • Extensive logging/tags.

What Needs to Be Done (Modifications and Fixes)

Prioritized fixes to complete re-entry functionality:

  1. Update SavePosGlobal/LoadPosGlobals to store/parse string "masterTicket|level|entryPrice"; always load without skipping if master absent.
  2. Remove skips in LoadPosGlobals for missing masters; keep mappings persistent.
  3. Add ResolvePendingPositionMaps() in OnInit/OnTick to link loaded mappings to masters when they appear.
  4. Add masterTicket to ReEntry struct; use FindManagedTradeIndexByTicket for checks.
  5. Reduce fallback tolerance to 50 points everywhere.
  6. Ensure atomicity: Use local tickets, more debugs around mutations.
  7. Update OnTradeTransaction SavePosGlobal calls to pass full params.
  8. Add periodic DebugDumpPositionMap in OnTimer (debug mode).
  9. Verify/Delete globals with new formats.
  10. Implement acceptance tests in README (step-by-step with expected logs).

Deliverables: Updated .mq5/.ex5, README with tests/guide, short changelog.

Budget/Time: [To be discussed; initial estimate based on fixes]. Provide questions on unclear points before starting.


Bewerbungen

1
Entwickler 1
Bewertung
(7)
Projekte
9
22%
Schlichtung
0
Frist nicht eingehalten
0
Beschäftigt
2
Entwickler 2
Bewertung
(13)
Projekte
17
18%
Schlichtung
3
67% / 0%
Frist nicht eingehalten
0
Arbeitet
3
Entwickler 3
Bewertung
(13)
Projekte
15
0%
Schlichtung
1
0% / 0%
Frist nicht eingehalten
0
Arbeitet
4
Entwickler 4
Bewertung
(425)
Projekte
748
49%
Schlichtung
61
16% / 49%
Frist nicht eingehalten
136
18%
Arbeitet
5
Entwickler 5
Bewertung
(94)
Projekte
136
17%
Schlichtung
4
25% / 25%
Frist nicht eingehalten
12
9%
Beschäftigt
6
Entwickler 6
Bewertung
(290)
Projekte
520
36%
Schlichtung
62
34% / 35%
Frist nicht eingehalten
187
36%
Arbeitet
7
Entwickler 7
Bewertung
(4)
Projekte
4
0%
Schlichtung
1
0% / 0%
Frist nicht eingehalten
0
Arbeitet
8
Entwickler 8
Bewertung
(1)
Projekte
1
0%
Schlichtung
0
Frist nicht eingehalten
0
Frei
9
Entwickler 9
Bewertung
(8)
Projekte
17
41%
Schlichtung
1
0% / 100%
Frist nicht eingehalten
3
18%
Arbeitet
10
Entwickler 10
Bewertung
(6)
Projekte
6
17%
Schlichtung
2
0% / 0%
Frist nicht eingehalten
1
17%
Beschäftigt
11
Entwickler 11
Bewertung
(12)
Projekte
18
44%
Schlichtung
2
0% / 100%
Frist nicht eingehalten
3
17%
Arbeitet
12
Entwickler 12
Bewertung
(7)
Projekte
11
18%
Schlichtung
6
17% / 0%
Frist nicht eingehalten
3
27%
Arbeitet
13
Entwickler 13
Bewertung
(11)
Projekte
16
31%
Schlichtung
3
67% / 0%
Frist nicht eingehalten
0
Frei
14
Entwickler 14
Bewertung
(53)
Projekte
85
41%
Schlichtung
3
0% / 100%
Frist nicht eingehalten
3
4%
Arbeitet
15
Entwickler 15
Bewertung
(7)
Projekte
8
13%
Schlichtung
5
40% / 20%
Frist nicht eingehalten
0
Frei
16
Entwickler 16
Bewertung
(1)
Projekte
1
0%
Schlichtung
2
0% / 0%
Frist nicht eingehalten
0
Arbeitet
Ähnliche Aufträge
Kamohelo Phehlane 50 - 150 USD
The robot must make me a large profits and it must be strategic.It must look for strategies and tell me whether to buy or sell the trade.And it must take a half of the profit and must give me the other half of the profit
Hey, I need someone to troubleshoot and fix my current Breakout EA. Shouldn't take too long to fix, work ist mostly already done. The strategy is clear and simple, but some things don't work like they should: - Optimization / Backtesting problems, trades are not closing as they are supposed to at their desired time - Check my inbuilt Randomization features (I doubt they are working correctly) - Info Panel needs some
Døsh forex 30 - 200 USD
I want a robot that will help me and trade the the robot will be very good I don’t want to loose money I repeat I don’t want to loose money
I need an EA that takes MT5 Alert information and converts those alerts into trades. Assets can be Forex, Indices, Metals. EA to be installed on a separate chart of the MT5 instance. These are the required settings: - A trade filter that assists in filtering the desired asset and direction of the trade from the alert message. This can be a text string that precedes the actual asset. e.g. assume the alert: 'Scalper
Hello good people, I’m looking for a skilled MQL5 developer to create a fully automated Expert Advisor (EA) for MetaTrader 5 based purely on price action. No indicators, no grid trading, and no martingale—just clean, confirmed price action logic. 🔸 ENTRY STRATEGY The EA must detect the following patterns: Bullish/Bearish Engulfing Candles Pin Bars (bullish/bearish), including spike rejections Breakouts of support
hello I have a strategy based on 4 zigzag indicators and 1 parabolic sar that i want to implement in a robot with adjustable settings please contact me for more info and if you are able to this job
Hello, I’m currently looking to hire a professional and experienced developer to help create a custom trading bot (Expert Advisor) for the MetaTrader platform (MT5). What I Need: I’m looking for someone who can: Develop a fully automated trading bot based on my strategy (which I will provide in detail)
We are looking for an experienced MT5/MQL5 developer to build a data bridge that connects an external market data feed (tick-by-tick from a third-party provider) into the MT5 platform. Requirements: Capture real-time tick-by-tick market data (bid/ask/last price, volume) from an external API (source: WebSocket/REST or DLL). Push this data into MT5 in a format compatible with native charts and order execution
MosesRobot 30+ USD
1. Timeframe & Pairs Timeframe: 5-minute chart (M5) Pairs: EURUSD, GBPUSD, USDJPY,XAUUSD (low spreads, high liquidity) Trading Hours: London & New York session overlap (13:00–17:00 UTC) for volatility. --- 2. Indicators & Filters Main Signals: 1. EMA Crossover: EMA 9 (fast) & EMA 21 (slow) Buy signal: EMA 9 crosses above EMA 21 Sell signal: EMA 9 crosses below EMA 21 2. RSI Filter (Period 14): Buy only if RSI > 50
GOLDEN WEALTH EA 30 - 40 USD
I need developer that will help me to convert indication with signal to EA. that will be able to trade on any pair and brokers with the features of take profit, stop loss, risk management and trade in the adjustable % of equity or account balance

Projektdetails

Budget
100 - 150 USD

Kunde

Veröffentlichte Aufträge1
Anzahl der Schlichtungen0