% 编程环境: Matlab 2014a, win7 32bit, Microsoft SQL Server 2008r2
%%
% 清屏
clc;
clear all;
close all;
%%
% 链接 yahoo 网站下载日线数据(将以下注释内容另存为 GetYahoocnStockDayData.m)
StockData=GetYahoocnStockDayData(1, 1, 2016, 3, 1, 2016,'002237');
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%%
%{
%%
% 雅虎数据接口(日线):
% urlread(['http://table.finance.yahoo.com/table.csv?a=0&b=1&c=2012&d=3&e=19&f=2012&s=600000.ss'])
% 函数调用格式(沪深):
% GetYahoocnStockDayData(1, 1, 2016, 3, 1, 2016,'600000')
%%
function StockData=GetYahoocnStockDayData(StartDateM,StartDateD,StartDateY,EndDateM,EndDateD,EndDateY,StockName)
%%
StartDateM=num2str(StartDateM);
StartDateD=num2str(StartDateD);
StartDateY=num2str(StartDateY);
EndDateM=num2str(EndDateM);
EndDateD=num2str(EndDateD);
EndDateY=num2str(EndDateY);
if StockName(1)=='6'
StockName=[StockName,'.ss'];
URL=['http://table.finance.yahoo.com/table.csv?a=',StartDateM,'&b=',StartDateD,'&c=',StartDateY,'&d=',EndDateM,'&e=',EndDateD,'&f=',EndDateY,'&s=',StockName];
StockData=urlread(URL);
elseif StockName(1)=='0'
StockName=[StockName,'.sz'];
URL=['http://table.finance.yahoo.com/table.csv?a=',StartDateM,'&b=',StartDateD,'&c=',StartDateY,'&d=',EndDateM,'&e=',EndDateD,'&f=',EndDateY,'&s=',StockName];
StockData=urlread(URL);
else
disp('调用函数格式错误!');
disp(['调用格式为:',...
'GetYahoocnStockDayData(StartDateM(int),StartDateD(int),StartDateY(int),EndDateM(int),EndDateD(int),EndDateY(int),StockName(string))']);
end
[Date,Open,High,Low,Close,Volume,AdjustedClose]=strread(StockData,'%s%s%s%s%s%s%s','delimiter',',');%由于接受的是一串字符串,需处理
counter=length(Date(:,1));
Date=Date(2:counter);
Open=Open(2:counter);
High=High(2:counter);
Low=Low(2:counter);
Close=Close(2:counter);
Volume=Volume(2:counter);
AdjustedClose=AdjustedClose(2:counter);
StockData=[Date,Open,High,Low,Close,Volume,AdjustedClose];
end
%}
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%%
% 查询 SQL Server 下指定表
conn=database('ShuJuYuan','PERELMAN','');% 链接源【ODBC - ShuJuYuan】指定库表【SQL Server - ShuJuKu - StockData】
p=ping(conn);% 测试是否成功链接
if p.AutoCommitTransactions% 如果链接成功
curs=exec(conn,'select * from [ShuJuKu].[dbo].[StockData]');% 用于调用成员函数 Message 查询是否存在指定表
if curs.Message%如果数据库ShuJuKu下没有表 StockData ,则返回值非空,则新建表 StockData
sqlquery1='use ShuJuKu';% SQL 语句,选择数据库 ShuJuKu
sqlquery2=['create table StockData('...% SQL 语句,在数据库 ShuJuKu 下新建表 IBM
'D date NOT NULL PRIMARY KEY,'...% 日期不能为空且设置为主键避免数据重复
'O real NULL,'...
'H real NULL,'...
'L real NULL,'...
'C real NULL,'...
'V int NULL,'...
'AdjustedClose real NULL)'];
exec(conn,sqlquery1);
exec(conn,sqlquery2);
else
close(curs);
end
else
disp('数据库链接错误');
end
%%
% 数据写入 SQL Server
counter=length(StockData(:,1));
for i=1:counter
s=['''',cell2mat(StockData(i,1)),'''',',',...% 引用单引号方法(输入 '' 输出 ')
cell2mat(StockData(i,2)),',',...
cell2mat(StockData(i,3)),',',...
cell2mat(StockData(i,4)),',',...
cell2mat(StockData(i,5)),',',...
cell2mat(StockData(i,6)),',',...
cell2mat(StockData(i,7))];
sqlquery6='use ShuJuKu';
sqlquery7=['INSERT INTO StockData(D,O,H,L,C,V,AdjustedClose) VALUES (',s,')'];% 插入数据语句
exec(conn,sqlquery6);
exec(conn,sqlquery7);
end
%%
% 关闭数据库,用完要关闭,良好习惯
close(conn);
%%
% 调用 SQL Server 数据
conn=database('ShuJuYuan','PERELMAN','');
curs=exec(conn,'select * from [ShuJuKu].[dbo].[StockData]');
curs=fetch(curs);
Data=curs.data;
%%
% 展示
Data
%% 展示K线图
d=cell2mat(Data(:,1));
o=cell2mat(Data(:,2));
h=cell2mat(Data(:,3));
l=cell2mat(Data(:,4));
c=cell2mat(Data(:,5));
v=cell2mat(Data(:,6));
adjustedclose=cell2mat(Data(:,7));
%%
% 日期数据
date=datenum(datestr(d));
%%
% K 线图
candle(h,l,c,o,'k',date,'yyyy/mm/dd');
%%
% 个性设置
title('002237.sz-Made by DengChaohai','FontSize',12,'FontWeight','bold','Color','b');
set(gca,'YTick',min(l):0.1:max(h));
grid on;
axis tight;
xlabel('日期','FontSize',12,'FontWeight','bold','Color','b');
ylabel('价格','FontSize',12,'FontWeight','bold','Color','b');
legend('休市','上涨','下跌');
%%
%clcall;