m0_59047090
2021-08-30 08:54
采纳率: 100%
浏览 37
已结题

索引错误:索引1超出大小为1的轴0的界限

def EMA(self,x, n):
self.y = (self._close[0] * 2 + self.y * (n - 1) / (n + 1))

def MA(self,data_array, n=4):
    from collections import deque  # 导入双队列
    import itertools  # 创建迭代器
    it = iter(self.data_array)  # iter()把列表变为一个可迭代对象
    d = deque(itertools.islice(it, n - 1))  # 表示获取it中的前N个数据
    s = sum(d)
    d.appendleft(0)
    for elem in it:
        s += elem - d.popleft()
        d.append(elem)
        ma_ = s / float(n)

def cjdx(self):
    self.var1.append((2 * self._close[0] + self._high[0] + self._low[0]) / 4)
    if len(self.var1) > 5:
        var2_t = self.EMA(self.EMA(self.EMA(self.var1[0], 4), 4), 4)
        self.var2 = np.append(self.var2, var2_t)
        # J: (VAR2-REF(VAR2,1))/REF(VAR2,1)*100
        if self.var2!=0:
            self.j = (self.var2[0] - self.var2[1]) / self.var2[1] * 100
            # D: MA(J,3);
            self.d = self.MA(j, 3)
            # K: MA(J,1);
            self.k = self.MA(j, 1)

输出:
File "C:/Users/Administrator/PycharmProjects/pythonProject1/venv/爬虫/移动平均函数.py", line 162, in cjdx
self.j = (self.var2[0] - self.var2[1]) / self.var2[1] * 100
IndexError: index 1 is out of bounds for axis 0 with size 1

  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • 吃肉的小馒头 2021-08-30 09:58
    已采纳

    打印self.var2 输出看看,取不到索引

    已采纳该答案
    评论
    解决 1 无用
    打赏 举报

相关推荐 更多相似问题