Notebook
In [1]:
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.research import run_pipeline

from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import Q500US
In [2]:
class Close_Open_Delta(CustomFactor):  
    # Define inputs
    inputs = [USEquityPricing.open, USEquityPricing.high]
    window_length = 1 
    
    def compute(self, today, assets, out, open, close):
        out[:] = (close - open)/open
        
In [3]:
def my_pipeline():
    """
    Function to create a pipeline with high, low, open,and the High_Open_Delta custom factors
    """
    
    # Factors for the latest pricing data can be easily created using the "latest" method 
    # from the desired dataset
    
    open = USEquityPricing.open.latest
    high = USEquityPricing.high.latest
    low = USEquityPricing.low.latest
    close = USEquityPricing.close.latest 
    
    # Custom factors need to be instantiated explicitly
    close_minus_open = Close_Open_Delta()
    
    # Create a pipeline and add the factors to it
    p = Pipeline()
    p.add(open, 'open')
    p.add(high, 'high')
    p.add(low, 'low') 
    p.add(close, 'close')  
    p.add(close_minus_open, 'close_minus_open') 
    
    # Create filters to select and sort securities based upon factors
    # Always good to use an initial "universe" filter to start with a realistic set of tradable securities
    my_universe = Q500US()    # built in universe of 500 larger stocks

    # Create a "top gainers" filter. Use a mask to ensure only qualified securities get counted.
    # Set the number to however many "off the top" one wishes to purchase (in this case 20)
    top_gainers = close_minus_open.top(5, mask = (my_universe ))
    
    # Set a screen for our pipeline. Don't really need the my_universe and other filters because they were
    # included in the top_gainers mask
    p.set_screen(top_gainers)
    
    return p
In [4]:
# Run the pipeline to see what the results are

results = run_pipeline(my_pipeline(), '2018-03-01', '2018-03-01')
results.sort('close_minus_open', ascending = False)
/usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:4: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)
  after removing the cwd from sys.path.
Out[4]:
close close_minus_open high low open
2018-03-01 00:00:00+00:00 Equity(50499 [EVHC]) 38.50 0.059642 39.62 37.39 37.39
Equity(4521 [LOW]) 89.59 0.055149 92.22 86.75 87.40
Equity(50683 [SNAP]) 17.32 0.052475 17.65 16.70 16.77
Equity(47063 [ANET]) 269.74 0.052231 273.58 260.00 260.00
Equity(10984 [MAC]) 58.94 0.049862 60.85 57.78 57.96
In [7]:
results['close_minus_open']
Out[7]:
2018-03-01 00:00:00+00:00  Equity(4521 [LOW])      0.055149
                           Equity(10984 [MAC])     0.049862
                           Equity(47063 [ANET])    0.052231
                           Equity(50499 [EVHC])    0.059642
                           Equity(50683 [SNAP])    0.052475
Name: close_minus_open, dtype: float64
In [8]:
results['close_minus_open'][1]
Out[8]:
0.049861973775017263
In [10]:
results['close_minus_open'].index.tolist()
Out[10]:
[(Timestamp('2018-03-01 00:00:00+0000', tz='UTC'), Equity(4521 [LOW])),
 (Timestamp('2018-03-01 00:00:00+0000', tz='UTC'), Equity(10984 [MAC])),
 (Timestamp('2018-03-01 00:00:00+0000', tz='UTC'), Equity(47063 [ANET])),
 (Timestamp('2018-03-01 00:00:00+0000', tz='UTC'), Equity(50499 [EVHC])),
 (Timestamp('2018-03-01 00:00:00+0000', tz='UTC'), Equity(50683 [SNAP]))]