Notebook

CloseOnN Custom Factor

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

from quantopian.pipeline.data.builtin import USEquityPricing

class CloseOnN(CustomFactor):  
    # Define inputs
    inputs = [USEquityPricing.close]
    
    # Set window_length to whatever number of days to lookback as a default
    # in the case where no window_length is given when instantiated.
    # This can also be set/over-ridden as shown below:
    # my_close_on_10 = CloseOnN(window_length = 10)
    
    window_length = 2 
    
    def compute(self, today, assets, out, close):  
        out[:] = close[0]
        
In [6]:
def create_pipeline():
    # Create a Pipeline computing the previous close factor.
    close_5 = CloseOnN(window_length = 5)
    
    p = Pipeline()
    p.add(close_5, 'close_5')
    
    return p

# Run the pipeline with desired date(s). close_5 collumn will be close 5 trading days before that date 
# In this case it will be the close on 6-30-2016 because of the July 4 holiday.
results = run_pipeline(create_pipeline(), '7-8-2016', '7-8-2016')
results
Out[6]:
close_5
2016-07-08 00:00:00+00:00 Equity(2 [AA]) 9.280000
Equity(21 [AAME]) 3.960000
Equity(24 [AAPL]) 95.600000
Equity(25 [AA_PR]) 86.900000
Equity(31 [ABAX]) 47.260000
Equity(39 [DDC]) 8.840000
Equity(41 [ARCB]) 16.250000
Equity(52 [ABM]) 36.324228
Equity(53 [ABMD]) 109.260000
Equity(62 [ABT]) 39.300000
Equity(64 [ABX]) 21.335000
Equity(66 [AB]) 23.300000
Equity(67 [ADSK]) 54.140000
Equity(69 [ACAT]) 17.000000
Equity(70 [VBF]) 18.900000
Equity(76 [TAP]) 101.120000
Equity(84 [ACET]) 21.890000
Equity(100 [IEP]) 53.990000
Equity(106 [ACU]) 18.620000
Equity(110 [ACXM]) 22.020000
Equity(112 [ACY]) 9.200000
Equity(114 [ADBE]) 95.800000
Equity(117 [AEY]) 1.800000
Equity(122 [ADI]) 56.660000
Equity(128 [ADM]) 42.880000
Equity(149 [ADX]) 12.670000
Equity(153 [AE]) 38.600000
Equity(154 [AEM]) 53.490000
Equity(157 [AEG]) 4.010000
Equity(161 [AEP]) 70.090000
... ...
Equity(50061 [WFC_PRX]) 25.840000
Equity(50062 [MHLA]) 25.500000
Equity(50066 [SSW_PRG]) 25.140000
Equity(50068 [SELB]) 13.990000
Equity(50070 [HTZ]) 43.170000
Equity(50074 [WALA]) 26.060000
Equity(50077 [TWLO]) 36.660000
Equity(50078 [TALL]) NaN
Equity(50079 [OILD]) NaN
Equity(50080 [OILU]) NaN
Equity(50082 [KKR_PRB]) 25.680000
Equity(50086 [DYNC]) 107.720000
Equity(50087 [XTH]) NaN
Equity(50088 [XWEB]) 52.092000
Equity(50089 [BOKF_L]) NaN
Equity(50091 [SYRS]) 18.000000
Equity(50092 [MSDI_W]) NaN
Equity(50093 [ESGD]) 52.900000
Equity(50094 [ESGE]) NaN
Equity(50095 [MSDI]) NaN
Equity(50096 [HONE]) 12.950000
Equity(50097 [DESC]) NaN
Equity(50099 [GMRE]) 10.110000
Equity(50100 [INN_PRD]) NaN
Equity(50101 [AMH_PRE]) NaN
Equity(50103 [RNDB]) NaN
Equity(50104 [OEW]) NaN
Equity(50105 [LONE]) NaN
Equity(50107 [GDV_PRG]) NaN
Equity(50109 [SCAP]) NaN

8362 rows × 1 columns

In [11]:
# Select columns from the pipe output to do calculations.
# In this case select close_5 and calculate the mean (as an example)

mean_close_5 = results['close_5'].mean() # average of closes
max_close_5 = results['close_5'].max() # max close
std_close_5 = results['close_5'].std() # standard deviation of the closes

print mean_close_5, max_close_5, std_close_5

# Select row(s) (securities) which have max close price. Note either dot notation or bracket/quote can be used
# Berkshire Hathaway wins the prize for max close at $217,000 / share
print results[results.close_5 == max_close_5] 
60.1468103005 217000.0 2437.32524009
                                                close_5
2016-07-08 00:00:00+00:00 Equity(1091 [BRK_A])   217000
In [ ]: