数据加载
在实盘中,RQData通过实时载入数据进行策略的初始化。该功能主要在CTA实盘引擎engine.py内实现。下面介绍具体流程:
- 在菜单栏点击“配置”,进入全局配置页面输入RQData账号密码;或者直接配置json文件,即在用户目录下.vntrader文件夹找到vt_setting.json,如图。
- 初始化RQData客户端:从vt_setting.json中读取RQData的账户、密码到rq_client.init()函数进行初始化
- def init_rqdata(self):
- """
- Init RQData client.
- """
- username = SETTINGS["rqdata.username"]
- password = SETTINGS["rqdata.password"]
- if not username or not password:
- return
- import rqdatac
- self.rq_client = rqdatac
- self.rq_client.init(username, password,
- ('rqdatad-pro.ricequant.com', 16011))
- RQData载入实盘数据:输入vt_symbol后,首先会转换成符合RQData格式的rq_symbol,通过get_price()函数下载数据,并且插入到数据库中。
- def query_bar_from_rq(
- self, vt_symbol: str, interval: Interval, start: datetime, end: datetime
- ):
- """
- Query bar data from RQData.
- """
- symbol, exchange_str = vt_symbol.split(".")
- rq_symbol = to_rq_symbol(vt_symbol)
- if rq_symbol not in self.rq_symbols:
- return None
- end += timedelta(1) # For querying night trading period data
- df = self.rq_client.get_price(
- rq_symbol,
- frequency=interval.value,
- fields=["open", "high", "low", "close", "volume"],
- start_date=start,
- end_date=end
- )
- data = []
- for ix, row in df.iterrows():
- bar = BarData(
- symbol=symbol,
- exchange=Exchange(exchange_str),
- interval=interval,
- datetime=row.name.to_pydatetime(),
- open_price=row["open"],
- high_price=row["high"],
- low_price=row["low"],
- close_price=row["close"],
- volume=row["volume"],
- gateway_name="RQ"
- )
- data.append(bar)