根据文档和论坛中查找的资料来进行操作,通过VN UI界面操作与通过脚本来下载获取数据均为空的情况.
已经申请了RQ DATA 的试用,同时,通过UI界面填写了RQ DATA 的密钥等相关信息.
1.通过UI界面操作: 参考链接 : https://www.vnpy.com/docs/cn/data_manager.html#id6
UI 界面 查询数据为空,图片如下:
- 通过脚本操作,参考链接: https://www.vnpy.com/docs/cn/datafeed.html#rqdata
复制该代码执行,返回结果为空
参考脚本1, 来源于官方文档 https://www.vnpy.com/docs/cn/datafeed.html#rqdata
from datetime import datetime
from vnpy.trader.constant import Exchange, Interval
from vnpy.trader.datafeed import get_datafeed
from vnpy.trader.object import HistoryRequest
获取数据服务实例
datafeed = get_datafeed()
req = HistoryRequest(
# 合约代码(示例cu888为米筐连续合约代码,仅用于示范,具体合约代码请根据需求查询数据服务提供商)
symbol="cu888",
# 合约所在交易所
exchange=Exchange.SHFE,
# 历史数据开始时间
start=datetime(2019, 1, 1),
# 历史数据结束时间
end=datetime(2021, 1, 20),
# 数据时间粒度,默认可选分钟级、小时级和日级,具体选择需要结合该数据服务的权限和需求自行选择
interval=Interval.DAILY
)
获取k线历史数据
data = datafeed.query_bar_history(req)
参考脚本2,来源于论坛(改动了部分代码,以适用于当前版本): https://www.vnpy.com/forum/topic/4578-quan-shi-chang-qi-huo-shu-ju-de-pi-liang-he-geng-xin?page=1
import pandas as pd
import vnpy.trader.database as db
from vnpy.trader.setting import SETTINGS
SETTINGS["database.timezone"] = "Asia/Shanghai"
SETTINGS["database.name"] = "mysql"
SETTINGS["database.driver"] = "mysql"
SETTINGS["database.database"] = "vnpy"
SETTINGS["database.host"] = "localhost"
SETTINGS["database.user"] = "root"
SETTINGS["database.password"] = "root"
from vnpy.trader.database import BaseDatabase ,DB_TZ,get_database
from vnpy.trader.object import BarData
from vnpy.trader.constant import Exchange,Interval
symbols = {
"SHFE": ["RB"]
}
symbol_type = ""
from datetime import datetime
start_date = datetime(2005,1,1)
end_date = datetime(2023,1,10)
from vnpy.trader.datafeed import get_datafeed
from vnpy.trader.constant import Exchange, Interval
from vnpy.trader.object import HistoryRequest
datafeed = get_datafeed()
def load_data(req):
print(req)
data = datafeed.query_bar_history(req)
print(data)
database = get_database()
database.save_bar_data(data)
print(f"{req.symbol}历史数据下载完成")
for exchange, symbols_list in symbols.items():
for s in symbols_list:
req = HistoryRequest(
symbol=s+symbol_type,
exchange=Exchange(exchange),
start=start_date,
interval=Interval.DAILY,
end=end_date,
)
load_data(req=req)
输出的结果为:
HistoryRequest(symbol='RB', exchange=<Exchange.SHFE: 'SHFE'>, start=datetime.datetime(2005, 1, 1, 0, 0), end=datetime.datetime(2023, 1, 10, 0, 0), interval=<Interval.DAILY: 'd'>)
[]
由于 从RQ DATA 获取的数据为空数组,导致提示保存到数据库失败的报错..
有没有社友能帮忙尝试一下你们的是否正常?我不太清楚哪里出了问题..