Yahoo fetching currency in Matlab?

萝らか妹 提交于 2019-12-04 17:09:34
Amro

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

Maas

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!