Reputation: 1028
I need to obtain JSON or XML response with the chronology of currency exchange rates, for example, from 2015-01-07 to 2015-03-07.
With this answer we can get just the latest info on currency exchange rates for chosen currencies.
Here we can get the currency exchange rates for certain date using the URL: http://finance.yahoo.com/connection/currency-converter-cache?date=20150307 and parsing the obtained JSON for certain currency.
But I need to get currency exchange rates for the range of dates as it is here but at the JSON or XML format.
Is there a way to do that?
Upvotes: 14
Views: 34383
Reputation: 567
Alternatively, you could also use currencyapi.com. It provides historical exchange rates in JSON for over 150 currencies worldwide and is compatible with all major programming languages.
To retrieve specific exchange rates between two dates, use the following URL
https://api.currencyapi.com/v3/range?apikey={{ API_KEY }}&datetime_start=2023-01-01T00:00:00Z&datetime_end=2023-01-31T23:59:59Z
The JSON response will be:
{
"data": [
{
"datetime": "2023-01-01T23:59:59Z",
"currencies": {
"CAD": {
"code": "CAD",
"value": 1.353397
},
"EUR": {
"code": "EUR",
"value": 0.934186
}
}
},
{
"datetime": "2023-01-02T23:59:59Z",
"currencies": {
"CAD": {
"code": "CAD",
"value": 1.356151
},
"EUR": {
"code": "EUR",
"value": 0.936562
}
}
},
{
"datetime": "2023-01-03T23:59:59Z",
"currencies": {
"CAD": {
"code": "CAD",
"value": 1.367522
},
"EUR": {
"code": "EUR",
"value": 0.948182
}
}
},
{...},
{...}
]
}
To retrieve only one specific date, you can use the historical endpoint as well: https://currencyapi.com/docs/historical
Upvotes: 0
Reputation: 467
For the simplest solution, you should use CurrencyFreaks API. It provides historical exchange rates in JSON and XML formats for 179 currencies worldwide compatible with various programming languages such as Shell, Node.js, Java, JS, Python, PHP, Ruby, C, C#, Go, Swift
To get the historical rates between two dates, it provides a time-series endpoint.
$ curl 'https://api.currencyfreaks.com/timeseries?apikey=YOUR_APIKEY&start_date=2016-01-07&end_date=2016-03-07&base=EUR&symbols=EUR,USD'
The JSON response will be:
{
"start_date": "2016-01-07",
"end_date": "2016-01-10",
"base": "EUR",
"rates": {
"2016-01-07 00:00:00+00": {
"EUR": "1.0",
"USD": "1.0776"
},
"2016-01-08 00:00:00+00": {
"EUR": "1.0",
"USD": "1.0921"
},
"2016-01-09 00:00:00+00": {
"EUR": "1.0",
"USD": "1.0932"
},
"2016-01-10 00:00:00+00": {
"EUR": "1.0",
"USD": "1.0932"
}
}
}
To get the historical rates for a specific date, use the historical rates endpoint.
I hope it will useful for you.
Upvotes: 2
Reputation: 926
The Yahoo curreny API has been discontinued.
There are several alternative APIs offering currency conversion data. One option I suggest is SWOP currency exchange rate API, a fast, easy to use, reliable and transparent foreign exchange rate API made from developers for developers.
Full disclaimer: I'm one of the devs who created SWOP :)
Upvotes: 0
Reputation: 67
Yahoo API doesn't work anymore there is however other APIs that provides currency data in a JSON format. FXMarketAPI is the only API that offers a Pandas compatible API that provides historical data in a JSON format. There is a pandas endpoint which helps you pull data. Though there is a limit of 1000 request for free users. you can see an example below.
URL = "https://fxmarketapi.com/apipandas"
params = {'currency' : 'EURUSD',
'start_date' : '2018-07-02',
'end_date':'2018-12-06',
'api_key':'**************'}
response = requests.get("https://fxmarketapi.com/apipandas", params=params)
df= pd.read_json(response.text)
remember to get access to api_key and add to the above request.
Upvotes: 0
Reputation: 1795
Here's a solution to get your data into a pandas DataFrame. You can then export from the DataFrame to JSON, XML etc. using functions like pandas.DataFrame.to_json.
The symbols also may be different from the ones you are using.
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime
start = datetime(2016, 1, 1)
end = datetime(2017, 3, 31)
aud = web.DataReader('AUD=X', 'yahoo', start, end)
In [29]: aud.head(5)
Out[29]:
Open High Low Close Volume Adj Close
Date
2016-01-01 1.3752 1.3752 1.3752 1.3752 0 1.3752
2016-01-04 1.3725 1.3950 1.3712 1.3723 0 1.3723
2016-01-05 1.3921 1.4017 1.3857 1.3916 0 1.3916
2016-01-06 1.3963 1.4168 1.3941 1.3961 0 1.3961
2016-01-07 1.4124 1.4322 1.4109 1.4124 0 1.4124
You will need to install pandas-datareader. (I am assuming you already have pandas).
sudo -H pip install pandas-datareader (ubuntu)
pip install pandas-datareader (windows)
Upvotes: 2
Reputation: 184
Use YQL (https://developer.yahoo.com/yql/)
Then you can retrieve the data you need with a query like this:
SELECT *
FROM
yahoo.finance.historicaldata
WHERE
symbol = "EUR=X"
AND
startDate = "2009-09-11"
AND
endDate = "2010-03-10"
Upvotes: 13