Matlab读取yahoo股票数据

本文介绍使用Matlab从雅虎财经网站获取股票数据的方法,通过编写专门的脚本减少网络连接不稳定带来的影响,并详细解释了fetch函数的各种用法及数据输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值