[英]How to get weekly data on Python MetaTrader 5
我正在开发一个脚本来获取历史数据。 我正在尝试获取每周数据,但 MetaTrader 5 返回一个空的 dataframe。 我在用着:
import MetaTrader5 as mt5
import pandas as pd
mt5.initialize()
ticks = mt5.copy_rates_range(
'WIN$N',
mt5.TIMEFRAME_W1,
datetime(2022, 2, 28),
datetime(2022, 3, 4)
)
print (pd.DataFrame(ticks))
为什么我得到一个空的 dataframe? 将此脚本与分钟或每小时数据一起使用,它可以完美运行。 我尝试查看 MT5 文档,但没有使用 WEEKLY 或 MONTLHY 数据的示例。
获取每周和每月数据的解决方案是(更改您的股票的 WIN$N):
years = [2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015,
2016, 2017, 2018, 2019, 2020,
2021]
months = [1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12]
timeframes = {
'W1' : 1 | 0x8000,
'MN1': 1 | 0xC000
}
for year in years:
for month in months:
for timeframe in timeframes:
path = day.strftime("data/" + str(timeframe) + "/%Y/%m")
os.makedirs(path, exist_ok=True)
# request tick data
if month != 12:
ticks = mt5.copy_rates_range(
'WIN$N',
timeframes[timeframe],
datetime(year, month, 1),
datetime(year, month + timedelta(month=1), 1)
)
ticks = pd.DataFrame(ticks)
ticks.to_csv(path + "/" + str(year) + '_' + str(month) + ".csv")
else:
ticks = mt5.copy_rates_range(
'WIN$N',
timeframes[timeframe],
datetime(year, month, 1),
datetime(year, month, calendar.monthrange(ano,mes)[1])
)
ticks = pd.DataFrame(ticks)
ticks.to_csv(path + "/" + str(year) + '_' + str(month) + ".csv")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.