Help with Time Zone Handling and Data Fetching in MT5 - page 2

 

Ok, so let's create datetime in UTC time zone.

    symbol = "BITCOIN"
    timeframe = mt5.TIMEFRAME_M1

    # Step 1: Local time (Budapest)
    local_now = datetime.now()
    print("Local time:", local_now)

    # Step 2: Convert local to UTC
    utc_now = local_now.astimezone(pytz.utc)
    print("UTC time now:", utc_now)

    # Step 3: Define from and to in UTC
    from_datetime = utc_now - timedelta(hours=10)
    to_datetime = utc_now

    # Step 4: Fetch
    mt5.initialize()
    rates = mt5.copy_rates_range(symbol, timeframe, from_datetime, to_datetime)

    # Step 5: Create DataFrame
    df = pd.DataFrame(rates)
    df["datetime"] = pd.to_datetime(df["time"], unit="s", utc=True).dt.tz_convert('Asia/Nicosia')
    df = df.set_index('datetime')
    print(df[["open", "high", "low", "close"]])

This will output:

                               open      high       low     close
datetime
2025-04-27 05:17:00+03:00  94586.48  94630.05  94586.48  94629.36
2025-04-27 05:18:00+03:00  94629.36  94646.65  94629.36  94646.65
2025-04-27 05:19:00+03:00  94646.65  94668.29  94639.95  94667.94
2025-04-27 05:20:00+03:00  94667.94  94673.73  94652.65  94660.34
2025-04-27 05:21:00+03:00  94660.34  94681.54  94652.81  94653.14
...                             ...       ...       ...       ...
2025-04-27 15:12:00+03:00  94023.46  94031.36  94022.96  94022.96
2025-04-27 15:13:00+03:00  94022.96  94028.36  94022.96  94024.36
2025-04-27 15:14:00+03:00  94024.36  94024.36  94024.36  94024.36
2025-04-27 15:15:00+03:00  94024.36  94042.25  94024.36  94042.25
2025-04-27 15:16:00+03:00  94042.25  94048.53  94042.25  94048.45

So closing price at 15:16 (UTC+3, Cyprus-time) = 14:16 (UTC+2, my local time) should be 94048.45.

But it's not true. 14:16 is right now, and prices on that candle at the moment:

                               open      high       low     close
2025-04-27 14:16:00+02:00  93726.31  93726.31  93724.21  93724.21
 

The only way to fetch the current, latest prices, is by adding 3 hours of offset. Why?

to_datetime = utc_now + timedelta(hours=3)

where utc_now is:

UTC time now: 2025-04-27 12:19:36.873149+00:00


So I'm not able to fetch the correct, up-to-date price values if I use UTC, only if I use broker's local time.

 

Even better, I fetched the broker time (for safety, without timezone information):

Broker time without timezone: 2025-04-27 15:43:40.261526

and input it into the data fetcher formula:

    from_datetime = utc_now - timedelta(hours=10)
    to_datetime = broker_time_without_timezone

    mt5.initialize()
    rates = mt5.copy_rates_range(symbol, timeframe, from_datetime, to_datetime)

    df = pd.DataFrame(rates)
    df["datetime"] = pd.to_datetime(df["time"], unit="s", utc=True)
    df = df.set_index('datetime')
    print(df[["open", "high", "low", "close"]])

Even this doesn't return the correct, current prices:

                               open      high       low     close
datetime
2025-04-27 02:48:00+00:00  94493.82  94502.62  94484.34  94502.62
2025-04-27 02:49:00+00:00  94502.62  94532.61  94492.80  94492.86
2025-04-27 02:50:00+00:00  94492.94  94492.96  94477.33  94477.33
2025-04-27 02:51:00+00:00  94477.33  94477.33  94465.30  94476.33
2025-04-27 02:52:00+00:00  94476.33  94487.53  94464.53  94465.04
...                             ...       ...       ...       ...
2025-04-27 13:43:00+00:00  93915.81  93915.81  93915.81  93915.81
2025-04-27 13:44:00+00:00  93915.81  93938.97  93911.60  93937.30
2025-04-27 13:45:00+00:00  93937.30  93937.30  93933.01  93933.01
2025-04-27 13:46:00+00:00  93933.01  93967.69  93933.01  93946.20
2025-04-27 13:47:00+00:00  93946.20  93946.20  93930.01  93930.01

But expected last row is:

          open      high       low     close
1616  93778.08  93778.08  93691.11  93726.51
 
Levente Csibi #:

The only way to fetch the current, latest prices, is by adding 3 hours of offset. Why?

where utc_now is:

UTC time now: 2025-04-27 12:19:36.873149+00:00


So I'm not able to fetch the correct, up-to-date price values if I use UTC, only if I use broker's local time.

Do you take it into account that different programming languages use different notations for UTC offset?

MQL5's built-in functions such as as TimeGMTOffset() and other time-related, use the offset notation, that positive time zones, such as GMT+3, are denoted by negative offsets, such as -10800, and vice versa. This notation is used by some other programming languages, like JaveScript, but there is also other languages (like Python), which denote positive time zones by positive offsets, and negative time zones by negative offsets.

 

Привет @Stanislav Korotky

I didn't take it into account but I still fail to understand how the +/- sign affects my calculations. If I subtract the timedelta of 3 hours, I won't get the correct candle values even close.  

Stanislav Korotky
Stanislav Korotky
  • 2025.04.10
  • www.mql5.com
Trader's profile
 
Levente Csibi #:

Furthermore, I know that my broker is located in Cyprus, so the time there is well-known: UTC+3. 

Yet, I don't understand that if I want to fetch data till 10:34 AM according to my local time (UTC+2), why do I have to input 13:34 in the command, when the difference between me and my broker is supposedly only 1 hour? 

Hello Levente,

Most brokers set their timezone for price data which is somehow linked to US time. Say my broker is situated in Australia but its server time (which you will get along with prices are returned) to GMT+3 while US Daylight saying is in effect, else it is set to GTM+2.

So brokers location does not matter, but what time he is following is matter.

Check these links for details on timezone https://www.mql5.com/en/users/amrali and https://www.mql5.com/en/users/gooly. Both of them have created library to handle time zones.

Check this link https://www.mql5.com/en/docs/python_metatrader5/mt5copyratesrange_py on how to adjust time zone (provided you know time zone followed by Broker) while retrieving data into Python.

You can also ask your Broker which time zone they follow.

Hope this helps you man.

amrali
amrali
  • 2025.03.18
  • www.mql5.com
Trader's profile
 
Levente Csibi #:

The only way to fetch the current, latest prices, is by adding 3 hours of offset. Why?

where utc_now is:

UTC time now: 2025-04-27 12:19:36.873149+00:00


So I'm not able to fetch the correct, up-to-date price values if I use UTC, only if I use broker's local time.

Yes, it looks like the documentation is incorrect, and the timestamps in the quotes are returned in the timezone of the server, the same way as on charts and in MT5 history.

Forum on trading, automated trading systems and testing trading strategies

python api does not return UTC times

Fernando Carreiro, 2022.02.02 16:47

It seems that the "Python" documentation is in conflict with the "MQL5" documentation. You probably should take the MQL version as being the most correct, and in that case it will always be in Broker server time and not UTC.