请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 6848|回复: 0

MATLAB获取新浪实时股票数据

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2017-5-6 23:49:33 | 显示全部楼层 |阅读模式
MATLAB获取新浪实时股票数据:http://blog.sina.com.cn/s/blog_530e99a40102v01e.html
  1. function [CurrentStockData, CurrentStockInfo] = get_stock_data(StockCode)
  2. % StockCode = '000001'--字符类型
  3. if(str2num(StockCode)>=999999)
  4.     symbol=strcat('sh000001');
  5. elseif(StockCode(1)=='6')
  6.     symbol=strcat('sh',(StockCode));  % 上海
  7. elseif(StockCode(1)=='0')
  8.     symbol=strcat('sz',(StockCode));  % 深圳
  9. end
  10. url2Read=['http://hq.sinajs.cn/list=',symbol];
  11. s=urlread(url2Read);
  12. result=textscan(s,'%s','delimiter', ',');
  13. result=result{1};   % 当前的结果,cell类型
  14. % 说明
  15. % 这个字符串由许多数据拼接在一起,不同含义的数据用逗号隔开了,按照程序员的思路,顺序号从0开始。
  16. % 0:”大秦铁路”,股票名字;
  17. % 1:”27.55″,今日开盘价;
  18. % 2:”27.25″,昨日收盘价;
  19. % 3:”26.91″,当前价格;
  20. % 4:”27.55″,今日最高价;
  21. % 5:”26.20″,今日最低价;
  22. % 6:”26.91″,竞买价,即“买一”报价;
  23. % 7:”26.92″,竞卖价,即“卖一”报价;
  24. % 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百;
  25. % 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万;
  26. % 10:”4695″,“买一”申请4695股,即47手;
  27. % 11:”26.91″,“买一”报价;
  28. % 12:”57590″,“买二”
  29. % 13:”26.90″,“买二”
  30. % 14:”14700″,“买三”
  31. % 15:”26.89″,“买三”
  32. % 16:”14300″,“买四”
  33. % 17:”26.88″,“买四”
  34. % 18:”15100″,“买五”
  35. % 19:”26.87″,“买五”
  36. % 20:”3100″,“卖一”申报3100股,即31手;
  37. % 21:”26.92″,“卖一”报价
  38. % (22, 23), (24, 25), (26,27), (28, 29)分别为“卖二”至“卖四的情况”
  39. % 30:”2008-01-11″,日期;
  40. % 31:”15:05:32″,时间;
  41. StockName = result{1,1}(22:end);       % 股票名称-char类型
  42. OpenPrice=str2double(result{2});       % 今日开盘价
  43. YesterdayPrice=str2double(result{3});  % 昨日收盘价
  44. CurrentPrice=str2double(result{4});    % 当前价格
  45. HighPrice=str2double(result{5});       % 今日最高价格
  46. LowPrice=str2double(result{6});        % 今日最低价格
  47. BidBuy1 = str2double(result{7});       % 竞买价,即“买一”报价;
  48. BidSell1 = str2double(result{8});      % 竞卖价,即“卖一”报价;
  49. TradeVolume = str2double(result{9});   % 今日成交量,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百
  50. TradeMoney = str2double(result{10});   % 成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万;
  51. BuyMount1 = str2double(result{11});    % “买一”申请股数
  52. BuyPrice1 = str2double(result{12});    % “买一”报价
  53. BuyMount2 = str2double(result{13});    % “买二”申请股数
  54. BuyPrice2 = str2double(result{14});    % “买二”报价
  55. BuyMount3 = str2double(result{15});    % “买三”申请股数
  56. BuyPrice3 = str2double(result{16});    % “买三”报价
  57. BuyMount4 = str2double(result{17});    % “买四”申请股数
  58. BuyPrice4 = str2double(result{18});    % “买四”报价
  59. BuyMount5 = str2double(result{19});    % “买五”申请股数
  60. BuyPrice5 = str2double(result{20});    % “买五”报价
  61. SellMount1 = str2double(result{21});   % “卖一”申请股数
  62. SellPrice1 = str2double(result{22});   % “卖一”报价
  63. SellMount2 = str2double(result{23});   % “卖二”申请股数
  64. SellPrice2 = str2double(result{24});   % “卖二”报价
  65. SellMount3 = str2double(result{25});   % “卖三”申请股数
  66. SellPrice3 = str2double(result{26});   % “卖三”报价
  67. SellMount4 = str2double(result{27});   % “卖四”申请股数
  68. SellPrice4 = str2double(result{28});   % “卖四”报价
  69. SellMount5 = str2double(result{29});   % “卖五”申请股数
  70. SellPrice5 = str2double(result{30});   % “卖五”报价
  71. date = result{31};                     % ”2008-01-11″,日期;
  72. time = result{32};                     % ”15:05:32″,时间;

  73. CurrentStockData=single([OpenPrice,YesterdayPrice,CurrentPrice,HighPrice,LowPrice,...
  74.    BidBuy1,BidSell1,TradeVolume,TradeMoney,BuyMount1,BuyPrice1,BuyMount2,BuyPrice2,...
  75.    BuyMount3,BuyPrice3,BuyMount4,BuyPrice4,BuyMount5,BuyPrice5,SellMount1,SellPrice1,...
  76.    SellMount2,SellPrice2,SellMount3,SellPrice3,SellMount4,SellPrice4,SellMount5,SellPrice5 ]);
  77. CurrentStockInfo = {StockName;date;time};
复制代码



算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-29 02:13 , Processed in 0.228999 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表