Loading…

Close All Except Largest Winner

"Take everything off except my best runner" is a standard de-risking move before news, before a weekend, or when a basket has done its job and one position deserves more room. Done by hand it means closing N-1 positions while carefully not touching one - the exact situation where a mis-click costs the most, because the one position you must not close is also the most valuable one on the book. The trap in a naive version of this script is subtle: profits move while you close. Closing a dozen positions takes seconds, prices tick the whole time, and if the script re-ranked the book as it worked, the "best" position could change mid-run - and the script would then close the position its own prompt promised to keep. This implementation therefore decides everything from a snapshot taken before the first close: which position is kept, which are closed, and the totals shown in the prompt. The keeper is skipped by snapshot index, not by re-ranking. If another position overtakes it thirty seconds later, the confirmed keeper still survives, because that is what you agreed to. The ranking is net of costs - profit plus swap plus commission on MQL4, profit plus swap on MQL5 (see the platform notes) - because raw price profit routinely crowns the wrong winner once a few nights of swap are counted. And the winner must be real. If nothing on the book is in net profit, the script refuses: the "keeper" would just be the least bad loser, and flattening everything around it is a full-flatten decision in disguise. A full flatten wants a close-all script, chosen deliberately. Positions that close themselves mid-run (a take profit hit while the script worked through the list) are counted as done, not failed - the goal for every non-keeper is a flat book, and the book is no less flat for it. Notes and limitations - Market positions only. Pending orders are not touched. - The keeper is fixed at snapshot time. A position that overtakes it during the run is still closed - by design, because it is what the prompt showed and you confirmed. - The realised total in the summary is the sum of each position's last-seen net figure at the moment it was closed; the account statement is the authority (on MQL5 it excludes commission). - Closing the rest of a hedged book around one keeper changes net exposure and can raise margin requirements rather than lower them. Check free margin before running this on a heavily hedged account. - On a FIFO account the server can refuse closes that are out of order; each refusal is reported and counted as failed.

Script · MT4 · v1.0 · Free

About Close All Except Largest Winner

Close All Except Largest Winner is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 6 settings, such as Current symbol only, Magic filter and Slippage pips. MQL source code included.

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

Settings

SettingDefaultWhat it does
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Magic number (-1 takes every magic)
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per position before giving up
Retry wait ms500Pause between attempts, milliseconds
Confirm firsttrueAsk for confirmation before closing

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: News Trading · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria