Alphalens
lesson 1 Introduction
什么是alphalens?
- alphalens是一个用来分析给定alpha因子预测未来回报有效性的工具,需要提醒的是,alpha因子表达了某些给定信息集与未来回报之间的预测关系。
我们该什么时候使用alphalens?
-
在量化工作流程中,Alphalens被设计为早期和经常使用。一旦你定义了你的资产池并在pipeline中构造了一个因子,最好在回测前使用Alphalens来分析它。在量化工作流程的背景下:
1.定义您的交易领域,并建立一个阿尔法因子使用pipeline API。
2.分析阿尔法因子的预测能力。
3.使用optimal API在quant’ s IDE中创建基于您的阿尔法因子的交易策略。
4.用Pyfolio测试你的交易策略并分析结果。
在管道API教程中,您学习了如何定义一个交易领域并构建一个alpha因子。alphalens可以让你检查一个因子,看看它是如何预测的。第1步和第2步在研究笔记中完成,第3步和第4步在算法IDE中完成。
lesson 2 创建Creating Teaersheets
用 alphalens 创建tear sheets
- 在这节课中,你将学习如何使用它的四个步骤:
1.通过在一定时间内创建和运行一个pipeline来表达一个阿尔法因子并定义一个交易范围。
2.使用get_pricing()查询同一时间段交易范围中资产的定价数据
3.使用get_clean_factor_and_forward_returns()将alpha因子数据与定价数据对齐
4.使用create_full_tear_sheet()可视化alpha因子对未来价格走势的预测
建立和运行一个pipeline
- 下面的代码表示了一个基于资产增长的alpha因子,然后使用run_pipeline()运行它。
from quantopian.pipeline.data import factset
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import QTradableStocksUS
def make_pipeline():
# Measures a company's asset growth rate.
asset_growth = factset.Fundamentals.assets_gr_qf.latest
return Pipeline(
columns={
'asset_growth': asset_growth},
screen=QTradableStocksUS() & asset_growth.notnull()
)
factor_data = run_pipeline(pipeline=make_pipeline(), start_date='2014-1-1', end_date='2016-1-1')
# Show the first 5 rows of factor data
factor_data.head(5)
现在我们有了因子的数据。数据的前五行是这样的:
查询价格数据
- 现在我们有了因子数据,让我们得到相同时期的价格数据。get_pricing()返回指定时间段内资产列表的定价数据。它需要四个参数:
1.我们希望为其定价的资产列表。
2.开始日期。
3.结束日期。
4.是采用开盘价、高点、低点还是收盘价。
pricing_data = get_pricing(
symbols=factor_data.index.levels[1], # Finds all assets that appear at least once in "factor_data"
start_date='2014-1-1',
end_date='2016-2-1', # must be after run_pipeline()'s end date. Explained more in lesson 4
fields='open_price' # Generally, you should use open pricing. Explained more in lesson 4
)
# Show the first 5 rows of pricing_data
pricing_data.head(5)
我们现在有了交易系统中每种资产的定价数据。前五行是这样的:
对其数据
- get_clean_factor_and_forward_returns()将来自pipeline的因子数据与来自get_pricing()的定价数据进行对齐,并返回一个适合使用Alphalens的图表函数进行分析的对象。它需要两个参数:
1.我们使用run_pipeline()创建的因子数据。
2.我们使用get_pricing()创建的定价数据。
from alphalens.utils import get_clean_factor_and_forward_returns
merged_data = get_clean_factor_and_forward_returns(
factor=factor_data,
prices=pricing_data
)
# Show the first 5 rows of merged_data
merged_data.head(5)
- 我们现在已经将定价和要素数据合并成一种格式,这种格式可以用来分析我们的要素数据是否会在未来的某个时期影响价格。默认情况下,这些时间段是1天、5天和10天。
可视化回报
最后,将get_clean_factor_and_forward_returns()的输出传递给create_full_tear_sheet()。
lesson 3 interpreting tearsheets
在前一课中,您学习了如何查询和处理数据,以便我们能够使用Alphalens tear sheet分析数据。在本节课中,您将通过分析这些数据片来体验量化工作流的alpha发现阶段的几个迭代。在这节课中,我们将:
1.使用create_information_tear_sheet()分析alpha因子对未来价格走势的预测效果。
2.试着通过与另一个因子结合来改善我们原来的因子。
3.使用create_returns_tear_sheet()预览alpha因子的盈利能力。
我们的初始因子
- 下面的代码根据公司的净收入和市值表达了一个alpha因子,然后为该alpha因子创建一个 information tear sheet。我们将通过查看它的信息系数(IC)来开始分析alpha因子。IC是一个从-1到1的数字,它量化了一个因子的可预测性。任何大于0的数都被认为具有一定的预测性。
- 首先要看的是IC均值,它是一个因子在给定时间段内的平均IC。你希望因子的IC均值尽可能高。一般来说,一个因子的IC均值是否大于0是值得研究的。如果在一个大的交易区间内,它的IC均值接近于。1(或更高),那么这个因素可能真的很好。
from quantopian.pipeline.data import factset
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import CustomFactor, SimpleMovingAverage
from quantopian.pipeline.filters import QTradableStocksUS
from alphalens.tears import create_information_tear_sheet
from alphalens.