Close Newest Position
Everyone eventually opens a position they did not mean to open: a double-click on the one-click panel, the wrong symbol in focus, a test order on what turned out to be the live account. The reflex is to get rid of the last thing that happened, fast - and fast is exactly when the wrong row of the Trade tab gets closed by hand. This script is that reflex made safe: it finds the most recent entry, shows you its ticket, symbol, volume, open time and floating result, and closes it only after you confirm. The detail that separates it from the naive version is the tie-break. A double-click fires two orders into the same second, and the open-time stamp has one-second resolution, so by timestamp alone the two accidents are indistinguishable. Tickets settle it: the server issues them in sequence, so within a tied second the higher ticket is the newer fill - precisely the second click you are trying to undo. A version of this script without the tie-break picks whichever of the two the pool happens to list first, which is a coin toss on exactly the occasion the script exists for. The second decision: if the chosen position leaves the book between the snapshot and the close - it hit a stop, or another program closed it - the script reports that and stops. It never falls through to the next newest, because that would close a position you were never shown. Run it again for a fresh, named pick. An undo has limits worth stating. The fill happened; only the exposure can be removed, at the current price, not at the entry price. Spread and any slippage are already paid. Notes and limitations - Market positions only. A mistaken PENDING order wants deleting, not closing; that is a different tool. - Exactly one position is closed per run. - This is an undo of exposure, not of the fill: the close happens at the current market price, and spread plus any slippage from the accidental entry are already paid. - If the mistake was closed by its own stop or by another program first, the script reports that and touches nothing. - On a FIFO account the server may refuse to close the newest position while older ones on the same symbol remain; the refusal is reported as-is.
Script · MT4 · v1.0 · Free
About Close Newest Position
Close Newest Position 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: CloseNewestPosition.ex4, CloseNewestPosition.mq4, CloseNewestPosition.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Current symbol only | false | Restrict to this chart's symbol |
| Magic filter | -1 | Magic number (-1 considers 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: Alerts · MT4 Script · MQL4 Source Code