@@ -107,6 +107,9 @@ def _in_chunks(seq, size):
107
107
'time' : 't1' , 'short_ratio' : 's7' }
108
108
109
109
110
+ _YAHOO_QUOTE_URL = 'http://finance.yahoo.com/d/quotes.csv?'
111
+
112
+
110
113
def get_quote_yahoo (symbols ):
111
114
"""
112
115
Get current yahoo quote
@@ -124,8 +127,7 @@ def get_quote_yahoo(symbols):
124
127
125
128
data = defaultdict (list )
126
129
127
- url_str = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (sym_list ,
128
- request )
130
+ url_str = _YAHOO_QUOTE_URL + 's=%s&f=%s' % (sym_list , request )
129
131
130
132
with urlopen (url_str ) as url :
131
133
lines = url .readlines ()
@@ -175,6 +177,9 @@ def _retry_read_url(url, retry_count, pause, name):
175
177
"return a 200 for url %r" % (retry_count , name , url ))
176
178
177
179
180
+ _HISTORICAL_YAHOO_URL = 'http://ichart.finance.yahoo.com/table.csv?'
181
+
182
+
178
183
def _get_hist_yahoo (sym , start , end , retry_count , pause ):
179
184
"""
180
185
Get historical data for the given name from yahoo.
@@ -183,8 +188,7 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
183
188
Returns a DataFrame.
184
189
"""
185
190
start , end = _sanitize_dates (start , end )
186
- yahoo_url = 'http://ichart.finance.yahoo.com/table.csv?'
187
- url = (yahoo_url + 's=%s' % sym +
191
+ url = (_HISTORICAL_YAHOO_URL + 's=%s' % sym +
188
192
'&a=%s' % (start .month - 1 ) +
189
193
'&b=%s' % start .day +
190
194
'&c=%s' % start .year +
@@ -196,6 +200,9 @@ def _get_hist_yahoo(sym, start, end, retry_count, pause):
196
200
return _retry_read_url (url , retry_count , pause , 'Yahoo!' )
197
201
198
202
203
+ _HISTORICAL_GOOGLE_URL = 'http://www.google.com/finance/historical?'
204
+
205
+
199
206
def _get_hist_google (sym , start , end , retry_count , pause ):
200
207
"""
201
208
Get historical data for the given name from google.
@@ -204,13 +211,13 @@ def _get_hist_google(sym, start, end, retry_count, pause):
204
211
Returns a DataFrame.
205
212
"""
206
213
start , end = _sanitize_dates (start , end )
207
- google_URL = 'http://www.google.com/finance/historical?'
208
214
209
215
# www.google.com/finance/historical?q=GOOG&startdate=Jun+9%2C+2011&enddate=Jun+8%2C+2013&output=csv
210
- url = google_URL + urlencode ({"q" : sym ,
211
- "startdate" : start .strftime ('%b %d, ' '%Y' ),
212
- "enddate" : end .strftime ('%b %d, %Y' ),
213
- "output" : "csv" })
216
+ url = "%s%s" % (_HISTORICAL_GOOGLE_URL ,
217
+ urlencode ({"q" : sym ,
218
+ "startdate" : start .strftime ('%b %d, ' '%Y' ),
219
+ "enddate" : end .strftime ('%b %d, %Y' ),
220
+ "output" : "csv" }))
214
221
return _retry_read_url (url , retry_count , pause , 'Google' )
215
222
216
223
@@ -251,6 +258,9 @@ def _calc_return_index(price_df):
251
258
return df
252
259
253
260
261
+ _YAHOO_COMPONENTS_URL = 'http://download.finance.yahoo.com/d/quotes.csv?'
262
+
263
+
254
264
def get_components_yahoo (idx_sym ):
255
265
"""
256
266
Returns DataFrame containing list of component information for
@@ -275,8 +285,7 @@ def get_components_yahoo(idx_sym):
275
285
stats = 'snx'
276
286
# URL of form:
277
287
# http://download.finance.yahoo.com/d/quotes.csv?s=@%5EIXIC&f=snxl1d1t1c1ohgv
278
- url = ('http://download.finance.yahoo.com/d/quotes.csv?s={0}&f={1}'
279
- '&e=.csv&h={2}' )
288
+ url = _YAHOO_COMPONENTS_URL + 's={0}&f={1}&e=.csv&h={2}'
280
289
281
290
idx_mod = idx_sym .replace ('^' , '@%5E' )
282
291
url_str = url .format (idx_mod , stats , 1 )
@@ -430,6 +439,9 @@ def get_data_google(symbols=None, start=None, end=None, retry_count=3,
430
439
adjust_price , ret_index , chunksize , 'google' , name )
431
440
432
441
442
+ _FRED_URL = "http://research.stlouisfed.org/fred2/series/"
443
+
444
+
433
445
def get_data_fred (name , start = dt .datetime (2010 , 1 , 1 ),
434
446
end = dt .datetime .today ()):
435
447
"""
@@ -443,14 +455,12 @@ def get_data_fred(name, start=dt.datetime(2010, 1, 1),
443
455
"""
444
456
start , end = _sanitize_dates (start , end )
445
457
446
- fred_URL = "http://research.stlouisfed.org/fred2/series/"
447
-
448
458
if not is_list_like (name ):
449
459
names = [name ]
450
460
else :
451
461
names = name
452
462
453
- urls = [fred_URL + '%s' % n + '/downloaddata/%s' % n + '.csv' for
463
+ urls = [_FRED_URL + '%s' % n + '/downloaddata/%s' % n + '.csv' for
454
464
n in names ]
455
465
456
466
def fetch_data (url , name ):
@@ -470,11 +480,12 @@ def fetch_data(url, name):
470
480
return df
471
481
472
482
483
+ _FAMAFRENCH_URL = 'http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp'
484
+
485
+
473
486
def get_data_famafrench (name ):
474
487
# path of zip files
475
- zip_file_url = ('http://mba.tuck.dartmouth.edu/pages/faculty/'
476
- 'ken.french/ftp' )
477
- zip_file_path = '{0}/{1}.zip' .format (zip_file_url , name )
488
+ zip_file_path = '{0}/{1}.zip' .format (_FAMAFRENCH_URL , name )
478
489
479
490
with urlopen (zip_file_path ) as url :
480
491
raw = url .read ()
@@ -618,10 +629,12 @@ def get_options_data(self, month=None, year=None, expiry=None):
618
629
return [f (month , year , expiry ) for f in (self .get_put_data ,
619
630
self .get_call_data )]
620
631
632
+ _OPTIONS_BASE_URL = 'http://finance.yahoo.com/q/op?s={sym}'
633
+
621
634
def _get_option_data (self , month , year , expiry , table_loc , name ):
622
635
year , month = self ._try_parse_dates (year , month , expiry )
623
636
624
- url = 'http://finance.yahoo.com/q/op?s={sym}' .format (sym = self .symbol )
637
+ url = self . _OPTIONS_BASE_URL .format (sym = self .symbol )
625
638
626
639
if month and year : # try to get specified month from yahoo finance
627
640
m1 , m2 = _two_char_month (month ), month
0 commit comments