首先从网易财经下载上证指数,以收盘价作为每日价格。
之后进行数据处理。
figure(1)绘制收盘价数据
figure(2)绘制对数收益率数据
figure(3)利用子图subplot方式把收盘价和对数收益率汇总
figure(4)利用双坐标轴在图一幅图中绘制收盘价和对数收益率
%shzh = readtable('zhishu.xlsx');
%data_cell = table2cell(shzh(:,1));
%data = datatime(data_cell,'InputFormat','yyyy/MM/dd');
%shzhpt = table2array(shzh(:,4:7)); %Convert table to a homogeneous array.
%pclose = shzhpt(:1);
[num,txt,raw] = xlsread('000001.xlsx');
date_cell = raw(2:end,1);
%date_cell2 = txt(2:end,1);
datenum = datenum(date_cell); %Converts the datetime values in datetime attay t to serial date numbers.
x1 = datenum;
pclose = num(:,1);
figure(1);
plot(x1,pclose);
axis([min(x1) max(x1) min(pclose)-100 max(pclose)+100]) %x,y范围
dateaxis('x',1); %Date axis labels
%set(gca,'XTickLabel',)
figure(2);
rlog = 100*diff(log(pclose)); %对数收益率
T = length(pclose);
rsimple = [];
for j = 1:T-1
rsimple(j) = 100*pclose(j+1)/pclose(j)-1;
end
x2 = datenum(2:end);
plot(x2,rlog);
axis([min(x2) max(x2) min(rlog)-3 max(rlog)+3])
dateaxis('x',1);
figure(3);
subplot(2,1,1);
plot(x1,pclose);
axis([min(x1) max(x1) min(pclose)-100 max(pclose)+100])
dateaxis('x',1);
subplot(2,1,2);
plot(x2,rlog);
axis([min(x1) max(x1) min(rlog)-3 max(rlog)+3])
dateaxis('x',1);
figure(4);
p2 = pclose(2:end);
plotyy(x2,p2,x2,rlog);
[AX,H1,H2] = plotyy(x2,p2,x2,rlog);
set(AX,'Xlim',[min(datenum),max(datenum)]);
set(AX(1),'Ylim',[min(pclose)-4000,max(pclose)+100]);
set(AX(2),'Ylim',[min(rlog)-5 max(rlog)+30]);
dateaxis('x',1);
xlabel('time t');
ylabel('Stock Price');
title('Plot of Stock Price and Log Returns');