定量交易python库
QTPyLib的Python项目详细描述
qtpylib,pythonic算法交易
qtpylib(quantitativet半径pythonlibrary) 是一个用python编写的简单的事件驱动算法交易库, 支持回溯测试,以及通过 Interactive Brokers。
我开发qtpylib是因为我想要一个简单的, 但强大的交易库让我可以专注于 交易逻辑本身而忽略其他一切。
功能
- 一个连续运行的记事本,让你捕捉市场数据,即使你的算法没有运行。
- tick、bar和trade数据存储在mysql中,以便以后进行分析和回溯测试。
- 使用pub/sub架构,使用ØMQ(zeromq)在algo和吸墨器之间进行通信,允许在同一台机器上运行单个吸墨器/多个algo。
- 支持基于订单、报价、时间、勾号或数量的策略解决方案。
- 包括许多可以无缝地在算法中使用的公共指标。
- 市场数据事件使用异步、非阻塞体系结构。
- 通过短信将订单发送到您的手机(需要Nexmo或Twilio帐户)。
- 通过专用模块(see documentation)与TA-Lib完全集成。
- 能够导入任何python库(例如scikit-learn或TensorFlow)以在算法中使用它们。
快速启动
qtpylib有5个主要组件:
- Blotter-处理市场数据检索和处理。
- Broker-发送和处理订单/位置(抽象层)。
- Algo-(tt2}$的子类)与Blotter通信,将市场数据传递给您的策略,并通过Broker处理/定位订单。
- Reports-通过web应用程序,以及用于交易、未平仓和市场数据的简单rest api,提供对交易和未平仓的实时监控。
- 最后,your strategies是Algo的子类,处理交易逻辑/规则。这是您编写大部分代码的地方。
一。获取市场数据
要开始,您需要首先创建一个吸墨纸脚本:
# blotter.pyfromqtpylib.blotterimportBlotterclassMainBlotter(Blotter):pass# we just need the nameif__name__=="__main__":blotter=MainBlotter()blotter.run()
然后,在ib tws/gw运行时,从命令行运行blotter:
$ python blotter.py
如果您的策略需要订单/市场深度数据,请在命令中添加--orderbook标记:
$ python blotter.py --orderbook
2.编写算法
当吸墨器在后台运行时,编写并执行您的算法:
# strategy.pyfromqtpylib.algoimportAlgoclassCrossOver(Algo):defon_start(self):passdefon_fill(self,instrument,order):passdefon_quote(self,instrument):passdefon_orderbook(self,instrument):passdefon_tick(self,instrument):passdefon_bar(self,instrument):# get instrument historybars=instrument.get_bars(window=100)# or get all instruments history# bars = self.bars[-20:]# skip first 20 days to get full windowsiflen(bars)<20:return# compute averages using internal rolling_meanbars['short_ma']=bars['close'].rolling_mean(window=10)bars['long_ma']=bars['close'].rolling_mean(window=20)# get current position datapositions=instrument.get_positions()# trading logic - entry signalifbars['short_ma'].crossed_above(bars['long_ma'])[-1]:ifnotinstrument.pending_ordersandpositions["position"]==0:# buy one contractinstrument.buy(1)# record values for later analysisself.record(ma_cross=1)# trading logic - exit signalelifbars['short_ma'].crossed_below(bars['long_ma'])[-1]:ifpositions["position"]!=0:# exit / flatten positioninstrument.exit()# record values for later analysisself.record(ma_cross=-1)if__name__=="__main__":strategy=CrossOver(instruments=[("ES","FUT","GLOBEX","USD",201609,0.0,"")],# ib tuplesresolution="1T",# Pandas resolution (use "K" for tick bars)tick_window=20,# no. of ticks to keepbar_window=5,# no. of bars to keeppreload="1D",# preload 1 day history when startingtimezone="US/Central"# convert all ticks/bars to this timezone)strategy.run()
要在live环境中运行algo,请在命令行中键入:
$ python strategy.py --logpath ~/qtpy/
生成的交易将保存在~/qtpy/STRATEGY_YYYYMMDD.csv中,以便以后进行分析。
三。查看实时交易
当吸墨器在后台运行时,编写仪表板:
# dashboard.pyfromqtpylib.reportsimportReportsclassDashboard(Reports):pass# we just need the nameif__name__=="__main__":dashboard=Dashboard(port=5000)dashboard.run()
要运行仪表板,请从命令行运行它:
$ python dashboard.py >>> Dashboard password is: a0f36d95a9 >>> Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
现在,将浏览器指向http://localhost:5000,并使用生成的密码访问仪表板。
注意
您可以在qtpylib/examples directory中找到其他示例。 请参考Full Documentation来学习 如何启用短信通知,使用捆绑的指示器,等等。
安装
使用pip:
安装$ pip install qtpylib --upgrade --no-cache-dir
要求
- Python>;=3.4
- Pandas(经测试可与>;=0.18.1一起工作)
- Numpy(经测试可与>;=1.11.1一起工作)
- PyZMQ(经测试可与>;=15.2.1一起使用)
- PyMySQL(经测试可与>;=0.7.6一起工作)
- pytz(经测试可与>;=2016.6.1一起使用)
- dateutil(经测试可与>;=2.5.1一起工作)
- Nexmo-Python用于短信支持(测试为工作WIth>;=1.2.0)
- Twilio-Python用于短信息支持(测试为与>;=5.4.0一起工作)
- Flask用于仪表板(测试为与>;=0.11一起工作)
- Requests(经测试可与>;=2.10.0一起工作)
- IbPy2(经测试可与>;=0.8.0一起工作)
- ezIBpy(ibpy包装器,测试为与>;=1.12.66一起工作)
- 机器上安装并运行的最新交互代理TWS或IB Gateway
- MySQL Server安装并运行qtpylib数据库
法律事务
qtpylib是根据apache许可证2.0版授权的。其副本包含在license.txt中。
qtpylib不是交互式代理的产品,也不隶属于交互式代理。
P.S.
我对你在qtpylib的经历很感兴趣。请给我一张便条,上面有你的任何反馈。
ran