Example Automatically Reading Yahoo Financial Data

The following discussion is a bit off-topic, but as it is a direct continuation of the previous example, this is probably a logical spot for it.

There's an alternative method to manually saving the charts.xls file from NASDAQ. One such option is using the matplotlib.finance module. The two core functions that fetch the data and parse it are fetch_historical_yahoo() and parse_yahoo_historical() (although you could easily parse the data yourself). Another function of interest is the candlestick() function, which plots a candlestick graph of the stocks.

Listing 4-4 is a modification of the previous example to use the functions from the matplotlib.finance module. Notice that there are some other minor changes to the code because the data structure is a bit different from the NASDAQ charts.xls file. You can control the stock you wish to view and the start and end dates by changing the values stock name, t start, and t_end.

Listing 4-4. Fetching and Plotting Yahoo! Data from pylab import *

from matplotlib.finance import *

# stock name and period stock_name = 'NDX'

t_start = datetime.datetime(2008, 1, l)

t_end = datetime.datetime(2008, 1, 31)

year_start = datetime.datetime(2008, 1, l)

# retrieve and parse stock data data = fetch_historical_yahoo(stock_name, t_start, t_end) y = array(parse_yahoo_historical(data))

# dates might not be trade days, so update values

# to show actual dates retrieved t_start = num2date(y[0, 0]) t_end = num2date(y[-l, 0])

# normalize the x-axis to show values from the start of year y[;.>0] =y[;j 0]-date2num(year_start)+l

# plot a candlestick graph figure()

candlestick(gca(), y)

# annotate the graph with additional text start_str = "%d-%02d-%02d" % (t_start.year, t_start.month, t_start.day)

end_str = "%d-%02d-%02d" % (t_end.year, t_end.month, t_end.day)

title('Stock: %s, period %s to %s' % (stock_name, start_str, end_str))

xlabel('Days from start of the year %d' % t_start.year)

savefig('../images/%s_candlestick_yahoo-%s-%s.png' % \ (stock_name, start_str, end_str))

Some notes:

• The time base is normalized, that is, the dates are shown from the start of the year 2008 and not the epoch. This is implemented in line y[:,0] = y[:,0]-date2num(year start)+l.

• The actual dates requested might not be trade days. Therefore, the start and end times are updated after the data is fetched and parsed. This is done in line tstart = num2date(y[0, 0]) and t_end = num2date(y[-l, 0]).

Figure 4-2 shows the results of the example in Listing 4-4.

Matplotlib Yahoo
Figure 4-2. Automatically generated candlestick graph

Was this article helpful?

0 0


  • secondo
    How to fetch financial data from yahoo?
    8 years ago

Post a comment