Daily Loss Guard
- 유틸리티
-
Jorge Eliecer Valencia Ferreira
🤖 Quantitative Developer & Algorithmic Trading Specialist | MQL5 & Python
Welcome to my official MQL5 profile. I am a Quantitative Developer and Algorithmic Trader focused on creating high-quality, institutionally robust Expert Advisors with strict risk management. - 버전: 1.0
- 활성화: 10
Daily Loss Guard watches the equity of your account and enforces a daily loss limit that you set. When the loss for the day reaches the limit, it closes the open positions, deletes the pending orders, and keeps closing anything opened afterwards until the next daily reset. Every action is written to a CSV log with the time, ticket, price and retcode.
It is for traders who run one or several Expert Advisors, trade manually, or work on a prop firm account with a daily drawdown rule, and want a rule that does not depend on their own discipline. It is not a trading strategy. It never opens a trade on your account, only closes. The one exception is the Strategy Tester demo described below.
Reviews of this kind of tool mention a few problems: limits that were not respected, panels that are heavy on the terminal, and lost state after a restart. The design here is simple. One timer, retries on every close, and a daily state that is saved so a restart does not reset the day.
How the limit works
- The basis is taken at the start of the trading day: balance, equity, or the higher of the two, as you choose.
- The daily loss is the basis minus current equity, so floating losses count.
- The limit is a percentage of the basis, a fixed amount in account currency, or both. If both are set, the smaller money value applies.
- When the loss reaches the limit, the guard locks. It closes positions and deletes pending orders as set in the inputs. For anything opened or placed after the lock, it closes or deletes it too if "Block new trades" is on.
- At the daily reset time (server time) the lock is released and a new basis is taken.
- Closing starts with the position with the largest loss.
- Deposits and withdrawals made during the day adjust the basis, so they do not count as profit or loss.
Optional rules:
- Warning once per day when a chosen percentage of the limit has been used (80 by default).
- Maximum total loss from an initial balance. When equity falls to that level, it closes and blocks until equity is above the level again.
- Daily profit lock. When the profit for the day reaches a percentage of the basis, it closes everything and blocks until the next reset.
State and restarts
The day basis and the lock status are saved in terminal global variables and in a small file in MQL5\Files (DLG_state_LOGIN.txt). If you restart the terminal inside the same day, the state comes back, including a lock already triggered. If you attach the EA in the middle of a day, it rebuilds the day-start balance from the account history. In that case the day-start equity is not known, so the balance is used.
How to use it
- Attach it to one chart of the account. It manages the whole account from that chart, whatever the symbol of the chart.
- Check that Algo Trading is on in the terminal and that the EA is allowed to trade.
- Set the limit (percentage, amount or both), the basis and the reset time. Choose the scope: all account, the chart symbol only, or a list of magic numbers.
- Watch the panel. Status is ARMED, WARNING or LOCKED. Open DailyLossGuard_log.csv to see what it did.
Inputs
Limits
- InpDailyLossPct (default 3.0): daily loss limit, % of the basis.
- InpDailyLossUSD (default 0): daily loss limit in account currency. 0 = off.
- InpBasis (default Day-start balance): day-start balance, day-start equity, or the higher of both.
- InpMaxTotalLossPct (default 0): maximum total loss, % of the initial balance. 0 = off.
- InpInitialBalance (default 0): initial balance for the total loss. 0 = balance when first attached.
- InpDailyProfitLockPct (default 0): daily profit lock, % of the basis. 0 = off.
Day reset
- InpResetHour (default 0) and InpResetMinute (default 0): reset time, server time.
Scope
- InpScope (default All account): all account, this chart symbol only, or magic numbers in a list.
- InpMagicList (default 123,456): magic numbers, used only with the list scope. Use 0 for manual trades.
Actions
- InpClosePositions (default true): close positions on breach.
- InpDeletePendings (default true): delete pending orders on breach.
- InpBlockNewTrades (default true): close new positions and delete new pending orders until the next reset.
- InpWarnAtPct (default 80): warning at this % of the limit used. 0 = off.
- InpAlerts (default true): terminal alerts. If off, messages go to the Experts log.
Execution
- InpSlippagePoints (default 50): maximum slippage for closing, in points.
- InpMaxRetries (default 5): attempts per close.
- InpTimerMs (default 500): monitoring timer in milliseconds.
Panel
- InpShowPanel (default true), InpCorner (default top left), InpFontSize (default 9) and five color inputs.
Strategy Tester
- InpTesterDemo (default true): demo trades in the Strategy Tester only.
Output
Panel on the chart: status, basis, equity, today's profit or loss in money and percent, the limit with the percentage used, distance to the limit, next reset time, scope, and the last action taken.
CSV log: MQL5\Files\DailyLossGuard_log.csv. Semicolon separated. Columns: time; event; symbol; ticket; volume; price; retcode; equity; detail. Events include INIT, NEWDAY, RESTORE, WARNING, BREACH, PROFIT_LOCK, BREACH_TOTAL, TOTAL_RELEASED, CLOSE, DELETE, DEFER, WAIT and BALANCE_OP.
What happens when a close fails
Each close is retried. If the broker answers with a requote, price change or reject, it tries again at once. If the filling mode is not accepted, it tries the other modes the symbol allows. If the market is closed, the symbol is not tradable or there are no prices, the close is deferred and tried again after a few seconds (a longer wait when the session is closed), and the log records the reason once per minute. If trading is not allowed or the terminal is disconnected, it writes WAIT to the log and keeps waiting.
Limitations
- It cannot press the AutoTrading button, and it cannot stop another EA or a manual order from being sent. It acts after the trade exists: it closes new positions and deletes new pending orders. A trade opened after the lock can stay open for a short time, until the next check.
- Closing is at market. The price you get depends on the broker and the market at that moment, so slippage applies. The final loss can be larger than the limit, especially with fast markets, gaps, wide spreads or positions that cannot be closed because the market is closed.
- It measures the loss from account equity, even when the scope is one symbol or a list of magic numbers. The scope decides which positions and orders are closed, not which losses are counted.
- It needs the terminal to be running, connected, with Algo Trading enabled and the EA allowed to trade. If the computer or terminal is off, it does nothing. For this reason a VPS is a common choice, but that is your decision.
- The daily reset uses server time. Check that the reset time matches the rule of your broker or prop firm. The tool does not know the rules of any firm.
- If you attach it in the middle of the day, the basis is rebuilt from history and the day-start equity is approximated with the balance.
- It does not replace the stop loss of each trade.
Strategy Tester demo
In the Strategy Tester, if InpTesterDemo is on, the EA opens a few demo trades by itself, only to show how the guard works. It opens up to 3 trades per day, in set time windows, alternating buy and sell, with a stop loss and take profit based on the ATR of H1 and a size of 1.5% of equity at that stop. It uses magic number 77001. The guard then reacts to the losses as in a real lock. This demo never runs outside the tester. On a real or demo account chart the EA opens no trades. The tester panel is shown in visual mode only and no state is saved between runs.
Notes
- Works on any symbol and any timeframe.
- One instance per account is enough. Two instances on the same account would both act on the same positions.
Risk warning: this tool limits exposure after losses have happened. It is not trading advice, it does not generate signals and it does not guarantee that any loss limit will be respected.
