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

2024-10-02 16:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

现在有一个自定义因子出现了这个问题,它在日志中不断地说我有一个错误,我的数组大小是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.

Tags: close错误npabsoutaprlowroll