Stale Trade Expiry Guard
- 유틸리티
-
AL MOOSAWI ABDULLAH JAFFER BAQER
To trade successfully we need to go through the proven path that others had gone and reached!
Here are 17 quotes from such successful investors:
1- Limit your size in any position so that fear does not become the prevailing instinct guiding your judgment.
Joe Vidich - 버전: 1.0
- 활성화: 5
Try it free before anything else: run Stale Trade Expiry Guard in the Strategy Tester with visual mode on. It opens small demo positions there on purpose and leaves them alone, so you can watch each one age on the panel, see its countdown reach zero, and see what the guard does next.
A TRADE WITHOUT A DEADLINE IS A TRADE NOBODY IS MANAGING. THIS GUARD GIVES EVERY POSITION ONE.
Stale Trade Expiry Guard is a utility Expert Advisor for MetaTrader 5. It never places entries of its own on a real account. You decide how long a position may stay open, in hours, in bars, or both. A position that grows older than that and has still not reached the profit you expected is called stale, and the guard handles it the way you chose: a warning, a close, a close only while the loss is still small, or a stop loss moved to breakeven once it is in profit.
In short
- Maximum age in hours, in bars of a timeframe you pick, or both (whichever comes first).
- Age can count trading time only, so a weekend or a closed session does not make a position older.
- A minimum profit that makes a position a runner: runners are never touched, however old they are.
- Four modes: 0 alert only, 1 close, 2 close only if the loss is smaller than your limit, 3 move the stop loss to breakeven if in profit.
- An "expires in" countdown for every position, and the margin that stale positions are holding, shown on the panel.
- Filters: all symbols or the chart symbol, any magic, manual trades only, or one magic number.
- Not a grid, no martingale, no averaging, no DLLs, no web requests, no extra files.
START HERE
Attach it to one chart, for example EURUSD H1. One copy watches every symbol of the account unless you limit it. Set the maximum age to the longest time your trades are normally meant to need, for example 48 trading hours for intraday swings. Run mode 0 (alert only) for a week and see which positions it calls stale. When those alerts match what you would have done, switch to mode 2 or 3 and turn on Algo Trading.
Why give a trade an expiry date
Most trades are opened for a reason that has a time attached: a news reaction, a session breakout, a bounce from a level. When that time has passed and the trade has done nothing, the reason is gone, but the position stays. It holds margin, pays swap, and waits for the next surprise to decide its result. Nobody chose to keep it; it simply was not looked at again. The guard looks at it for you, every second, and asks one question: has this trade had its chance?
What one position's life looks like
Young. The position is AGEING. The panel shows its age, a countdown to its expiry and a bar with how much of its allowed life it has used. The bar turns amber in the last quarter.
Doing well. If its profit reaches your minimum profit it becomes a RUNNER and the clock no longer matters. A trade that works is left to work.
Expired. If the countdown reaches zero while the profit is below that level, it is STALE and your mode decides. Mode 2 in practice: a stale trade with a small loss is closed and the loss stays small; a stale trade with a big loss is kept and you get one message (KEPT BIG LOSS), and the guard closes it later if the loss shrinks below your limit. Mode 3 in practice: a stale trade in profit gets its stop loss moved to the open price plus a few points, so it can no longer turn into a loss; a stale trade still in loss waits (WAIT PROFIT).
What is new compared with our earlier guards
Every position gets its own "expires in" column, and the plan shows which position expires next and when. The TIME-LOCKED MARGIN row adds up the margin held by stale positions, next to the margin of all positions in scope and as a percent of equity, so you can see what old trades are costing you in buying power, not only in profit.
How age is counted
Hours are counted from the position's open time. With trading time only (the default), only the time inside the symbol's trading sessions counts, as your terminal reports them. Friday evening to Monday morning adds nothing. A symbol that lists no sessions counts all time. Bars are counted on the timeframe you choose, from the bar of the open time. When both rules are on, the first one reached makes the position stale, and the countdown shows the nearer one.
Inputs, every one explained
AGE
InpMaxAgeHours = 48. Maximum age in hours. 0 turns this rule off.
InpMaxAgeBars = 0. Maximum age in bars. 0 turns this rule off. At least one of the two rules must be on.
InpBarsTF = H1. Timeframe used to count bars.
InpCountTradingTimeOnly = true. Hours count only inside the symbol's trading sessions. Set false to count calendar time, weekends included.
ACTION
InpMinProfitMoney = 20. A position whose profit (including swap) is this amount or more in account currency is a runner and is never stale. 0 turns this off, so any old position can be stale.
InpMode = 2. 0 alerts once per position and changes nothing. 1 closes every stale position. 2 closes a stale position only if its loss is smaller than InpMaxLossMoney (a stale position in small profit is closed too). 3 moves the stop loss to breakeven on a stale position in profit.
InpMaxLossMoney = 15. Mode 2 only: the loss in account currency that is still small enough to close.
InpBEPlusPoints = 10. Mode 3 only: points beyond the open price for the breakeven stop, to cover spread or commission. The stop is set only when the broker's stop and freeze levels allow it; otherwise the state is STOPS NEAR and the guard tries again on the next pass. An existing stop that is already better is never moved back.
EXECUTION
InpSlippagePoints = 30. Maximum slippage accepted on a close.
InpFailAlertAt = 3. A refused close or stop change is retried with a growing pause, up to two minutes. After this many failures on one position you get one alert. If the broker refuses the filling mode, the next allowed mode is tried at once.
FILTER - WHICH POSITIONS ARE CHECKED
InpSymbolScope = All symbols. Chart symbol only watches the chart symbol alone.
InpMagicFilter = Any magic. Only manual trades means magic 0. Only the magic number limits the guard to one Expert Advisor.
InpMagic = 0. The number for that last choice.
ALERTS
InpAlertPopup = true. Pop-up alert in the terminal.
InpAlertPush = false. Push notification to the MetaTrader app on your phone, which must be enabled in the terminal options.
PANEL
ShowPanel = true. Show or hide the panel. The guard works either way.
PanelX = 12, PanelY = 15. Offset from the top left corner. The MOVE button lets you drag it.
PanelFontSize = 9. Base font size.
MaxTableRows = 5, MaxHistoryRows = 4. Rows in the position table and the action history. On a short chart the panel drops its least important rows first.
STRATEGY TESTER DEMO
InpTesterDemo = true. In the Strategy Tester only: one small position per weekday on the chart symbol (up to 0.1 lot within 10 percent of free margin, checked for margin times 1.5, sides alternate, at most 6 open), left alone so it can grow old. A demo position still open after 10 days is closed. No new demo position after a 20 percent balance loss. This code never runs on a real or demo account chart.
The panel
NEXT EXPIRY shows the next position to expire, its countdown and how much of its life is used. EXPIRY PLAN lists the next expiry, stale positions and their profit, the time-locked margin, the last action, the mode, Algo Trading on or off, your age rule, the runner level, the loss limit or breakeven offset of the active mode, failed sends, totals and the positions in scope. POSITIONS, OLDEST FIRST shows symbol, side, lots, profit, age, expires in, state and a life-used bar. States are AGEING, RUNNER, STALE, ALERTED, KEPT BIG LOSS, WAIT PROFIT, AT BREAKEVEN, STOPS NEAR, NO BARS, MARKET SHUT, TRADE OFF and RETRY. ACTION HISTORY keeps alerts, closes, stop changes and kept positions with server time. QUICK CONTROLS: ARMED or PAUSED, a mode button that cycles the four modes, and RESCAN NOW. The right side of the chart stays clear.
What it cannot do
It does not know why you opened a trade. A swing trade that needs two weeks will be called stale after 48 hours if you leave the default. Set the age to your own style, and use the magic filter to give different strategies different guards.
Closing is real. A close books the current result and pays spread and commission. Mode 1 closes losers of any size; mode 2 exists for that reason.
It only works while it runs. With the terminal off, Algo Trading off, the market closed or a disabled symbol, nothing is sent; it acts on the next pass when trading is possible. Keep the terminal running, for example on a VPS.
Its memory is short. History and totals reset on restart; only "already alerted" is kept per position, so a restart does not repeat alerts.
What was and was not tested
Tested in the Strategy Tester on a hedging account, 1 to 30 June 2026, H1, deposit 10000, leverage 100, four runs. EURUSD mode 2: 20 demo positions, 14 became stale, 13 closed (some first kept with a big loss and closed later when the loss shrank), 6 kept messages. EURUSD mode 0: 11 stale, 11 alerts, nothing changed. EURUSD mode 1 with a 30-bar rule and calendar time: 18 stale, 18 closed. XAUUSD mode 3: 12 stale, 8 stops moved to breakeven. No send failed and no journal error appeared. On a live demo chart the panel was seen on a Sunday: the countdowns stood still because no session was open, and the alert flag was kept across a restart.
Not tested at the time of writing: a real close or stop change on a live or demo broker, a netting account, retries after a broker refusal, a filling mode refusal, chart symbol scope, magic filters, pop-up and push alerts, and the panel buttons. These paths exist in the code and are described by design. Please try them on a demo account with your own broker first.
Try it in the Strategy Tester
Choose Stale Trade Expiry Guard, EURUSD or XAUUSD, H1, a deposit of a few thousand, visual mode, and press Start. Pick each mode in turn and watch the countdowns run out two trading days after each demo position opens.
If the guard cleared out a few trades you had forgotten about, or you think it should handle something differently, an honest review on the product page helps other traders decide and tells me what to improve next.
