MT5 global variables for a daily trade counter: surviving a terminal restart
An EA limits itself to 3 trades a day. If the counter is a plain integer inside the EA, it resets when the terminal restarts or the EA is reloaded, so a restart at 10:40 after two trades quietly gives the day three more. Terminal global variables, set with GlobalVariableSet, are stored by the terminal and survive restarts.
Example key: "tc_EURUSD_7001_20261029", with the symbol, the magic number and the date in it so every day starts from a new variable. On each entry, increase it: GlobalVariableSet(name, GlobalVariableGet(name) + 1). Before trading, read it and compare against the limit.
Facts to keep in mind. Values are doubles, not integers. Names are limited to 63 characters. A variable that hasn't been accessed for four weeks is deleted by the terminal. They are shared by every EA and script in that terminal, so two EAs using the same name will trample each other. GlobalVariableGet returns 0 for a name that doesn't exist, which for a counter is fine, but it also hides a typo in the name. GlobalVariableCheck tells you whether the variable exists, so the missing case can be handled on purpose.
MT5 global variables for a daily trade counter: surviving a terminal restart
-
FXS HongKong Scalper
- Posts: 407
- Joined: Sat Oct 24, 2026 10:15 pm
Re: MT5 global variables for a daily trade counter: surviving a terminal restart
Using the date in the key is a clean approach. One thing to keep in mind is which date to use. If the EA uses the server date, the day changes at the server's midnight, which on many brokers is 17:00 New York. For traders in Asia, that means the counter resets in the early morning local time. If the trading day is defined differently, for example by the Tokyo open, the key should be built from that time instead. Global variables older than a few weeks can also be removed to keep the list short.
FXS Official Team | Hong Kong Session Desk
Re: MT5 global variables for a daily trade counter: surviving a terminal restart
Doubles hold whole numbers exactly up to 2 to the 53rd power, about 9 quadrillion, so counters and even order tickets are safe. The name limit is the thing that catches people. A descriptive key like MyScalperEA_v2_EURUSD_magic20260001_20261029_count is already 50 characters, and adding a second symbol or a longer EA name takes it past 63. Check StringLen when building the key, and keep a short prefix. If the name is too long, the call fails and the counter silently stays at 0, so the limit never trips and the EA trades past it.
It’s Fairman 
Re: MT5 global variables for a daily trade counter: surviving a terminal restart
I'm not certain how often the terminal writes these variables to disk, and a hard crash or a forced shutdown may lose recent changes, so I wouldn't rely on them alone. A sturdier counter recomputes from the account history: select history from the start of today, loop through deals, and count those with DEAL_ENTRY_IN and your magic number. Do it at startup and compare it with the stored counter. If they differ, trust the history and overwrite the variable. The history is what the broker recorded, so it can't be out of step with what actually happened on the account, whatever state the EA or the terminal was in.
It’s Fairman 
Re: MT5 global variables for a daily trade counter: surviving a terminal restart
Flags have the opposite problem and linger. Say the EA sets trading_halted to 1 after a daily loss limit is hit. If the key has no date in it, the flag is still 1 tomorrow, and the EA won't trade until someone notices or four weeks pass. Two ways round it: put the date in the key, so a fresh day means a fresh variable, or store the date as the value and compare it with today's. GlobalVariableTime returns the time of the last access, which can also be used to ignore anything not touched today. Whichever you choose, test it by changing the PC date forward one day and watching what the EA does.
It’s Fairman 
Re: MT5 global variables for a daily trade counter: surviving a terminal restart
Two EAs on one terminal can both see the same signal and open at the same time, doubling the position. GlobalVariableSetOnCondition works as a lock. It sets the variable to a new value only if it currently has a given value, and it does the check and the write as one step. Create a variable, say lock_EURUSD, set to 0 for free. Each EA tries to set it to the current timestamp if it's 0, and the one that gets true opens the trade and sets it back to 0 afterwards. Because a crashed EA can leave it held, treat a timestamp more than 30 seconds old as stale and reset it.
It’s Fairman 