1

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

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

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

4

0 回答 0