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

指定

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.


応答済み

1
開発者 1
評価
(7)
プロジェクト
9
22%
仲裁
0
期限切れ
0
取り込み中
2
開発者 2
評価
(13)
プロジェクト
17
18%
仲裁
3
67% / 0%
期限切れ
0
仕事中
3
開発者 3
評価
(13)
プロジェクト
15
0%
仲裁
1
0% / 0%
期限切れ
0
仕事中
4
開発者 4
評価
(425)
プロジェクト
748
49%
仲裁
61
16% / 49%
期限切れ
136
18%
仕事中
5
開発者 5
評価
(94)
プロジェクト
136
17%
仲裁
4
25% / 25%
期限切れ
12
9%
取り込み中
6
開発者 6
評価
(290)
プロジェクト
520
36%
仲裁
62
34% / 35%
期限切れ
187
36%
仕事中
7
開発者 7
評価
(4)
プロジェクト
4
0%
仲裁
1
0% / 0%
期限切れ
0
仕事中
8
開発者 8
評価
(1)
プロジェクト
1
0%
仲裁
0
期限切れ
0
9
開発者 9
評価
(8)
プロジェクト
17
41%
仲裁
1
0% / 100%
期限切れ
3
18%
仕事中
10
開発者 10
評価
(6)
プロジェクト
6
17%
仲裁
2
0% / 0%
期限切れ
1
17%
取り込み中
11
開発者 11
評価
(12)
プロジェクト
18
44%
仲裁
2
0% / 100%
期限切れ
3
17%
仕事中
12
開発者 12
評価
(7)
プロジェクト
11
18%
仲裁
6
17% / 0%
期限切れ
3
27%
仕事中
13
開発者 13
評価
(11)
プロジェクト
16
31%
仲裁
3
67% / 0%
期限切れ
0
14
開発者 14
評価
(53)
プロジェクト
85
41%
仲裁
3
0% / 100%
期限切れ
3
4%
仕事中
15
開発者 15
評価
(7)
プロジェクト
8
13%
仲裁
5
40% / 20%
期限切れ
0
16
開発者 16
評価
(1)
プロジェクト
1
0%
仲裁
2
0% / 0%
期限切れ
0
仕事中
17
開発者 17
評価
(1)
プロジェクト
1
100%
仲裁
1
0% / 0%
期限切れ
0
仕事中
類似した注文
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
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
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
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
📌 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
A few months ago, I started conducting some trading funding tests. I want to develop a management system that allows me to apply it to any account, regardless of the funding company. The idea is that ONLY from 11:00 PM to midnight, Monday through Friday, and on Saturday and Sunday, I can indicate the maximum number of trades allowed per day for each account. This means that if the system detects that I have already
$50 - $150 80+ USD
I need someone who can create a scalping trading bot for me. With a 1 - 5 minutes timeframe entry. A good robot that automatically opens a trade and close in profit

プロジェクト情報

予算
100 - 150 USD

依頼者

出された注文1
裁定取引数0