| 1 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | Technical Analysis Factors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | -------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from bottleneck import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     nanargmax, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     nanmax, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     nanmean, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     nansum, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from numpy import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     abs, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     clip, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     diff, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     fmax, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     inf, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     isnan, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     NINF, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from numexpr import evaluate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from zipline.pipeline.data import USEquityPricing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | from zipline.pipeline.term import SingleInputMixin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | from zipline.utils.control_flow import ignore_nanwarnings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | from .factor import CustomFactor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | class Returns(CustomFactor): | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     Calculates the percent change in close price over the given window_length. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     **Default Inputs**: [USEquityPricing.close] | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     inputs = [USEquityPricing.close] | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     def compute(self, today, assets, out, close): | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         out[:] = (close[-1] - close[0]) / close[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | class RSI(CustomFactor, SingleInputMixin): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     Relative Strength Index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     **Default Inputs**: [USEquityPricing.close] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     **Default Window Length**: 15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     window_length = 15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     inputs = (USEquityPricing.close,) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     def compute(self, today, assets, out, closes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         diffs = diff(closes, axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ups = nanmean(clip(diffs, 0, inf), axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         downs = abs(nanmean(clip(diffs, -inf, 0), axis=0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return evaluate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             "100 - (100 / (1 + (ups / downs)))", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             local_dict={'ups': ups, 'downs': downs}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             global_dict={}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             out=out, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | class SimpleMovingAverage(CustomFactor, SingleInputMixin): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     Average Value of an arbitrary column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     **Default Inputs**: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     **Default Window Length**: None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     # numpy's nan functions throw warnings when passed an array containing only | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     # nans, but they still returns the desired value (nan), so we ignore the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     # warning. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     ctx = ignore_nanwarnings() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     def compute(self, today, assets, out, data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         out[:] = nanmean(data, axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | class WeightedAverageValue(CustomFactor): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     Helper for VWAP-like computations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     **Default Inputs:** None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     **Default Window Length:** None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     def compute(self, today, assets, out, base, weight): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         out[:] = nansum(base * weight, axis=0) / nansum(weight, axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | class VWAP(WeightedAverageValue): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     Volume Weighted Average Price | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     **Default Inputs:** [USEquityPricing.close, USEquityPricing.volume] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     **Default Window Length:** None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     inputs = (USEquityPricing.close, USEquityPricing.volume) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | class MaxDrawdown(CustomFactor, SingleInputMixin): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     Max Drawdown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     **Default Inputs:** None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     **Default Window Length:** None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     ctx = ignore_nanwarnings() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     def compute(self, today, assets, out, data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         drawdowns = fmax.accumulate(data, axis=0) - data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         drawdowns[isnan(drawdowns)] = NINF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         drawdown_ends = nanargmax(drawdowns, axis=0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         # TODO: Accelerate this loop in Cython or Numba. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         for i, end in enumerate(drawdown_ends): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             peak = nanmax(data[:end + 1, i]) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |             out[i] = (peak - data[end, i]) / data[end, i] | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |