Zipline Beginner Tutorial

Zipline是一个Python开源算法交易模拟器,提供实时模拟、流式处理和常见工具。教程介绍了如何定义算法的基本函数,如initialize和handle_data,以及如何运行算法,包括命令行界面和IPython Notebook。示例中展示了如何使用历史数据执行双移动平均交叉策略。

Zipline Beginner Tutorial

Basics

Zipline is an open-source algorithmic trading simulator written in Python .Zipline是一个用Python编写的开源算法交易模拟器。

The source can be found at: https://github.com/quantopian/zipline 源码地址在

Some benefits include: 一些优势包括:

  • Realistic: slippage, transaction costs, order delays. 实时:滑点,交易成本,订单延迟。
  • Stream-based: Process each event individually, avoids look-ahead bias. 基于流:分别处理每个事件,避免预见偏差。
  • Batteries included: Common transforms (moving average) as well as common risk calculations (Sharpe). 电池包括:常见转换(移动平均值)以及常见风险计算(夏普)。
  • Developed and continuously updated by Quantopian which provides an easy-to-use web-interface to Zipline, 10 years of minute-resolution historical US stock data, and live-trading capabilities. This tutorial is directed at users wishing to use Zipline without using Quantopian. If you instead want to get started on Quantopian, see here.由Quantopian开发并不断更新,为Zipline提供简单易用的网络界面,10年分钟级历史美国股票数据和实时交易功能。本教程面向希望在不使用Quantopian的情况下使用Zipline的用户。如果您想开始使用Quantopian,请参阅此处。

This tutorial assumes that you have zipline correctly installed, see the installation instructions if you haven’t set up zipline yet.本教程假定您已正确安装了zipline,如果尚未设置zipline,请参阅安装说明。

Every zipline algorithm consists of two functions you have to define: 每个zipline算法由两个你必须定义的函数组成:

  • initialize(context)
  • handle_data(context, data)

Before the start of the algorithm, zipline calls the initialize() function and passes in a context variable. context is a persistent namespace for you to store variables you need to access from one algorithm iteration to the next.

在算法开始之前,zipline调用initialize()函数并传入一个context变量。context 是一个持久的名称空间,用于存储您需要从一个算法迭代访问下一个变量的变量。

 

After the algorithm has been initialized, zipline calls the handle_data() function once for each event. At every call, it passes the same context variable and an event-frame called data containing the current trading bar with open, high, low, and close (OHLC) prices as well as volume for each stock in your universe. For more information on these functions, see the relevant part of the Quantopian docs.

算法初始化后,zipline为每个事件调用一次handle_data()函数。每次调用时,它都会传递相同的context变量和一个名为data的事件框架,其中包含当前交易栏的开盘价,最高价,最低价和收盘价(OHLC)以及您的全球每只股票的交易量。 有关这些功能的更多信息,请参阅Quantopian文档的相关部分。

 

My First Algorithm我的第一个算法

Lets take a look at a very simple algorithm from the examples directory, buyapple.py: 让我们看看examples目录中的一个非常简单的算法, buyapple.py

复制代码
from zipline.examples import buyapple

from zipline.api import order, record, symbol


def initialize(context):
    pass


def handle_data(context, data):
    order(symbol('AAPL'), 10)
    record(AAPL=data.current(symbol('AAPL'), 'price'))
复制代码

 


As you can see, we first have to import some functions we would like to use. All functions commonly used in your algorithm can be found in zipline.api. Here we are using order() which takes two arguments: a security object, and a number specifying how many stocks you would like to order (if negative, order() will sell/short stocks). In this case we want to order 10 shares of Apple at each iteration. For more documentation on order(), see the Quantopian docs.

正如你所看到的,我们首先必须导入一些我们想使用的函数。您的算法中常用的所有函数都可以在zipline.api中找到。在这里我们使用了order(),它带有两个参数:一个安全对象和一个数字,该数字用于指定要订购多少股票(如果为负数,order()将卖出/卖空股票)。在这种情况下,我们希望在每次迭代中购买10股苹果股票。有关order()的更多文档,请参阅Quantopian文档。

Finally, the record() function allows you to save the value of a variable at each iteration. You provide it with a name for the variable together with the variable itself: varname=var. After the algorithm finished running you will have access to each variable value you tracked with record() under the name you provided (we will see this further below). You also see how we can access the current price data of the AAPL stock in the data event frame (for more information see here).

最后,record()函数允许您在每次迭代中保存一个变量的值。您为变量提供变量名称和变量本身:varname = var。算法完成运行后,您将可以使用您提供的名称(我们将在下面进一步了解)使用record()跟踪每个变量值。您还会看到我们如何在数据事件框架中访问AAPL股票的当前价格数据。

Running the Algorithm 运行算法

To now test this algorithm on financial data, zipline provides three interfaces: A command-line interface, IPython Notebook magic, and run_algorithm(). 为了现在在金融数据上测试这个算法,zipline提供了三个接口:命令行界面,IPython Notebook magic和run_algorithm()。

Ingesting Data 摄取数据

If you haven’t ingested the data, you’ll need a Quandl API key to ingest the default bundle. Then run: 如果您没有摄入数据,则需要使用Quandl API密钥来获取默认包。然后运行:

$ QUANDL_API_KEY=<yourkey> zipline ingest [-b <bundle>]

where <bundle> is the name of the bundle to ingest, defaulting to quandl. 其中<bundle>是要摄取的包的名称,默认为quandl。

you can check out the ingesting data section for more detail. 您可以查看获取数据部分了解更多详细信息。

Command Line Interface 命令行界面

After you installed zipline you should be able to execute the following from your command lin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值