Notebook
In [1]:
import numpy as np
import statsmodels.api as sm
import math
import pandas as pd
from quantopian.research import run_pipeline, returns, get_pricing
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor, Returns, PercentChange, SimpleMovingAverage, BusinessDaysSincePreviousEvent, BusinessDaysUntilNextEvent, VWAP
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.data import factset
from quantopian.pipeline import factors, filters, classifiers
import alphalens
from scipy.stats.mstats import winsorize
from sklearn import preprocessing
from scipy.stats.mstats import gmean
from zipline.utils.numpy_utils import (
    repeat_first_axis,
    repeat_last_axis,
)

from quantopian.pipeline.data.factset.ownership import Form3AggregatedTrades
from quantopian.pipeline.data.factset.ownership import Form4and5AggregatedTrades
In [2]:
sd = '2014-01-04'
ed = '2017-05-01'
                
insider_txns_form3_1d = Form3AggregatedTrades.slice(False, 1)
insider_txns_form4and5_1d = Form4and5AggregatedTrades.slice(False, 1)

insider_txns_form3_90d = Form3AggregatedTrades.slice(False, 90)
insider_txns_form4and5_90d = Form4and5AggregatedTrades.slice(False, 90)


#Get unique buyers/sellers    
filers_form3_1d = insider_txns_form3_1d.num_unique_filers
buyers_form4and5_1d = insider_txns_form4and5_1d.num_unique_buyers
sellers_form4and5_1d = insider_txns_form4and5_1d.num_unique_sellers

filers_form3_90d = insider_txns_form3_90d.num_unique_filers
buyers_form4and5_90d = insider_txns_form4and5_90d.num_unique_buyers
sellers_form4and5_90d = insider_txns_form4and5_90d.num_unique_sellers


class TxnsCount(CustomFactor):
    window_length = 3
    window_safe = True
    def compute(self, today, assets, out, b):  
        b=np.nan_to_num(b)
        m = b[-1]
        
        idx_zero = np.where(m == 0)[0]
        m[idx_zero] = b[-2][idx_zero]

        idx_zero = np.where(m == 0)[0]
        m[idx_zero] = b[-3][idx_zero]

        out[:] = m

                
base_universe = QTradableStocksUS()

f3_prev = BusinessDaysSincePreviousEvent(inputs=[insider_txns_form3_1d.asof_date])  
f5_prev = BusinessDaysSincePreviousEvent(inputs=[insider_txns_form4and5_1d.asof_date])  


class Asof(CustomFactor):
    window_length = 1
    window_safe = True
    def compute(self, today, assets, out, b):  
        out[:] = b[-1]

        
class TxnsCount(CustomFactor):
    window_length = 3
    window_safe = True
    def compute(self, today, assets, out, b):  
        b=np.nan_to_num(b)
        m = b[-1]
        
        idx_zero = np.where(m == 0)[0]
        m[idx_zero] = b[-2][idx_zero]

        idx_zero = np.where(m == 0)[0]
        m[idx_zero] = b[-3][idx_zero]

        out[:] = m
        

pipe = Pipeline(
    columns={
        
        'f3_prev_days': f3_prev,
        'f5_prev_days': f5_prev,
        
        'f3_asof': Asof(inputs=[insider_txns_form3_1d.asof_date]),
        'f5_asof': Asof(inputs=[insider_txns_form4and5_1d.asof_date]),
        
        'f3_buyers': TxnsCount(inputs=[filers_form3_1d]),
        'f5_buyers': TxnsCount(inputs=[buyers_form4and5_1d]),
        'f5_sellers': TxnsCount(inputs=[sellers_form4and5_1d]),
    },
    
    screen = base_universe & (filers_form3_90d.latest.notnull() | buyers_form4and5_90d.latest.notnull() | sellers_form4and5_90d.latest.notnull()),
)


output = run_pipeline(pipe, sd, ed) 

Pipeline Execution Time: 21.78 Seconds
In [4]:
output['f5_asof'] = pd.to_datetime(output['f5_asof'])
output['f3_asof'] = pd.to_datetime(output['f3_asof'])

print output.head(10)
                                               f3_asof  f3_buyers  \
2014-01-06 00:00:00+00:00 Equity(2 [ARNC])  2013-10-22        0.0   
                          Equity(24 [AAPL]) 2012-08-25        0.0   
                          Equity(31 [ABAX]) 2009-09-02        0.0   
                          Equity(41 [ARCB]) 2012-01-02        0.0   
                          Equity(52 [ABM])  2012-01-16        0.0   
                          Equity(53 [ABMD]) 2009-03-06        0.0   
                          Equity(62 [ABT])  2013-10-15        0.0   
                          Equity(67 [ADSK]) 2011-09-23        0.0   
                          Equity(69 [ACAT]) 2012-03-20        0.0   
                          Equity(76 [TAP])  2011-10-02        0.0   

                                             f3_prev_days    f5_asof  \
2014-01-06 00:00:00+00:00 Equity(2 [ARNC])           54.0 2014-01-01   
                          Equity(24 [AAPL])         355.0 2014-01-01   
                          Equity(31 [ABAX])        1133.0 2013-12-10   
                          Equity(41 [ARCB])         525.0 2013-12-07   
                          Equity(52 [ABM])          515.0 2013-12-27   
                          Equity(53 [ABMD])        1261.0 2013-11-22   
                          Equity(62 [ABT])           59.0 2013-12-21   
                          Equity(67 [ADSK])         596.0 2014-01-04   
                          Equity(69 [ACAT])         469.0 2013-09-14   
                          Equity(76 [TAP])          590.0 2014-01-01   

                                             f5_buyers  f5_prev_days  \
2014-01-06 00:00:00+00:00 Equity(2 [ARNC])         0.0           3.0   
                          Equity(24 [AAPL])        0.0           3.0   
                          Equity(31 [ABAX])        0.0          19.0   
                          Equity(41 [ARCB])        0.0          20.0   
                          Equity(52 [ABM])         0.0           6.0   
                          Equity(53 [ABMD])        0.0          31.0   
                          Equity(62 [ABT])         0.0          10.0   
                          Equity(67 [ADSK])        1.0           0.0   
                          Equity(69 [ACAT])        0.0          80.0   
                          Equity(76 [TAP])         0.0           3.0   

                                             f5_sellers  
2014-01-06 00:00:00+00:00 Equity(2 [ARNC])          0.0  
                          Equity(24 [AAPL])         1.0  
                          Equity(31 [ABAX])         0.0  
                          Equity(41 [ARCB])         0.0  
                          Equity(52 [ABM])          0.0  
                          Equity(53 [ABMD])         0.0  
                          Equity(62 [ABT])          0.0  
                          Equity(67 [ADSK])         2.0  
                          Equity(69 [ACAT])         0.0  
                          Equity(76 [TAP])          0.0  
In [ ]: