Notebook
In [4]:
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import Q1500US
from quantopian.research import run_pipeline

import pandas as pd
import numpy as np
In [6]:
# This make_pipeline function could also take factor columns as an optional
# argument (i.e. you would omit factor columns for the future prices
# pipeline).
def make_pipeline(screen=None):
    open_price = USEquityPricing.open.latest
    
    columns = { "Price": open_price }
    
    if screen == None:
        final_screen = open_price.notnull()
    else:
        final_screen = (screen & open_price.notnull())
    
    return Pipeline(screen=final_screen, columns=columns)

# This function just gets the pipeline output for a single date in the form
# of a single DataFrame with stocks as the index and factors/prices as the
# columns.
def get_pipeline_results_for_date(pipeline, date):
    pipeline_output = run_pipeline(pipeline, start_date=date, end_date=date)
    panel = pipeline_output.to_panel().swapaxes(2, 0).swapaxes(0, 1)
    results = panel.iloc[0]
    return results

# This function takes every stock in the current results and tries to find
# the return given the future price. If the stock is missing in the future
# results, then it is assumed to have gone broke, so we have -100% as the
# resultant return.
def compute_returns(current_results, future_results):
    returns_list = []
    for stock in current_results.index:
        if stock in future_results.index:
            current_price = current_results.loc[stock]["Price"]
            future_price = future_results.loc[stock]["Price"]
            returns = (future_price / current_price) - 1.0
            returns_list.append(returns)
        else:
            returns_list.append(-1.0)
    
    return pd.Series(returns_list, index=current_results.index)

# The current pipeline uses your screen (and could pass in some factor
# columns).
current_pipeline = make_pipeline(Q1500US())

# Note that the future pipeline should have no screen so that we can include
# the cases where the stock is no longer in the set of stocks of the original
# screen (vs the stock actually no longer being listed).
future_pipeline = make_pipeline(None)

current_results = get_pipeline_results_for_date(current_pipeline, '2015-01-01')
future_results = get_pipeline_results_for_date(future_pipeline, '2016-01-01')

returns = compute_returns(current_results, future_results)
current_results["Returns"] = returns
In [7]:
current_results
Out[7]:
Price Returns
Equity(2 [ARNC]) 15.85 -0.377287
Equity(24 [AAPL]) 112.82 -0.051498
Equity(41 [ARCB]) 46.46 -0.549074
Equity(62 [ABT]) 45.70 -0.012910
Equity(67 [ADSK]) 60.83 0.021371
Equity(76 [TAP]) 75.26 0.251661
Equity(88 [ACI]) 1.73 -0.473988
Equity(110 [ACXM]) 20.63 0.036840
Equity(114 [ADBE]) 73.40 0.295232
Equity(122 [ADI]) 55.94 0.009117
Equity(128 [ADM]) 52.90 -0.307372
Equity(161 [AEP]) 61.82 -0.048528
Equity(166 [AES]) 14.03 -0.322880
Equity(168 [AET]) 90.21 0.201086
Equity(185 [AFL]) 62.06 -0.030777
Equity(197 [AGCO]) 45.90 -0.001089
Equity(205 [AGN]) 214.13 -1.000000
Equity(216 [HES]) 73.89 -0.353904
Equity(239 [AIG]) 56.73 0.098184
Equity(266 [AJG]) 47.95 -0.140772
Equity(270 [AKRX]) 36.55 0.016142
Equity(300 [ALK]) 60.26 0.344175
Equity(301 [ALKS]) 58.38 0.339157
Equity(328 [ALTR]) 37.57 -1.000000
Equity(337 [AMAT]) 25.31 -0.251679
Equity(351 [AMD]) 2.64 0.125000
Equity(353 [AME]) 53.54 0.006911
Equity(357 [TWX]) 86.20 -0.255800
Equity(368 [AMGN]) 161.67 0.002969
Equity(410 [AN]) 61.11 -0.016364
... ... ...
Equity(46907 [WPG]) 17.38 -0.387802
Equity(46929 [TRUE]) 23.37 -0.589645
Equity(46939 [CVEO]) 4.11 -0.630170
Equity(46949 [DNOW]) 26.12 -0.388974
Equity(46965 [TIME]) 24.72 -0.374191
Equity(46989 [PE]) 15.58 0.123877
Equity(47063 [ANET]) 60.70 0.288633
Equity(47123 [ABY]) 27.13 -0.288610
Equity(47126 [MRD]) 18.01 -0.119933
Equity(47128 [RYAM]) 22.38 -0.569705
Equity(47134 [SSE]) 5.61 -0.821747
Equity(47162 [TMST]) 36.93 -0.773084
Equity(47163 [INFO]) 26.70 0.135206
Equity(47168 [ECR]) 6.82 -0.759531
Equity(47169 [KITE]) 56.15 0.103295
Equity(47208 [GPRO]) 65.06 -0.719182
Equity(47230 [NSAM]) 22.45 -0.468597
Equity(47334 [TERP]) 30.49 -0.593637
Equity(47340 [PGN]) 2.84 -1.000000
Equity(47382 [LOCO]) 20.85 -0.399041
Equity(47415 [SYF]) 30.11 0.003321
Equity(47430 [MBLY]) 40.89 0.012472
Equity(47752 [CDK]) 40.22 0.214570
Equity(47777 [CFG]) 25.25 0.038416
Equity(47779 [CYBR]) 40.50 0.121728
Equity(47888 [FCAU]) 11.85 -0.222809
Equity(47921 [KEYS]) 33.87 -0.159138
Equity(48091 [VA]) 45.00 -0.186667
Equity(48104 [PGRE]) 18.75 -0.033067
Equity(48220 [LC]) 26.00 -0.583077

1500 rows × 2 columns

In [ ]: