0

I am relatively new to python and trying to graph stock prices and 2 exponential moving averages but when I try to graph the exponential moving average it throws out an error.

Error Occurs:

'''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

Error Message:

ValueError: x and y must have same first dimension, but have shapes (78,) and (1,)

Entire Code:

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()
Robert
  • 7,394
  • 40
  • 45
  • 64

0 Answers0