@@ -180,7 +180,7 @@ def _retry_read_url(url, retry_count, pause, name):
180
180
_HISTORICAL_YAHOO_URL = 'http://ichart.finance.yahoo.com/table.csv?'
181
181
182
182
183
- def _get_hist_yahoo (sym , start , end , retry_count , pause ):
183
+ def _get_hist_yahoo (sym , start , end , interval , retry_count , pause ):
184
184
"""
185
185
Get historical data for the given name from yahoo.
186
186
Date format is datetime
@@ -195,15 +195,15 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
195
195
'&d=%s' % (end .month - 1 ) +
196
196
'&e=%s' % end .day +
197
197
'&f=%s' % end .year +
198
- '&g=d' +
198
+ '&g=%s' % interval +
199
199
'&ignore=.csv' )
200
200
return _retry_read_url (url , retry_count , pause , 'Yahoo!' )
201
201
202
202
203
203
_HISTORICAL_GOOGLE_URL = 'http://www.google.com/finance/historical?'
204
204
205
205
206
- def _get_hist_google (sym , start , end , retry_count , pause ):
206
+ def _get_hist_google (sym , start , end , interval , retry_count , pause ):
207
207
"""
208
208
Get historical data for the given name from google.
209
209
Date format is datetime
@@ -314,14 +314,14 @@ def get_components_yahoo(idx_sym):
314
314
return idx_df
315
315
316
316
317
- def _dl_mult_symbols (symbols , start , end , chunksize , retry_count , pause ,
317
+ def _dl_mult_symbols (symbols , start , end , interval , chunksize , retry_count , pause ,
318
318
method ):
319
319
stocks = {}
320
320
failed = []
321
321
for sym_group in _in_chunks (symbols , chunksize ):
322
322
for sym in sym_group :
323
323
try :
324
- stocks [sym ] = method (sym , start , end , retry_count , pause )
324
+ stocks [sym ] = method (sym , start , end , interval , retry_count , pause )
325
325
except IOError :
326
326
warnings .warn ('Failed to read symbol: {0!r}, replacing with '
327
327
'NaN.' .format (sym ), SymbolWarning )
@@ -343,20 +343,20 @@ def _dl_mult_symbols(symbols, start, end, chunksize, retry_count, pause,
343
343
_source_functions = {'google' : _get_hist_google , 'yahoo' : _get_hist_yahoo }
344
344
345
345
346
- def _get_data_from (symbols , start , end , retry_count , pause , adjust_price ,
346
+ def _get_data_from (symbols , start , end , interval , retry_count , pause , adjust_price ,
347
347
ret_index , chunksize , source ):
348
348
349
349
src_fn = _source_functions [source ]
350
350
351
351
# If a single symbol, (e.g., 'GOOG')
352
352
if isinstance (symbols , (compat .string_types , int )):
353
- hist_data = src_fn (symbols , start , end , retry_count , pause )
353
+ hist_data = src_fn (symbols , start , end , interval , retry_count , pause )
354
354
# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
355
355
elif isinstance (symbols , DataFrame ):
356
- hist_data = _dl_mult_symbols (symbols .index , start , end , chunksize ,
356
+ hist_data = _dl_mult_symbols (symbols .index , start , end , interval , chunksize ,
357
357
retry_count , pause , src_fn )
358
358
else :
359
- hist_data = _dl_mult_symbols (symbols , start , end , chunksize ,
359
+ hist_data = _dl_mult_symbols (symbols , start , end , interval , chunksize ,
360
360
retry_count , pause , src_fn )
361
361
if source .lower () == 'yahoo' :
362
362
if ret_index :
@@ -369,7 +369,7 @@ def _get_data_from(symbols, start, end, retry_count, pause, adjust_price,
369
369
370
370
def get_data_yahoo (symbols = None , start = None , end = None , retry_count = 3 ,
371
371
pause = 0.001 , adjust_price = False , ret_index = False ,
372
- chunksize = 25 ):
372
+ chunksize = 25 , interval = 'd' ):
373
373
"""
374
374
Returns DataFrame/Panel of historical stock prices from symbols, over date
375
375
range, start to end. To avoid being penalized by Yahoo! Finance servers,
@@ -398,12 +398,17 @@ def get_data_yahoo(symbols=None, start=None, end=None, retry_count=3,
398
398
If True, includes a simple return index 'Ret_Index' in hist_data.
399
399
chunksize : int, default 25
400
400
Number of symbols to download consecutively before intiating pause.
401
+ interval : string, default 'd'
402
+ Time interval code, valid values are 'd' for daily, 'w' for weekly,
403
+ 'm' for monthly and 'v' for dividend.
401
404
402
405
Returns
403
406
-------
404
407
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
405
408
"""
406
- return _get_data_from (symbols , start , end , retry_count , pause ,
409
+ if interval not in ['d' , 'w' , 'm' , 'v' ]:
410
+ raise ValueError ("Invalid interval: valid values are 'd', 'w', 'm' and 'v'" )
411
+ return _get_data_from (symbols , start , end , interval , retry_count , pause ,
407
412
adjust_price , ret_index , chunksize , 'yahoo' )
408
413
409
414
@@ -437,7 +442,7 @@ def get_data_google(symbols=None, start=None, end=None, retry_count=3,
437
442
-------
438
443
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
439
444
"""
440
- return _get_data_from (symbols , start , end , retry_count , pause ,
445
+ return _get_data_from (symbols , start , end , None , retry_count , pause ,
441
446
adjust_price , ret_index , chunksize , 'google' )
442
447
443
448
0 commit comments