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.
ENH: Add sample notebooks for QE Getting Started tutorial.
- Loading branch information
Ernesto Perez
committed
Oct 30, 2018
1 parent
8963f25
commit 9bc9355
Showing
3 changed files
with
912 additions
and
0 deletions.
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
notebooks/tutorials/1_factset_getting_started_lesson2/notebook.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
315 changes: 315 additions & 0 deletions
315
notebooks/tutorials/1_factset_getting_started_lesson3/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,315 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Pipeline API" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The Pipeline API is a powerful tool for cross-sectional analysis of asset data. It allows us to define a set of calculations on multiple data inputs and analyze a large amount of assets at a time. Some common uses for the Pipeline API include: \n", | ||
"\n", | ||
"* Selecting assets based on filtering rules \n", | ||
"* Ranking assets based on a scoring function \n", | ||
"* Calculating portfolio allocations \n", | ||
"\n", | ||
"To begin, let's import the Pipeline class and create a function that returns an empty pipeline. Putting our pipeline definition inside a function helps us keep things organized as our pipeline grows in complexity. This is particularly helpful when transferring data pipelines between Research and the IDE." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Pipeline class\n", | ||
"from quantopian.pipeline import Pipeline\n", | ||
"\n", | ||
"def make_pipeline():\n", | ||
" # Create and return an empty Pipeline\n", | ||
" return Pipeline()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"To add an output to our pipeline we need to include a reference to a dataset, and specify the computations we want to carry out on that data. For example, we will add a reference to the `close` column from the `USEquityPricing` dataset. Then, we can define our output to be the latest value from this column as follows:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import Pipeline class and USEquityPricing dataset\n", | ||
"from quantopian.pipeline import Pipeline\n", | ||
"from quantopian.pipeline.data import USEquityPricing\n", | ||
"\n", | ||
"def make_pipeline():\n", | ||
" # Get latest closing price\n", | ||
" close_price = USEquityPricing.close.latest\n", | ||
"\n", | ||
" # Return Pipeline containing latest closing price\n", | ||
" return Pipeline(\n", | ||
" columns={\n", | ||
" 'close_price': close_price,\n", | ||
" }\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The Pipeline API also provides a number of built-in calculations, some of which are computed over trailing windows of data. For example, the following code defines an output as the 3 day moving average of the `close` column we added above:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import Pipeline class and datasets\n", | ||
"from quantopian.pipeline import Pipeline\n", | ||
"from quantopian.pipeline.data import USEquityPricing\n", | ||
"\n", | ||
"# Import built-in moving average calculation\n", | ||
"from quantopian.pipeline.factors import SimpleMovingAverage\n", | ||
"\n", | ||
"\n", | ||
"def make_pipeline():\n", | ||
" # Get latest closing price\n", | ||
" close_price = USEquityPricing.close.latest\n", | ||
"\n", | ||
" # Calculate 3 day average closing price\n", | ||
" avg_close = SimpleMovingAverage(\n", | ||
" inputs=[USEquityPricing.close],\n", | ||
" window_length=3,\n", | ||
" )\n", | ||
"\n", | ||
" # Return Pipeline containing close_price\n", | ||
" # and avg_close\n", | ||
" return Pipeline(\n", | ||
" columns={\n", | ||
" 'close_price': close_price,\n", | ||
" 'avg_close': avg_close,\n", | ||
" }\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Universe Selection" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"An important part of developing a strategy is defining the set of assets that we want to consider trading in our portfolio. We usually refer to this set of assets as our trading universe. A trading universe should be as large as possible, while also excluding assets that aren't appropriate for our portfolio. For example, we might want to exclude stocks that are illiquid or difficult to trade. \n", | ||
"\n", | ||
"The Pipeline API allows us to easily create filters from data columns and calculation results for this purpose. In the following example we create filters for price, market value, and average dollar volume. Then, we set the screen parameter of our pipeline constructor to be the intersection (`&`) of all 3 filters:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Import Pipeline class and datasets\n", | ||
"from quantopian.pipeline import Pipeline\n", | ||
"from quantopian.pipeline.data import factset\n", | ||
"from quantopian.pipeline.data import USEquityPricing\n", | ||
"\n", | ||
"# Import built-in moving average and\n", | ||
"# average dollar volume calculations\n", | ||
"from quantopian.pipeline.factors import (\n", | ||
" AverageDollarVolume,\n", | ||
" SimpleMovingAverage,\n", | ||
")\n", | ||
"\n", | ||
"def make_pipeline():\n", | ||
" # Trading universe characteristics\n", | ||
" mcap_filter = factset.Fundamentals.mkt_val.latest > 500000000\n", | ||
" adv_filter = AverageDollarVolume(window_length=200) > 2500000\n", | ||
" price_filter = USEquityPricing.close.latest > 5\n", | ||
"\n", | ||
" # Combine filters\n", | ||
" base_universe = mcap_filter & adv_filter & price_filter\n", | ||
"\n", | ||
" # Get latest closing price\n", | ||
" close_price = USEquityPricing.close.latest\n", | ||
"\n", | ||
" # Calculate 3 day average closing price\n", | ||
" avg_close = SimpleMovingAverage(\n", | ||
" inputs=[USEquityPricing.close],\n", | ||
" window_length=3,\n", | ||
" )\n", | ||
"\n", | ||
" # Return Pipeline containing close_price\n", | ||
" # and avg_close\n", | ||
" return Pipeline(\n", | ||
" columns={\n", | ||
" 'close_price': close_price,\n", | ||
" 'avg_close': avg_close,\n", | ||
" },\n", | ||
" screen=base_universe,\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now that our pipeline definition is complete, we can execute it over a specific period of time using `run_pipeline`. The output will be a pandas DataFrame indexed by date and asset, with columns corresponding to the outputs we added to our pipeline definition:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th></th>\n", | ||
" <th>avg_close</th>\n", | ||
" <th>close_price</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th rowspan=\"10\" valign=\"top\">2014-12-31 00:00:00+00:00</th>\n", | ||
" <th>Equity(47430 [MBLY])</th>\n", | ||
" <td>42.410000</td>\n", | ||
" <td>41.11</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47578 [LTRP_A])</th>\n", | ||
" <td>27.300000</td>\n", | ||
" <td>27.15</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47740 [BABA])</th>\n", | ||
" <td>105.880000</td>\n", | ||
" <td>105.77</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47752 [CDK])</th>\n", | ||
" <td>40.906667</td>\n", | ||
" <td>40.42</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47764 [AVAL])</th>\n", | ||
" <td>10.609355</td>\n", | ||
" <td>10.49</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47777 [CFG])</th>\n", | ||
" <td>25.310000</td>\n", | ||
" <td>25.22</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47779 [CYBR])</th>\n", | ||
" <td>41.006667</td>\n", | ||
" <td>39.90</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47785 [CNXM])</th>\n", | ||
" <td>23.610000</td>\n", | ||
" <td>24.31</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47788 [TVPT])</th>\n", | ||
" <td>18.026667</td>\n", | ||
" <td>18.04</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>Equity(47921 [KEYS])</th>\n", | ||
" <td>33.863333</td>\n", | ||
" <td>33.95</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" avg_close close_price\n", | ||
"2014-12-31 00:00:00+00:00 Equity(47430 [MBLY]) 42.410000 41.11\n", | ||
" Equity(47578 [LTRP_A]) 27.300000 27.15\n", | ||
" Equity(47740 [BABA]) 105.880000 105.77\n", | ||
" Equity(47752 [CDK]) 40.906667 40.42\n", | ||
" Equity(47764 [AVAL]) 10.609355 10.49\n", | ||
" Equity(47777 [CFG]) 25.310000 25.22\n", | ||
" Equity(47779 [CYBR]) 41.006667 39.90\n", | ||
" Equity(47785 [CNXM]) 23.610000 24.31\n", | ||
" Equity(47788 [TVPT]) 18.026667 18.04\n", | ||
" Equity(47921 [KEYS]) 33.863333 33.95" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Import run_pipeline method\n", | ||
"from quantopian.research import run_pipeline\n", | ||
"\n", | ||
"# Execute pipeline created by make_pipeline\n", | ||
"# between start_date and end_date\n", | ||
"pipeline_output = run_pipeline(\n", | ||
" make_pipeline(),\n", | ||
" start_date='2014-01-01',\n", | ||
" end_date='2014-12-31'\n", | ||
")\n", | ||
"\n", | ||
"# Display last 10 rows\n", | ||
"pipeline_output.tail(10)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In the next lesson we will formalize the strategy our algorithm will use to select assets to trade. Then, we will use a factor analysis tool to evaluate the predictive power of our strategy over historical data." | ||
] | ||
} | ||
], | ||
"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.