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
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
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
# 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.
|2018-03-01 00:00:00+00:00||Equity(50499 [EVHC])||38.50||0.059642||39.62||37.39||37.39|
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
[(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]))]