{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pipeline Example" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from quantopian.pipeline import Pipeline\n", "from quantopian.research import run_pipeline\n", "from quantopian.pipeline.data.builtin import USEquityPricing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting the Securities we want.\n", "\n", "### The Q500US and Q1500US\n", "\n", "These gropus of tradeable stocks are refered to as \"universes\", because all your trades will use these stocks as their \"Universe\" of available stock, they won't be trading with anything outside these groups." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from quantopian.pipeline.filters import Q1500US" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two main benefits of the Q500US and Q1500US. Firstly, they greatly reduce the risk of an order not being filled. Secondly, they allow for more meaningful comparisons between strategies as now they will be used as the standard universes for algorithms." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "universe = Q1500US()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filtering the universe further with Classifiers\n", "\n", "Let's only grab stocks in the energy sector: https://www.quantopian.com/help/fundamentals#industry-sector" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from quantopian.pipeline.data import morningstar" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sector = morningstar.asset_classification.morningstar_sector_code.latest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternative:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#from quantopian.pipeline.classifiers.morningstar import Sector\n", "#morningstar_sector = Sector()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "energy_sector = sector.eq(309)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Masking Filters\n", "\n", "Masks can be also be applied to methods that return filters like top, bottom, and percentile_between.\n", "\n", "Masks are most useful when we want to apply a filter in the earlier steps of a combined computation. For example, suppose we want to get the 50 securities with the highest open price that are also in the top 10% of dollar volume. \n", "\n", "Suppose that we then want the 90th-100th percentile of these securities by close price. We can do this with the following:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Dollar volume factor\n", "dollar_volume = AverageDollarVolume(window_length=30)\n", "\n", "# High dollar volume filter\n", "high_dollar_volume = dollar_volume.percentile_between(90,100)\n", "\n", "# Top open price filter (high dollar volume securities)\n", "top_open_price = USEquityPricing.open.latest.top(50, mask=high_dollar_volume)\n", "\n", "# Top percentile close price filter (high dollar volume, top 50 open price)\n", "high_close_price = USEquityPricing.close.latest.percentile_between(90, 100, mask=top_open_price)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Applying Filters and Factors\n", "\n", "Let's apply our own filters, following along with some of the examples above. Let's select the following securities:\n", "\n", "* Stocks in Q1500US\n", "* Stocks that are in the energy Sector\n", "* They must be relatively highly traded stocks in the market (by dollar volume traded, need to be in the top 5% traded)\n", "\n", "Then we'll calculate the percent difference as we've done previously. Using this percent difference we'll create an unsophisticated strategy that shorts anything with negative percent difference (the difference between the 10 day mean and the 30 day mean)." ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_pipeline():\n", " \n", " # Base universe filter.\n", " base_universe = Q1500US()\n", " \n", " # Sector Classifier as Filter\n", " energy_sector = sector.eq(309)\n", " \n", " # Masking Base Energy Stocks\n", " base_energy = base_universe & energy_sector\n", " \n", " # Dollar volume factor\n", " dollar_volume = AverageDollarVolume(window_length=30)\n", "\n", " # Top half of dollar volume filter\n", " high_dollar_volume = dollar_volume.percentile_between(95,100)\n", " \n", " # Final Filter Mask\n", " top_half_base_energy = base_energy & high_dollar_volume\n", " \n", " # 10-day close price average.\n", " mean_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10, mask=top_half_base_energy)\n", "\n", " # 30-day close price average.\n", " mean_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30, mask=top_half_base_energy)\n", "\n", " # Percent difference factor.\n", " percent_difference = (mean_10 - mean_30) / mean_30\n", " \n", " # Create a filter to select securities to short.\n", " shorts = percent_difference < 0\n", " \n", " # Create a filter to select securities to long.\n", " longs = percent_difference > 0\n", " \n", " # Filter for the securities that we want to trade.\n", " securities_to_trade = (shorts | longs)\n", " \n", " return Pipeline(\n", " columns={\n", " 'longs': longs,\n", " 'shorts': shorts,\n", " 'percent_diff':percent_difference\n", " },\n", " screen=securities_to_trade\n", " )" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | \n", " | longs | \n", "percent_diff | \n", "shorts | \n", "
---|---|---|---|---|
2015-05-05 00:00:00+00:00 | \n", "Equity(216 [HES]) | \n", "True | \n", "0.036528 | \n", "False | \n", "
Equity(448 [APA]) | \n", "True | \n", "0.035926 | \n", "False | \n", "|
Equity(455 [APC]) | \n", "True | \n", "0.049153 | \n", "False | \n", "|
Equity(858 [BHI]) | \n", "True | \n", "0.033807 | \n", "False | \n", "|
Equity(1746 [COG]) | \n", "True | \n", "0.058078 | \n", "False | \n", "|
Equity(2368 [DVN]) | \n", "True | \n", "0.046264 | \n", "False | \n", "|
Equity(2564 [EOG]) | \n", "True | \n", "0.032102 | \n", "False | \n", "|
Equity(2621 [ESV]) | \n", "True | \n", "0.060197 | \n", "False | \n", "|
Equity(3443 [HAL]) | \n", "True | \n", "0.049257 | \n", "False | \n", "|
Equity(3647 [HP]) | \n", "True | \n", "0.040991 | \n", "False | \n", "|
Equity(5035 [MRO]) | \n", "True | \n", "0.061598 | \n", "False | \n", "|
Equity(5213 [NBL]) | \n", "True | \n", "0.010443 | \n", "False | \n", "|
Equity(5214 [NBR]) | \n", "True | \n", "0.064133 | \n", "False | \n", "|
Equity(5249 [NE]) | \n", "True | \n", "0.037559 | \n", "False | \n", "|
Equity(5729 [OXY]) | \n", "True | \n", "0.029776 | \n", "False | \n", "|
Equity(6928 [SLB]) | \n", "True | \n", "0.046555 | \n", "False | \n", "|
Equity(7244 [SWN]) | \n", "True | \n", "0.070788 | \n", "False | \n", "|
Equity(7612 [ANDV]) | \n", "True | \n", "0.005997 | \n", "False | \n", "|
Equity(7990 [VLO]) | \n", "False | \n", "-0.017145 | \n", "True | \n", "|
Equity(8214 [WMB]) | \n", "True | \n", "0.018876 | \n", "False | \n", "|
Equity(8347 [XOM]) | \n", "True | \n", "0.017343 | \n", "False | \n", "|
Equity(8461 [CHK]) | \n", "True | \n", "0.014265 | \n", "False | \n", "|
Equity(9038 [RIG]) | \n", "True | \n", "0.048180 | \n", "False | \n", "|
Equity(13176 [CAM]) | \n", "True | \n", "0.082110 | \n", "False | \n", "|
Equity(17436 [PXD]) | \n", "True | \n", "0.010248 | \n", "False | \n", "|
Equity(19249 [RRC]) | \n", "True | \n", "0.087062 | \n", "False | \n", "|
Equity(19336 [WFT]) | \n", "True | \n", "0.049141 | \n", "False | \n", "|
Equity(22784 [FTI]) | \n", "True | \n", "0.054529 | \n", "False | \n", "|
Equity(23112 [CVX]) | \n", "True | \n", "0.018972 | \n", "False | \n", "|
Equity(23998 [COP]) | \n", "True | \n", "0.023902 | \n", "False | \n", "|
Equity(24809 [NOV]) | \n", "True | \n", "0.024940 | \n", "False | \n", "|
Equity(25707 [WLL]) | \n", "True | \n", "0.048205 | \n", "False | \n", "|
Equity(33856 [CLR]) | \n", "True | \n", "0.064304 | \n", "False | \n", "|
Equity(34440 [CXO]) | \n", "True | \n", "0.042184 | \n", "False | \n", "|
Equity(39797 [OAS]) | \n", "True | \n", "0.042388 | \n", "False | \n", "|
Equity(40852 [KMI]) | \n", "True | \n", "0.023016 | \n", "False | \n", "|
Equity(41636 [MPC]) | \n", "True | \n", "0.011952 | \n", "False | \n", "|
Equity(42788 [PSX]) | \n", "True | \n", "0.020911 | \n", "False | \n", "