0

having this issue right now with a custom factor, it keeps saying in the log that I have an error and that my array size is 0 (after backtesting a little, I know this is where that error lies). If you need more information, please feel free to ask.

The purpose of this code is to create a normalized Average True Range divided by Price on a 0-1 Scale, the source I'm using to code is quantopian.

The exact error is :

ValueError: zero-size array to reduction operation fmin which has no identity

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

Here's a different version of the code:

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

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.
martineau
  • 119,623
  • 25
  • 170
  • 301
  • `np.amin(np.arange(10)[-2:-5])` produces a similar error. That slice produces an 0 element array. A slice like `[-2: -101]` doesn't make sense. What's it supposed to do? – hpaulj May 24 '18 at 07:39
  • Take around 100 samples of the ATR and average them together to get an average ATR, and then compare that with today's ATR ([-1]) – Mathew Crogan May 24 '18 at 15:01
  • You need to use `[-101:-2]` or `[-2:-101:-1]` to get a reverse list. Play with numbers like that in `arange` if you don't understand. – hpaulj May 24 '18 at 15:16

0 Answers0