from datetime import datetime
import MetaTrader5 as mt5
# display data on the MetaTrader 5 package
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
 
# import the 'pandas' module for displaying data obtained in the tabular form
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500)      # max table width to display
# import pytz module for working with time zone
import pytz
 
# establish connection to MetaTrader 5 terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()
 
# set time zone to UTC
timezone = pytz.timezone("Etc/UTC")
# create 'datetime' object in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(2025, 1, 10, 0, 0, tzinfo=timezone)
# get 10 EURUSD H4 bars starting from 01.10.2020 in UTC time zone
rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_H1, utc_from, 1)

# Check without any UTC reference
dt_from = datetime(2025, 1, 10, 0, 0)
ratesCheck = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_H1, dt_from, 1)

 
# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()
# display each element of obtained data in a new line
print("Display obtained 'UTC TZ' data 'as is'")
for rate in rates:
   print(rate)
 
# create DataFrame out of the obtained data
rates_frame = pd.DataFrame(rates)
# convert time in seconds into the datetime format
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
                           
# display data
print("\nDisplay dataframe with date_from using UTC")
print(rates_frame)  

# display each element of obtained data in a new line
print("Display obtained 'local TZ' data 'as is'")
for rate in rates:
   print(ratesCheck)

# create DataFrame out of the obtained data
rates_frame_check = pd.DataFrame(ratesCheck)
# convert time in seconds into the datetime format
rates_frame_check['time']=pd.to_datetime(rates_frame_check['time'], unit='s')
                           
# display data
print("\nDisplay dataframe with date_from using local time (no TZ specified)")
print(rates_frame_check)  