Working with modules

Before you run this notebook, please download the yahooFinance.py module and save it in the same directory as this notebook.

Then you can import it by using the import command. Note: we are using import as command, which allowes us to use a shorthand notation of the library name. This way you only need to type yf instead of yahooFinance in further code. Another advantage is that the module can be reloaded (handy if you change the module code) without restarting the interpreter.


In [1]:
import yahooFinance as yf  # import

Now let's take a look at the module documentation


In [2]:
help(yf)


Help on module yahooFinance:

NAME
    yahooFinance - Toolset working with yahoo finance data

FILE
    d:\my dropbox\public\notebooks\yahoofinance.py

DESCRIPTION
    This module includes functions for easy access to YahooFinance data
    
    Functions
    ----------
    - `getHistoricData`  get historic data for a single symbol
    - `getQuote` get current quote for a symbol
    - `getScreenerSymbols` load symbols from a yahoo stock screener file
    
    Classes
    ---------
    - `HistData` a class for working with multiple symbols

CLASSES
    __builtin__.object
        HistData
    
    class HistData(__builtin__.object)
     |  a class for working with yahoo finance data
     |  
     |  Methods defined here:
     |  
     |  __init__(self, autoAdjust=True)
     |  
     |  __repr__(self)
     |  
     |  downloadData(self, symbols='all')
     |      get data from yahoo
     |  
     |  getDataFrame(self, field='close')
     |      return a slice on wide panel for a given field
     |  
     |  load(self, dataFile)
     |      load data from HDF
     |  
     |  save(self, dataFile)
     |      save data to HDF
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  symbols

FUNCTIONS
    getHistoricData(symbol, sDate=(1990, 1, 1), eDate=(2013, 4, 14), verbose=True)
        get data from Yahoo finance and return pandas dataframe
        
        symbol: Yahoo finanance symbol
        sDate: start date (y,m,d)
        eDate: end date (y,m,d)
    
    getQuote(symbols)
        get current yahoo quote
        
        
        , return a DataFrame
    
    getScreenerSymbols(fileName)
        read symbols from a .csv saved by yahoo stock screener


Show a list of all names defined in the package


In [3]:
dir(yf)


Out[3]:
['DataFrame',
 'HDFStore',
 'HistData',
 'Index',
 'WidePanel',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '_adjust',
 '_historicDataUrll',
 'date',
 'datetime',
 'getHistoricData',
 'getQuote',
 'getScreenerSymbols',
 'np',
 'os',
 'urllib2']

Let's run some functions from the module


In [4]:
yf.getQuote(['SPY','QQQ','AAPL']) # get last available quote from Yahoo


Out[4]:
PE change_pct last short_ratio time
SPY NaN -0.24% 158.80 NaN 4:00pm
QQQ NaN -0.07% 69.94 NaN 4:00pm
AAPL 9.85 -1.04% 429.80 1.2 4:00pm

In [5]:
df = yf.getHistoricData('SPY',(2012,1,1)) # get daily historic data from Yahoo

print df.tail(20) # print last 20 days of data table


Got 320 days of data
            adj_close   close    high     low    open     volume
2013-03-15     155.83  155.83  156.04  155.31  155.85  138601100
2013-03-18     154.97  154.97  155.64  154.20  154.34  126704300
2013-03-19     154.61  154.61  155.51  153.59  155.30  167567300
2013-03-20     155.69  155.69  155.95  155.26  155.52  113759300
2013-03-21     154.36  154.36  155.64  154.10  154.76  128605000
2013-03-22     155.60  155.60  155.60  154.73  154.85  111163600
2013-03-25     154.95  154.95  156.27  154.35  156.01  151322300
2013-03-26     156.19  156.19  156.23  155.42  155.59   86856600
2013-03-27     156.19  156.19  156.24  155.00  155.26   99950600
2013-03-28     156.67  156.67  156.85  155.75  156.09  102932800
2013-04-01     156.05  156.05  156.91  155.67  156.59   99194100
2013-04-02     156.82  156.82  157.21  156.37  156.61  101504300
2013-04-03     155.23  155.23  157.03  154.82  156.91  154167400
2013-04-04     155.86  155.86  156.17  155.09  155.43  131885000
2013-04-05     155.16  155.16  155.35  153.77  153.95  159645300
2013-04-08     156.21  156.21  156.22  154.75  155.27   86450000
2013-04-09     156.75  156.75  157.32  155.98  156.50  100805700
2013-04-10     158.67  158.67  158.87  157.13  157.17  135157000
2013-04-11     159.19  159.19  159.71  158.54  158.70  110046600
2013-04-12     158.80  158.80  159.04  157.92  158.68  116342000

By importing the yahooFinance module you can access functions for working with yahoo finance. The good thing about modules is that you don't even have to know how they work or what is inside them. The only thing you need is a little bit of documentation about the contained functionality.

We will be taking a closer look inside the yahooFinance.py next week.