Matlab在读取雅虎数据时经常出现不稳定的现象,因此,为了能够将更多精力集中到数据处理上不再每次花费漫长时间在等待连接上,可以将读取数据的代码单独放置在一个m中,比如:
clear;
c=yahoo;
D=fetch(c,'600177.ss','05/18/09','06/18/09'); %雅戈尔
format long g %不以默认的科学计数法显示数据
D %输出数据
save testD D %保存数据
上述程序处理后,编号为“600177.ss”深市的股票“雅戈尔”的相关数据被保存在当前目录下“testD”中D变量内,以后在需要的时候可以随时读取。
fetch函数的含义为:
fetch Request data from Yahoo!.
D = fetch(C,S) returns data for all fields from the Yahoo's web site for
given securities, S, given the connection handle, C.
D = fetch(C,S,F) returns the data for the specified fields, F.
D = fetch(C,S,D1) returns all data fields for the given security for
the date D1. If D1 is today's date, the data from yesterday will
be returned.
D = fetch(C,S,F,D1) returns the data for the specified fields, F, for the
date D1.
D = fetch(C,S,D1,D2) returns the data for the given security
for the date range D1 to D2.
D = fetch(C,S,F,D1,D2) returns the data for the specified fields, F,
for the date range D1 to D2.
D = fetch(C,S,D1,D2,P) returns the data for the given security
for the date range D1 to D2 with a period of P. P can be
entered as:
'd' for daily values.
'w' for weekly values.
'm' for monthly values.
'v' for dividends.
可以看到,fetch支持多种参数设置形式,可以根据自己的需要进行设置。
下面看看如何读取数据D,
clc;
load testD.mat %导入
format long g %显示形式
datestr(D(:,1)) %日期显示格式为可识别方式。
上述程序能够输出数据D的第一列,结果为:
ans =
18-Jun-2009
17-Jun-2009
16-Jun-2009
15-Jun-2009
12-Jun-2009
11-Jun-2009
10-Jun-2009
09-Jun-2009
08-Jun-2009
05-Jun-2009
04-Jun-2009
03-Jun-2009
02-Jun-2009
01-Jun-2009
29-May-2009
28-May-2009
27-May-2009
26-May-2009
25-May-2009
22-May-2009
21-May-2009
20-May-2009
19-May-2009
18-May-2009
其中,
datestr(D(:,1)) %日期显示格式为可识别方式。
语句用来将第一列的数据进行格式转换后输出,否则输出结果为:
ans =
733942
733941
733940
733939
733936
733935
733934
733933
733932
733929
733928
733927
733926
733925
733922
733921
733920
733919
733918
733915
733914
733913
733912
733911
当然,需要更多的开盘数据、收盘价、交易量等,可以通过
D(:,N)
来实现,N可以是不同的数字,分别为2~7.