简体   繁体   English

如何通过 pandas 和 yahoo finance 获取“USDJPY”(货币汇率)?

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

I am learning and using the pandas and python.我正在学习和使用 pandas 和 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/USD”的价格时,我会这样编码。

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'] ---------------------------------------------- ---------------------- IOError Traceback(最近调用最后一次)在 () ----> 1 jpy = web.DataReader('USDJPY =X','雅虎')['调整关闭']

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 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_yahoo(symbols, start, end, retry_count, pause, adjust_price, ret_index, chunksize, name) 388 """ 389 return _get_data_from(symbols, 开始, 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, (eg, 'GOOG') 335 if isinstance(symbols, (basestring, int)): --> 336 hist_data = src_fn(symbols, start, end, retry_count, pause) 337 # Or multiple symbols, (eg, ['GOOG', 'AAPL', 'MSFT']) 338 elif isinstance(symbols, DataFrame): 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 # 或者多个符号,(例如,['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 _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 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 "为 url %r" % (retry_count, name, url)) 170 171 返回 200

IOError: after 3 tries, Yahoo! IOError:尝试 3 次后,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 '没有为 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”吗???

Yahoo Finance doesn't provide historical data on exchange rates (ie 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

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')

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

The free and easy way is Yahoo:免费和简单的方法是雅虎:

# get fx rates
# https://finance.yahoo.com/currencies
# example EUR/USD = EURUSD%3DX?p=EURUSD%3DX
import pandas as pd
import pandas_datareader as dr
    
# change date range here
start_date = '2021-02-26'
end_date = '2021-03-01'
    
# retrieve market data of current ticker symbol
print('This is the table with HLOC, Volume, Adj Close prices')
eurusd = dr.data.DataReader('EURUSD%3DX', data_source='yahoo', start=start_date, end=end_date)
print(eurusd)
    
# just get latest adjusted close for further use
print('This is the Adj Close prices only')
print(eurusd['Adj Close'])

and it also works with other crosses, contrary to the above statements:它也适用于其他十字架,与上述陈述相反:

# EURCHF%3DX
eurchf = dr.data.DataReader('EURCHF%3DX', data_source='yahoo', start=start_date, end=end_date)
print(eurchf)
#!pip install yfinance
#!pip install mplfinance

from datetime import datetime
import yfinance as yf
import mplfinance as mpf

#import pandas as pd
#import pandas_datareader as dr

# change date range here

start_date = '2021-02-26'
end_date = '2021-03-01'



#This Does NOT WORK#   
# retrieve market data of current ticker symbol

print('This is the table with HLOC, Volume, Adj Close prices')
eurusd = dr.data.DataReader('EURUSD%3DX', data_source='yahoo', 
start=start_date, end=end_date)
print(eurusd)


#This Does#


data = yf.download('USDCAD=X', start=start_date, end=end_date)

#If someone can figure out how to get the S5,S30, M1, M3 etc.  Please share

I think you can use custom intervals by passing it as an argument to the yf.download() function. For example:我认为您可以通过将自定义间隔作为参数传递给yf.download() function 来使用自定义间隔。例如:

data = yf.download('USDCAD=X', start=start_date, end=end_date, interval='1m')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM