Pandas: Create a plot of adjusted closing prices, thirty days simple moving average and exponential moving average
Pandas: Plotting Exercise-15 with Solution
Write a Pandas program to create a plot of adjusted closing prices, 30 days simple moving average and exponential moving average of Alphabet Inc. between two specific dates.
Use the alphabet_stock_data.csv file to extract data.
What is Simple Moving Average (SMA)?
In financial applications a simple moving average (SMA) is the unweighted mean of the previous n data. However, in science and engineering, the mean is normally taken from an equal number of data on either side of a central value. This ensures that variations in the mean are aligned with the variations in the data rather than being shifted in time.
What Is an Exponential Moving Average (EMA)?
From investopedia.com - An exponential moving average (EMA) is a type of moving average (MA) that places a greater weight and significance on the most recent data points. The exponential moving average is also referred to as the exponentially weighted moving average. An exponentially weighted moving average reacts more significantly to recent price changes than a simple moving average (SMA), which applies an equal weight to all observations in the period.
Python Code :
import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv("alphabet_stock_data.csv") start_date = pd.to_datetime('2020-4-1') end_date = pd.to_datetime('2020-9-30') df['Date'] = pd.to_datetime(df['Date']) new_df = (df['Date']>= start_date) & (df['Date']<= end_date) df1 = df.loc[new_df] stock_data = df1.set_index('Date') close_px = stock_data['Adj Close'] stock_data['SMA_30_days'] = stock_data.iloc[:,4].rolling(window=30).mean() stock_data['EMA_20_days'] = stock_data.iloc[:,4].ewm(span=20,adjust=False).mean() plt.figure(figsize=[15,10]) plt.grid(True) plt.title('Historical stock prices of Alphabet Inc. [01-04-2020 to 30-09-2020]\n',fontsize=18, color='black') plt.plot(stock_data['Adj Close'],label='Adjusted Closing Price', color='black') plt.plot(stock_data['SMA_30_days'],label='30 days Simple moving average', color='red') plt.plot(stock_data['EMA_20_days'],label='20 days Exponential moving average', color='green') plt.legend(loc=2) plt.show()
Click for download alphabet_stock_data.csv
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to create a plot of Open, High, Low, Close, Adjusted Closing prices and Volume of Alphabet Inc. between two specific dates.
Next: Write a Pandas program to create a scatter plot of the trading volume/stock prices of Alphabet Inc. stock between two specific dates.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
>>> "Python ROCK" if True else " I AM GRUMPY" "Python ROCK"
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework