Compare bond and stock performance
Bonds and stocks are the most important investment alternatives. Now that you can import data from both the Federal Reserve and Yahoo! Finance, you can compare the performance of both asset classes. You'll be using a Total Return Index for each class, which accounts for returns due to both price increases and payments like interest or dividends.
For bonds, you'll use the Bank of America Merrill Lynch US High Yield Total Return Index Value ('BAMLHYH0A0HYM2TRIV'
). For stocks, you'll use the S&P 500 Index ('SP500'
). Both are available for the past 10 years from the Federal Reserve's FRED service.
In this exercise, you will download both series and compare their performance. DataReader
, date
, pandas
as pd
, and matplotlib.pyplot
as plt
have been imported.
This exercise is part of the course
Importing and Managing Financial Data in Python
Exercise instructions
- Using
date()
, set thestart
date to January 1, 2008. - Set the
series
codes as a list containing'BAMLHYH0A0HYM2TRIV'
and'SP500'
. - Use
DataReader()
to import both series from'fred'
and assign todata
. - Plot and show
data
withsubplots
, titled'Performance Comparison'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set the start date
start = ____
# Set the series codes
series = ['BAMLHYH0A0HYM2TRIV', 'SP500']
# Import the data
data = ____
# Plot the results
____
# Show the plot
plt.show()