Notebook
In [20]:
# Import modules needed for pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline import Pipeline

# Import any datasets we need
from quantopian.pipeline.data import USEquityPricing

# Import any built in factors we want to use
from quantopian.pipeline.factors import CustomFactor, Returns, AverageDollarVolume

# Import any built in filters we need
from quantopian.pipeline.filters import QTradableStocksUS, Q500US

# Import nupy and pandas because they rock
import numpy as np
import pandas as pd
In [21]:
class Previous_Strong(CustomFactor):  
    inputs = [USEquityPricing.low] 
    window_length = 22  

    def compute(self, today, asset_id, out, low): 
        # Make a pandas dataframe so we can use the rolling methods
        low_df = pd.DataFrame(low)
        
        # Set a value of True whenever the first value of the window minus the mean is less than a constant
        MIN_DELTA = .15
        first_lower_than_mean = low_df.rolling(3).apply(lambda group: (group[0]-group.mean() < MIN_DELTA))
        
        # Sum the number of times the above condition is true
        score = first_lower_than_mean.sum()
        out[:] = score 
In [22]:
def make_pipeline():
    
    base_universe = Q500US()
    
    # Define all the conditions for scoring
    return_gt_1 = Returns(window_length=5) > .1
    rerturns_top_50 = Returns(window_length=5).top(50, mask=base_universe)
    price_gt_5 = EquityPricing.close.latest > 5
    dollar_volume_gt_mean = AverageDollarVolume(window_length=10).demean(mask=(base_universe)) > 0
    
    # Place the conditions into the pipeline output
    return Pipeline(
        columns={
            'Weeklyreturn_gt_1': return_gt_1,
            'rerturns_top_50': rerturns_top_50,
            'price_gt_5': price_gt_5,
            'dollar_volume_gt_mean': dollar_volume_gt_mean,
            'previous_strong': Previous_Strong(mask=base_universe)
        },
        screen=base_universe
    )
In [23]:
# Run pipeline to get the data into a dataframe
pipe_output = run_pipeline(make_pipeline(), '2013-01-01', '2014-05-01')

# Add a column for 'score' to the dataframe
# It will be the sum of the true conditions. Use 'sum' since True=1
pipe_output['score'] = pipe_output.sum(axis=1)

pipe_output.head(10)

Pipeline Execution Time: 34.52 Seconds
Out[23]:
Weeklyreturn_gt_1 dollar_volume_gt_mean previous_strong price_gt_5 rerturns_top_50 score
2013-01-02 00:00:00+00:00 Equity(2 [HWM]) False False 20.0 True False 21.0
Equity(24 [AAPL]) False True 7.0 True True 10.0
Equity(62 [ABT]) False True 17.0 True False 19.0
Equity(67 [ADSK]) False False 14.0 True False 15.0
Equity(88 [ACI]) False False 17.0 True False 18.0
Equity(114 [ADBE]) False True 17.0 True False 19.0
Equity(122 [ADI]) False False 16.0 True False 17.0
Equity(128 [ADM]) False False 16.0 True False 17.0
Equity(161 [AEP]) False False 15.0 True False 16.0
Equity(166 [AES]) False False 19.0 True False 20.0
In [24]:
# One can plot various things.
# Lets check the distribution of scores using the `hist` method
pipe_output.score.hist()
Out[24]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f593414ab70>
In [25]:
# Can also plot how the score of an asset changed over time
# The `xs` method will slice a single date or asset
pipe_output.xs(symbols('AAPL'), level=1).score.plot()
Out[25]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f59292368d0>
In [ ]:
 
In [ ]: