czipline-pro


Nameczipline-pro JSON
Version 1.13.2 PyPI version JSON
download
home_pagehttps://github.com/yanjlee/czipline
SummaryZipline是QUANTOPIAN开发的算法交易库。这是一个事件驱动,支持回测和实时交易的系统。Zipline目前有一个免费的[回测平台](https://www.quantopian.com),可以很方便编写交易策略和执行回测。为处理A股数据,增加或修改基础数据相关部分,用于本机策略回测分析。.
upload_time2024-06-01 07:33:16
maintainerNone
docs_urlNone
authoryanjlee
requires_pythonNone
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            
![Zipline](https://media.quantopian.com/logos/open_source/zipline-logo-03_.png)

Zipline是QUANTOPIAN开发的算法交易库。这是一个事件驱动,支持回测和实时交易的系统。
Zipline目前有一个免费的[回测平台](https://www.quantopian.com),可以很方便编写交易策略和执行回测。为处理A股数据,增加或修改基础数据相关部分,用于本机策略回测分析。

#### 功能

##### 新增`USEquityPricing`数据列

+ `prev_close` 前收盘
+ `turnover` 换手率
+ `amount` 成交额
+ `tmv` 总市值
+ `cmv` 流通市值
+ `circulating_share` 流通股本
+ `total_share` 总股本

**注意** 以上数据不会因股票分红派息而调整

##### 新增`Fundamentals`数据容器

`Fundamentals`是用于`pipeline`的数据集容器,包括偶发性变化及固定信息数据,如上市日期、行业分类、所属概念、财务数据等信息。

##### 投资组合优化
参考quantopian公司帮助文档,完成`optimize`模块,实现在限定条件下的`MaximizeAlpha`及`TargetWeights`投资组合优化。

##### 技术分析图
`AnalysisFigure`绘制OHLCV及常用`MACD`等技术指标图,使用买入卖出信号标注来辅助策略改进。
<div>
<img src="/images/mdjt.png" />
</div>

##### 提供后台自动提取、转换数据脚本

通过定时后台计划任务,自动提取回测所需网络数据,并转换为符合`zipline`需求的数据格式,可以更加专注于编写策略及相关分析。

#### 安装

##### 克隆


```bash
$ git clone https://github.com/zhangshoug/czipline.git
```
##### 建立环境
```bash
$ conda create -n zipline python=3.6 # 环境名称随意,python版本要求3.6
```

##### 安装依赖包
以下安装需要进入环境
```bash
$ source activate zipline # 进入刚才建立的环境 
$ activate zipline        # windows 进入环境
```

下载并安装ta-lib包以及数据处理包、统计分析包。转移至项目安装文件所在目录后执行:

参考`talib`[安装方法](https://github.com/mrjbq7/ta-lib)

```bash
$ # 以下需确保在zipline项目安装目录下执行
$ pip install -r ./etc/requirements.txt
$ pip install -r ./etc/requirements_add.txt
```


##### 编译`C`扩展库


```bash
$ python setup.py build_ext --inplace
```

##### 安装`zipline`


```bash
$ python setup.py install
$ # 如需开发安装
$ python setup.py develop
```

#### 准备数据
成功安装`zipline`后,`cswd`包也已经成功安装。在相应环境下,执行以下命令:


```bash
$ init-stock-data # 初始化基础数据。耗时大约24小时(主要下载日线及最近一个月的分时交易数据。对数据量小,但抓取网页数据耗时长的,整理好的数据存放在github,初始化时会从该处提取,节约初始化时间。)

$ zipline ingest # 转换日线数据,耗时约10分钟

$ sql-to-bcolz # `Fundamentals`数据,耗时约1.5分钟

# 如需要进行回测分析,请运行以下指令,生成ff因子数据(empyrical包使用)
$ gen-ff-factors
```
初始化数据后,参考[如何设置后台数据自动处理](./docs/介绍材料/bg_tasks.cron),设置后台计划任务。后台在盘后自动完成数据导入及转换。网络数据的采集可能因各种原失败,请注意查阅日志文档。文档默认路径为"~/stockdata/logs"。

#### 验证安装
如能正常运行以下代码,证明已经安装成功。(注意,由于版本兼容问题,安装过程中会有大量警告信息,只要不是错误,可忽略)

```python
from zipline import get_calendar
c = get_calendar('SZSH')
c.first_session
# Timestamp('1990-12-19 00:00:00+0000', tz='UTC', freq='C')
```

#### 使用

计算`Fama-French`三因子案例涉及到:
1. 如何在`Notebook`运行回测
2. 选择基准收益率指数代码
3. 计划函数用法
4. `Fundamentals`及财务数据
5. `pipeline`及自定义因子用法
6. 回测速度

比较适合作为演示材料


```python
%load_ext zipline
```


```python
%%zipline --start 2017-1-1 --end 2018-1-1 --bm-symbol 399001
from zipline.api import symbol, sid, get_datetime

import pandas as pd
import numpy as np
from zipline.api import (attach_pipeline, pipeline_output, get_datetime,
                         calendars, schedule_function, date_rules)
from zipline.pipeline import Pipeline
from zipline.pipeline import CustomFactor
from zipline.pipeline.data import USEquityPricing
from zipline.pipeline.fundamentals import Fundamentals

# time frame on which we want to compute Fama-French
normal_days = 31
# approximate the number of trading days in that period
# this is the number of trading days we'll look back on,
# on every trading day.
business_days = int(0.69 * normal_days)


# 以下自定义因子选取期初数
class Returns(CustomFactor):
    """
    每个交易日每个股票窗口长度"business_days"期间收益率
    """
    window_length = business_days
    inputs = [USEquityPricing.close]

    def compute(self, today, assets, out, price):
        out[:] = (price[-1] - price[0]) / price[0] * 100


class MarketEquity(CustomFactor):
    """
    每个交易日每只股票所对应的总市值
    """
    window_length = business_days
    inputs = [USEquityPricing.tmv]

    def compute(self, today, assets, out, mcap):
        out[:] = mcap[0]


class BookEquity(CustomFactor):
    """
    每个交易日每只股票所对应的账面价值(所有者权益)
    """
    window_length = business_days
    inputs = [Fundamentals.balance_sheet.A107]

    def compute(self, today, assets, out, book):
        out[:] = book[0]


def initialize(context):
    """
    use our factors to add our pipes and screens.
    """
    pipe = Pipeline()
    mkt_cap = MarketEquity()
    pipe.add(mkt_cap, 'market_cap')

    book_equity = BookEquity()
    # book equity over market equity
    be_me = book_equity / mkt_cap
    pipe.add(be_me, 'be_me')

    returns = Returns()
    pipe.add(returns, 'returns')

    attach_pipeline(pipe, 'ff_example')
    schedule_function(
        func=myfunc,
        date_rule=date_rules.month_end())


def before_trading_start(context, data):
    """
    every trading day, we use our pipes to construct the Fama-French
    portfolios, and then calculate the Fama-French factors appropriately.
    """

    factors = pipeline_output('ff_example')

    # get the data we're going to use
    returns = factors['returns']
    mkt_cap = factors.sort_values(['market_cap'], ascending=True)
    be_me = factors.sort_values(['be_me'], ascending=True)

    # to compose the six portfolios, split our universe into portions
    half = int(len(mkt_cap) * 0.5)
    small_caps = mkt_cap[:half]
    big_caps = mkt_cap[half:]

    thirty = int(len(be_me) * 0.3)
    seventy = int(len(be_me) * 0.7)
    growth = be_me[:thirty]
    neutral = be_me[thirty:seventy]
    value = be_me[seventy:]

    # now use the portions to construct the portfolios.
    # note: these portfolios are just lists (indices) of equities
    small_value = small_caps.index.intersection(value.index)
    small_neutral = small_caps.index.intersection(neutral.index)
    small_growth = small_caps.index.intersection(growth.index)

    big_value = big_caps.index.intersection(value.index)
    big_neutral = big_caps.index.intersection(neutral.index)
    big_growth = big_caps.index.intersection(growth.index)

    # take the mean to get the portfolio return, assuming uniform
    # allocation to its constituent equities.
    sv = returns[small_value].mean()
    sn = returns[small_neutral].mean()
    sg = returns[small_growth].mean()

    bv = returns[big_value].mean()
    bn = returns[big_neutral].mean()
    bg = returns[big_growth].mean()

    # computing SMB
    context.smb = (sv + sn + sg) / 3 - (bv + bn + bg) / 3

    # computing HML
    context.hml = (sv + bv) / 2 - (sg + bg) / 2


def myfunc(context, data):
    d = get_datetime('Asia/Shanghai')
    print(d, context.smb, context.hml)
```

    2017-01-26 15:00:00+08:00 0.014014289806335789 6.605843892342312
    2017-02-28 15:00:00+08:00 4.1169182374497195 7.690119769984805
    2017-03-31 15:00:00+08:00 0.35808304923773615 2.7492806758694215
    2017-04-28 15:00:00+08:00 -4.318408584890385 5.414312699826368
    2017-05-31 15:00:00+08:00 -0.4828317045367072 3.0869028143557147
    2017-06-30 15:00:00+08:00 0.8640245866550513 0.09803178533289003
    2017-07-31 15:00:00+08:00 -2.3024594948720227 6.2829537294457145
    2017-08-31 15:00:00+08:00 3.2003154621799155 2.269609384481118
    2017-09-29 15:00:00+08:00 1.1669055941862554 -0.6079568594636064
    2017-10-31 15:00:00+08:00 -1.6233534895267374 -0.795885505339075
    2017-11-30 15:00:00+08:00 -2.965097825507776 4.4434701009908615
    2017-12-29 15:00:00+08:00 -1.1942883365086068 -0.38062423581176485
    [2018-05-02 02:07:02.681398] INFO: zipline.finance.metrics.tracker: Simulated 244 trading days
    first open: 2017-01-03 01:31:00+00:00
    last close: 2017-12-29 07:00:00+00:00

**运行时长10-12秒**

+ 有关如何使用,请参考[quantopian使用手册](https://www.quantopian.com/help)。
+ 有关本项目的说明,请参阅[介绍材料](./docs/介绍材料)
+ 有关后台自动数据处理,请参考[脚本](./docs/介绍材料/bg_tasks.cron)

**特别说明**:个人当前使用Ubuntu18.04操作系统

#### 参考配置

![系统](./images/sys_info.png)

+ Ubuntu 18.04
+ Anaconda 
+ python 3.6

#### 交流

该项目纯属个人爱好,水平有限,欢迎加入来一起完善。

**添加个人微信(ldf10728268),请务必备注`zipline`**
<div>
<img src="/images/ldf.png" width="30%" height="30%" />
</div>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yanjlee/czipline",
    "name": "czipline-pro",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "yanjlee",
    "author_email": "yanjlee@163.com",
    "download_url": "https://files.pythonhosted.org/packages/4c/2d/bd3759e1abc6ae3c11738bcfa00a7eb2e71a035ca372eb850550d1063725/czipline_pro-1.13.2.tar.gz",
    "platform": null,
    "description": "\r\n![Zipline](https://media.quantopian.com/logos/open_source/zipline-logo-03_.png)\r\n\r\nZipline\u662fQUANTOPIAN\u5f00\u53d1\u7684\u7b97\u6cd5\u4ea4\u6613\u5e93\u3002\u8fd9\u662f\u4e00\u4e2a\u4e8b\u4ef6\u9a71\u52a8\uff0c\u652f\u6301\u56de\u6d4b\u548c\u5b9e\u65f6\u4ea4\u6613\u7684\u7cfb\u7edf\u3002\r\nZipline\u76ee\u524d\u6709\u4e00\u4e2a\u514d\u8d39\u7684[\u56de\u6d4b\u5e73\u53f0](https://www.quantopian.com)\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7f16\u5199\u4ea4\u6613\u7b56\u7565\u548c\u6267\u884c\u56de\u6d4b\u3002\u4e3a\u5904\u7406A\u80a1\u6570\u636e\uff0c\u589e\u52a0\u6216\u4fee\u6539\u57fa\u7840\u6570\u636e\u76f8\u5173\u90e8\u5206\uff0c\u7528\u4e8e\u672c\u673a\u7b56\u7565\u56de\u6d4b\u5206\u6790\u3002\r\n\r\n#### \u529f\u80fd\r\n\r\n##### \u65b0\u589e`USEquityPricing`\u6570\u636e\u5217\r\n\r\n+ `prev_close` \u524d\u6536\u76d8\r\n+ `turnover` \u6362\u624b\u7387\r\n+ `amount` \u6210\u4ea4\u989d\r\n+ `tmv` \u603b\u5e02\u503c\r\n+ `cmv` \u6d41\u901a\u5e02\u503c\r\n+ `circulating_share` \u6d41\u901a\u80a1\u672c\r\n+ `total_share` \u603b\u80a1\u672c\r\n\r\n**\u6ce8\u610f** \u4ee5\u4e0a\u6570\u636e\u4e0d\u4f1a\u56e0\u80a1\u7968\u5206\u7ea2\u6d3e\u606f\u800c\u8c03\u6574\r\n\r\n##### \u65b0\u589e`Fundamentals`\u6570\u636e\u5bb9\u5668\r\n\r\n`Fundamentals`\u662f\u7528\u4e8e`pipeline`\u7684\u6570\u636e\u96c6\u5bb9\u5668\uff0c\u5305\u62ec\u5076\u53d1\u6027\u53d8\u5316\u53ca\u56fa\u5b9a\u4fe1\u606f\u6570\u636e\uff0c\u5982\u4e0a\u5e02\u65e5\u671f\u3001\u884c\u4e1a\u5206\u7c7b\u3001\u6240\u5c5e\u6982\u5ff5\u3001\u8d22\u52a1\u6570\u636e\u7b49\u4fe1\u606f\u3002\r\n\r\n##### \u6295\u8d44\u7ec4\u5408\u4f18\u5316\r\n\u53c2\u8003quantopian\u516c\u53f8\u5e2e\u52a9\u6587\u6863\uff0c\u5b8c\u6210`optimize`\u6a21\u5757\uff0c\u5b9e\u73b0\u5728\u9650\u5b9a\u6761\u4ef6\u4e0b\u7684`MaximizeAlpha`\u53ca`TargetWeights`\u6295\u8d44\u7ec4\u5408\u4f18\u5316\u3002\r\n\r\n##### \u6280\u672f\u5206\u6790\u56fe\r\n`AnalysisFigure`\u7ed8\u5236OHLCV\u53ca\u5e38\u7528`MACD`\u7b49\u6280\u672f\u6307\u6807\u56fe\uff0c\u4f7f\u7528\u4e70\u5165\u5356\u51fa\u4fe1\u53f7\u6807\u6ce8\u6765\u8f85\u52a9\u7b56\u7565\u6539\u8fdb\u3002\r\n<div>\r\n<img src=\"/images/mdjt.png\" />\r\n</div>\r\n\r\n##### \u63d0\u4f9b\u540e\u53f0\u81ea\u52a8\u63d0\u53d6\u3001\u8f6c\u6362\u6570\u636e\u811a\u672c\r\n\r\n\u901a\u8fc7\u5b9a\u65f6\u540e\u53f0\u8ba1\u5212\u4efb\u52a1\uff0c\u81ea\u52a8\u63d0\u53d6\u56de\u6d4b\u6240\u9700\u7f51\u7edc\u6570\u636e\uff0c\u5e76\u8f6c\u6362\u4e3a\u7b26\u5408`zipline`\u9700\u6c42\u7684\u6570\u636e\u683c\u5f0f\uff0c\u53ef\u4ee5\u66f4\u52a0\u4e13\u6ce8\u4e8e\u7f16\u5199\u7b56\u7565\u53ca\u76f8\u5173\u5206\u6790\u3002\r\n\r\n#### \u5b89\u88c5\r\n\r\n##### \u514b\u9686\r\n\r\n\r\n```bash\r\n$ git clone https://github.com/zhangshoug/czipline.git\r\n```\r\n##### \u5efa\u7acb\u73af\u5883\r\n```bash\r\n$ conda create -n zipline python=3.6 # \u73af\u5883\u540d\u79f0\u968f\u610f\uff0cpython\u7248\u672c\u8981\u6c423.6\r\n```\r\n\r\n##### \u5b89\u88c5\u4f9d\u8d56\u5305\r\n\u4ee5\u4e0b\u5b89\u88c5\u9700\u8981\u8fdb\u5165\u73af\u5883\r\n```bash\r\n$ source activate zipline # \u8fdb\u5165\u521a\u624d\u5efa\u7acb\u7684\u73af\u5883 \r\n$ activate zipline        # windows \u8fdb\u5165\u73af\u5883\r\n```\r\n\r\n\u4e0b\u8f7d\u5e76\u5b89\u88c5ta-lib\u5305\u4ee5\u53ca\u6570\u636e\u5904\u7406\u5305\u3001\u7edf\u8ba1\u5206\u6790\u5305\u3002\u8f6c\u79fb\u81f3\u9879\u76ee\u5b89\u88c5\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u540e\u6267\u884c\uff1a\r\n\r\n\u53c2\u8003`talib`[\u5b89\u88c5\u65b9\u6cd5](https://github.com/mrjbq7/ta-lib)\r\n\r\n```bash\r\n$ # \u4ee5\u4e0b\u9700\u786e\u4fdd\u5728zipline\u9879\u76ee\u5b89\u88c5\u76ee\u5f55\u4e0b\u6267\u884c\r\n$ pip install -r ./etc/requirements.txt\r\n$ pip install -r ./etc/requirements_add.txt\r\n```\r\n\r\n\r\n##### \u7f16\u8bd1`C`\u6269\u5c55\u5e93\r\n\r\n\r\n```bash\r\n$ python setup.py build_ext --inplace\r\n```\r\n\r\n##### \u5b89\u88c5`zipline`\r\n\r\n\r\n```bash\r\n$ python setup.py install\r\n$ # \u5982\u9700\u5f00\u53d1\u5b89\u88c5\r\n$ python setup.py develop\r\n```\r\n\r\n#### \u51c6\u5907\u6570\u636e\r\n\u6210\u529f\u5b89\u88c5`zipline`\u540e\uff0c`cswd`\u5305\u4e5f\u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u3002\u5728\u76f8\u5e94\u73af\u5883\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a\r\n\r\n\r\n```bash\r\n$ init-stock-data # \u521d\u59cb\u5316\u57fa\u7840\u6570\u636e\u3002\u8017\u65f6\u5927\u7ea624\u5c0f\u65f6(\u4e3b\u8981\u4e0b\u8f7d\u65e5\u7ebf\u53ca\u6700\u8fd1\u4e00\u4e2a\u6708\u7684\u5206\u65f6\u4ea4\u6613\u6570\u636e\u3002\u5bf9\u6570\u636e\u91cf\u5c0f\uff0c\u4f46\u6293\u53d6\u7f51\u9875\u6570\u636e\u8017\u65f6\u957f\u7684\uff0c\u6574\u7406\u597d\u7684\u6570\u636e\u5b58\u653e\u5728github\uff0c\u521d\u59cb\u5316\u65f6\u4f1a\u4ece\u8be5\u5904\u63d0\u53d6\uff0c\u8282\u7ea6\u521d\u59cb\u5316\u65f6\u95f4\u3002)\r\n\r\n$ zipline ingest # \u8f6c\u6362\u65e5\u7ebf\u6570\u636e\uff0c\u8017\u65f6\u7ea610\u5206\u949f\r\n\r\n$ sql-to-bcolz # `Fundamentals`\u6570\u636e\uff0c\u8017\u65f6\u7ea61.5\u5206\u949f\r\n\r\n# \u5982\u9700\u8981\u8fdb\u884c\u56de\u6d4b\u5206\u6790\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u6307\u4ee4\uff0c\u751f\u6210ff\u56e0\u5b50\u6570\u636e(empyrical\u5305\u4f7f\u7528)\r\n$ gen-ff-factors\r\n```\r\n\u521d\u59cb\u5316\u6570\u636e\u540e\uff0c\u53c2\u8003[\u5982\u4f55\u8bbe\u7f6e\u540e\u53f0\u6570\u636e\u81ea\u52a8\u5904\u7406](./docs/\u4ecb\u7ecd\u6750\u6599/bg_tasks.cron)\uff0c\u8bbe\u7f6e\u540e\u53f0\u8ba1\u5212\u4efb\u52a1\u3002\u540e\u53f0\u5728\u76d8\u540e\u81ea\u52a8\u5b8c\u6210\u6570\u636e\u5bfc\u5165\u53ca\u8f6c\u6362\u3002\u7f51\u7edc\u6570\u636e\u7684\u91c7\u96c6\u53ef\u80fd\u56e0\u5404\u79cd\u539f\u5931\u8d25\uff0c\u8bf7\u6ce8\u610f\u67e5\u9605\u65e5\u5fd7\u6587\u6863\u3002\u6587\u6863\u9ed8\u8ba4\u8def\u5f84\u4e3a\"\uff5e/stockdata/logs\"\u3002\r\n\r\n#### \u9a8c\u8bc1\u5b89\u88c5\r\n\u5982\u80fd\u6b63\u5e38\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\uff0c\u8bc1\u660e\u5df2\u7ecf\u5b89\u88c5\u6210\u529f\u3002(\u6ce8\u610f\uff0c\u7531\u4e8e\u7248\u672c\u517c\u5bb9\u95ee\u9898\uff0c\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u4f1a\u6709\u5927\u91cf\u8b66\u544a\u4fe1\u606f\uff0c\u53ea\u8981\u4e0d\u662f\u9519\u8bef\uff0c\u53ef\u5ffd\u7565)\r\n\r\n```python\r\nfrom zipline import get_calendar\r\nc = get_calendar('SZSH')\r\nc.first_session\r\n# Timestamp('1990-12-19 00:00:00+0000', tz='UTC', freq='C')\r\n```\r\n\r\n#### \u4f7f\u7528\r\n\r\n\u8ba1\u7b97`Fama-French`\u4e09\u56e0\u5b50\u6848\u4f8b\u6d89\u53ca\u5230\uff1a\r\n1. \u5982\u4f55\u5728`Notebook`\u8fd0\u884c\u56de\u6d4b\r\n2. \u9009\u62e9\u57fa\u51c6\u6536\u76ca\u7387\u6307\u6570\u4ee3\u7801\r\n3. \u8ba1\u5212\u51fd\u6570\u7528\u6cd5\r\n4. `Fundamentals`\u53ca\u8d22\u52a1\u6570\u636e\r\n5. `pipeline`\u53ca\u81ea\u5b9a\u4e49\u56e0\u5b50\u7528\u6cd5\r\n6. \u56de\u6d4b\u901f\u5ea6\r\n\r\n\u6bd4\u8f83\u9002\u5408\u4f5c\u4e3a\u6f14\u793a\u6750\u6599\r\n\r\n\r\n```python\r\n%load_ext zipline\r\n```\r\n\r\n\r\n```python\r\n%%zipline --start 2017-1-1 --end 2018-1-1 --bm-symbol 399001\r\nfrom zipline.api import symbol, sid, get_datetime\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\nfrom zipline.api import (attach_pipeline, pipeline_output, get_datetime,\r\n                         calendars, schedule_function, date_rules)\r\nfrom zipline.pipeline import Pipeline\r\nfrom zipline.pipeline import CustomFactor\r\nfrom zipline.pipeline.data import USEquityPricing\r\nfrom zipline.pipeline.fundamentals import Fundamentals\r\n\r\n# time frame on which we want to compute Fama-French\r\nnormal_days = 31\r\n# approximate the number of trading days in that period\r\n# this is the number of trading days we'll look back on,\r\n# on every trading day.\r\nbusiness_days = int(0.69 * normal_days)\r\n\r\n\r\n# \u4ee5\u4e0b\u81ea\u5b9a\u4e49\u56e0\u5b50\u9009\u53d6\u671f\u521d\u6570\r\nclass Returns(CustomFactor):\r\n    \"\"\"\r\n    \u6bcf\u4e2a\u4ea4\u6613\u65e5\u6bcf\u4e2a\u80a1\u7968\u7a97\u53e3\u957f\u5ea6\"business_days\"\u671f\u95f4\u6536\u76ca\u7387\r\n    \"\"\"\r\n    window_length = business_days\r\n    inputs = [USEquityPricing.close]\r\n\r\n    def compute(self, today, assets, out, price):\r\n        out[:] = (price[-1] - price[0]) / price[0] * 100\r\n\r\n\r\nclass MarketEquity(CustomFactor):\r\n    \"\"\"\r\n    \u6bcf\u4e2a\u4ea4\u6613\u65e5\u6bcf\u53ea\u80a1\u7968\u6240\u5bf9\u5e94\u7684\u603b\u5e02\u503c\r\n    \"\"\"\r\n    window_length = business_days\r\n    inputs = [USEquityPricing.tmv]\r\n\r\n    def compute(self, today, assets, out, mcap):\r\n        out[:] = mcap[0]\r\n\r\n\r\nclass BookEquity(CustomFactor):\r\n    \"\"\"\r\n    \u6bcf\u4e2a\u4ea4\u6613\u65e5\u6bcf\u53ea\u80a1\u7968\u6240\u5bf9\u5e94\u7684\u8d26\u9762\u4ef7\u503c\uff08\u6240\u6709\u8005\u6743\u76ca\uff09\r\n    \"\"\"\r\n    window_length = business_days\r\n    inputs = [Fundamentals.balance_sheet.A107]\r\n\r\n    def compute(self, today, assets, out, book):\r\n        out[:] = book[0]\r\n\r\n\r\ndef initialize(context):\r\n    \"\"\"\r\n    use our factors to add our pipes and screens.\r\n    \"\"\"\r\n    pipe = Pipeline()\r\n    mkt_cap = MarketEquity()\r\n    pipe.add(mkt_cap, 'market_cap')\r\n\r\n    book_equity = BookEquity()\r\n    # book equity over market equity\r\n    be_me = book_equity / mkt_cap\r\n    pipe.add(be_me, 'be_me')\r\n\r\n    returns = Returns()\r\n    pipe.add(returns, 'returns')\r\n\r\n    attach_pipeline(pipe, 'ff_example')\r\n    schedule_function(\r\n        func=myfunc,\r\n        date_rule=date_rules.month_end())\r\n\r\n\r\ndef before_trading_start(context, data):\r\n    \"\"\"\r\n    every trading day, we use our pipes to construct the Fama-French\r\n    portfolios, and then calculate the Fama-French factors appropriately.\r\n    \"\"\"\r\n\r\n    factors = pipeline_output('ff_example')\r\n\r\n    # get the data we're going to use\r\n    returns = factors['returns']\r\n    mkt_cap = factors.sort_values(['market_cap'], ascending=True)\r\n    be_me = factors.sort_values(['be_me'], ascending=True)\r\n\r\n    # to compose the six portfolios, split our universe into portions\r\n    half = int(len(mkt_cap) * 0.5)\r\n    small_caps = mkt_cap[:half]\r\n    big_caps = mkt_cap[half:]\r\n\r\n    thirty = int(len(be_me) * 0.3)\r\n    seventy = int(len(be_me) * 0.7)\r\n    growth = be_me[:thirty]\r\n    neutral = be_me[thirty:seventy]\r\n    value = be_me[seventy:]\r\n\r\n    # now use the portions to construct the portfolios.\r\n    # note: these portfolios are just lists (indices) of equities\r\n    small_value = small_caps.index.intersection(value.index)\r\n    small_neutral = small_caps.index.intersection(neutral.index)\r\n    small_growth = small_caps.index.intersection(growth.index)\r\n\r\n    big_value = big_caps.index.intersection(value.index)\r\n    big_neutral = big_caps.index.intersection(neutral.index)\r\n    big_growth = big_caps.index.intersection(growth.index)\r\n\r\n    # take the mean to get the portfolio return, assuming uniform\r\n    # allocation to its constituent equities.\r\n    sv = returns[small_value].mean()\r\n    sn = returns[small_neutral].mean()\r\n    sg = returns[small_growth].mean()\r\n\r\n    bv = returns[big_value].mean()\r\n    bn = returns[big_neutral].mean()\r\n    bg = returns[big_growth].mean()\r\n\r\n    # computing SMB\r\n    context.smb = (sv + sn + sg) / 3 - (bv + bn + bg) / 3\r\n\r\n    # computing HML\r\n    context.hml = (sv + bv) / 2 - (sg + bg) / 2\r\n\r\n\r\ndef myfunc(context, data):\r\n    d = get_datetime('Asia/Shanghai')\r\n    print(d, context.smb, context.hml)\r\n```\r\n\r\n    2017-01-26 15:00:00+08:00 0.014014289806335789 6.605843892342312\r\n    2017-02-28 15:00:00+08:00 4.1169182374497195 7.690119769984805\r\n    2017-03-31 15:00:00+08:00 0.35808304923773615 2.7492806758694215\r\n    2017-04-28 15:00:00+08:00 -4.318408584890385 5.414312699826368\r\n    2017-05-31 15:00:00+08:00 -0.4828317045367072 3.0869028143557147\r\n    2017-06-30 15:00:00+08:00 0.8640245866550513 0.09803178533289003\r\n    2017-07-31 15:00:00+08:00 -2.3024594948720227 6.2829537294457145\r\n    2017-08-31 15:00:00+08:00 3.2003154621799155 2.269609384481118\r\n    2017-09-29 15:00:00+08:00 1.1669055941862554 -0.6079568594636064\r\n    2017-10-31 15:00:00+08:00 -1.6233534895267374 -0.795885505339075\r\n    2017-11-30 15:00:00+08:00 -2.965097825507776 4.4434701009908615\r\n    2017-12-29 15:00:00+08:00 -1.1942883365086068 -0.38062423581176485\r\n    [2018-05-02 02:07:02.681398] INFO: zipline.finance.metrics.tracker: Simulated 244 trading days\r\n    first open: 2017-01-03 01:31:00+00:00\r\n    last close: 2017-12-29 07:00:00+00:00\r\n\r\n**\u8fd0\u884c\u65f6\u957f10-12\u79d2**\r\n\r\n+ \u6709\u5173\u5982\u4f55\u4f7f\u7528\uff0c\u8bf7\u53c2\u8003[quantopian\u4f7f\u7528\u624b\u518c](https://www.quantopian.com/help)\u3002\r\n+ \u6709\u5173\u672c\u9879\u76ee\u7684\u8bf4\u660e\uff0c\u8bf7\u53c2\u9605[\u4ecb\u7ecd\u6750\u6599](./docs/\u4ecb\u7ecd\u6750\u6599)\r\n+ \u6709\u5173\u540e\u53f0\u81ea\u52a8\u6570\u636e\u5904\u7406\uff0c\u8bf7\u53c2\u8003[\u811a\u672c](./docs/\u4ecb\u7ecd\u6750\u6599/bg_tasks.cron)\r\n\r\n**\u7279\u522b\u8bf4\u660e**\uff1a\u4e2a\u4eba\u5f53\u524d\u4f7f\u7528Ubuntu18.04\u64cd\u4f5c\u7cfb\u7edf\r\n\r\n#### \u53c2\u8003\u914d\u7f6e\r\n\r\n![\u7cfb\u7edf](./images/sys_info.png)\r\n\r\n+ Ubuntu 18.04\r\n+ Anaconda \r\n+ python 3.6\r\n\r\n#### \u4ea4\u6d41\r\n\r\n\u8be5\u9879\u76ee\u7eaf\u5c5e\u4e2a\u4eba\u7231\u597d\uff0c\u6c34\u5e73\u6709\u9650\uff0c\u6b22\u8fce\u52a0\u5165\u6765\u4e00\u8d77\u5b8c\u5584\u3002\r\n\r\n**\u6dfb\u52a0\u4e2a\u4eba\u5fae\u4fe1(ldf10728268)\uff0c\u8bf7\u52a1\u5fc5\u5907\u6ce8`zipline`**\r\n<div>\r\n<img src=\"/images/ldf.png\" width=\"30%\" height=\"30%\" />\r\n</div>\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Zipline\u662fQUANTOPIAN\u5f00\u53d1\u7684\u7b97\u6cd5\u4ea4\u6613\u5e93\u3002\u8fd9\u662f\u4e00\u4e2a\u4e8b\u4ef6\u9a71\u52a8\uff0c\u652f\u6301\u56de\u6d4b\u548c\u5b9e\u65f6\u4ea4\u6613\u7684\u7cfb\u7edf\u3002Zipline\u76ee\u524d\u6709\u4e00\u4e2a\u514d\u8d39\u7684[\u56de\u6d4b\u5e73\u53f0](https://www.quantopian.com)\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7f16\u5199\u4ea4\u6613\u7b56\u7565\u548c\u6267\u884c\u56de\u6d4b\u3002\u4e3a\u5904\u7406A\u80a1\u6570\u636e\uff0c\u589e\u52a0\u6216\u4fee\u6539\u57fa\u7840\u6570\u636e\u76f8\u5173\u90e8\u5206\uff0c\u7528\u4e8e\u672c\u673a\u7b56\u7565\u56de\u6d4b\u5206\u6790\u3002.",
    "version": "1.13.2",
    "project_urls": {
        "Homepage": "https://github.com/yanjlee/czipline"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7e3d1032355affdf4b0d1f85c4111a86f893d45519905f31177e85dfa41ad681",
                "md5": "196107f6bde53f33f2357e9213634780",
                "sha256": "522b5f6aa503a142d4070766e3497b1bf644ae2754e97d21f909cff961cbd13b"
            },
            "downloads": -1,
            "filename": "czipline_pro-1.13.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "196107f6bde53f33f2357e9213634780",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 905567,
            "upload_time": "2024-06-01T07:33:11",
            "upload_time_iso_8601": "2024-06-01T07:33:11.098071Z",
            "url": "https://files.pythonhosted.org/packages/7e/3d/1032355affdf4b0d1f85c4111a86f893d45519905f31177e85dfa41ad681/czipline_pro-1.13.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4c2dbd3759e1abc6ae3c11738bcfa00a7eb2e71a035ca372eb850550d1063725",
                "md5": "0e3e6ab3853ff7dcdca96b64a247ee88",
                "sha256": "1b8879dc7a0319787e0b2dcad657b4e491603c50ba2995c35669d8651ae55957"
            },
            "downloads": -1,
            "filename": "czipline_pro-1.13.2.tar.gz",
            "has_sig": false,
            "md5_digest": "0e3e6ab3853ff7dcdca96b64a247ee88",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 980399,
            "upload_time": "2024-06-01T07:33:16",
            "upload_time_iso_8601": "2024-06-01T07:33:16.208013Z",
            "url": "https://files.pythonhosted.org/packages/4c/2d/bd3759e1abc6ae3c11738bcfa00a7eb2e71a035ca372eb850550d1063725/czipline_pro-1.13.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-01 07:33:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yanjlee",
    "github_project": "czipline",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": false,
    "appveyor": true,
    "lcname": "czipline-pro"
}
        
Elapsed time: 4.36066s