自定义因子问题,零大小数组

时间:2018-05-24 03:41:09

标签: python arrays numpy indexing quantopian

现在有了这个问题的自定义因素,它在日志中一直说我有一个错误并且我的数组大小为0(在回测试之后,我知道这就是错误所在的位置)。如果您需要更多信息,请随时提出。

此代码的目的是创建一个归一化的平均真实范围除以0-1等级的价格,我用来代码的源是quantopian。

确切的错误是:

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

以下是代码的不同版本:

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

这次我的错误是:

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

0 个答案:

没有答案