Time for action – plotting a year’s worth of stock quotes

We can plot a year’s worth of stock quotes data with the matplotlib.finance package. This will require a connection to Yahoo Finance, which will be the data source.

  1. Determine the start date by subtracting 1 year from today.
    from matplotlib.dates import DateFormatter
    from matplotlib.dates import DayLocator
    from matplotlib.dates import MonthLocator
    from matplotlib.finance import quotes_historical_yahoo
    from matplotlib.finance import candlestick
    import sys
    from datetime import date
    import matplotlib.pyplot as plt
    today = date.today()
    start = (today.year - 1, today.month, today.day)
  2. We need to create so-called locators. These objects from the matplotlib.dates package are needed to locate months and days on the x-axis.
    alldays = DayLocator()
    months = MonthLocator()
  3. Create a date formatter to format the dates on the x axis. This formatter will create a string containing the short name of a month and the year.
    month_formatter = DateFormatter("%b %Y")
  4. Download the stock quote data from Yahoo finance with the following code:
    quotes = quotes_historical_yahoo(symbol, start, today)
  5. Create a Matplotlib figure object—this is a top-level container for plot components.
    fig = plt.figure()
  6. Add a subplot to the figure.
    ax = fig.add_subplot(111)
  7. Set the major locator on the x axis to the months locator. This locator is responsible for the big ticks on the x axis.
    ax.xaxis.set_major_locator(months)
  8. Set the minor locator on the x axis to the days locator. This locator is responsible for the small ticks on the x axis.
    ax.xaxis.set_minor_locator(alldays)
  9. Set the major formatter on the x axis to the months formatter. This formatter is responsible for the labels of the big ticks on the x axis.
    ax.xaxis.set_major_formatter(month_formatter)
  10. A function in the matplotlib.finance package allows us to display candlesticks. Create the candlesticks using the quotes data. It is possible to specify the width of the candlesticks. For now, use the default value.
    candlestick(ax, quotes)
  11. Format the labels on the x axis as dates. This should rotate the labels on the x axis, so that they fit better.
    fig.autofmt_xdate()
    plt.show()

    The candlestick chart for DISH (Dish Network Corp.) would appear as follows:

    Time for action – plotting a year’s worth of stock quotes

What just happened?

We downloaded a year’s worth of data from Yahoo Finance. We charted this data using candlesticks (see candlesticks.py):

from matplotlib.dates import DateFormatter
from matplotlib.dates import DayLocator
from matplotlib.dates import MonthLocator
from matplotlib.finance import quotes_historical_yahoo
from matplotlib.finance import candlestick
import sys
from datetime import date
import matplotlib.pyplot as plt

today = date.today()
start = (today.year - 1, today.month, today.day)

alldays = DayLocator()              
months = MonthLocator()
month_formatter = DateFormatter("%b %Y")

symbol = 'DISH’

if len(sys.argv) == 2:
   symbol = sys.argv[1]

quotes = quotes_historical_yahoo(symbol, start, today)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(months)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(month_formatter)

candlestick(ax, quotes)
fig.autofmt_xdate()
plt.show()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86