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

Python 自定义因子问题,零大小数组,python,arrays,numpy,indexing,quantopian,Python,Arrays,Numpy,Indexing,Quantopian,现在有一个自定义因子出现了这个问题,它在日志中不断地说我有一个错误,我的数组大小是0(经过一点回溯测试,我知道这就是错误所在)。如果您需要更多信息,请随时询问 这段代码的目的是创建一个标准化的平均真实范围除以0-1标度上的价格,我用来编码的源代码是quantopian 确切的错误是: ValueError:零大小数组到没有标识的缩减操作fmin ATrComp类(自定义系数): inputs=[USEquityPricing.close,USEquityPricing.high,USEquity

现在有一个自定义因子出现了这个问题,它在日志中不断地说我有一个错误,我的数组大小是0(经过一点回溯测试,我知道这就是错误所在)。如果您需要更多信息,请随时询问

这段代码的目的是创建一个标准化的平均真实范围除以0-1标度上的价格,我用来编码的源代码是quantopian

确切的错误是:

ValueError:零大小数组到没有标识的缩减操作fmin
ATrComp类(自定义系数):
inputs=[USEquityPricing.close,USEquityPricing.high,USEquityPricing.low]
窗长=200
def计算(自我、今天、资产、退出、关闭、高、低):
hml=高-低
hmpc=np.abs(高-np.roll(闭合,1,轴=0))
lmpc=np.abs(低-np.roll(关闭,1,轴=0))
tr=np.最大值(hml,np.最大值(hmpc,lmpc))
atr=np.平均值(tr[-1:-21],轴=0)#跳过第一个,因为它是NaN
apr=atr*100/关闭[-1]
aprcomp=(apr[-1]-np.amin(apr[-2:-101],轴=0))/(np.amax(apr[-2:-101],轴=0)-np.amin(apr[-2:-101],轴=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.\uuu getitem\uuuuuuu()的值应为
键入zipline.assets.\u assets.assets作为参数“key”,但改为int。

np.amin(np.arange(10)[-2:-5])
产生类似的错误。该切片生成一个0元素数组。像
[-2:-101]
这样的切片没有意义。它应该做什么?取大约100个ATR样本,将它们平均起来,得到一个平均的ATR,然后将其与今天的ATR([-1])进行比较。你需要使用
[-101:-2]
[-2:-101:-1]
获得一个相反的列表。如果你不懂,可以在
arange
中玩类似的数字。