14

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?

Community
  • 1
  • 1
Sergey V.
  • 811
  • 2
  • 10
  • 21

5 Answers5

13

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"

Check here

Iharob Al Asimi
  • 51,091
  • 5
  • 53
  • 91
dym
  • 184
  • 2
  • 3
  • this seems to work for stock-quotes only, and not for currencies – Asain Kujovic Sep 02 '15 at 20:41
  • 3
    This works also for currency as you can see clicking on the link i've posted in the answer – dym Sep 02 '15 at 22:08
  • my mistake, yesterday i received only null arrays. GRATE ANSWER, and best way for get historical data. – Asain Kujovic Sep 03 '15 at 17:49
  • how about asking for EURCHF=X, just putting this as symbol gives a null output ... am I missing anything basic? – Rho Phi Aug 13 '16 at 16:52
  • @RobertoFranceschini from what I know, the api should return just the comparison between one currency respect USD. So if you want to compare EUR with CHF you have to do by yourself. Use EUR=X to get EUR/USD and CHF=X to get CHF/USD, then divide them. – dym Aug 13 '16 at 17:10
  • thanks for this suggestion. This is what I have been doing and I checked sometimes it matches with the official quote of the day from yahoo up to 4 digits. Still, I was never sure this is how things are supposed to be done. I mean it's probably ok for the "Closure" prices, but I suspect that on instantaneous data (Forex?) this might be not accurate (I know nothing about how currency market works!) – Rho Phi Aug 14 '16 at 20:00
  • @RobertoFranceschini there are tons of professional apis for realtime quote. The yahoo api isn't the best choise if you want instantaneous data because the data is updated every x time.(I don't remember exactly how much). Btw the query I've posted is for historical data, if you just need the last quote you should use another table (yahoo.finance.quote). – dym Aug 17 '16 at 11:01
  • this does not work anymore. The YQL table is not available! – sscarduzio Sep 29 '16 at 14:15
  • @sscarduzio it works, just activate "Show Community Tables" on the left top corner or simply follow the link "Check here" in the answer – dym Sep 29 '16 at 14:29
  • @dym which one of the value do you divide by to get EUR/CHF? I need to create a chart from the historical price, (to give an overall view of a currency pairs performance over a given period of time). I'm not sure which one of value I should use for this - bid, close, open, low or high? – Jamie White Feb 16 '17 at 06:35
  • @JamieWhite - bid is the current quote, close/open the quotation when the market has closed/opened (this not make sense for currency since there isn't a "market" that close/open), high/low the best/worst quotation of the day. All the terms derive from the stock market, so many of them are not really useful for the currency market. – dym Mar 07 '17 at 21:13
  • @dym Not clear what you're suggesting, shall i use the 'Adjusted Closing Price' (bid is not available for historical data)? – Jamie White Mar 08 '17 at 11:16
  • 1
    This API has been closed, I think. And given links doesn't work. Can you suggest an alternative to YQL? – Usman Liaqat Oct 08 '20 at 06:31
2

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)
dmdip
  • 1,227
  • 10
  • 11
  • Have connection error with chart.finance.yahoo.com. Maybe this api is no longer available and is replaced by YQL. – user39086 Jun 22 '17 at 17:02
1

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.

Rameez
  • 357
  • 11
0

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.

jimmy rickard
  • 51
  • 1
  • 1
0

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

  • The SWOP API offers current and historical rates for 180+ currencies. They are gathered directly from trusted sources (various Central Banks and other important banks).
  • The SWOP API has two endpoints, GraphQL and REST/JSON, for developer convenience.
  • There's a free plan allowing 1,000 requests per month, paid plans for more requests and features.
Luzian
  • 617
  • 1
  • 7
  • 6