QuantAxis快速入门与闭坑教程
QuantAxis快速入门与闭坑教程
学习quantaxis快一个月了,中间踩了很多坑,现在还没跳出坑,基本上只是入门的水平。现在把自己踩过的坑写出来,形成对新手的一个入门闭坑指南。个人水平也不高,有错误望批评指正。
1.环境搭建¶
直接上docker环境,传送门:
https://doc.yutiansut.com/docker
这时候你应该能看到如下红框几个镜像,后面镜像升级你看到的有可能稍有不同:
2.初始化使用¶
安装好docker容器之后,需要初始化操作,主要是初始化数据库,以下来自官方群公告:
第一次部署docker版QA必须的初始化操作步骤:
一、在浏览器输入docker宿主机"ip地址:8888";
二、进入jupyterlab登录界面后输入口令"quantaxis";
三、进入jupyterlab启动页,点击Terminal进入终端;
四、在终端界面输入"/bin/bash",进入bash状态;
五、bash状态输入"quantaxis",进入数据库操作状态;
六:依次输入完成数据库初始化
"save stock_list","save single_index_day 000300"
输入exit退出终端,在docker管理界面重启qaweb容器,最后浏览器输入docker宿主机"ip地址:81",弹出的登录界面把登录地址端口改为docker宿主机"ip地址:8010",就可以进入QA前端界面;
以上命令第一次接触有可能有些迷糊,结合这幅图就知道在说啥了。
3.进入81管理界面¶
接下来请打开81端口网址:http://localhost:81 会弹出登录界面,修改那个地址为localhost即可登录。然后你会进入下面的界面:
注意,如果这一步你仍然进入不了这个界面,请重启一下docker后重新进入。如果还是不行,那应该是你的问题,你确定按照上面的操作完成初始化数据了吗?
进入这个界面,主要是为了后面能够看到2个东西:
1、回测结果:
2、qifi回测界面:
记得修改81端口的配置,都改成localhost就行,参考我的:
下面教大家怎么看到他们:
4.回测¶
回测包括股票和期货回测,最简单的是通过qastrategy实现。官方也给出了一些案例但是基本都跑不通,传送门:https://github.com/yutiansut/QAStrategy/tree/master/example
回测这部分跑不通的原因不是代码不对,有可能是数据库没有数据。
4.1 期货回测¶
期货回测跑通只需要运行如下代码即可:
官方的案例运行出错是因为给的是'1min'频率,而通过“save x”等命令下载的数据1min有时间限制,所以跑不通。
这里使用15min频率。运行之前请通过save future等命令存储相关数据
import QUANTAXIS as QA
from QAStrategy import QAStrategyCTABase
class MACD(QAStrategyCTABase):
def on_bar(self, bar):
res = self.macd()
if res.DIF[-1] > res.DEA[-1]:
print('LONG')
if self.positions.volume_long == 0:
self.send_order('BUY', 'OPEN', price=bar['close'], volume=100)
if self.positions.volume_short > 0:
self.send_order('BUY', 'CLOSE', price=bar['close'], volume=100)
else:
print('SHORT')
if self.positions.volume_short == 0:
self.send_order('SELL', 'OPEN', price=bar['close'], volume=100)
if self.positions.volume_long > 0:
self.send_order('SELL', 'CLOSE', price=bar['close'], volume=100)
def macd(self, ):
return QA.QA_indicator_MACD(self.market_data)
def risk_check(self):
pass
# pprint.pprint(self.qifiacc.message)
if __name__ == '__main__':
MACD = MACD(code='RBL8', frequence='15min',strategy_id='macd_example_backtest4', start='2019-10-01', end='2019-11-01')
MACD.run_backtest()
然后就可以看到回测的第一个界面:
4.2 股票回测¶
股票回测,官方没有给出demo,需要从期货改写,请运行以下demo:
前提条件是数据库要提前存储数据,请通过 save stock等命令存储。
import QUANTAXIS as QA
from QAStrategy.qastockbase import QAStrategyStockBase
class strategy(QAStrategyStockBase):
def on_bar(self, data):
# print(self.running_time)
code = data.name[1]
pos = self.get_positions(code)
res = self.ma(code)
if res.MA2[-1] > res.MA5[-1]:
if pos.volume_long == 0:
cash = self.get_cash()/2
close = data['close']
volume = int(cash/close/100)*100
self.send_order('BUY', 'OPEN',code=code, price=close, volume=volume)
print('Open', code)
else:
if pos.volume_long > 0:
self.send_order('SELL', 'CLOSE', code=code, price=data['close'], volume=pos.volume_long)
print('Close', code)
def ma(self,code):
self.code_market_data = self.get_code_marketdata(code)
return QA.QA_indicator_MA(self.code_market_data, 2, 5)
if __name__ == '__main__':
s = strategy(code=['000001', '000002'], frequence='day', start='2019-01-01', end='2020-04-01', strategy_id='stock_demo6')
s.run_backtest()
然后回测界面多了如下一行记录:
5.QIFI回测¶
qifi回测传送门:https://github.com/yutiansut/QIFIAccount/tree/master/usage
这个回测并没有集成在qastrategy里面,使用门槛相对较高,我也不知道怎么拿他写策略,自己编写的一个例子如下:
import QIFIAccount
acc = QIFIAccount.QAQIFIAccount.QIFI_Account(username='test37', password='test37', model="BACKTEST")
acc.initial()
for i in range(10,29):
date = '2021-01-%s'%i
price = 20+i/10
acc.on_price_change('000001', price, date+' 09:35:00')
# 奇数开仓,偶数平仓
if i %2 ==0:
order = acc.send_order('000001', 100, price, QIFIAccount.ORDER_DIRECTION.BUY, datetime=date+' 09:58:00')
acc.make_deal(order)
else:
order = acc.send_order('000001', 100, price, QIFIAccount.ORDER_DIRECTION.SELL, datetime=date+' 10:58:00')
acc.make_deal(order)
pos = acc.get_position('000001')
# print(pos.realtime_message)
acc.settle()
然后就能看到如下结果:
qifi由于并没有集成在qastrategy里,或者集成到qastrategy我还不知道怎么用,目前的研究暂且到这一步。
6. 关于本地环境¶
实测对于python3.8直接通过pip install quantaxis,pip install qastrategy方式就可以安装qa,然后以上所有代码都可以通过本地环境跑通。也就是说这时候你可以直接用你熟悉的方式写代码跑回测。 我个人使用winPython测试的,你用anaconda结果应该也一样。qawebservice启动本地网页好像有问题,我没有跑通,这个不影响,我用docker环境的端口也一样。
7. 关于数据库更新¶
这个位置存放了数据库定时更新的代码。天神说docker内的数据更新通过 crontab实现,不知道这个是否是自动更新,在跟踪测试。如果没有自动更新也可以自己设置个定时器跑,或者通过本地环境定时运行这个文件,效果应该是一样的。
8.关于rabbitmq与其15672端口¶
这个应该是实盘模拟转发行情用的,如下图所示,我期货和股票的订阅一直采集不到数据,结果就是这个样子。实盘模拟的事情下面说。
9. 关于实盘模拟¶
根据我的理解,上面的期货代码最后两行换成如下代码就可以实现期货实盘模拟,注意你在回测的时候使用'RBL8'是因为你的数据库只有L8这样的主力合约。实盘的时候主力合约对应代码就是rb2105,所以这里使用rb2105.
MACD = MACD(code='rb2105', frequence='1min', data_host='127.0.0.1', mongo_ip='127.0.0.1', trade_host='127.0.0.1',
send_wx=True, strategy_id='macd_example_sim', start='2019-10-01', end='2019-11-01', model='rust')
MACD.run_sim()
由于simnow关闭了,这个暂时无法测试,我也卡了很久。
qastrategy好像没有集成股票的数据采集信息,暂时不知道股票的实盘模拟通过什么方式实现。
股票的实时数据采集和实盘模拟可以通过quantaxis-hexos来实现,这东西就和qifi回测一样,不知道如何集成在qastrategy。
综上,个人水平就这些,实盘模拟暂未搞定,也就是入门水平。
日期2021年4月23日:
# quantaxis 相关版本
!pip show quantaxis qastrategy qifiaccount quantaxis-pubsub
Name: quantaxis Version: 1.10.12 Summary: QUANTAXIS:Quantitative Financial Strategy Framework Home-page: https://github.com/quantaxis/quantaxis Author: yutiansut Author-email: yutiansut@qq.com License: MIT Location: /opt/conda/lib/python3.8/site-packages Requires: seaborn, delegator.py, pytdx, tornado, demjson, IPython, pytesseract, matplotlib, statsmodels, websocket-client, numba, tushare, motor, pyconvert, pyecharts, janus, retrying, numpy, apscheduler, gevent-websocket, requests, pandas, pymongo, lxml, bs4, attrs, six, pyecharts-snapshot, scipy, protobuf, async-timeout, flask, zenlog Required-by: quantaxis-webserver, quantaxis-run, quantaxis-pubsub, qifimanager, qifiaccount, qavifiserver, QAStrategy --- Name: QAStrategy Version: 0.0.25 Summary: QUANTAXIS:Quantitative Financial Strategy Framework Home-page: https://github.com/yutiansut/QAStrategy Author: yutiansut Author-email: yutiansut@qq.com License: MIT Location: /opt/conda/lib/python3.8/site-packages Requires: quantaxis-pubsub, qifiaccount, qaenv, quantaxis Required-by: --- Name: qifiaccount Version: 1.14.0 Summary: QUANTAXIS:Quantitative Financial Strategy Framework Home-page: https://github.com/yutiansut/qifiaccount Author: yutiansut Author-email: yutiansut@qq.com License: MIT Location: /opt/conda/lib/python3.8/site-packages Requires: pytest, quantaxis-pubsub, quantaxis, click, qaenv Required-by: qifimanager, qavifiserver, QAStrategy --- Name: quantaxis-pubsub Version: 1.11 Summary: QUANTAXIS PUBSUB:QUANTAXIS PUB/SUB MODEL Home-page: https://github.com/yutiansut/QAPUBSUB Author: yutiansut Author-email: yutiansut@qq.com License: MIT Location: /opt/conda/lib/python3.8/site-packages Requires: quantaxis, pika Required-by: qifimanager, qifiaccount, qavifiserver, QAStrategy