Loading…

Rebuild Position As Exit Ladder

A single flat target on a large position is an all-or-nothing decision: the whole thing bags its profit at one price or it does not. Scaling out manually after the fact means clicking a partial close at exactly the right moment, which most traders miss. This script turns one ticket into a ladder of several, each free to hit its own target independently, in one run. It works by closing the original ticket completely and reopening its size as InpRungs new market orders, spaced from InpTargetStartPips and stepping out by InpTargetStepPips per further rung. That is the whole trick, and it is also the whole cost: closing and reopening the same net exposure pays the spread twice instead of once. The script does not pretend this is free. It reads the live spread at the moment it runs and prints an estimate of the round trip cost in account currency, so the trade-off against a plain partial close is visible rather than assumed. The lot split respects the symbol's lot step exactly, folding any rounding remainder into the last rung so the sum matches the original size to the lot step - not an approximation that leaves a sliver of size unaccounted for. If the position is too small to split at the symbol's minimum lot, the script refuses rather than silently opening fewer rungs than asked. Each candidate target and the optional carried-over stop loss are checked against the broker's stop and freeze distance before being sent; a level that would be rejected is dropped for that one rung rather than aborting the whole rebuild. The most important thing this script gets right is what happens when a rung fails to reopen after the original ticket is already closed. That is a real gap in market exposure, not a rounding error, and it is reported as loudly as the summary alert allows rather than folded quietly into a lower total. Notes and limitations - The original ticket is closed before any rung is reopened. Between those two moments there is genuinely no position open - this is not an atomic operation and cannot be made one on this platform. - The spread cost reported is an estimate at the moment the script runs, not a guaranteed figure; slippage on the actual fills can move it either way. - Commission, where the broker charges one, is not included in the cost estimate - it is not knowable from the terminal before the trade is sent. - Every rung gets the same stop-loss price, not a scaled one. A rung's risk in money terms is smaller than the original only because its lot size is smaller, not because the stop distance changed. - Works on market positions only. Pending orders are rejected outright.

Script · MT4 · v1.0 · Free

About Rebuild Position As Exit Ladder

Rebuild Position As Exit Ladder is an expert advisor (trading robot) for MetaTrader 4. It opens a grid of orders at fixed price steps. It has 9 settings, such as Ticket, Rungs and Target start pips. MQL source code included.

Files in the download: RebuildPositionAsLadder.mq4, RebuildPositionAsLadder.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Ticket0Ticket number of the position to rebuild
Rungs3Number of new tickets to split into
Target start pips20.0Distance from entry to the first rung's target, pips
Target step pips20.0Extra distance added per further rung, pips
Keep stop losstrueGive every new ticket the original stop-loss price
Magic number20200Magic number tagged on the new tickets
Slippage points30Maximum acceptable slippage, points
Max retries5Attempts per trade action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Grid · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria