雅虎在Matlab中获取货币? [英] Yahoo fetching currency in Matlab?

查看:105
本文介绍了雅虎在Matlab中获取货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何获得一个日期范围,该日期范围适用于从雅虎?以下代码可以很好地捕获所需的最新费率?我正在寻找日期范围的完整时间序列或相同信息的矩阵.我尝试使用Mathworks.com提供的示例,但显示以下错误.这段代码可以正常工作:

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)

其中

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.

谢谢

推荐答案

首先,如果您仔细阅读了文档,则:

First, if you carefully read the documentation:

注意一次检索多个证券的历史数据 不支持Yahoo.您只能获取历史数据 一次提供单一安全性.

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

我得到:

>> 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',您会收到错误消息:

But for the opposite conversion 'USDEUR=X', you get the error:

对于给定的安全性,无法返回历史数据.

Unable to return historical data for given security.

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

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

将其粘贴到您喜欢的浏览器中,您将获得一个包含预期数据的CSV文件.如果将其从EURUSD更改为USDEUR,则会出现404错误:Sorry, the page you requested was not found..

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

我不确定这些代码是否正确,但是我尝试了:JPY=XCAD=XEUR=XGBP=X,它们都返回有效结果...

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

请注意:我已经进行了快速研究,据我了解,MATLAB函数正在使用较旧的 YQL 来访问数据,该API返回XML/JSON,但是我没有这方面的经验.有一个

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

我刚刚在MATLAB R2016b中测试了上述内容,并且Yahoo Finance API似乎有所改变;如果基础不是美元,它将不再返回历史货币汇率.换句话说,所使用的符号只能是???=X的形式(其中???是3个字母的代码):

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)

请求EURUSD=XUSDEUR=X仅在未指定日期或日期范围的情况下有效:

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

为了确认,我尝试将YQL控制台直接用于

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"

具有相似的结果.如果将符号更改为USDEUR=X,结果中将出现"404 Not Found"错误

with similar results. If you change the symbol to USDEUR=X you get "404 Not Found" errors in the results

(顺便说一句,YQL查询在下面使用相同的CSV端点.如果您有兴趣,请访问如果要查找受支持的货币符号列表,请参见以下API调用:

If you're looking for the list of supported currency symbols, see this API call:

http://finance.yahoo.com/webservice/v1/symbols /allcurrencies/quote

就其价值而言,似乎Yahoo Finance API从未被用作公共服务!引用此答案:

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:

缺少文档的原因是我们没有财务API .似乎有些人对它们用来提取财务数据的API进行了反向工程,但是这样做违反了我们的服务条款(不重新分发财务数据),因此,我鼓励您避免使用这些Web服务.

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

这篇关于雅虎在Matlab中获取货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆