Notebook

CAGR Custom Factor

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

from quantopian.pipeline.data.builtin import USEquityPricing
import quantopian.pipeline.data.morningstar as morningstar
In [40]:
class CAGR(CustomFactor):  
    # Define inputs and outputs
    inputs = [USEquityPricing.close]
    outputs = ['start', 'end', 'cagr']
    
    # Set default window_length
    # This can also be set/over-ridden whem the class is instantiated as shown below:
    # my_cagr = CAGR(window_length = 250)
    
    window_length = 750 # approx 3 years 
    
    def compute(self, today, assets, out, close):
        # The input 'close' is a Numpy array with close prices as axis 0 (total rows = window length)
        # and axis 1 are all the assets (ie stocks and securities).

        trade_days = len(close)
        years = trade_days / 250.0 # approximation to get years. decimal forces years to be real.
        
        start = close[0]
        end = close[-1]
        
        out.start[:] = start # start and end aren't required 
        out.end[:] = end     # these though can be used to manually check the output
        out.cagr[:] = ((end / start) ** (1/years)) - 1  # annualized CAGR formula 
        
In [41]:
def create_pipeline():
    """
    Function to create a pipeline with our CAGR custom factor and sample fundamental filter
    """
    
    # Instantiate a factor from the custom factor CAGR and set desired look-back window length
    factor = CAGR(window_length = 500)

    start = factor.start
    end = factor.end
    cagr = factor.cagr
    
    # Instantiate a sample fundamental factor and create a filter from it
    financial_health_grade = morningstar.asset_classification.financial_health_grade.latest
    grade_a = financial_health_grade.eq('A')
    
    # Create a pipeline and add the factors to it
    p = Pipeline()
    p.add(cagr, 'cagr')
    p.add(start, 'start')
    p.add(end, 'end')    
    p.add(financial_health_grade, 'financial_health_grade') 
    
    # Set a screen for our pipeline (in this case on Morningstar grade A equities)
    p.set_screen(grade_a)
    
    return p
In [42]:
# Run the pipeline with desired date(s) 
# The pipeline filters results by "financial_health_grade" equal to A (as an example fiter)
# Results (which is a Pandas dataset) is then sorted by cagr from high to low
# Note that Close can return NaaN which results in a NaaN cagr

results = run_pipeline(create_pipeline(), '10-13-2016', '10-13-2016')
results.sort('cagr', ascending = False)
Out[42]:
cagr end financial_health_grade start
2016-10-13 00:00:00+00:00 Equity(53 [ABMD]) 1.325890 127.400 A 23.550000
Equity(19725 [NVDA]) 0.972549 66.430 A 17.072945
Equity(38137 [GSV]) 0.947322 2.389 A 0.630000
Equity(47330 [GLOB]) 0.822087 43.990 A 13.250000
Equity(46370 [INGN]) 0.732197 59.050 A 19.680000
Equity(21669 [NTES]) 0.727852 261.410 A 87.560694
Equity(46779 [WB]) 0.702382 53.470 A 18.450000
Equity(13777 [AEIS]) 0.672411 47.800 A 17.090000
Equity(16841 [AMZN]) 0.650870 834.100 A 306.050000
Equity(34501 [MASI]) 0.647789 58.920 A 21.700000
Equity(41243 [ELLI]) 0.636474 100.400 A 37.490000
Equity(14972 [NBIX]) 0.618589 43.070 A 16.440000
Equity(969 [BMI]) 0.608443 32.930 A 12.728592
Equity(26781 [MKTX]) 0.581708 157.220 A 62.842718
Equity(2602 [EA]) 0.537209 82.540 A 34.930000
Equity(24434 [CEVA]) 0.527290 32.470 A 13.920000
Equity(21382 [EW]) 0.523095 118.160 A 50.935000
Equity(2295 [DW]) 0.504962 95.860 A 42.323970
Equity(13810 [CCF]) 0.479214 68.900 A 31.488880
Equity(27418 [SIMO]) 0.476268 51.030 A 23.415067
Equity(3796 [ICUI]) 0.471403 151.790 A 70.110000
Equity(6949 [AOS]) 0.460621 49.870 A 23.375685
Equity(22889 [EGHT]) 0.452415 14.640 A 6.940000
Equity(26811 [MPWR]) 0.447362 78.120 A 37.291347
Equity(47752 [CDK]) 0.436516 54.980 A 26.643026
Equity(24873 [STZ]) 0.424040 170.010 A 83.835865
Equity(1722 [CMN]) 0.423785 76.890 A 37.929835
Equity(17651 [FDP]) 0.419323 60.130 A 29.848927
Equity(46648 [TWOU]) 0.415999 35.710 A 17.810000
Equity(34953 [ULTA]) 0.411911 238.960 A 119.870000
... ... ... ... ...
Equity(49323 [AIMT]) NaN 16.420 A NaN
Equity(49334 [ELEC]) NaN NaN A NaN
Equity(49345 [ECAC_U]) NaN NaN A NaN
Equity(49385 [HCAC]) NaN 9.800 A NaN
Equity(49386 [WYIG]) NaN NaN A NaN
Equity(49413 [PEN]) NaN 71.790 A NaN
Equity(49447 [CSWI]) NaN 31.750 A NaN
Equity(49458 [MSG]) NaN 168.030 A NaN
Equity(49491 [ECAC]) NaN NaN A NaN
Equity(49495 [GRSH]) NaN 11.030 A NaN
Equity(49530 [PAAC]) NaN NaN A NaN
Equity(49550 [WVVI_P]) NaN NaN A NaN
Equity(49576 [AC]) NaN 34.400 A NaN
Equity(49649 [ANDA]) NaN NaN A NaN
Equity(49723 [PSA_PRB]) NaN 25.940 A NaN
Equity(49758 [OSB]) NaN 26.170 A NaN
Equity(49783 [JSYN_U]) NaN 10.170 A NaN
Equity(49817 [HCM]) NaN 11.600 A NaN
Equity(49843 [EVGBC]) NaN 100.030 A NaN
Equity(49907 [JSYN]) NaN NaN A NaN
Equity(49941 [PRE_PRG]) NaN 28.420 A NaN
Equity(49942 [PRE_PRI]) NaN 26.305 A NaN
Equity(49943 [PRE_PRH]) NaN 29.650 A NaN
Equity(49957 [PLSE]) NaN 6.280 A NaN
Equity(49977 [PSA_PRC]) NaN 25.540 A NaN
Equity(50112 [CFCO]) NaN NaN A NaN
Equity(50145 [PSA_PRD]) NaN 25.170 A NaN
Equity(50147 [SRTS]) NaN 6.350 A NaN
Equity(50271 [NBLX]) NaN 28.970 A NaN
Equity(50361 [ADSW]) NaN 20.000 A NaN

625 rows × 4 columns

In [ ]: