我有证券交易记录的清单。我想下载收盘价并将其存储到变量中。我写了这个脚本:
function y=fetchDataFromYahoo()
ticker={'ABFS','TCB','NE','FGP'};%,'IMO','CAJ','CAG','GMCR','HSH','HAIN','SIM'};
c=yahoo;
for i=1:4
Price.(ticker{i})=fetch(c,ticker(i),'Adj Close','Jan 1 00','Apr 19 13','d');
temp=Price.(ticker{i});
ClosePrice(:,i)=temp(:,2);
end
y=ClosePrice;
end
当我在数组中有3个证券时,它可以工作,但当数组中有3个以上的证券时,它会抛出错误。错误消息如下:
Subscripted assignment dimension mismatch.
Error in fetchDataFromYahoo (line 7)
ClosePrice(:,i)=temp(:,2);
你能帮我解决这个问题吗?
发布于 2013-04-19 22:31:10
您假设所有序列的长度都相同。只需将其保存在一个结构中:
fetchDataFromYahoo({'ABFS','TCB','NE','FGP'})
其中我将您的函数修改为:
function Price = fetchDataFromYahoo(ticker)
c = yahoo;
for i = 1:numel(ticker)
Price.(ticker{i}) = fetch(c,ticker(i),'Adj Close','Jan 1 00','Apr 19 13','d');
end
end
调用的结果(第一行代码):
ans =
ABFS: [3337x2 double]
TCB: [3337x2 double]
NE: [3337x2 double]
FGP: [3343x2 double]
编辑以处理注释
为了在矩阵中容纳不同长度的金融时间序列,您需要在缺少特定日期的数据的地方填充NaNs。你需要rude()
和my Pivot()
% Use the modified function that returns a structure
D = fetchDataFromYahoo({'ABFS','TCB','NE','FGP'});
% Convert to cell
C = struct2cell(D);
% Count how many rows each cell
len = cellfun('size',C,1);
% Expand id to match each series length
Out = Pivot([rude(len,1:numel(C))', cat(1,C{:})]);
您可以在Out
中看到ABFS、TCB和NE缺少与第37行对应的日期(您也可以在D中仔细检查这一点,缺少日期730539 )。
https://stackoverflow.com/questions/16106840
复制相似问题