Technical question on DateTime with Python integration

 

Hi everyone, just started working with MQL5 Python integration and found a question that I just can't get right

import MetaTrader5 as mt5

time_now = datetime.now(pytz.timezone("UTC"))
today_start = time_now.replace(hour=0, minute=0, second=0, microsecond=0)

print(f"START TIME - {today_start}\n")
print(f"TIME NOW - {time_now}\n")

bars = mt5.copy_rates_range( symbol_name, mt5.TIMEFRAME_H1, today_start, time_now ) 
df = pd.DataFrame(bars)
for index, row in df.iterrows(): print(  f"Time - {row.time}, "  f"Open - {row.open}, Close - {row.close}")

When i use datetime with UTC timezone to get myself todays 00:00:00 dateime and then call 
mt5.copy_rates_range
i get results which seem understandable at first:

START TIME - 2023-11-27 00:00:00+00:00

TIME NOW - 2023-11-27 10:18:52.154960+00:00

Time - 1701043200.0, Open - 163.444, Close - 163.612
...
Time - 1701079200.0, Open - 163.139, Close - 163.145
The timestamp 1701043200.0 converts to datetime UTC as I expected it to be - 2023-11-27 00:00:00+00:00

print(datetime.fromtimestamp(1701043200, tz=pytz.timezone("UTC")))

# 2023-11-27 00:00:00+00:00
The problem starts at the end of the sequence of data The timestamp 1701079200.0 converts to datetime UTC as I originally expected - 2023-11-27 10:00:00+00:00 corresponding with the limit (time_now) I used in my request but than i noticed that Close of the latest data record is not changing, and I expected it to change at subsequent requests (as the latest candle should still be moving). after adding a few hours to the copy_rates_range request I found that there are two more timestamps with data:
bars = mt5.copy_rates_range(
    symbol_name, mt5.TIMEFRAME_H1, today_start, time_now + timedelta(hours=2)
)  // added 2 hours to the request time limit
gave me the following picture:
Time - 1701043200.0, Open - 163.444, Close - 163.612
...
Time - 1701079200.0, Open - 163.139, Close - 163.145
Time - 1701082800.0, Open - 163.143, Close - 163.45
Time - 1701086400.0, Open - 163.446, Close - 163.416
Ad now the latest record changes it's Close value on subsequent requests meaning that it is indeed the latest candle of the data but the problem i face is that the timestamp of that data is converted to datetime UTC as 2023-11-27 12:00:00+00:00 Which seems impossible to me as it is 10:35 UTC now, at the moment i'm making the request Please help me understand what i'm getting wrong or where is my mistake

 
Delacrua:

Hi everyone, just started working with MQL5 Python integration and found a question that I just can't get right


When i use datetime with UTC timezone to get myself todays 00:00:00 dateime and then call  i get results which seem understandable at first:

The timestamp 1701043200.0 converts to datetime UTC as I expected it to be - 2023-11-27 00:00:00+00:00

The problem starts at the end of the sequence of data The timestamp 1701079200.0 converts to datetime UTC as I originally expected - 2023-11-27 10:00:00+00:00 corresponding with the limit (time_now) I used in my request but than i noticed that Close of the latest data record is not changing, and I expected it to change at subsequent requests (as the latest candle should still be moving). after adding a few hours to the copy_rates_range request I found that there are two more timestamps with data: gave me the following picture: Ad now the latest record changes it's Close value on subsequent requests meaning that it is indeed the latest candle of the data but the problem i face is that the timestamp of that data is converted to datetime UTC as 2023-11-27 12:00:00+00:00 Which seems impossible to me as it is 10:35 UTC now, at the moment i'm making the request Please help me understand what i'm getting wrong or where is my mistake

I use Python for other non MT5 projects, but out of curiosity I took a look at this - unfortunately I could not get your code to compile (I use Eclipse and PyDev)

Can you paste a complete compilable unit please? 

 
R4tna C #:

I use Python for other non MT5 projects, but out of curiosity I took a look at this - unfortunately I could not get your code to compile (I use Eclipse and PyDev)

Can you paste a complete compilable unit please? 

Added a .py with code
the main problem is that time now shows me 6:33 UTC while the data of latest obtained Symbol candle shows 8:00 UTC (at the moment ofc)

Files:
 
Delacrua #:

Added a .py with code
the main problem is that time now shows me 6:33 UTC while the data of latest obtained Symbol candle shows 8:00 UTC (at the moment ofc)

Thanks - will take a look and if I find anything will update... 

 
R4tna C #:

Thanks - will take a look and if I find anything will update... 


thanks a lot anyway )