Close Oldest Position
Working a book down oldest-first is a common discipline: the stalest exposure goes first, one position at a time, each one looked at before it is closed. Doing that by eye means sorting the Trade tab, squinting at timestamps and hoping the sort did not shift under a new tick. This script does the pick and shows its working. The pick is the earliest open time, measured on the server clock, which is the clock that stamped it. The prompt names the exact ticket, symbol, direction, volume, open time, age in hours and current net result, so you agree to a specific position, not to an idea of one. Ties are handled rather than ignored. An EA can fire a basket of orders into the same second, and the one-second resolution of the timestamp cannot order them. Tickets can: the server issues them in sequence, so within a tied second the lower ticket is the earlier entry and wins the pick. The choice is deterministic - running the script twice on the same book picks the same position. The other decision worth stating: if the chosen position leaves the book between the snapshot and the close - it hit its own take profit, or another program got there first - the script reports that and stops. It never silently falls through to the next oldest, because that would close a position you were never shown and never confirmed. Run it again and the new oldest is named first, like the old one was. Notes and limitations - Market positions only. A pending order that has sat unfilled for a week is old in a different sense and wants deleting, not closing - this script never touches pendings. - Exactly one position is closed per run. Working a book down oldest-first means running it once per position, confirming each. - Age is measured against TimeCurrent(), which only advances on incoming quotes; over a weekend the age display freezes with the clock. - On a FIFO account the oldest position is normally the one the server wants closed first anyway, so this script and FIFO rules agree.
Script · MT4 · v1.0 · Free
About Close Oldest Position
Close Oldest 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: CloseOldestPosition.ex4, CloseOldestPosition.mq4, CloseOldestPosition.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