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

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'

Other currencies like 'GBPUSD' also have same problem.

Can you solve this problem?

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

有帮助吗?

解决方案

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...

import pandas.io.data as web

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

UPDATE: hase moved the 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

其他提示

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

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

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)

Get the historical exchange rates from 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

#!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:

data = yf.download('USDCAD=X', start=start_date, end=end_date, interval='1m')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top