Yahoo Finance

Getting historic data

The module is usually imported as follows:

In [1]: from tradingWithPython import yahooFinance as yf

Singe symbol

Then, to get raw yahoo finance data for a symbol use getSymbolData()

In [2]: df = yf.getSymbolData("SPY")
Got 4510 days of data

In [3]: df.head()
Out[3]: 
              open    high     low   close  adj_close    volume
Date                                                           
1999-12-31  146.84  147.50  146.25  146.88     105.37   3172700
2000-01-03  148.25  148.25  143.88  145.44     104.34   8164300
2000-01-04  143.53  144.06  139.64  139.75     100.26   8089800
2000-01-05  139.94  141.53  137.25  140.00     100.44  12177900
2000-01-06  139.62  141.50  137.75  137.75      98.82   6227200

We can also normalize OHLC with the adj_close data column. After normalization, the close column will be equal to adj_close , so the latter is omitted from the result.

In [4]: df = yf.getSymbolData("SPY",adjust=True)
Got 4510 days of data

In [5]: df.head()
Out[5]: 
             close    volume    open    high     low
Date                                                
1999-12-31  105.37   3172700  105.35  105.82  104.92
2000-01-03  104.34   8164300  106.36  106.36  103.22
2000-01-04  100.26   8089800  102.97  103.35  100.18
2000-01-05  100.44  12177900  100.39  101.54   98.46
2000-01-06   98.82   6227200  100.17  101.51   98.82

Multiple symbols

getHistoricData() will accept one ore more symbols and download them while displaying a progress bar.

In [6]: symbols = ['XLE','USO','SPY']

In [7]: data = yf.getHistoricData(symbols)
Downloading data:

 [                       0%                       ]
 [**********************67%*******                ]  2 of 3 complete
 [*********************100%***********************]  3 of 3 complete
In [8]: print(data)
symbol         SPY                                                 USO         \
ohlcv         open    high     low   close adj_close     volume   open   high   
Date                                                                            
1999-12-31  146.84  147.50  146.25  146.88    105.37    3172700    NaN    NaN   
2000-01-03  148.25  148.25  143.88  145.44    104.34    8164300    NaN    NaN   
2000-01-04  143.53  144.06  139.64  139.75    100.26    8089800    NaN    NaN   
...            ...     ...     ...     ...       ...        ...    ...    ...   
2017-11-29  263.02  263.63  262.20  262.71    262.71   77512100  11.58  11.66   
2017-11-30  263.76  266.05  263.67  265.01    265.01  122980900  11.55  11.58   
2017-12-01  264.76  265.31  260.79  264.46    264.46  147858445  11.65  11.78   

symbol                                            XLE                       \
ohlcv         low  close adj_close      volume   open   high    low  close   
Date                                                                         
1999-12-31    NaN    NaN       NaN         NaN  27.00  27.25  26.81  27.09   
2000-01-03    NaN    NaN       NaN         NaN  27.31  27.31  26.38  26.56   
2000-01-04    NaN    NaN       NaN         NaN  26.31  26.31  25.88  26.06   
...           ...    ...       ...         ...    ...    ...    ...    ...   
2017-11-29  11.36  11.47     11.47  16093700.0  67.68  68.21  67.54  68.08   
2017-11-30  11.37  11.47     11.47  17779500.0  68.24  69.23  68.24  69.10   
2017-12-01  11.60  11.67     11.67  14139965.0  69.59  70.16  68.94  69.68   

symbol                          
ohlcv      adj_close    volume  
Date                            
1999-12-31     19.50     34800  
2000-01-03     19.12    380300  
2000-01-04     18.76    722200  
...              ...       ...  
2017-11-29     68.08  13599300  
2017-11-30     69.10  21971900  
2017-12-01     69.68  21109479  

[4510 rows x 18 columns]

Getting current quotes

The getQuote() is used to get current quote

In [9]: quote = yf.getQuote(['SPY','XLE','QQQ'])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-9-ba43f48120e7> in <module>()
----> 1 quote = yf.getQuote(['SPY','XLE','QQQ'])

AttributeError: module 'tradingWithPython.lib.yahooFinance' has no attribute 'getQuote'

In [10]: quote
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-0669fb119297> in <module>()
----> 1 quote

NameError: name 'quote' is not defined

Note

YahooFinance quotes may be delayed for more than 15 minutes

Functions

tradingWithPython.lib.yahooFinance.getSymbolData(symbol, sDate=(2000, 1, 1), adjust=False, verbose=True, dumpDest=None)[source]

get data from Yahoo finance and return pandas dataframe

Parameters:

symbol : str

Yahoo finanance symbol

sDate : tuple , default (2000,1,1)

start date (y,m,d)

adjust : bool , default False

use adjusted close values to correct OHLC. adj_close will be ommited

verbose : bool , default True

print output

dumpDest : str, default None

dump raw data for debugging

Returns:

DataFrame

tradingWithPython.lib.yahooFinance.getHistoricData(symbols, **options)[source]

get data from Yahoo finance and return pandas dataframe Will get OHLCV data frame if sinle symbol is provided. If many symbols are provided, it will return a wide panel

Parameters:

symbols : str or list

Yahoo finanance symbol or a list of symbols

sDate : tuple (optional)

start date (y,m,d)

adjust : bool

T/[F] adjust data based on adj_close

Returns:

DataFrame, multi-index