forked from quantopian/research_public
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aaf37c
commit 6619305
Showing
5 changed files
with
995 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
notebooks/tutorials/3_factset_alphalens_lesson_2/notebook.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Companion notebook for Alphalens tutorial lesson 2\n", | ||
"\n", | ||
"# Creating Tear Sheets With Alphalens\n", | ||
"\n", | ||
"In the previous lesson, you learned what Alphalens is. In this lesson, you will learn a four step process for how to use it:\n", | ||
"\n", | ||
"1. Express an alpha factor and define a trading universe by creating and running a Pipeline over a certain time period.\n", | ||
"2. Query pricing data for the assets in our universe during that same time period with `get_pricing()`.\n", | ||
"3. Align the alpha factor data with the pricing data with `get_clean_factor_and_forward_returns()`.\n", | ||
"4. Visualize how well our alpha factor predicts future price movements with `create_full_tear_sheet()`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Build And Run A Pipeline\n", | ||
"The following code creates a trading universe and expresses an alpha factor within a pipeline, then runs it with `run_pipeline()`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from quantopian.pipeline import Pipeline\n", | ||
"from quantopian.research import run_pipeline\n", | ||
"from quantopian.pipeline.factors import AverageDollarVolume\n", | ||
"from quantopian.pipeline.data import factset, USEquityPricing\n", | ||
"\n", | ||
"def make_pipeline():\n", | ||
" # Filter out equities with low market capitalization\n", | ||
" market_cap_filter = factset.Fundamentals.mkt_val.latest > 500000000\n", | ||
"\n", | ||
" # Filter out equities with low volume\n", | ||
" volume_filter = AverageDollarVolume(window_length=200) > 2500000\n", | ||
"\n", | ||
" # Filter out equities with a close price below $5\n", | ||
" price_filter = USEquityPricing.close.latest > 5\n", | ||
"\n", | ||
" # Our final base universe\n", | ||
" base_universe = market_cap_filter & volume_filter & price_filter\n", | ||
"\n", | ||
" # Measures a company's asset growth rate.\n", | ||
" asset_growth = factset.Fundamentals.assets_gr_qf.latest\n", | ||
"\n", | ||
" return Pipeline(\n", | ||
" columns={'asset_growth': asset_growth},\n", | ||
" screen=base_universe & asset_growth.notnull()\n", | ||
" )\n", | ||
"\n", | ||
"pipeline_output = run_pipeline(pipeline=make_pipeline(), start_date='2014-1-1', end_date='2016-1-1')\n", | ||
"\n", | ||
"# Show the first 5 rows of factor data\n", | ||
"pipeline_output.head(5)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Query Pricing Data\n", | ||
"\n", | ||
"Now that we have factor data, let's get pricing data for the same time period. `get_pricing()` returns pricing data for a list of assets over a specified time period. It requires four arguments:\n", | ||
"- A list of assets for which we want pricing.\n", | ||
"- A start date\n", | ||
"- An end date\n", | ||
"- Whether to use open, high, low or close pricing.\n", | ||
"\n", | ||
"Execute the following cell to get pricing data." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"pricing_data = get_pricing(\n", | ||
" symbols=pipeline_output.index.levels[1], # Finds all assets that appear at least once in \"pipeline_output\" \n", | ||
" start_date='2014-1-1',\n", | ||
" end_date='2016-2-1', # must be after run_pipeline()'s end date. Explained more in lesson 4\n", | ||
" fields='open_price' # Generally, you should use open pricing. Explained more in lesson 4\n", | ||
")\n", | ||
"\n", | ||
"# Show the first 5 rows of pricing_data\n", | ||
"pricing_data.head(5)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Align Data\n", | ||
"\n", | ||
"`get_clean_factor_and_forward_returns()` aligns the factor data created by `run_pipeline()` with the pricing data created by `get_pricing()`, and returns an object suitable for analysis with Alphalens' charting functions. It requires two arguments:\n", | ||
"- The factor data we created with `run_pipeline()`.\n", | ||
"- The pricing data we created with `get_pricing()`.\n", | ||
"\n", | ||
"Execute the following cell to align the factor data with the pricing data." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from alphalens.utils import get_clean_factor_and_forward_returns\n", | ||
"\n", | ||
"factor_data = get_clean_factor_and_forward_returns(\n", | ||
" factor=pipeline_output, \n", | ||
" prices=pricing_data\n", | ||
")\n", | ||
"\n", | ||
"# Show the first 5 rows of merged_data\n", | ||
"factor_data.head(5)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Visualize Results\n", | ||
"\n", | ||
"Finally, execute the following cell to pass the output of `get_clean_factor_and_forward_returns()` to a function called `create_full_tear_sheet()`. This will create whats known as a tear sheet." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from alphalens.tears import create_full_tear_sheet\n", | ||
"\n", | ||
"create_full_tear_sheet(factor_data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## That's It!\n", | ||
"\n", | ||
"In the next lesson, we will show you how to interpret the charts produced by `create_full_tear_sheet()`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 2", | ||
"language": "python", | ||
"name": "python2" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.