Automatically detecting the broker's server time offset so my session filter doesn't break
my EA had inputs for the session start and end in server time, and every time the broker's clock shifted for daylight saving I had to remember to change them. I forgot more than once and it traded in the wrong hours for a week.
My fix was to calculate the offset in the code. The EA compares the server time with GMT, which the platform can give me, and works out the difference. Then I enter the session times in GMT, and the EA converts them to the current server time each tick or each bar. When the broker's clock changes, the offset changes with it automatically.
There are some caveats. In the strategy tester the GMT function may not behave the same way as live, so I tested separately. And around the daylight saving transitions there can be an hour of mismatch because the broker's switch and the reference one don't happen on the same day. I just disable trading for a few hours around those dates to be safe.
It's a small improvement but it removed one of my recurring mistakes.
Automatically detecting the broker's server time offset so my session filter doesn't break
Re: Automatically detecting the broker's server time offset so my session filter doesn't break
On the tester point, what I found is that the GMT time function in the tester just returns the server time, so the offset comes out as zero. For backtests I added an input where I set the offset manually, along with dates for when it changes. Live, the EA calculates it, and in the tester it uses the input. It is a bit clunky, but it means the backtest uses the same session hours as live trading would have.
It’s Fairman 
Re: Automatically detecting the broker's server time offset so my session filter doesn't break
A tip for checking the offset is working. I make the EA print the calculated offset and the converted session start and end times to the log once a day. After a clock change I look at the log the following Monday and check the times match what I expect. It takes a minute and it has already caught one case where my broker changed its server time on a different weekend than I had assumed. Since then the check is part of my Monday routine.
It’s Fairman 
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: Automatically detecting the broker's server time offset so my session filter doesn't break
One thing to guard against is that the GMT time the terminal reports comes from the computer's own clock and time zone settings, not from the broker. If a VPS clock drifts or its zone is set wrong, the offset is wrong too, and the platform will not complain.
The other input, the server time, is the time of the last tick received. On a quiet pair or just after the weekend open it can be minutes or even days old, so an offset worked out at that moment can come out as an odd figure like 2 hours 47 minutes.
Rounding the difference to the nearest whole hour and only accepting it when a tick arrived in the last minute removes most of that. A sanity range helps too, rejecting anything outside 0 to 4 hours for a broker known to run on GMT plus 2 or 3.
The daily log line would catch a bad value, but a check in the code stops the EA trading on it first. Falling back to the last good offset seems safer than trusting a strange number.
Is the VPS clock synced to an internet time server? Without that, a drift of a minute or two over a few weeks is not unusual on some machines.
The other input, the server time, is the time of the last tick received. On a quiet pair or just after the weekend open it can be minutes or even days old, so an offset worked out at that moment can come out as an odd figure like 2 hours 47 minutes.
Rounding the difference to the nearest whole hour and only accepting it when a tick arrived in the last minute removes most of that. A sanity range helps too, rejecting anything outside 0 to 4 hours for a broker known to run on GMT plus 2 or 3.
The daily log line would catch a bad value, but a check in the code stops the EA trading on it first. Falling back to the last good offset seems safer than trusting a strange number.
Is the VPS clock synced to an internet time server? Without that, a drift of a minute or two over a few weeks is not unusual on some machines.
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: Automatically detecting the broker's server time offset so my session filter doesn't break
The automatic offset will not fix one thing, which is that the sessions themselves move in GMT. The London open is 07:00 GMT in summer and 08:00 in winter, so a fixed GMT window drifts an hour against London twice a year.
This year the UK switch is on Sunday 25 October. From Monday the London open is at 09:00 in Lagos instead of 08:00, while a GMT based filter keeps the old hour.
The US follows on Sunday 1 November. For that one week the London and New York opens are four hours apart instead of five, and a broker on New York close time only moves from GMT plus 3 to plus 2 on the US date.
The detection code will follow the broker without help. The session times need their own rule, either London and New York local hours with the switch dates coded in, or summer and winter inputs.
Europe switches at 01:00 UTC on the last Sunday of March and October, while America uses the second Sunday in March and first Sunday in November. Both are easy to compute in the EA.
Does the filter use London hours or a fixed GMT window? If it is the second, next Monday's log line would show the drift.
This year the UK switch is on Sunday 25 October. From Monday the London open is at 09:00 in Lagos instead of 08:00, while a GMT based filter keeps the old hour.
The US follows on Sunday 1 November. For that one week the London and New York opens are four hours apart instead of five, and a broker on New York close time only moves from GMT plus 3 to plus 2 on the US date.
The detection code will follow the broker without help. The session times need their own rule, either London and New York local hours with the switch dates coded in, or summer and winter inputs.
Europe switches at 01:00 UTC on the last Sunday of March and October, while America uses the second Sunday in March and first Sunday in November. Both are easy to compute in the EA.
Does the filter use London hours or a fixed GMT window? If it is the second, next Monday's log line would show the drift.
-
FXS Prop Desk
- Posts: 1689
- Joined: Wed Oct 07, 2026 9:52 am
Re: Automatically detecting the broker's server time offset so my session filter doesn't break
Calculating the offset automatically is a much better solution than changing inputs twice a year. One thing to watch: during the weekend, TimeGMT() can be unreliable in the strategy tester, so many developers use a fixed offset table for testing and the live calculation only on real accounts.
Another detail is the gap between US and European daylight saving changes. For a few weeks in March and October, the broker's offset to New York changes while the offset to London does not, so session filters based on New York times need an extra check.
Another detail is the gap between US and European daylight saving changes. For a few weeks in March and October, the broker's offset to New York changes while the offset to London does not, so session filters based on New York times need an extra check.
FXS Official Team | The Idea Lab & Strategy
-
Shadow Trader
- Posts: 5594
- Joined: Wed Sep 09, 2026 5:19 pm
Re: Automatically detecting the broker's server time offset so my session filter doesn't break
A connection to a price feed can fail in two ways. The loud way is a dropped socket, which raises an error that code can catch. The quiet way is a connection that stays open while no useful data arrives, and an automated system may keep acting on prices that stopped updating minutes earlier.
Detecting the quiet failure requires a watchdog. The simplest form tracks the time since the last message and raises an alarm when the gap exceeds a threshold suited to the instrument, since a major pair rarely goes seconds without a tick while an exotic may. Heartbeat messages, where the provider offers them, separate a dead link from a genuinely quiet market.
Reconnection should wait progressively longer between attempts, with a small random offset, so a failing server is not hit by a flood of synchronized retries. A cap on the wait and a limit on attempts keep behaviour bounded.
Recovery has its own questions. Subscriptions must be re-established, any gap in data may need to be back-filled before indicators resume, and open orders should be reconciled before new ones are sent. Until all of this completes, a sensible system stays out of the market. Logging each disconnect with its duration later reveals whether the problem is the provider, the network or the host machine.
Detecting the quiet failure requires a watchdog. The simplest form tracks the time since the last message and raises an alarm when the gap exceeds a threshold suited to the instrument, since a major pair rarely goes seconds without a tick while an exotic may. Heartbeat messages, where the provider offers them, separate a dead link from a genuinely quiet market.
Reconnection should wait progressively longer between attempts, with a small random offset, so a failing server is not hit by a flood of synchronized retries. A cap on the wait and a limit on attempts keep behaviour bounded.
Recovery has its own questions. Subscriptions must be re-established, any gap in data may need to be back-filled before indicators resume, and open orders should be reconciled before new ones are sent. Until all of this completes, a sensible system stays out of the market. Logging each disconnect with its duration later reveals whether the problem is the provider, the network or the host machine.