Loading…

Staged Partial Close Manager

Scaling out of a winning position in stages is standard practice: bank part of the gain as targets are reached, let the remainder run. This EA automates up to three stages, each defined as a profit distance in pips and a percentage of volume to close when that distance is reached. The percentage in each stage is always a share of the position's ORIGINAL volume, fixed the first time this EA sees the ticket - not a share of whatever volume happens to remain after earlier stages have already fired. That distinction is what makes "33/33/34" actually add up to the whole position. Percentages of a shrinking remainder compound in the wrong direction and never quite finish the job; percentages of the fixed original volume do, and the stages fire strictly in order even if a fast spike jumps straight past two targets in one tick, so the close sequence in the log is always predictable. Every close amount is rounded down to the broker's lot step with an epsilon guard, and a computed slice below the broker's minimum lot is skipped rather than sent and rejected - the position simply carries that stage's share into the next one instead. Read the risk note before attaching this to a live account: which stages have already fired for a given ticket is tracked only in this EA's memory, in a plain array, not written to any file or global variable. That is a deliberate scope limitation, not an oversight - see notes and limitations for exactly what that means in practice and how to avoid an unpleasant surprise from it. Notes and limitations - RISK NOTE - state does not survive a restart. Which stages have fired for each ticket lives only in RAM, in an array populated as the EA discovers open tickets. Removing the EA, recompiling it, changing an input (which reloads the EA), or restarting the terminal all clear this state. When the EA restarts and rediscovers an existing position, every stage is treated as not-yet-fired and evaluated fresh against the position's current profit - a position already well past stage 1's distance can fire stage 1 (and any other stage whose distance it has already passed) again on restart, closing more than the intended cumulative percentage. This is an accepted limitation for this scope. If you need the tracking to survive a restart, keep the chart and EA running continuously rather than recompiling or reattaching mid-trade, or manage that position's remaining stages by hand after any restart. - Percentages are of the original volume at first sight, not the remaining volume - see the full description for why. - A stage whose computed slice is below the broker's minimum lot is skipped and logged, not force-closed at the minimum. - This EA never opens a position and never moves a stop loss - it only closes staged volume from an existing one. - Evaluated every tick because a profit target can be reached and retreat within a single bar; the comparison itself is a cheap price subtraction.

Expert advisor · MT4 · v1.0 · Free

About Staged Partial Close Manager

Staged Partial Close Manager is a script for MetaTrader 4. It has 10 settings, such as Stage1 pips, Stage1 percent and Stage2 pips. MQL source code included.

Files in the download: PartialCloseManager.ex4, PartialCloseManager.mq4, PartialCloseManager.txt.

Settings

SettingDefaultWhat it does
Stage1 pips20Stage 1 profit distance, pips (0 disables this stage)
Stage1 percent33.0Stage 1 percent of ORIGINAL volume to close
Stage2 pips40Stage 2 profit distance, pips (0 disables this stage)
Stage2 percent33.0Stage 2 percent of ORIGINAL volume to close
Stage3 pips60Stage 3 profit distance, pips (0 disables this stage)
Stage3 percent34.0Stage 3 percent of ORIGINAL volume to close
Magic filter-1Magic number filter (-1 = every magic)
Current symbol onlytrueRestrict to this chart's symbol
Max retries5Attempts per close 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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: Volume · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria