Aan de slagGa gratis aan de slag

Calculate and plot SMAs

Daily price data is inherently messy and noisy. You want to analyze the Apple stock daily price data, and plan to add a simple moving average (SMA) indicator to smooth out the data. Specifically, you decide to use the 50-day SMA.

The stock data has been preloaded in aapl_data, and matplotlib.pyplot has been imported as plt. Additional customizations to the plot such as a title and legend have already been provided for you.

Deze oefening maakt deel uit van de cursus

Financial Trading in Python

Cursus bekijken

Oefeninstructies

  • Calculate the 50-day SMA using the Close price, and save it in a new column named sma_50.
  • Plot a line chart using the data in the columns sma_50 and Close.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Calculate SMA
aapl_data['sma_50'] = aapl_data['____'].____.mean()

# Plot the SMA
____(aapl_data['____'], color='green', label='SMA_50')
# Plot the close price
____(aapl_data['____'], color='red', label='Close')

# Customize and show the plot
plt.title('Simple moving averages')
plt.legend()
plt.show()
Code bewerken en uitvoeren