雅虎在 Matlab 中获取货币?

发布于 2024-11-23 20:21:34 字数 777 浏览 5 评论 0原文

有谁知道如何获取适用于从 Yahoo< 获取货币对的日期范围/a>?下面的代码可以很好地捕获所需的最新汇率吗?我正在寻找一系列日期的相同信息的完整时间序列或矩阵。我尝试使用 Mathworks.com 中的示例,但出现下面显示的错误。这段代码工作正常:

Connect = yahoo;
k = {'USDJPY=X' 'USDEUR=X' 'USDCAD=X' 'USDGBP=X'};
data = fetch(Connect, k)

如果

USDJPY=X = USD to JPY
USDEUR=X = USD to EUR
etc...

我执行一系列日期,我会收到此错误:

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-11-30 20:21:34

首先,如果你仔细阅读文档:

注意一次检索多个证券的历史数据
雅虎不支持。您只能获取历史数据
一次只有一个安全性。

所以你必须一次指定一个......

至于另一部分,这是我尝试过的一个例子:

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))
];

我得到:

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

但是对于相反的转换'USDEUR=X',你会得到错误:

无法返回给定安全性的历史数据。

通过单步执行代码,用于获取数据的 URL 为:

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

将其粘贴到您最喜欢的浏览器中,您将获得一个包含预期数据的 CSV 文件。如果您将其从 EURUSD 更改为 USDEUR,您会收到 404 错误:抱歉,找不到您请求的页面。

我不确定这些代码是否正确,但我尝试过:JPY=XCAD=XEUR=X、< code>GBP=X,并且它们都返回有效结果...

附带说明:我做了一个快速研究,据我所知,MATLAB 函数正在使用旧的 Yahoo CSV API。有一个更新的基于 REST 的 API,用于使用返回 XML/JSON 的 YQL 访问数据,但是我没有这方面的经验。有一个 YQL 控制台...

HTH


更新(2017 年 4 月)

我刚刚在 MATLAB R2016b 中测试了上述内容,雅虎财经 API 中似乎发生了一些变化;如果基础不是美元,它不再返回历史货币汇率。换句话说,所使用的符号只能采用 ???=X 形式(其中 ??? 是 3 个字母的代码):

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

请求 EURUSD =XUSDEUR=X 仅在您未指定日期或日期范围时才有效:

data = fetch(conn, 'EURUSD=X')
data = fetch(conn, 'USDEUR=X')

为了确认,我尝试直接使用 YQL 控制台和 查询像这样:

SELECT * 
FROM 
    yahoo.finance.historicaldata 
WHERE 
    symbol = "EUR=X" 
AND 
    startDate = "2017-01-01" 
AND 
    endDate = "2017-04-11"

具有相似的结果。如果您将符号更改为 USDEUR=X,结果中会出现“404 Not Found”错误

(顺便说一句,YQL 查询使用下面相同的 CSV 端点。如果您有兴趣,请在此处是 一些 URL 中参数含义的线索)。

如果您正在查找支持的货币符号列表,请参阅此 API 称呼:

http://finance.yahoo.com/webservice/v1/symbols /所有货币/报价

就其价值而言,雅虎财经 API 似乎从来就没有打算用作公共服务!引用这个答案

缺少文档的原因是我们没有 Finance API。似乎有些人对用于提取财务数据的 API 进行了逆向工程,但他们这样做违反了我们的服务条款(不重新分发财务数据),因此我建议您避免使用这些网络服务。

所以不能保证它将来不会损坏......

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

静谧 2024-11-30 20:21:34

YQL 只是在后台/作为基础使用“较旧的”CSV API,因为 YQL 是 CSV API 的容器。区别仅在于请求类型(REST 或 YQL)。结果是一样的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文