Notebook

MaxHigh Custom Factor

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

from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import Latest

import numpy as np
In [2]:
class MaxHigh(CustomFactor):  
    # Define inputs
    inputs = [USEquityPricing.high]
    
    # 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_high_10 = MaxHigh(window_length = 10)
    
    window_length = 2 
    
    def compute(self, today, assets, out, high):
        # The input 'high' is a Numpy array with high prices as axis 0 (total rows = window length)
        # and axis 1 are all the assets (ie stocks and securities).
        # Here we use the built in Numpy method to return the high in each row. 
        # The nanmax method is used because it excludes nans unlike the standard max
        out[:] = np.nanmax(high, axis=0)
        
In [15]:
def create_pipeline():
    # Create the high factors.
    high_250 = MaxHigh(window_length = 250)
    high_5 = MaxHigh(window_length = 5)

    # Create volume and close factors
    # This is easier because they are just versions of the built in 'Latest' factor.    
    volume = Latest(inputs=[USEquityPricing.volume])  
    close = Latest(inputs=[USEquityPricing.close]) 
    
    p = Pipeline()
    p.add(high_250, 'high_250')
    p.add(high_5, 'high_5')
    p.add(volume, 'volume')
    p.add(close, 'close')
    
    # Below is what we would really like to do but can't currently compare factors
    # pipe_screen = ((close > 1.0) & (close < 20.0) & (high_5 > high_250))
    # so this step is done after the results of the pipe are retrieved
    # lets just screen for price and volume now

    p.set_screen((close > 1.0) & (close < 20.0))

    return p

# Run the pipeline with desired date(s). close 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[15]:
close high_250 high_5 volume
2016-07-08 00:00:00+00:00 Equity(2 [AA]) 9.330 11.466667 9.590 21467142
Equity(21 [AAME]) 4.150 4.979592 4.150 391
Equity(39 [DDC]) 8.670 12.839715 9.510 447146
Equity(41 [ARCB]) 16.050 34.484492 17.010 82274
Equity(69 [ACAT]) 16.820 34.067656 18.010 131175
Equity(70 [VBF]) 19.160 19.190000 19.190 29600
Equity(106 [ACU]) 18.920 19.660000 19.660 1200
Equity(117 [AEY]) 1.800 2.400000 1.800 100
Equity(149 [ADX]) 12.700 13.398455 12.740 163563
Equity(157 [AEG]) 3.520 7.604279 4.040 2096722
Equity(166 [AES]) 12.090 12.872477 12.530 3765824
Equity(192 [ATAX]) 5.550 5.555000 5.555 99880
Equity(312 [ALOT]) 15.530 15.789572 15.680 5810
Equity(351 [AMD]) 5.010 5.520000 5.190 15453139
Equity(366 [AVD]) 14.870 17.396485 15.620 117263
Equity(371 [HWAY]) 11.700 14.220000 11.800 131212
Equity(392 [AMS]) 1.860 2.860000 2.100 1491
Equity(393 [AMSC]) 8.510 12.500000 8.740 113146
Equity(397 [AMSW_A]) 10.450 11.110199 10.624 29822
Equity(447 [AP]) 11.280 19.220000 12.170 73393
Equity(450 [CLFD]) 17.550 20.280000 18.160 36278
Equity(451 [APB]) 9.661 11.530552 9.800 15230
Equity(542 [ASA]) 15.670 16.239000 16.239 451032
Equity(547 [ASB]) 16.890 20.532655 17.180 734496
Equity(624 [ATW]) 11.740 24.868480 13.788 5072491
Equity(629 [AU]) 19.990 20.550000 20.550 5476837
Equity(660 [AVP]) 3.750 6.500490 3.990 3305017
Equity(662 [AVHI]) 12.100 15.240000 12.470 23563
Equity(677 [AXAS]) 1.080 2.720000 1.160 930052
Equity(680 [AXR]) 4.850 5.380000 4.940 1600
... ... ... ... ...
Equity(49920 [GWRS]) 8.890 9.090000 9.090 7625
Equity(49933 [MILN]) 15.366 15.366000 15.366 100
Equity(49934 [NTLA]) 19.010 30.400000 22.899 209493
Equity(49938 [SBPH]) 8.910 11.500000 9.460 770
Equity(49954 [TPB]) 9.970 12.100000 10.357 6933
Equity(49955 [JHD]) 10.180 10.500000 10.500 18288
Equity(49957 [PLSE]) 4.400 5.000000 5.000 11726
Equity(49973 [PZRX]) 4.263 5.768000 4.400 2100
Equity(49974 [MRUS]) 8.500 10.735000 8.530 5417
Equity(49982 [MIII_U]) 9.700 10.000000 10.000 2751900
Equity(49983 [CFCO_U]) 9.900 10.100000 9.959 145757
Equity(49985 [SUPV]) 12.700 13.900000 13.900 54032
Equity(49993 [CLSD]) 6.740 8.450000 7.200 7220
Equity(49995 [RETA]) 18.000 26.900000 20.550 114746
Equity(49996 [LCAH_U]) 10.010 10.290000 10.050 10899
Equity(50001 [EHT]) 10.000 10.200000 10.060 43062
Equity(50005 [ZDGE]) 4.490 7.850000 5.000 93942
Equity(50007 [BBU]) 18.660 31.020000 19.310 34353
Equity(50013 [MBRX]) 6.760 9.580000 7.200 11172
Equity(50015 [NH]) 13.920 19.600000 14.350 71455
Equity(50035 [ONS]) 3.381 4.480000 3.700 9741
Equity(50039 [COE]) 19.700 24.120000 20.910 11950
Equity(50040 [ATKR]) 16.030 16.940000 16.940 571150
Equity(50041 [VIVE]) 4.520 5.140000 4.710 23942
Equity(50068 [SELB]) 14.000 16.320000 14.320 79978
Equity(50091 [SYRS]) 17.280 21.500000 21.500 58207
Equity(50096 [HONE]) 12.690 13.040000 13.040 309446
Equity(50099 [GMRE]) 10.110 10.290000 10.290 208764
Equity(50103 [RNDB]) 12.400 12.700000 12.700 68785
Equity(50105 [LONE]) 12.300 16.000000 16.000 10875

3392 rows × 4 columns

In [10]:
# Now just select the rows where the 5 day high is greater than or equal to the 250 day high
# Notice that all the high_250 are equal to the high_5 which makes sense since we include the 
# most recent 5 days in our 250 days. This works, but if one would like to show "previous high"
# then the MaxHigh factor would need to be modified a bit. At that point it may be advantageous
# to make a single "ReachedNewHighestHigh" custom factor?
# We then select the top 50 securities with the highest volume that have a new high

highest_highs = results[results.high_5 >= results.high_250]
highest_highs.sort('volume', ascending=False).iloc[:50]
Out[10]:
close high_250 high_5 volume
2016-07-08 00:00:00+00:00 Equity(32133 [GDX]) 29.540 30.680000 30.680000 57656193
Equity(42277 [ZNGA]) 2.760 2.880000 2.880000 34581540
Equity(43572 [WWAV]) 56.230 56.820000 56.820000 34128722
Equity(3149 [GE]) 31.820 32.100000 32.100000 33110375
Equity(64 [ABX]) 22.369 23.470000 23.470000 22473012
Equity(19660 [XLU]) 52.025 53.020000 53.020000 21706447
Equity(6653 [T]) 42.290 43.404021 43.404021 21141743
Equity(28368 [SLV]) 18.720 19.240000 19.240000 17814157
Equity(5923 [PFE]) 35.760 35.930000 35.930000 15513475
Equity(1637 [CMCS_A]) 66.250 66.420000 66.420000 13518213
Equity(19659 [XLP]) 55.140 55.550000 55.550000 13235352
Equity(1595 [CLF]) 5.940 6.240000 6.240000 12984889
Equity(40574 [LEDS]) 10.000 11.350000 11.350000 12831467
Equity(33655 [HYG]) 84.470 84.700000 84.700000 12347999
Equity(26807 [GLD]) 129.780 131.150000 131.150000 12099803
Equity(38926 [GDXJ]) 47.130 48.950000 48.950000 11316778
Equity(21839 [VZ]) 55.370 56.380399 56.380399 11007675
Equity(25714 [AUY]) 5.655 5.910000 5.910000 10205903
Equity(8347 [XOM]) 92.960 94.490000 94.490000 9843168
Equity(3585 [HL]) 5.650 5.770000 5.770000 9124905
Equity(19725 [NVDA]) 48.880 49.060000 49.060000 8721453
Equity(43414 [SRC]) 12.945 13.125000 13.125000 8711505
Equity(22226 [GG]) 19.620 20.380000 20.380000 8455295
Equity(5885 [PEP]) 107.510 109.000000 109.000000 8225558
Equity(27437 [SLW]) 24.880 25.620000 25.620000 8215277
Equity(27794 [BGI]) 3.250 5.150000 5.150000 8013717
Equity(5261 [NEM]) 40.960 41.460000 41.460000 7749141
Equity(5029 [MRK]) 58.770 59.270000 59.270000 7575351
Equity(24064 [CNP]) 23.520 24.200000 24.200000 7372976
Equity(17702 [NLY]) 10.800 11.053902 11.053902 7247676
Equity(21652 [IYR]) 81.890 82.980000 82.980000 6751561
Equity(24491 [IAG]) 4.580 4.820000 4.820000 6746888
Equity(23921 [TLT]) 142.520 143.190000 143.190000 6459727
Equity(1131 [BSX]) 23.660 23.800000 23.800000 6432209
Equity(23112 [CVX]) 103.070 105.000000 105.000000 6363547
Equity(41872 [VER]) 10.230 10.370000 10.370000 6035891
Equity(8229 [WMT]) 73.400 73.980000 73.980000 6002339
Equity(31886 [TCK]) 13.220 14.450000 14.450000 5795627
Equity(9883 [ATVI]) 40.370 40.770000 40.770000 5683900
Equity(5938 [PG]) 84.790 85.950000 85.950000 5682578
Equity(7845 [MUX]) 4.360 4.740000 4.740000 5660470
Equity(629 [AU]) 19.990 20.550000 20.550000 5476837
Equity(9936 [GFI]) 5.350 5.560000 5.560000 5244361
Equity(50049 [FTV]) 47.740 54.340000 54.340000 5189025
Equity(26981 [IAU]) 13.110 13.250000 13.250000 5156157
Equity(27323 [NGD]) 4.710 5.000000 5.000000 5066072
Equity(44884 [BTG]) 2.805 2.950000 2.950000 4943559
Equity(1374 [CDE]) 11.790 12.250000 12.250000 4891305
Equity(17773 [TSM]) 26.310 26.650000 26.650000 4870844
Equity(4151 [JNJ]) 122.500 123.230000 123.230000 4867938
In [ ]: