Zipline is a Pythonic algorithmic trading library. It is an event-driven system for backtesting. Zipline is currently used in production as the backtesting and live-trading engine powering Quantopian -- a free, community-centered, hosted platform for building and executing trading strategies.
zipline是一个Pythonic算法交易库。它是一个事件驱动的回测系统。Zipline目前为Quantopian在生产中被用作回测和实时交易引擎,Quantopian是一个用于建立和执行交易策略的免费的社区中心和托管平台。
Features
特征
Ease of Use: Zipline tries to get out of your way so that you can focus on algorithm development. See below for a code example.
使用方便:Zipline试图解放的双手,以便你可以专注于算法开发。请参见下面的代码示例。
"Batteries Included": many common statistics like moving average and linear regression can be readily accessed from within a user-written algorithm.
“内置电池”:许多常见的统计功能,如移动平均和线性回归,可以很容易地从用户编写的算法中调用。
PyData Integration: Input of historical data and output of performance statistics are based on Pandas DataFrames to integrate nicely into the existing PyData ecosystem.
PyDATA集成:历史数据的输入和性能统计的输出是基于Pandas DataFrames格式,以很好地集成到现有的PyDATA生态系统中。
Statistics and Machine Learning Libraries: You can use libraries like matplotlib, scipy, statsmodels, and sklearn to support development, analysis, and visualization of state-of-the-art trading systems.
统计和机器学习库:您可以使用像matplotlib, scipy, statsmodels, 和 sklearn这样的库来支持最先进的交易系统的开发、分析和可视化。
Quickstart
快速启动
See our getting started tutorial.
看我们入门教程。
The following code implements a simple dual moving average algorithm.
下面的代码实现了一个简单的双移动平均算法。
from zipline.api import order_target, record, symbol def initialize(context): context.i = 0 context.asset = symbol('AAPL') def handle_data(context, data): # Skip first 300 days to get full windows context.i += 1 if context.i < 300: return # Compute averages # data.history() has to be called with the same params # from above and returns a pandas dataframe. short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean() long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean() # Trading logic if short_mavg > long_mavg: # order_target orders as many shares as needed to # achieve the desired number of shares. order_target(context.asset, 100) elif short_mavg < long_mavg: order_target(context.asset, 0) # Save values for later inspection record(AAPL=data.current(context.asset, 'price'), short_mavg=short_mavg, long_mavg=long_mavg)
You can then run this algorithm using the Zipline CLI; you'll need a Quandl API key to ingest the default data bundle. Once you have your key, run the following from the command line:
然后可以使用 Zipline CLI运行此算法;您需要一个QUANDL API密钥来获取默认数据包。一旦拥有了密钥,就从命令行运行以下命令:
$ QUANDL_API_KEY=<yourkey> zipline ingest -b quandl $ zipline run -f dual_moving_average.py --start 2014-1-1 --end 2018-1-1 -o dma.pickle
This will download asset pricing data data from quandl, and stream it through the algorithm over the specified time range. Then, the resulting performance DataFrame is saved in dma.pickle, which you can load an analyze from within Python.
这将从QuANDL下载证券价格数据,并在指定的时间范围内将其数据流通过算法。然后,将得到的DataFrame性能数据保存在dma.pickle中,可以从Python中加载分析。
You can find other examples in the zipline/examples directory.
您可以在Zipline /examples目录中找到其他示例。
Questions?
问题?
If you find a bug, feel free to open an issue and fill out the issue template.
如果发现bug,请自由打开问题并填写问题模板。
Contributing
贡献
All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome. Details on how to set up a development environment can be found in our development guidelines.
所有的贡献、bug报告、bug修复、文档改进、增强和想法都是受欢迎的。关于如何建立开发环境的细节可以在我们的开发指南中找到。
If you are looking to start working with the Zipline codebase, navigate to the GitHub issues tab and start looking through interesting issues. Sometimes there are issues labeled as Beginner Friendly or Help Wanted.
如果你想开始使用Zipline代码库,导航到GITHUB问题选项卡,开始浏览有趣的问题。有时有些问题被标示为初学友好或乐于助人。
Feel free to ask questions on the mailing list or on Gitter.
在邮件列表或GITER上自由提问。