Python – Metatrader API returns no data even though a manual search (using the GUI) shows that there is data

I’m trying to get all ticks from all symbols in the B3 exchange. But, for some reason, for specific dates I get empty vectors as result. That usually happens with highly liquid equities.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import vectorbt as vbt
from datetime import datetime
import MetaTrader5 as mt5
import pandas as pd
import pytz
from IPython.display import display, HTML, Markdown
from pandas.tseries.offsets import CustomBusinessDay
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday
import os
from tqdm import tqdm
pd.set_option('display.expand_frame_repr', True)
pd.set_option('display.min_rows', 100)
login, password = open(r'C:UsersJoãoWeckerleDownloadsmeta.txt').read().split()
timezone = pytz.timezone('Etc/GMT-3')
class feriados(AbstractHolidayCalendar):
rules = [
Holiday('New Years Day', month=1, day=1, year=2022),
Holiday('May Day', month=5, day=1, year=2022),
Holiday('14/04', month=4, day=17, year=2022),
Holiday('Constitution Day', month=4, day=21, year=2022),
Holiday('Pentecost Sunday', month=5, day=1, year=2022),
Holiday('Corpus Christi', month=9, day=7, year=2022),
Holiday('Assumption of the Blessed Virgin Mary', month=10, day=12, year=2022),
Holiday('All Saints Day', month=11, day=2, year=2022),
Holiday('Independence Day', month=11, day=15, year=2022),
Holiday('Christmas Day', month=12, day=25, year=2022),
Holiday('New Years Day', month=1, day=1, year=2023),
Holiday('Carnival', month=2, day=20, year=2023),
Holiday('Carnival', month=2, day=21, year=2023),
Holiday('Passion of Christ', month=4, day=7, year=2023),
Holiday('TakeTooths', month=4, day=21, year=2023),
Holiday('Labour Day', month=5, day=1, year=2023),
Holiday('Corpus Christi', month=6, day=8, year=2023),
Holiday('Independence Day', month=9, day=7, year=2023),
Holiday('Nossa Sr.a Aparecida - Padroeira do Brasil', month=10, day=12, year=2023),
Holiday('Finados', month=11, day=2, year=2023),
Holiday('Proclamação da República', month=11, day=15, year=2023),
Holiday('Christmas Day', month=12, day=25, year=2023),
Holiday('New Years Day', month=1, day=1, year=2024),
Holiday('Carnival', month=2, day=12, year=2024),
Holiday('Carnival', month=2, day=13, year=2024),
Holiday('Passion of Christ', month=3, day=29, year=2024),
Holiday('TakeTooths', month=4, day=21, year=2024),
Holiday('Labour Day', month=5, day=1, year=2024),
Holiday('Corpus Christi', month=5, day=30, year=2024),
Holiday('Independence Day', month=9, day=7, year=2024),
Holiday('Nossa Sr.a Aparecida - Padroeira do Brasil', month=10, day=12, year=2024),
Holiday('Finados', month=11, day=2, year=2024),
Holiday('Proclamação da República', month=11, day=15, year=2024),
Holiday('Black Consciousness', month=11, day=20, year=2024),
Holiday('Christmas Day', month=12, day=25, year=2024)]
BUSINESS_DAY = CustomBusinessDay(
calendar=feriados(),
weekmask='Mon Tue Wed Thu Fri')
agora = pd.Timestamp.now()
ontem = (agora - 1*BUSINESS_DAY)
anteontem = (agora - 2*BUSINESS_DAY).strftime('%Y%m%d')
mt5.shutdown()
if not mt5.initialize(login = 59307110, server= "XPMT5-DEMO", password = password):
print("initialize() failed, error code =",mt5.last_error())
mt5.shutdown()
# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())
inicio = datetime(ontem.year,ontem.month,ontem.day, tzinfo = timezone)
fim = datetime(ontem.year, ontem.month, ontem.month, tzinfo = timezone)
tickers = pd.read_csv(fr'C:UsersJoãoWeckerleOneDrive - B6 CapitalDados B3TradeInformationConsolidatedFile_{anteontem}_1.csv', low_memory=False,
skiprows=1, delimiter=';')
tickers = tickers['TckrSymb']
ticks = mt5.copy_ticks_range(
'PETR4', # Símbolo desejado
inicio, # Data Inicial
fim, # Data Final
mt5.COPY_TICKS_ALL) # Flag da busca. Podemos passar também mt5.COPY_TICKS_TRADE e mt5.COPY_TICKS_INFO.
ticks = pd.DataFrame(ticks)
</code>
<code>import vectorbt as vbt from datetime import datetime import MetaTrader5 as mt5 import pandas as pd import pytz from IPython.display import display, HTML, Markdown from pandas.tseries.offsets import CustomBusinessDay from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday import os from tqdm import tqdm pd.set_option('display.expand_frame_repr', True) pd.set_option('display.min_rows', 100) login, password = open(r'C:UsersJoãoWeckerleDownloadsmeta.txt').read().split() timezone = pytz.timezone('Etc/GMT-3') class feriados(AbstractHolidayCalendar): rules = [ Holiday('New Years Day', month=1, day=1, year=2022), Holiday('May Day', month=5, day=1, year=2022), Holiday('14/04', month=4, day=17, year=2022), Holiday('Constitution Day', month=4, day=21, year=2022), Holiday('Pentecost Sunday', month=5, day=1, year=2022), Holiday('Corpus Christi', month=9, day=7, year=2022), Holiday('Assumption of the Blessed Virgin Mary', month=10, day=12, year=2022), Holiday('All Saints Day', month=11, day=2, year=2022), Holiday('Independence Day', month=11, day=15, year=2022), Holiday('Christmas Day', month=12, day=25, year=2022), Holiday('New Years Day', month=1, day=1, year=2023), Holiday('Carnival', month=2, day=20, year=2023), Holiday('Carnival', month=2, day=21, year=2023), Holiday('Passion of Christ', month=4, day=7, year=2023), Holiday('TakeTooths', month=4, day=21, year=2023), Holiday('Labour Day', month=5, day=1, year=2023), Holiday('Corpus Christi', month=6, day=8, year=2023), Holiday('Independence Day', month=9, day=7, year=2023), Holiday('Nossa Sr.a Aparecida - Padroeira do Brasil', month=10, day=12, year=2023), Holiday('Finados', month=11, day=2, year=2023), Holiday('Proclamação da República', month=11, day=15, year=2023), Holiday('Christmas Day', month=12, day=25, year=2023), Holiday('New Years Day', month=1, day=1, year=2024), Holiday('Carnival', month=2, day=12, year=2024), Holiday('Carnival', month=2, day=13, year=2024), Holiday('Passion of Christ', month=3, day=29, year=2024), Holiday('TakeTooths', month=4, day=21, year=2024), Holiday('Labour Day', month=5, day=1, year=2024), Holiday('Corpus Christi', month=5, day=30, year=2024), Holiday('Independence Day', month=9, day=7, year=2024), Holiday('Nossa Sr.a Aparecida - Padroeira do Brasil', month=10, day=12, year=2024), Holiday('Finados', month=11, day=2, year=2024), Holiday('Proclamação da República', month=11, day=15, year=2024), Holiday('Black Consciousness', month=11, day=20, year=2024), Holiday('Christmas Day', month=12, day=25, year=2024)] BUSINESS_DAY = CustomBusinessDay( calendar=feriados(), weekmask='Mon Tue Wed Thu Fri') agora = pd.Timestamp.now() ontem = (agora - 1*BUSINESS_DAY) anteontem = (agora - 2*BUSINESS_DAY).strftime('%Y%m%d') mt5.shutdown() if not mt5.initialize(login = 59307110, server= "XPMT5-DEMO", password = password): print("initialize() failed, error code =",mt5.last_error()) mt5.shutdown() # request connection status and parameters print(mt5.terminal_info()) # get data on MetaTrader 5 version print(mt5.version()) inicio = datetime(ontem.year,ontem.month,ontem.day, tzinfo = timezone) fim = datetime(ontem.year, ontem.month, ontem.month, tzinfo = timezone) tickers = pd.read_csv(fr'C:UsersJoãoWeckerleOneDrive - B6 CapitalDados B3TradeInformationConsolidatedFile_{anteontem}_1.csv', low_memory=False, skiprows=1, delimiter=';') tickers = tickers['TckrSymb'] ticks = mt5.copy_ticks_range( 'PETR4', # Símbolo desejado inicio, # Data Inicial fim, # Data Final mt5.COPY_TICKS_ALL) # Flag da busca. Podemos passar também mt5.COPY_TICKS_TRADE e mt5.COPY_TICKS_INFO. ticks = pd.DataFrame(ticks) </code>
import vectorbt as vbt
from datetime import datetime
import MetaTrader5 as mt5
import pandas as pd
import pytz
from IPython.display import display, HTML, Markdown
from pandas.tseries.offsets import CustomBusinessDay
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday
import os
from tqdm import tqdm

pd.set_option('display.expand_frame_repr', True)
pd.set_option('display.min_rows', 100)

login, password = open(r'C:UsersJoãoWeckerleDownloadsmeta.txt').read().split()

timezone = pytz.timezone('Etc/GMT-3')


class feriados(AbstractHolidayCalendar):
    rules = [
        Holiday('New Years Day', month=1, day=1, year=2022),
        Holiday('May Day', month=5, day=1, year=2022),
        Holiday('14/04', month=4, day=17, year=2022),
        Holiday('Constitution Day', month=4, day=21, year=2022),
        Holiday('Pentecost Sunday', month=5, day=1, year=2022),
        Holiday('Corpus Christi', month=9, day=7, year=2022),
        Holiday('Assumption of the Blessed Virgin Mary', month=10, day=12, year=2022),
        Holiday('All Saints Day', month=11, day=2,  year=2022),
        Holiday('Independence Day', month=11, day=15, year=2022),
        Holiday('Christmas Day', month=12, day=25, year=2022),
        
        
        Holiday('New Years Day', month=1, day=1, year=2023),
        Holiday('Carnival', month=2, day=20, year=2023),
        Holiday('Carnival', month=2, day=21, year=2023),
        Holiday('Passion of Christ', month=4, day=7, year=2023),
        Holiday('TakeTooths', month=4, day=21, year=2023),
        Holiday('Labour Day', month=5, day=1, year=2023),
        Holiday('Corpus Christi', month=6, day=8, year=2023),
        Holiday('Independence Day', month=9, day=7, year=2023),
        Holiday('Nossa Sr.a Aparecida - Padroeira do Brasil', month=10, day=12, year=2023),
        Holiday('Finados', month=11, day=2, year=2023),
        Holiday('Proclamação da República', month=11, day=15, year=2023),
        Holiday('Christmas Day', month=12, day=25, year=2023),
        
        Holiday('New Years Day', month=1, day=1, year=2024),
        Holiday('Carnival', month=2, day=12, year=2024),
        Holiday('Carnival', month=2, day=13, year=2024),
        Holiday('Passion of Christ', month=3, day=29, year=2024),
        Holiday('TakeTooths', month=4, day=21, year=2024),
        Holiday('Labour Day', month=5, day=1, year=2024),
        Holiday('Corpus Christi', month=5, day=30, year=2024),
        Holiday('Independence Day', month=9, day=7, year=2024),
        Holiday('Nossa Sr.a Aparecida - Padroeira do Brasil', month=10, day=12, year=2024),
        Holiday('Finados', month=11, day=2, year=2024),
        Holiday('Proclamação da República', month=11, day=15, year=2024),
        Holiday('Black Consciousness', month=11, day=20, year=2024),
        Holiday('Christmas Day', month=12, day=25, year=2024)]
BUSINESS_DAY = CustomBusinessDay(
    calendar=feriados(),
    weekmask='Mon Tue Wed Thu Fri')

agora = pd.Timestamp.now()
ontem = (agora -  1*BUSINESS_DAY)
anteontem = (agora -  2*BUSINESS_DAY).strftime('%Y%m%d')
   
mt5.shutdown()

if not mt5.initialize(login = 59307110, server= "XPMT5-DEMO", password = password):
    print("initialize() failed, error code =",mt5.last_error())
    mt5.shutdown()
 
# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())


inicio = datetime(ontem.year,ontem.month,ontem.day,  tzinfo = timezone)
fim = datetime(ontem.year, ontem.month, ontem.month,  tzinfo = timezone)


tickers = pd.read_csv(fr'C:UsersJoãoWeckerleOneDrive - B6 CapitalDados B3TradeInformationConsolidatedFile_{anteontem}_1.csv', low_memory=False,
                     skiprows=1, delimiter=';')
tickers = tickers['TckrSymb']


ticks = mt5.copy_ticks_range(
        'PETR4', # Símbolo desejado
        inicio, # Data Inicial
        fim, # Data Final
        mt5.COPY_TICKS_ALL) # Flag da busca. Podemos passar também mt5.COPY_TICKS_TRADE e mt5.COPY_TICKS_INFO.

ticks = pd.DataFrame(ticks)

Does anyone know why this is happening? My guess would be the size of the data as if there is too much data to return you get nothing instead.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật