Python 如何通过熊猫和雅虎金融获得“USDJPY”(货币汇率)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21483959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:57:09  来源:igfitidea点击:

How can get ' USDJPY'(currency rates) with pandas and yahoo finance?

pythoniospandascurrencyyahoo-finance

提问by user3257837

I am learning and using the pandas and python.

我正在学习和使用熊猫和蟒蛇。

Today, I am trying to make a fx rate table, but I got a trouble with getting the pricess of 'USDJPY'.

今天,我正在尝试制作外汇汇率表,但在获取“USDJPY”的价格时遇到了麻烦。

When I get a prices of 'EUR/USD', i code like this.

当我得到“欧元/美元”的价格时,我会这样编码。

eur = web.DataReader('EURUSD=X','yahoo')['Adj Close']

it works.

有用。

But when I wrote

但是当我写

jpy = web.DataReader('USDJPY=X','yahoo')['Adj Close']

the error message comes like this:

错误消息是这样的:

--------------------------------------------------------------------------- IOError Traceback (most recent call last) in () ----> 1 jpy = web.DataReader('USDJPY=X','yahoo')['Adj Close']

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in DataReader(name, data_source, start, end, retry_count, pause) 70 return get_data_yahoo(symbols=name, start=start, end=end, 71 adjust_price=False, chunksize=25, ---> 72 retry_count=retry_count, pause=pause) 73 elif data_source == "google": 74 return get_data_google(symbols=name, start=start, end=end,

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in get_data_yahoo(symbols, start, end, retry_count, pause, adjust_price, ret_index, chunksize, name) 388 """ 389 return _get_data_from(symbols, start, end, retry_count, pause, --> 390 adjust_price, ret_index, chunksize, 'yahoo', name) 391 392

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in _get_data_from(symbols, start, end, retry_count, pause, adjust_price, ret_index, chunksize, source, name) 334 # If a single symbol, (e.g., 'GOOG') 335 if isinstance(symbols, (basestring, int)): --> 336 hist_data = src_fn(symbols, start, end, retry_count, pause) 337 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT']) 338 elif isinstance(symbols, DataFrame):

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in _get_hist_yahoo(sym, start, end, retry_count, pause) 188 '&g=d' + 189 '&ignore=.csv') --> 190 return _retry_read_url(url, retry_count, pause, 'Yahoo!') 191 192

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in _retry_read_url(url, retry_count, pause, name) 167 168 raise IOError("after %d tries, %s did not " --> 169 "return a 200 for url %r" % (retry_count, name, url)) 170 171

IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.yahoo.com/table.csv?s=USDJPY=X&a=0&b=1&c=2010&d=1&e=1&f=2014&g=d&ignore=.csv'

-------------------------------------------------- ------------------------- IOError Traceback (最近调用 last) in () ----> 1 jpy = web.DataReader('USDJPY =X','yahoo')['调整关闭']

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in DataReader(name, data_source, start, end, retry_count, pause) 70 return get_data_yahoo(symbols=name, start=start, end=end, 71 adjust_price=False, chunksize=25, ---> 72 retry_count=retry_count, pause=pause) 73 elif data_source == "google": 74 return get_data_google(symbols=name, start=start, end=end,

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in get_data_yahoo(symbols, start, end, retry_count, pause, adjust_price, ret_index, chunksize, name) 388 """ 389 return _get_data_from(symbols, start , 结束, retry_count, 暂停, --> 390 adjust_price, ret_index, chunksize, 'yahoo', name) 391 392

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in _get_data_from(symbols, start, end, retry_count, pause, adjust_price, ret_index, chunksize, source, name) 334 #如果是单个符号,(例如, 'GOOG') 335 if isinstance(symbols, (basestring, int)): --> 336 hist_data = src_fn(symbols, start, end, retry_count, pause) 337 # 或者多个符号, (eg, ['GOOG', 'AAPL', 'MSFT']) 338 elif isinstance(symbols, DataFrame):

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in _get_hist_yahoo(sym, start, end, retry_count, pause) 188 '&g=d' + 189 '&ignore=.csv') --> 190 return _retry_read_url(url, retry_count, pause, 'Yahoo!') 191 192

C:\Anaconda\lib\site-packages\pandas\io\data.pyc in _retry_read_url(url, retry_count, pause, name) 167 168 引发 IOError("在 %d 次尝试后,%s 没有" --> 169 "为 url %r" % (retry_count, name, url)) 170 171 返回 200

IOError:尝试 3 次后,Yahoo! 没有为 url 返回 200 ' http://ichart.yahoo.com/table.csv?s=USDJPY=X&a=0&b=1&c=2010&d=1&e=1&f=2014&g=d&ignore=.csv'

Other currencies like 'GBPUSD' also have same problem.

像“GBPUSD”这样的其他货币也有同样的问题。

Can you solve this problem?

你能解决这个问题吗?

Do you have any idea of getting 'USDJPY' from yahoo or google???

你知道从雅虎或谷歌获得“USDJPY”吗???

采纳答案by AP228

Yahoo Finance doesn't provide historical data on exchange rates (i.e. there's no "Historical Prices" link in the top left of the page like there would be for stocks, indices, etc...)

雅虎财经不提供汇率的历史数据(即页面左上角没有“历史价格”链接,就像股票、指数等......)

You can use FRED (Federal Reserve of St. Louis data) to get these exchange rates...

您可以使用 FRED(圣路易斯联邦储备银行数据)来获得这些汇率...

import pandas.io.data as web

jpy = web.DataReader('DEXJPUS', 'fred')

UPDATE: hase moved the pandas-datareader

更新:hase 移动了 pandas-datareader

from pandas_datareader import data
jpy = data.DataReader('DEXJPUS', 'fred')

or the more direct way...

或者更直接的方式...

jpy = web.get_data_fred('DEXJPUS')

A list of all of the exchange rate that FRED has daily data for can be found here: http://research.stlouisfed.org/fred2/categories/94

可以在此处找到 FRED 每日数据的所有汇率列表:http: //research.stlouisfed.org/fred2/categories/94

回答by Anthony FJ Garner

Yahoo Finance doesn't provide historical data on exchange rates

雅虎财经不提供汇率的历史数据

Yes it does but not on cross rates. All vs the USD List of Yahoo USD Exchange Rates

是的,但不是交叉汇率。全部 vs雅虎美元汇率的美元 列表

a = web.DataReader("JPY=X", 'yahoo')
a = web.DataReader("JPY=X", 'yahoo')

回答by Atti

Get the historical exchange rates from OANDA http://pandas-datareader.readthedocs.io/en/latest/remote_data.html

从 OANDA 获取历史汇率 http://pandas-datareader.readthedocs.io/en/latest/remote_data.html

In [1]: from pandas_datareader.oanda import get_oanda_currency_historical_rates
In [2]: start, end = "2016-01-01", "2016-06-01"
In [3]: quote_currency = "USD"
In [4]: base_currency = ["EUR", "GBP", "JPY"]
In [5]: df_rates = get_oanda_currency_historical_rates(
            start, end,
            quote_currency=quote_currency,
            base_currency=base_currency
        )
In [6]: print(df_rates)

Update: Oanda started charging for this lately https://www.oanda.com/fx-for-business/exchange-rates-api

更新:Oanda 最近开始为此收费 https://www.oanda.com/fx-for-business/exchange-rates-api