@@ -58,6 +58,10 @@ def DataReader(name, data_source=None, start=None, end=None,
58
58
return get_data_yahoo (symbols = name , start = start , end = end ,
59
59
adjust_price = False , chunk = 25 ,
60
60
retry_count = retry_count , pause = pause )
61
+ elif (data_source == "google" ):
62
+ return get_data_google (symbols = name , start = start , end = end ,
63
+ adjust_price = False , chunk = 25 ,
64
+ retry_count = retry_count , pause = pause )
61
65
elif (data_source == "fred" ):
62
66
return get_data_fred (name = name , start = start , end = end )
63
67
elif (data_source == "famafrench" ):
@@ -132,6 +136,9 @@ def get_quote_yahoo(symbols):
132
136
return DataFrame (data , index = idx )
133
137
134
138
139
+ def get_quote_google (symbols ):
140
+ raise NotImplementedError ("Google Finance doesn't have this functionality" )
141
+
135
142
def _get_hist_yahoo (sym = None , start = None , end = None , retry_count = 3 ,
136
143
pause = 0 , ** kwargs ):
137
144
"""
@@ -178,6 +185,41 @@ def _get_hist_yahoo(sym=None, start=None, end=None, retry_count=3,
178
185
"return a 200 for url %s" % (pause , url ))
179
186
180
187
188
+ def _get_hist_google (sym = None , start = None , end = None , retry_count = 3 ,
189
+ pause = 0 , ** kwargs ):
190
+ """
191
+ Get historical data for the given name from google.
192
+ Date format is datetime
193
+
194
+ Returns a DataFrame.
195
+ """
196
+ if (sym is None ):
197
+ warnings .warn ("Need to provide a name." )
198
+ return None
199
+
200
+ start , end = _sanitize_dates (start , end )
201
+
202
+ google_URL = 'http://www.google.com/finance/historical?'
203
+
204
+ # www.google.com/finance/historical?q=GOOG&startdate=Jun+9%2C+2011&enddate=Jun+8%2C+2013&output=csv
205
+ url = google_URL + urllib .urlencode ({"q" : sym , \
206
+ "startdate" : start .strftime ('%b %d, %Y' ), \
207
+ "enddate" : end .strftime ('%b %d, %Y' ), "output" : "csv" })
208
+ for _ in range (retry_count ):
209
+ resp = urllib2 .urlopen (url )
210
+ if resp .code == 200 :
211
+ lines = resp .read ()
212
+ rs = read_csv (StringIO (bytes_to_str (lines )), index_col = 0 ,
213
+ parse_dates = True )[::- 1 ]
214
+
215
+ return rs
216
+
217
+ time .sleep (pause )
218
+
219
+ raise Exception ("after %d tries, Google did not "
220
+ "return a 200 for url %s" % (pause , url ))
221
+
222
+
181
223
def _adjust_prices (hist_data , price_list = ['Open' , 'High' , 'Low' , 'Close' ]):
182
224
"""
183
225
Return modifed DataFrame or Panel with adjusted prices based on
@@ -347,6 +389,72 @@ def dl_mult_symbols(symbols):
347
389
348
390
return hist_data
349
391
392
+ def get_data_google (symbols = None , start = None , end = None , retry_count = 3 , pause = 0 ,
393
+ chunksize = 25 , ** kwargs ):
394
+ """
395
+ Returns DataFrame/Panel of historical stock prices from symbols, over date
396
+ range, start to end. To avoid being penalized by Google Finance servers,
397
+ pauses between downloading 'chunks' of symbols can be specified.
398
+
399
+ Parameters
400
+ ----------
401
+ symbols : string, array-like object (list, tuple, Series), or DataFrame
402
+ Single stock symbol (ticker), array-like object of symbols or
403
+ DataFrame with index containing stock symbols.
404
+ start : string, (defaults to '1/1/2010')
405
+ Starting date, timestamp. Parses many different kind of date
406
+ representations (e.g., 'JAN-01-2010', '1/1/10', 'Jan, 1, 1980')
407
+ end : string, (defaults to today)
408
+ Ending date, timestamp. Same format as starting date.
409
+ retry_count : int, default 3
410
+ Number of times to retry query request.
411
+ pause : int, default 0
412
+ Time, in seconds, to pause between consecutive queries of chunks. If
413
+ single value given for symbol, represents the pause between retries.
414
+ chunksize : int, default 25
415
+ Number of symbols to download consecutively before intiating pause.
416
+
417
+ Returns
418
+ -------
419
+ hist_data : DataFrame (str) or Panel (array-like object, DataFrame)
420
+ """
421
+
422
+ def dl_mult_symbols (symbols ):
423
+ stocks = {}
424
+ for sym_group in _in_chunks (symbols , chunksize ):
425
+ for sym in sym_group :
426
+ try :
427
+ stocks [sym ] = _get_hist_google (sym , start = start ,
428
+ end = end , ** kwargs )
429
+ except :
430
+ warnings .warn ('Error with sym: ' + sym + '... skipping.' )
431
+
432
+ time .sleep (pause )
433
+
434
+ return Panel (stocks ).swapaxes ('items' , 'minor' )
435
+
436
+ if 'name' in kwargs :
437
+ warnings .warn ("Arg 'name' is deprecated, please use 'symbols' instead." ,
438
+ FutureWarning )
439
+ symbols = kwargs ['name' ]
440
+
441
+ #If a single symbol, (e.g., 'GOOG')
442
+ if isinstance (symbols , (str , int )):
443
+ sym = symbols
444
+ hist_data = _get_hist_google (sym , start = start , end = end )
445
+ #Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
446
+ elif isinstance (symbols , DataFrame ):
447
+ try :
448
+ hist_data = dl_mult_symbols (Series (symbols .index ))
449
+ except ValueError :
450
+ raise
451
+ else : #Guess a Series
452
+ try :
453
+ hist_data = dl_mult_symbols (symbols )
454
+ except TypeError :
455
+ hist_data = dl_mult_symbols (Series (symbols ))
456
+
457
+ return hist_data
350
458
351
459
def get_data_fred (name = None , start = dt .datetime (2010 , 1 , 1 ),
352
460
end = dt .datetime .today ()):
0 commit comments