Is there a way to know the Broker's Timezone using the Python API for MT5?

 

I've come across a couple threads mentioning that this isn't possible, however, they are (atleast) over an year old. Was wondering if there have been any updates, and if anyone has found ways to get the Broker Timezone on Python?

All the "time" related data from the Python API are in the Broker's Timezone, and I'm looking to convert them into a different timezone - which isn't possible unless I know what the Broker's Timezone is. I know that I can manually look at the offset between the trade time (on MT5) and my local time and hardcode that, however, this is not feasible in my case since I'm looking to have the Timezone configurable - and not just limited to my local Timezone. 

Would be great if someone found a solution that works!

Documentation on MQL5: Date and Time / TimeLocal
Documentation on MQL5: Date and Time / TimeLocal
  • www.mql5.com
TimeLocal - Date and Time - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yashit Maheshwary:

I've come across a couple threads mentioning that this isn't possible, however, they are (atleast) over an year old. Was wondering if there have been any updates, and if anyone has found ways to get the Broker Timezone on Python?

All the "time" related data from the Python API are in the Broker's Timezone, and I'm looking to convert them into a different timezone - which isn't possible unless I know what the Broker's Timezone is. I know that I can manually look at the offset between the trade time (on MT5) and my local time and hardcode that, however, this is not feasible in my case since I'm looking to have the Timezone configurable - and not just limited to my local Timezone

Would be great if someone found a solution that works!

What prevent you to do it without hardcoding anything ?

Python is perfectly able to give your local timezone, from that you can deduct the broker server current GMT time shift.

The real difficulty comes with DST management if you need to work on historical data.

 
Alain Verleyen #:
Python is perfectly able to give your local timezone, from that you can deduct the broker server current GMT time shift.

How can I obtain the "broker server current GMT time shift" from the Python API? Without knowing what the time shift is, how can I deduct the time difference between my local timezone and the broker's timezone?

I plan to configure more brokers in the future, which is why I would like to do this programmatically. 

 
Yashit Maheshwary #: How can I obtain the "broker server current GMT time shift" from the Python API? Without knowing what the time shift is, how can I deduct the time difference between my local timezone and the broker's timezone? I plan to configure more brokers in the future, which is why I would like to do this programmatically. 

I was under the impression that the Python API offered functions equivalent to MQL5's TimeCurrent and TimeTradeServer, but it seems it does not.

I'm not a Python programmer, so I don't know if there are other workarounds to the issue, but the only thing I can think of at the moment, is to wait for an incomming tick, and then calculate the time difference, using the tick's timestamp and comparing it to the current UTC time.

 
Fernando Carreiro #:

I was under the impression that the Python API offered functions equivalent to MQL5's TimeCurrent and TimeTradeServer, but it seems it does not.

I'm not a Python programmer, so I don't know if there are other workarounds to the issue, but the only thing I can think of at the moment, is to wait for an incomming tick, and then calculate the time difference, using the tick's timestamp and comparing it to the current UTC time.

Another option is to monitor the "time" from symbol_info and when it changes, compre it to the UTC for calculating the difference.

This is somewhat similar to the tick timestamp example in the previous post.

Documentation on MQL5: Python Integration / symbol_info
Documentation on MQL5: Python Integration / symbol_info
  • www.mql5.com
symbol_info - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Yashit Maheshwary #:

How can I obtain the "broker server current GMT time shift" from the Python API? Without knowing what the time shift is, how can I deduct the time difference between my local timezone and the broker's timezone?

I plan to configure more brokers in the future, which is why I would like to do this programmatically. 

You know your local time.

You know your local timezone, so GMT shift, it's easy to get it with Python.

When you get live ticks data, you get the "live" server time, like Fernando suggested.

From these information it's trivial to calculate the broker server time shift related to GMT. All of that can be done by code, there is no need to hardcode anything.

 
Alain Verleyen #:

You know your local time.

You know your local timezone, so GMT shift, it's easy to get it with Python.

When you get live ticks data, you get the "live" server time, like Fernando suggested.

From these information it's trivial to calculate the broker server time shift related to GMT. All of that can be done by code, there is no need to hardcode anything.

Thank you for this approach. This works for the most part, however, when market closes, there are no live ticks obtained. Is there any workaround for that? I need to run a sync of positions every couple hours, so it would be ideal to have a continuous flow of server time, if possible.

I'm thinking to hardcode the market open/close times, so that I can know how long ago was the last tick. This would allow me to get the time-shift. However, this approach does not seem to be very ideal in my opinion. The API should just have a way to return the current server/broker time at all times.

I also thought of using any crypto tickers (since that market is open 24/7), however, I cannot assume that crypto tickers would be available on all brokers (since I'm making a generalized tool, to work on any broker).

Just checking in to see if there's a better approach?

 

Yashit Maheshwary #:

I'm thinking to hardcode the market open/close times, so that I can know how long ago was the last tick. This would allow me to get the time-shift. However, this approach does not seem to be very ideal in my opinion. The API should just have a way to return the current server/broker time at all times.


Actually, I just checked this. This wouldn't work when markets close. Brokers seems to close their shops at irregular intervals? I see a couple ending at 23:59 GMT+2, and some ending at 23:59 GMT+3. I'm not sure how to get past this variability.

 
Yashit Maheshwary #: Actually, I just checked this. This wouldn't work when markets close. Brokers seems to close their shops at irregular intervals? I see a couple ending at 23:59 GMT+2, and some ending at 23:59 GMT+3. I'm not sure how to get past this variability.

Save the state when the markets are open. Save it to a file so that it can be read when the program restarts and update it when the market is open.

You can also manually set a starting value for a new account in the file. That will be much better than hard coding it in your program.

Reason: