Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem with Custom Factor

Hey guys, I'm trying to normalize ATR/Price on a 0-1 Scale on a given time frame but I seem to be having a little bit of an issue and keep getting 'NaN' in return. Anyone know why??

Code to focus on:

class ATrComp(CustomFactor):  
    inputs = [USEquityPricing.close,USEquityPricing.high,USEquityPricing.low]  
    window_length = 200  
    def compute(self, today, assets, out, close, high, low):  
        hml = high - low  
        hmpc = np.abs(high - np.roll(close, 1, axis=0))  
        lmpc = np.abs(low - np.roll(close, 1, axis=0))  
        tr = np.maximum(hml, np.maximum(hmpc, lmpc))  
        atr = np.mean(tr[1:21], axis=0) #skip the first one as it will be NaN  
        apr = atr*100 / close[-1]  
        aprcomp = (apr[1] - np.min(apr[2:101], axis=0))/(np.max(apr[2:101], axis=0) - np.min(apr[2:101], axis=0))  
        out[:] = aprcomp  

And the backtest is attached.

Thank you!!!

3 responses
class ATrComp(CustomFactor):  
    inputs = [USEquityPricing.close,USEquityPricing.high,USEquityPricing.low]  
    window_length = 200  
    def compute(self, today, assets, out, close, high, low):  
        hml = high - low  
        hmpc = np.abs(high - np.roll(close, 1, axis=0))  
        lmpc = np.abs(low - np.roll(close, 1, axis=0))  
        tr = np.maximum(hml, np.maximum(hmpc, lmpc))  
        atr = np.mean(tr[-1:-21], axis=0) #skip the first one as it will be NaN  
        apr = atr*100 / close[-1]  
        aprcomp = (apr[-1] - np.amin(apr[-2:-101], axis=0))/(np.amax(apr[-2:-101], axis=0) - np.amin(apr[-2:-101], axis=0))  
        out[:] = aprcomp  

Updated code, sorry for that idiot indexing before, now I'm just getting an issue where it says my array size is 0?

class ATrp(CustomFactor):  
    inputs = [USEquityPricing.close,USEquityPricing.high,USEquityPricing.low]  
    window_length = 200  
    def compute(self, today, assets, out, close, high, low):  
        hml = high - low  
        hmpc = np.abs(high - np.roll(close, 1, axis=0))  
        lmpc = np.abs(low - np.roll(close, 1, axis=0))  
        tr = np.maximum(hml, np.maximum(hmpc, lmpc))  
        atr = np.mean(tr[-21:], axis=0) #skip the first one as it will be NaN  
        apr = atr*100 / close[-1]  
        out[:] = apr  
class ATrComp(CustomFactor):  
     inputs = [USEquityPricing.close,USEquityPricing.high,USEquityPricing.low]  
     window_length = 200  
     def compute(self, today, assets, out, close, high, low):  
        apr = ATrp()  
        aprcomp = (apr[-1] - np.amin(apr[-2:-101], axis=0))/(np.amax(apr[-2:-101], axis=0) - np.amin(apr[-2:-101], axis=0))  
        out[:] = aprcomp  

Different Version of code, still no results, this time my error is :

TypeError: zipline.pipeline.term.getitem() expected a value of type zipline.assets._assets.Asset for argument 'key', but got int instead.

keep getting 'NaN' in return.

One route is to forward fill the nans. They all become values here, however they're all the same value (in ATR%) so there is a deeper problem. This might help track it down. It resolved some in ATR also. By the way notice you have quite an outlier there: [BRK_A]) 4026.42950, maybe set a cap on price.

Before, with nanfill off (see source for this line):

return _in            # uncomment to not run the code below
2017-12-27 05:45 log_pipe:212 INFO $1e+06    2017-12-27 to 2017-12-29  
2017-12-27 05:45 log_pipe:267 INFO Rows: 614  Columns: 5  
                    min              mean              max  
    ATR         0.04275     8.38339575819        4026.4295     NaNs 4/614  
   ATR%             nan               nan              nan     NaNs 614/614  
Mkt Cap     5028.285712     41305.9588566     875759.59784  
  Price            5.72     578.053741231         296370.0  
 SMA 50          5.6233     554.961372045      285020.0494  
2017-12-27 05:45 log_pipe:282 INFO _ _ _   ATR   _ _ _  
    ... ATR highs  
                             ATR  ATR%        Mkt Cap      Price        SMA 50  
Equity(1091 [BRK_A])  4026.42950   NaN  487452.901712  296370.00  285020.04940  
Equity(5513 [NVR])      61.08775   NaN   12981.047118    3468.53    3307.57400  
Equity(16841 [AMZN])    19.36295   NaN  567047.900653    1176.94    1119.26122  
Equity(693 [AZO])       19.29925   NaN   19543.182157     714.29     643.52500  
    ... ATR lows  
                        ATR  ATR%       Mkt Cap    Price      SMA 50  
Equity(8289 [WSO_B])    NaN   NaN   6051.632706  169.160  167.213636  
Equity(23075 [MKC_V])   NaN   NaN  13360.046808  101.733   99.615089  
Equity(27358 [LBTY_B])  NaN   NaN  28145.312628   35.550   32.499857  
Equity(42173 [APTV])    NaN   NaN  22564.481714   84.880   83.452004  
2017-12-27 05:45 log_pipe:282 INFO _ _ _   ATR%   _ _ _  
    ... ATR% highs  
                        ATR  ATR%        Mkt Cap   Price      SMA 50  
Equity(24 [AAPL])  2.796950   NaN  875759.597840  170.57  169.083583  
Equity(53 [ABMD])  3.896350   NaN    8380.728123  189.64  189.389480  
Equity(62 [ABT])   0.821800   NaN   99214.239159   56.98   55.480200  
Equity(122 [ADI])  1.433582   NaN   32672.189890   88.64   88.428175  
    ... ATR% lows  
                           ATR  ATR%      Mkt Cap   Price     SMA 50  
Equity(50749 [BTU])   0.794050   NaN  5136.426000  39.100  32.684440  
Equity(50763 [SNDR])  0.785422   NaN  5041.066063  28.505  25.921354  
Equity(50869 [GDI])   0.759450   NaN  6670.249599  34.030  30.126300  

After, with nanfill active:

#return _in            # uncomment to not run the code below
2017-12-27 05:45 log_pipe:213 INFO $1e+06    2017-12-27 to 2017-12-29  
2017-12-27 05:45 log_pipe:268 INFO Rows: 614  Columns: 5  
                       min               mean                max  
    ATR            0.04275      8.37117248164          4026.4295  
   ATR%     0.154848396897     0.154848396897     0.154848396897  
Mkt Cap        5028.285712      41305.9588566       875759.59784  
  Price               5.72      578.053741231           296370.0  
 SMA 50             5.6233      554.961372045        285020.0494  
2017-12-27 05:45 log_pipe:283 INFO _ _ _   ATR   _ _ _  
    ... ATR highs  
                             ATR      ATR%        Mkt Cap      Price  \  
Equity(1091 [BRK_A])  4026.42950  0.154848  487452.901712  296370.00  
Equity(5513 [NVR])      61.08775  0.154848   12981.047118    3468.53  
Equity(16841 [AMZN])    19.36295  0.154848  567047.900653    1176.94  
Equity(693 [AZO])       19.29925  0.154848   19543.182157     714.29   

                            SMA 50  
Equity(1091 [BRK_A])  285020.04940  
Equity(5513 [NVR])      3307.57400  
Equity(16841 [AMZN])    1119.26122  
Equity(693 [AZO])        643.52500  
    ... ATR lows  
                              ATR      ATR%       Mkt Cap   Price     SMA 50  
Equity(157 [AEG])        0.072050  0.154848  12915.415746   6.220   6.045000  
Equity(43079 [PSA_PRU])  0.071135  0.154848  39384.282975  25.123  24.968165  
Equity(21689 [SPIL])     0.060950  0.154848   5200.072780   8.270   8.060160  
Equity(35531 [CPN])      0.042750  0.154848   5444.583685  15.100  15.004700  
2017-12-27 05:45 log_pipe:283 INFO _ _ _   ATR%   _ _ _  
    ... ATR% highs  
                         ATR      ATR%        Mkt Cap   Price      SMA 50  
Equity(24 [AAPL])    2.79695  0.154848  875759.597840  170.57  169.083583  
Equity(25555 [ACN])  2.15215  0.154848   94344.033954  152.98  146.012918  
Equity(25002 [E])    0.39310  0.154848   60489.361887   33.46   32.938980  
Equity(25006 [JPM])  2.05605  0.154848  371330.031250  107.01  101.803860  
    ... ATR% lows  
                          ATR      ATR%        Mkt Cap    Price      SMA 50  
Equity(7530 [TOL])   1.205100  0.154848    7294.098260   47.610   46.585400  
Equity(7538 [TOT])   0.573628  0.154848  139050.496606   55.470   54.963488  
Equity(7543 [TM])    1.108950  0.154848  187806.326571  128.009  125.028940  

Output is pipeline preview.

Wish List:

def Initialize(context):  
    set(factor_nanfill = 1)  
    set(pipe_preview   = 3)  # first 3 days