Close Worst Position
Cutting the worst trade is the oldest piece of risk discipline there is, and doing it by hand is exactly when it goes wrong: sorting the Trade tab by profit while the market moves, clicking the wrong row, closing the second worst. This script does the one thing: find the biggest loser, show it to you, close it. The ranking is done on the net running result rather than the raw price profit. On MQL4 that is profit plus swap plus commission, read straight off each order. A position that looks like the second worst on price is often the worst once weeks of negative swap are counted, and a ranking that ignores that closes the wrong trade. Ties on the net figure go to the lower ticket - the older position - so a symmetric book, hedged pairs included, gives the same answer on every run instead of depending on iteration order. Because the script picks its own victim, the confirmation prompt does not say "close 1 position" - it names the position: ticket, side, volume, symbol and the loss that will be realised, before anything is sent. What you approve is what dies. If the book shifts while the prompt sits open, the approved ticket is still the one that is closed - the script never silently substitutes a different position for the one you read. Run it again for the new worst. By default the script refuses to act when the worst position is not actually losing. "Close the worst" on an all-winning book would mean closing the smallest winner, which is rarely what the finger meant; an input allows it explicitly for people who use the script as a generic "close the bottom of the book" tool. One position per run, on purpose. Closing the two worst is two runs with two prompts, which is exactly the speed at which loss-cutting decisions should be made. Notes and limitations - Closing the worst position realises its loss. That is the point, but there is no undo, and the script does not know whether the trade was about to turn. It executes discipline, it does not provide it. - Exactly one position is closed per run. The prompt's number is the loss at ranking time; the realised figure can differ by however long the prompt stayed open, and the alert reports the fresher number. - If another position becomes worse while the prompt is open, the approved ticket is still the one closed. Rerun for the new worst. - On MQL5, commission is not part of the ranking figure - see the differences section. On accounts with very large per-lot commission and near-identical losses, check the Trade tab before trusting a photo finish. - Pending orders are never considered or touched. - A worst position protected by the broker (market closed for its symbol, trading disabled) fails with the reason printed; the script does not move on to the second worst by itself.
Script · MT4 · v1.0 · Free
About Close Worst Position
Close Worst Position is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Only if losing, Current symbol only and Magic filter. MQL source code included.
Files in the download: CloseWorstPosition.ex4, CloseWorstPosition.mq4, CloseWorstPosition.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Only if losing | true | Refuse when the worst position is not losing |
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Only this magic (-1 means every magic) |
| Slippage pips | 3 | Slippage tolerance, in pips |
| Max retries | 5 | Attempts before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before closing |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Volume · Alerts · MT4 Script · MQL4 Source Code