How to get data from MT5 terminal using python

 
Hello there,

How is it that a few weeks ago I fetched hourly data from mt5 through python but now I cannot? I seem to only get data for the daily, weekly or monthly timeframes


Initially I thought it was the broker I was using. I registered two more accounts with different other brokers to find out. I still  wasn't  able to fetch the data after many attempts.

What's going on? Any help? Any changes in MQL5?

Find the code below


my output from the code is: Empty DataFrame Columns: [] Index: []

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', 5000) # number of columns to be displayed
pd.set_option('display.width', 15000) # 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' objects in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(2015, 1, 1, tzinfo=timezone)
utc_to = datetime(2020, 5, 5, tzinfo=timezone)

# get bars from USDZAR H1 within the interval of 2020.01.01 00:00 - 2020.05.05 in UTC time zone

UCrateH = mt5.copy_rates_range("USDCAD", mt5.TIMEFRAME_H1, utc_from, utc_to)

# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()

# create DataFrame out of the obtained data
UZH= pd.DataFrame(UCrateH)
print(UZH.head(3))
 
I found out where my issue was coming from...I had messed up with the maximum number of bars in my mt5 terminal under tools! I am all good again.