Get startedGet started for free

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.

This exercise is part of the course

Financial Trading in Python

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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()
Edit and Run Code