Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get historical data for currency exchange rates via Yahoo Finance?

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?

like image 227
Sergey V. Avatar asked Mar 07 '15 19:03

Sergey V.


People also ask

How do you calculate old exchange rates?

To calculate the percentage discrepancy, take the difference between the two exchange rates, and divide it by the market exchange rate: 1.37 - 1.33 = 0.04/1.33 = 0.03. Multiply by 100 to get the percentage markup: 0.03 x 100 = 3%.

What is historical exchange rate?

Historical currency exchange rates are foreign exchange rates which give traders a historical reference of how a currency pair has traded in the past. Historical exchange rates help many forex traders to discern the direction of a given currency pair.


2 Answers

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

like image 93
dym Avatar answered Oct 17 '22 00:10

dym


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)
like image 37
dmdip Avatar answered Oct 17 '22 01:10

dmdip