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

Tarea técnica

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.


Han respondido

1
Desarrollador 1
Evaluación
(7)
Proyectos
9
22%
Arbitraje
0
Caducado
0
Trabajando
2
Desarrollador 2
Evaluación
(13)
Proyectos
17
18%
Arbitraje
3
67% / 0%
Caducado
0
Trabaja
3
Desarrollador 3
Evaluación
(13)
Proyectos
15
0%
Arbitraje
1
0% / 0%
Caducado
0
Trabaja
4
Desarrollador 4
Evaluación
(425)
Proyectos
748
49%
Arbitraje
61
16% / 49%
Caducado
136
18%
Trabaja
5
Desarrollador 5
Evaluación
(94)
Proyectos
136
17%
Arbitraje
4
25% / 25%
Caducado
12
9%
Trabajando
6
Desarrollador 6
Evaluación
(290)
Proyectos
520
36%
Arbitraje
62
34% / 35%
Caducado
187
36%
Trabaja
7
Desarrollador 7
Evaluación
(4)
Proyectos
4
0%
Arbitraje
1
0% / 0%
Caducado
0
Trabaja
8
Desarrollador 8
Evaluación
(1)
Proyectos
1
0%
Arbitraje
0
Caducado
0
Libre
9
Desarrollador 9
Evaluación
(8)
Proyectos
17
41%
Arbitraje
1
0% / 100%
Caducado
3
18%
Trabaja
10
Desarrollador 10
Evaluación
(6)
Proyectos
6
17%
Arbitraje
2
0% / 0%
Caducado
1
17%
Trabajando
11
Desarrollador 11
Evaluación
(12)
Proyectos
18
44%
Arbitraje
2
0% / 100%
Caducado
3
17%
Trabaja
12
Desarrollador 12
Evaluación
(7)
Proyectos
11
18%
Arbitraje
6
17% / 0%
Caducado
3
27%
Trabaja
13
Desarrollador 13
Evaluación
(11)
Proyectos
16
31%
Arbitraje
3
67% / 0%
Caducado
0
Libre
14
Desarrollador 14
Evaluación
(53)
Proyectos
85
41%
Arbitraje
3
0% / 100%
Caducado
3
4%
Trabaja
15
Desarrollador 15
Evaluación
(7)
Proyectos
8
13%
Arbitraje
5
40% / 20%
Caducado
0
Libre
16
Desarrollador 16
Evaluación
(1)
Proyectos
1
0%
Arbitraje
2
0% / 0%
Caducado
0
Trabaja
Solicitudes similares
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
PLEASE PLEASE!!! I NEED A PERSON WITH EXPERIENCE WHO WILL PERFECT THE BELO AND NOT A NEWBIE...I NEED SOMEONE WHO ALSO HAS DONE A LOT OF PROJECTS AND HAS A PERFECT CLIENT REVIEW. have sent the script and the bheuresko pattern indicator as well as the image of the script and indicator (below). The script is the image showing the arrows written ENTRAR and the Bheuresko pattern is the one written bullish engulfing and
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
Precision Shift EA 100 - 500 USD
Use the Daily (D1) chart as the higher timeframe reference. On the H1 timeframe, monitor for a sweep of the previous day’s high or low. A sweep means that price takes liquidity above or below the previous day’s level and then shifts back inside. After a sweep on H1, drop to the M5 timeframe. On M5, wait for a Change of State in Delivery / Market Structure Shift (CHOCH/BOS) to confirm entry. If the sweep occurs above
Sharper trading bot 30 - 200 USD
I have a $200,000 trading account challenge. I need the bot to work on trading view so it has to be a pine script I think. I’m going to attach a photo of the requirements for the account
📌 Forex EA Bot Requirement Document 1. General Information Trading Platform: MetaTrader 5 (MT5) / MetaTrader 4 (MT4) Trading Instruments: (e.g., GBP/JPY, XAU/USD, BTC/USD) Timeframes to Trade: (e.g., 15M, 1H, 4H, Daily) Trading Style: (Scalping, Swing trading, Intraday, Position trading) 2. Entry Rules Define the indicators/strategies for entry (e.g., Moving Averages, MACD, RSI, Order Blocks, Supply & Demand)
Hello, I want to make candles on the same chart as the one in the picture so that the trend is very clear and I know the upward or downward trend in a filtered way and with the same filter of the clear chart in front of you in the picture This is not an indicator, but a chart like Japanese candlesticks, Renko, etc. I want this chart as shown in the picture in front of you. It is the best chart I want because it shows
Sharp scalping 30 - 100 USD
I have $1 million account that I need a scalping box for with a 90% win rate I attached a photo of the daily draw down and the max draw down of the account because it is a prop firm challenge. I need it in Pine script for trading view. Thanks
Hello expert developer how are you doing ? Please see my strategy on the video but right now I want to create only ATM as my PDF file showing please let me know how much would you charge me for the only ATM to work as my images showing Auto trade works like this i.e, (It will send automatically orders on every bar) https://ninza.co/&nbsp; please check this website and see if you can create similar ATM based on my
Hi, I have a complicated EA. I would need to pick at least one pair and do the best optimization on it, plus a backtest. When would you do a test for me, optimizing the settings, ...? Thanks for the offers. P

Información sobre el proyecto

Presupuesto
100 - 150 USD

Cliente

Encargos realizados1
Número de arbitrajes0