@@ -188,7 +188,7 @@ def _retry_read_url(url, retry_count, pause, name):
188
188
_HISTORICAL_YAHOO_URL = 'http://ichart.finance.yahoo.com/table.csv?'
189
189
190
190
191
- def _get_hist_yahoo (sym , start , end , retry_count , pause ):
191
+ def _get_hist_yahoo (sym , start , end , interval , retry_count , pause ):
192
192
"""
193
193
Get historical data for the given name from yahoo.
194
194
Date format is datetime
@@ -203,15 +203,15 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
203
203
'&d=%s' % (end .month - 1 ) +
204
204
'&e=%s' % end .day +
205
205
'&f=%s' % end .year +
206
- '&g=d' +
206
+ '&g=%s' % interval +
207
207
'&ignore=.csv' )
208
208
return _retry_read_url (url , retry_count , pause , 'Yahoo!' )
209
209
210
210
211
211
_HISTORICAL_GOOGLE_URL = 'http://www.google.com/finance/historical?'
212
212
213
213
214
- def _get_hist_google (sym , start , end , retry_count , pause ):
214
+ def _get_hist_google (sym , start , end , interval , retry_count , pause ):
215
215
"""
216
216
Get historical data for the given name from google.
217
217
Date format is datetime
@@ -322,14 +322,14 @@ def get_components_yahoo(idx_sym):
322
322
return idx_df
323
323
324
324
325
- def _dl_mult_symbols (symbols , start , end , chunksize , retry_count , pause ,
325
+ def _dl_mult_symbols (symbols , start , end , interval , chunksize , retry_count , pause ,
326
326
method ):
327
327
stocks = {}
328
328
failed = []
329
329
for sym_group in _in_chunks (symbols , chunksize ):
330
330
for sym in sym_group :
331
331
try :
332
- stocks [sym ] = method (sym , start , end , retry_count , pause )
332
+ stocks [sym ] = method (sym , start , end , interval , retry_count , pause )
333
333
except IOError :
334
334
warnings .warn ('Failed to read symbol: {0!r}, replacing with '
335
335
'NaN.' .format (sym ), SymbolWarning )
@@ -351,20 +351,20 @@ def _dl_mult_symbols(symbols, start, end, chunksize, retry_count, pause,
351
351
_source_functions = {'google' : _get_hist_google , 'yahoo' : _get_hist_yahoo }
352
352
353
353
354
- def _get_data_from (symbols , start , end , retry_count , pause , adjust_price ,
354
+ def _get_data_from (symbols , start , end , interval , retry_count , pause , adjust_price ,
355
355
ret_index , chunksize , source ):
356
356
357
357
src_fn = _source_functions [source ]
358
358
359
359
# If a single symbol, (e.g., 'GOOG')
360
360
if isinstance (symbols , (compat .string_types , int )):
361
- hist_data = src_fn (symbols , start , end , retry_count , pause )
361
+ hist_data = src_fn (symbols , start , end , interval , retry_count , pause )
362
362
# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
363
363
elif isinstance (symbols , DataFrame ):
364
- hist_data = _dl_mult_symbols (symbols .index , start , end , chunksize ,
364
+ hist_data = _dl_mult_symbols (symbols .index , start , end , interval , chunksize ,
365
365
retry_count , pause , src_fn )
366
366
else :
367
- hist_data = _dl_mult_symbols (symbols , start , end , chunksize ,
367
+ hist_data = _dl_mult_symbols (symbols , start , end , interval , chunksize ,
368
368
retry_count , pause , src_fn )
369
369
if source .lower () == 'yahoo' :
370
370
if ret_index :
@@ -377,7 +377,7 @@ def _get_data_from(symbols, start, end, retry_count, pause, adjust_price,
377
377
378
378
def get_data_yahoo (symbols = None , start = None , end = None , retry_count = 3 ,
379
379
pause = 0.001 , adjust_price = False , ret_index = False ,
380
- chunksize = 25 ):
380
+ chunksize = 25 , interval = 'd' ):
381
381
"""
382
382
Returns DataFrame/Panel of historical stock prices from symbols, over date
383
383
range, start to end. To avoid being penalized by Yahoo! Finance servers,
@@ -406,12 +406,17 @@ def get_data_yahoo(symbols=None, start=None, end=None, retry_count=3,
406
406
If True, includes a simple return index 'Ret_Index' in hist_data.
407
407
chunksize : int, default 25
408
408
Number of symbols to download consecutively before intiating pause.
409
+ interval : string, default 'd'
410
+ Time interval code, valid values are 'd' for daily, 'w' for weekly,
411
+ 'm' for monthly and 'v' for dividend.
409
412
410
413
Returns
411
414
-------
412
415
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
413
416
"""
414
- return _get_data_from (symbols , start , end , retry_count , pause ,
417
+ if interval not in ['d' , 'w' , 'm' , 'v' ]:
418
+ raise ValueError ("Invalid interval: valid values are 'd', 'w', 'm' and 'v'" )
419
+ return _get_data_from (symbols , start , end , interval , retry_count , pause ,
415
420
adjust_price , ret_index , chunksize , 'yahoo' )
416
421
417
422
@@ -445,7 +450,7 @@ def get_data_google(symbols=None, start=None, end=None, retry_count=3,
445
450
-------
446
451
hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
447
452
"""
448
- return _get_data_from (symbols , start , end , retry_count , pause ,
453
+ return _get_data_from (symbols , start , end , None , retry_count , pause ,
449
454
adjust_price , ret_index , chunksize , 'google' )
450
455
451
456
0 commit comments