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

6

7

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

Brogdon answered 31/1, 2014 at 15:39 Comment(0)
S
15

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

Stereophotography answered 31/1, 2014 at 17:26 Comment(5)
It works! Really thanks for your Answer. Tables in FRED prices are looks weekly updates. Do you have any idea of getting prices quickly? not wait until weekends.Brogdon
Yes there are other sources. Quandl is a really useful resource. Quandl has data available from over 400+ sources and keeps them in one easy-to-search location. Quandl also has a Python package, and here is where Quandl's FX data is.Stereophotography
The quandl data link is dead. This is the quandl data I found quandl.com/data/CUR/EUR-Currency-Exchange-Rates-USD-EUR , however the free data stops 6 months ago... so isn't particularly useful. Up to data data is pretty pricey.Diminutive
@Stereophotography can you provide an explanation on how to use this API in R?Parasympathetic
Pandas IO data was removed, wasn't it?Multinuclear
C
10

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')
Carlyle answered 18/5, 2016 at 14:6 Comment(2)
as well as AZN.Curl
Currency = ["LSL=X","DKK=X","MKD=X","PGK=X","VEF=X"]Curl
A
2

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)
Arlyn answered 1/3, 2021 at 10:19 Comment(3)
Hi, yours worked for me, is there any way to get rates at different timeframes?Trouper
IF you have list of dates you can loop through that list, getting each dates rate successively, or if it is a range, then just change the dates in the code above.Arlyn
@Arlyn I wonder if KailiC meant is there anyway to get more granular timeframes. Ex. intra-day, hourly, etc..Tiossem
H
2
#!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
Handfasting answered 31/7, 2021 at 1:29 Comment(1)
I am trying to use this code. However, it reports that 'dr.' is not defined in the code. Which the first place where it should be insereted?Employee
J
1

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

Jaws answered 13/3, 2017 at 13:31 Comment(1)
OANDA started charging for this since this post was writtenJaws
E
1

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')
Endogen answered 28/2, 2022 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.