Does anyone know how to get a range of dates that works for fetching currency pairs from Yahoo? The code below works fine for capturing the latest rates needed? I am looking for a complete time series or matrix of the same info for a range of dates. I tried using the examples from Mathworks.com but get errors displayed below. This code works fine:
Connect = yahoo;
k = {'USDJPY=X' 'USDEUR=X' 'USDCAD=X' 'USDGBP=X'};
data = fetch(Connect, k)
where
USDJPY=X = USD to JPY
USDEUR=X = USD to EUR
etc...
If I do a range of dates, I get this error:
>> data = fetch(Connect, k, '24-Oct-2003',datestr(now))
Warning: Historical data fetch does not support multiple security input.
USDJPY=X data reurned.
> In yahoo.fetch at 310
??? Error using ==> yahoo.fetch at 363
Unable to return historical data for given security.
Thanks
First, if you carefully read the documentation:
Note Retrieving historical data for multiple securities at one time is not supported for Yahoo. You can fetch historical data for only a single security at a time.
So you have to specify one at a time...
As for the other part, here is an example I tried:
conn = yahoo;
data = fetch(conn, 'EURUSD=X', '01-Jun-2011', datestr(now,'dd-mmm-yyyy'), 'd');
close(conn)
d = [
{'date' 'open' 'high' 'low' 'close' 'volume' 'adj-close'}
cellstr(datestr(data(:,1))) num2cell(data(:,2:end))
];
I get:
>> d =
'date' 'open' 'high' 'low' 'close' 'volume' 'adj-close'
'15-Jul-2011' [1.41] [1.41] [1.41] [ 1.41] [ 0] [ 1.41]
'14-Jul-2011' [1.42] [1.42] [1.42] [ 1.42] [ 0] [ 1.42]
....
'02-Jun-2011' [1.45] [1.45] [1.45] [ 1.45] [ 0] [ 1.45]
'01-Jun-2011' [1.44] [1.44] [1.44] [ 1.44] [ 0] [ 1.44]
But for the opposite conversion 'USDEUR=X'
, you get the error:
Unable to return historical data for given security.
By stepping through the code, the URL used to fetch the data was:
http://ichart.yahoo.com/table.csv?s=EURUSD=X&a=5&b=1&c=2011&d=6&e=16&f=2011&g=d&ignore=.csv
Pasting that in your favorite browser, you will get a CSV file with the expected data. If you change it from EURUSD
to USDEUR
you get a 404 error: Sorry, the page you requested was not found.
.
I'm not sure if these are the correct codes, but I tried: JPY=X
, CAD=X
, EUR=X
, GBP=X
, and they all return valid results...
As a side note: I've done a quick research, and from what I understood, the MATLAB function is using the older Yahoo CSV API. There is a newer REST-based API for accessing the data using YQL which returns XML/JSON, but I have no experience in that. There is a YQL console you can play around with though...
HTH
Update (April 2017)
I just tested the above in MATLAB R2016b, and it seems something changed in the Yahoo Finance API; It no longer returns historical currencies exchange rates if the base is not USD. In other words, the symbol used can only be of the form ???=X
(where ???
is the 3-letter code):
% US dollar to euro
data = fetch(conn, 'EUR=X', '01-Jun-2011', datestr(now,'dd-mmm-yyyy'), 'd');
% format as table
t = array2table(data, 'VariableNames',{'date' 'open' 'high' 'low' 'close' 'volume' 'adjclose'});
t.date = cellstr(datestr(t.date));
disp(t)
Requesting EURUSD=X
or USDEUR=X
only works if you don't specify a date or a date range:
data = fetch(conn, 'EURUSD=X')
data = fetch(conn, 'USDEUR=X')
To confirm, I tried using the YQL console directly with a query like this:
SELECT *
FROM
yahoo.finance.historicaldata
WHERE
symbol = "EUR=X"
AND
startDate = "2017-01-01"
AND
endDate = "2017-04-11"
with similar results. If you change the symbol to USDEUR=X
you get "404 Not Found" errors in the results
(BTW the YQL query uses the same CSV endpoint underneath. In case you're interested, here are some clues to the meaning of the parameters in the URL).
If you're looking for the list of supported currency symbols, see this API call:
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote
For what it's worth, it appears that the Yahoo Finance API was never meant to be used as a public service! To quote this answer:
The reason for the lack of documentation is that we don't have a Finance API. It appears some have reverse engineered an API that they use to pull Finance data, but they are breaking our Terms of Service (no redistribution of Finance data) in doing this so I would encourage you to avoid using these webservices.
So no guarantees it won't break in the future...
YQL is just using the "older" CSV API in background/as base, because YQL is a container for the CSV API. The difference is only the request type (REST or YQL). The results are the same.
来源:https://stackoverflow.com/questions/6717218/yahoo-fetching-currency-in-matlab