0

我对 python 比较陌生,并试图绘制股票价格和 2 个指数移动平均线,但是当我尝试绘制指数移动平均线时,它会抛出一个错误。

发生错误:

'''Calculate MA'''
ShortMA = df.Close.ewm(span=12, adjust=False).mean

'''Plot MA'''
plt.figure(figsize=(12.5, 4.5))
plt.plot(df.index, ShortMA, label='ShortMA', color='blue')
plt.show

错误信息:

ValueError:x 和 y 必须具有相同的第一维,但具有形状 (78,) 和 (1,)

完整代码:

def SPY():
    plt.style.use('fivethirtyeight')

    '''Obtain SPY 5 Min and put into panda dataframe'''
    symbol = yf.Ticker("SPY")
    symbol_history = symbol.history(start='2022-02-08', end='2022-02-09', interval='5m', prepost='False', actions='False')
    df = pd.DataFrame(symbol_history)
    print(df)

    '''Plot the closing price'''
    plt.figure(figsize=(12.5, 4.5))  # plot size
    plt.xticks(rotation=45)  # turns x-axis value 45 degrees
    close_plot = plt.plot(df['Close'], label='Close')  # plot only closing price
    plt.xlabel("Date")  # labels x axis
    plt.ylabel("Price ($)")  # labels y axis
    #plt.show()  # show plot

    '''Calculate MA'''
    ShortMA = df.Close.ewm(span=12, adjust=False).mean

    '''Plot MA'''
    plt.figure(figsize=(12.5, 4.5))
    plt.plot(df.index, ShortMA, label='ShortMA', color='blue')
    plt.show

SPY()
4

0 回答 0