试图计算移动平均线时出错

时间:2013-11-09 00:28:04

标签: python list

我有一个计算移动平均线的函数。但它给我一个错误的输出“买入”信号。这是我的代码:

def calculate(self, alist, days=2):

    averages = []
    signals  = []
    prices   = [float(n) for n in alist[1::2]]
    window   = collections.deque(maxlen=days)

    for price in prices:
        window.append(price)
        averages.append(0)
        signals.append("")
        if len(window) == days:
            mavg = sum(window) / days
            averages[-1] = mavg
            if price < mavg:
                signals[-1] = "SELL"
            elif price > mavg:
                signals[-1] = "BUY"

    tol=0.01
    averages[:] = ("%.2f" % avg if abs(avg)>=tol else ' '  for avg in averages)

    return averages, signals

该功能检查当前收盘价是否高于当前日均移动平均线。然而,买入条件是当前收盘价应高于当日收盘价,而前一日收盘价应低于前一日均线。如何对此进行更改?

0 个答案:

没有答案