The measurement side of this has been covered well (day id from TimeTradeServer(), snapshot on first tick of the new day, GlobalVariablesFlush(), and not inventing a midnight equity figure from history after a mid-day restart).
The part I would add is what the EA does after the limit is true, because that is where a lock becomes dangerous on a multi-EA account.
A daily loss cap can be an account-level prop rule (flatten everything) or an EA-level guard (only this magic, optionally only this symbol). Those are different behaviours and are worth making explicit. Closing every position on the account from an EA that was only meant to police its own trades will take out other systems, and a manual trade, without the user expecting it.
When I want the EA-level version, I iterate PositionsTotal() backward, match POSITION_MAGIC (and usually POSITION_SYMBOL), and close by ticket. I also stop placing new orders until the next day id, including pending orders with that magic. I do not use PositionsTotal() as a live index after a close; the ticket list is collected first.
Call the check from OnTimer as well as OnTick. A quiet symbol will not tick at the reset hour, and a lock that only runs in OnTick can sit there with a breached limit until the next quote.
Closing is not instantaneous. If the rule is "1% of equity", start the close a little early; the fills, spread and slippage on the way out still count. Several replies already said this, and it is the part that shows up live more than in the tester.
On a netting account, PositionClose closes the whole position. On a hedging account it closes one ticket. If the account is netting and several EAs share the symbol, an EA-level lock cannot close "only its own" volume — there is only one position. That is a good reason to keep a flatten-everything option explicit, and to test both account modes.
I would treat "monitor only" (log and alert, do not close) as a third mode. It is the safe default while the measurement is being verified.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
The measurement side of this has been covered well (day id from TimeTradeServer(), snapshot on first tick of the new day, GlobalVariablesFlush(), and not inventing a midnight equity figure from history after a mid-day restart).
The part I would add is what the EA does after the limit is true, because that is where a lock becomes dangerous on a multi-EA account.
A daily loss cap can be an account-level prop rule (flatten everything) or an EA-level guard (only this magic, optionally only this symbol). Those are different behaviours and are worth making explicit. Closing every position on the account from an EA that was only meant to police its own trades will take out other systems, and a manual trade, without the user expecting it.
When I want the EA-level version, I iterate PositionsTotal() backward, match POSITION_MAGIC (and usually POSITION_SYMBOL), and close by ticket. I also stop placing new orders until the next day id, including pending orders with that magic. I do not use PositionsTotal() as a live index after a close; the ticket list is collected first.
A few operational details that go with this: