10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4

Usually, there are two ways to retrieve data:

• Manually download data from a specific location and then write a Python

program to retrieve and process it

• Use the functions contained in various Python modules, such as the function

called quotes_historical_yahoo_ohlc() in the matplotlib.finance

submodule

For both methods, there are some advantages and disadvantages. The main

advantage of the first method is that we know where to get our data. In addition,

since we write our own programs to download and process data, the logic of those

programs is clearer. The advantage of the second method is that it is quick and

convenient to retrieve data. In a sense, users don't even have to know from where to

retrievethe data and the structure of the original datasets. The disadvantage is that the

functions used might change. This might cause certain problems. For example, the old

version of quotes_historical_yahoo_ohlc() is quotes_historical_yahoo().

In order to retrieve useful information from the preceding sources, two submodules

could be used: pandas_datareader.data and matplotlib.financial. To find out

functions included in pandas_datareader.data, the dir() function is applied:

From the preceding output, it seems that we have eight functions related

to YahooFinance, such as YahooDailyReader(), YahooActionReader(),

YahooOptions(), YahooQuotesReader(), get_components_yahoo(), get_data_

yahoo(), get_data_yahoo_actions(), and get_quote_yahoo(). Actually, we

could use theDataReader() function as well.Similarly, a few functions are available

for retrieving data from Google, FRED, and from Prof. French's Data Library.

[ 111 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!