Loading…

Close Positions Older Than N Hours

An intraday trade that is still open two days later is not an intraday trade any more. It is a position nobody decided to hold, drifting along on swap, using margin that the strategy assumed would be free. Every book accumulates these, and clearing them is exactly the sort of routine housekeeping a script should do. The whole difficulty in this script is the clock, and it is the reason most published versions of it are quietly wrong. There are two clocks in a terminal. TimeLocal is your computer. TimeCurrent is the trade server, and it is the clock the server used when it stamped the order's open time. Subtract one from the other and you have imported the broker's GMT offset into your age calculation as a silent constant error - two hours, three hours, sometimes more, and it changes twice a year with daylight saving. On a two hour threshold that error is the entire threshold. Both timestamps here come from the server, and the script prints the server clock and the terminal clock side by side so the gap between them is visible rather than mysterious. The second clock trap is that TimeCurrent only advances when a quote arrives. Over a weekend, a market holiday, or a dropped connection it freezes at the last tick and ages stop growing. A position opened on Friday afternoon does not become 60 hours old over the weekend; it becomes 60 hours old a few seconds after Monday's open. Printing the server time makes that behaviour obvious the first time it matters. Before it closes anything, the script writes every affected position to the Experts tab with its symbol, direction, volume, server open time and age in hours. A time based sweep is precisely the kind of action you want a record of afterwards, and reading that list is the last chance to notice that the threshold was typed in the wrong units. The rest is the standard correctness work. Tickets are collected before anything is closed, because the order pool re-indexes the instant an order leaves it and a naive loop skips roughly half the book. Temporary errors are retried and permanent ones are not. Slippage is entered in pips and converted to points. A threshold of zero is refused, because zero would sweep a trade opened one second ago and nobody means that. Notes and limitations - Market positions only. A pending order that has sat unfilled for a week is stale in a different sense and wants deleting, not closing. - TimeCurrent does not advance without incoming quotes. Ages freeze over weekends, holidays and disconnections. This is broker behaviour, not a bug, and the printed server clock is there to make it visible. - The script runs once when you run it. It is not a background watchdog. If you want stale trades closed automatically, that needs an EA. - If the server clock jumps backwards - it happens across some daylight saving changes - ages are clamped to zero rather than becoming negative, so a fresh trade can never look ancient. - Nothing can close a position while that symbol's market is shut. Those are reported as failures with the broker's reason. - The MQL5 version judges success by looking for the position again after the request rather than by the return code, because a partially filled close reports success and leaves a remnant. The remnant is retried.

Script · MT4 · v1.0 · Free

About Close Positions Older Than N Hours

Close Positions Older Than N Hours 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 Max age hours, Current symbol only and Magic filter. MQL source code included.

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

Settings

SettingDefaultWhat it does
Max age hours24.0Close positions older than this many hours
Current symbol onlyfalseRestrict to this chart's symbol
Magic filter-1Magic number (-1 closes 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: Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria