Financial Plots
matplotlib also provides a small selection of special finance plots. These, like the
candlestick plot, are mainly used to visualize historical stock price data or similar financial
time series data. Those plotting capabilities are found in the matplotlib.finance
sublibrary:
In [ 25 ]: import matplotlib.finance as mpf
As a convenience function, this sublibrary allows for easy retrieval of historical stock
price data from the Yahoo! Finance website (cf. http://finance.yahoo.com). All you need
are start and end dates and the respective ticker symbol. The following retrieves data for
the German DAX index whose ticker symbol is ^GDAXI:
In [ 26 ]: start = ( 2014 , 5 , 1 )
end = ( 2014 , 6 , 30 )
quotes = mpf.quotes_historical_yahoo(‘^GDAXI’, start, end)
DATA QUALITY OF WEB SOURCES
Nowadays, a couple of Python libraries provide convenience functions to retrieve data from Yahoo! Finance. Be
aware that, although this is a convenient way to visualize financial data sets, the data quality is not sufficient to
base any important investment decision on it. For example, stock splits, leading to “price drops,” are often not
correctly accounted for in the data provided by Yahoo! Finance. This holds true for a number of other freely
available data sources as well.
quotes now contains time series data for the DAX index starting with Date (in epoch time
format), then Open, High, Low, Close, and Volume:
In [ 27 ]: quotes[: 2 ]
Out[27]: [(735355.0,
9611.7900000000009,
9556.0200000000004,
9627.3799999999992,
9533.2999999999993,
88062300.0),
(735358.0,
9536.3799999999992,
9529.5,
9548.1700000000001,
9407.0900000000001,
61911600.0)]
The plotting functions of matplotlib.finance understand exactly this format and the data
set can be passed, for example, to the candlestick function as it is. Figure 5-20 shows the
result. Daily positive returns are indicated by blue rectangles, and negative returns by red
ones. As you notice, matplotlib takes care of the right labeling of the x-axis given the
date information in the data set:
In [ 28 ]: fig, ax = plt.subplots(figsize=( 8 , 5 ))
fig.subplots_adjust(bottom=0.2)
mpf.candlestick(ax, quotes, width=0.6, colorup=‘b’, colordown=‘r’)
plt.grid(True)
ax.xaxis_date()
# dates on the x-axis
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation= 30 )