Notebook
In [51]:
# Pipeline imports
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.pipeline.filters import Q500US, Q1500US, QTradableStocksUS, StaticAssets

# Pipeline imports needed from research
from quantopian.research import run_pipeline

# Dataset imports
from quantopian.pipeline.data import USEquityPricing
from quantopian.pipeline.data import Fundamentals

#
import numpy as np
In [52]:
class Quantile_By_Asset(CustomFactor):
    """
    Returns the value of the input given at the specified quantile.
    This is per asset so each asset will have it's own value
    """    
    # No default input or window length is specified
    # The following creates a named input parameter
    # Notice the comma -that's required
    params = ('quantile',)

    def compute(self, today, assets, out, values, quantile):
        # Note that this fails if an all nan slice is encountered
        out[:] = np.nanpercentile(values, quantile, axis=0)
In [53]:
class Quantile_By_Date(CustomFactor):
    """
    Returns the value of the input given at the specified quantile.
    This is across all assets for most previous date
    This single value will be broadcast to all assets
    """    
    # No default input is specified
    window_length = 1

    # The following creates a named input parameter
    # Notice the comma -that's required
    params = ('quantile',)

    def compute(self, today, assets, out, values, quantile): 
        # Note that this fails if an all nan slice is encountered
        out[:] = np.nanpercentile(values[-1], quantile)
In [54]:
def make_pipeline():
    # Set the base set of securities to work with
    base_universe = Q500US()
    
    # Get PE and PB from Morningstar fields 
    q_50 = Quantile_By_Asset(inputs=[USEquityPricing.close], 
                             window_length=5, 
                             quantile=50, 
                             mask=base_universe)
    q_50_all = Quantile_By_Date(inputs=[USEquityPricing.close], 
                                quantile=50,
                                mask=base_universe)

    price = USEquityPricing.close.latest
    
    return Pipeline(
        columns={
            'q_50':q_50,
            'q_50_all':q_50_all,
            'price':price,
        },
        screen=base_universe
    )
In [55]:
pipeline_output = run_pipeline(
    make_pipeline(),
    start_date='2018-01-01',
    end_date='2019-7-31'
) 

Pipeline Execution Time: 16.62 Seconds
In [56]:
pipeline_output
Out[56]:
price q_50 q_50_all
2018-01-02 00:00:00+00:00 Equity(2 [ARNC]) 27.260 27.260000 72.1850
Equity(24 [AAPL]) 169.300 170.590000 72.1850
Equity(62 [ABT]) 57.130 57.130000 72.1850
Equity(67 [ADSK]) 104.830 104.570000 72.1850
Equity(76 [TAP]) 82.070 81.810000 72.1850
Equity(114 [ADBE]) 175.300 175.300000 72.1850
Equity(122 [ADI]) 89.070 89.070000 72.1850
Equity(128 [ADM]) 40.090 40.230000 72.1850
Equity(161 [AEP]) 73.580 73.580000 72.1850
Equity(168 [AET]) 180.650 180.650000 72.1850
Equity(185 [AFL]) 87.830 87.699000 72.1850
Equity(216 [HES]) 47.470 48.140000 72.1850
Equity(239 [AIG]) 59.590 59.530000 72.1850
Equity(270 [AKRX]) 32.229 31.960000 72.1850
Equity(300 [ALK]) 73.510 74.360000 72.1850
Equity(337 [AMAT]) 51.130 51.680000 72.1850
Equity(351 [AMD]) 10.290 10.525000 72.1850
Equity(357 [TWX]) 91.590 91.610000 72.1850
Equity(368 [AMGN]) 173.930 176.080000 72.1850
Equity(438 [AON]) 134.020 133.910000 72.1850
Equity(448 [APA]) 42.240 42.890000 72.1850
Equity(455 [APC]) 53.650 53.750000 72.1850
Equity(460 [APD]) 164.130 162.725425 72.1850
Equity(630 [ADP]) 117.200 117.260000 72.1850
Equity(679 [AXP]) 99.330 99.140000 72.1850
Equity(693 [AZO]) 711.370 711.370000 72.1850
Equity(698 [BA]) 295.020 295.360000 72.1850
Equity(700 [BAC]) 29.535 29.775000 72.1850
Equity(734 [BAX]) 64.650 64.769000 72.1850
Equity(739 [BBBY]) 21.960 22.670000 72.1850
... ... ... ... ...
2019-07-31 00:00:00+00:00 Equity(49610 [SQ]) 80.480 80.420000 84.7125
Equity(49655 [TEAM]) 137.000 137.000000 84.7125
Equity(50049 [FTV]) 76.800 78.460000 84.7125
Equity(50077 [TWLO]) 139.995 147.590000 84.7125
Equity(50288 [TTD]) 264.940 260.187000 84.7125
Equity(50338 [NTNX]) 23.596 23.750000 84.7125
Equity(50350 [COUP]) 138.030 141.580000 84.7125
Equity(50392 [YUMC]) 43.930 44.830000 84.7125
Equity(50428 [AA]) 23.035 23.130000 84.7125
Equity(50430 [LW]) 67.540 64.790000 84.7125
Equity(50683 [SNAP]) 16.920 17.608000 84.7125
Equity(50716 [DXC]) 56.710 56.500000 84.7125
Equity(50758 [OKTA]) 131.980 136.630000 84.7125
Equity(50860 [VST]) 21.870 21.780000 84.7125
Equity(50970 [ATUS]) 26.150 26.150000 84.7125
Equity(51012 [BHGE]) 24.520 24.630000 84.7125
Equity(51157 [DD]) 73.420 72.919384 84.7125
Equity(51231 [ROKU]) 105.770 106.910000 84.7125
Equity(51314 [MDB]) 142.040 155.700000 84.7125
Equity(51832 [ZS]) 84.660 86.420000 84.7125
Equity(51854 [DBX]) 23.800 24.550000 84.7125
Equity(51895 [SPOT]) 155.330 153.880000 84.7125
Equity(51937 [DOCU]) 54.170 54.170000 84.7125
Equity(52211 [TLRY]) 40.790 41.880000 84.7125
Equity(52424 [ELAN]) 33.370 33.160000 84.7125
Equity(52592 [LIN]) 196.490 199.890000 84.7125
Equity(52991 [DOW]) 48.410 49.100000 84.7125
Equity(53023 [LYFT]) 62.500 65.020000 84.7125
Equity(53116 [BYND]) 194.656 221.815000 84.7125
Equity(53158 [UBER]) 42.610 43.770000 84.7125

198128 rows × 3 columns

In [ ]: