フィルターのクリア

Huge problem when I import data from Yahoo

4 ビュー (過去 30 日間)
Martin
Martin 2013 年 9 月 2 日
Dear guys,
I Have a problem when I import data from Yahoo (e.g. stock data). This is quite frustrating.
For the non-stock guys: a ticker is a code (a name) for a stock. And C = yahoo, tells Matlab to import data from Yahoo...
Let me proceed: I use the following code to import stock data for three stocks:
C = yahoo
ticker = { 'GOOG' 'AA' 'AXP' } % ticker codes of stocks
for i = 1:3
Price.(ticker{i}) = fetch(C, ticker(i), 'Adj Close', 'Jan 5 10', 'Aug 1 13', 'm'); % 'steal' the individual stock data (all other ' ' information is just the time-series lenght and other finance stuff).
temp = Price.(ticker{i}); % take the individual stock price
ClosePrice(:,i) = temp(:,2); % and collect it in one matrix (only use second column because we do not need the first one)
end
This works great and provide me with great data. However, my problem arrives when I use another ticker code from Yahoo (which should be perfectly legal and fine)...
For example if I instead use
ticker = { 'DANSKE.CO' 'AA' 'AXP' },
in the same code, I will get the following error:
"Invalid field name: 'DANSKE.CO'."
It looks like it does not like the 'dot' in the ticker code. But 'DANSKE.CO' should be a perfectly legal ticker at Yahoo, and nevertheless: it also works with the Matlab 'dftool' command (another way to import data from Yahoo)...
Any suggestions?
hope to hear from someone, - thanks in advance.
All the best
Mergh

採用された回答

Cedric
Cedric 2013 年 9 月 3 日
You are right about the cause. Do you really need to have ticker codes used as field names? Why not using a more basic cell array, and work with ticker indices in the cell array of tickers, rather than with their codes? For example:
for k = 1:3
Price{k} = fetch(C, ticker(k), 'Adj Close', 'Jan 5 10', 'Aug 1 13', 'm');
temp = Price{k};
ClosePrice(:,k) = temp(:,2);
end
(not that I use k as a loop index instead of i, as i and j should be reserved for complex numbers)
  2 件のコメント
Martin
Martin 2013 年 9 月 3 日
Hi, how should that solve the problem? Somehow I need the ticker names inside the fetch..
Cedric
Cedric 2013 年 9 月 4 日
編集済み: Cedric 2013 年 9 月 4 日
It solves the problem because you don't use invalid field names (with a '.') but numeric indices instead. In situations where it appears that we should use strings as indices, it is almost always better to build a cell array of these strings (so each string has a numeric index) and to index the rest using these numeric indices. Let me know if you want/need an example.

サインインしてコメントする。

その他の回答 (1 件)

Shashank Prasanna
Shashank Prasanna 2013 年 9 月 3 日
編集済み: Shashank Prasanna 2013 年 9 月 3 日
Martin, the problem is not with Yahoo but with the name provided to a struct dynamic field name.
Price.(ticker{i})
for DANSKE.CO resolves to:
Price.DANSKE.CO
which is not what you intended I suppose. As Cedric recommended, you could either use a simple cell array Price{k}, or generate a different name convention that does not have a dot for the struct filed names.
  2 件のコメント
Martin
Martin 2013 年 9 月 3 日
It works (I was a little retard), great, thanks to both of you
Cedric
Cedric 2013 年 9 月 4 日
A 3rd solution would be to use a hash table like java.util.Hashtable or a MATLAB map container.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by