📄 qihuo.m
字号:
function varargout = qihuo(varargin)
% QIHUO M-file for qihuo.fig
% QIHUO, by itself, creates a new QIHUO or raises the existing
% singleton*.
%
% H = QIHUO returns the handle to a new QIHUO or the handle to
% the existing singleton*.
%
% QIHUO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in QIHUO.M with the given input arguments.
%
% QIHUO('Property','Value',...) creates a new QIHUO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before qihuo_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to qihuo_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help qihuo
% Last Modified by GUIDE v2.5 29-Nov-2004 14:31:16
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @qihuo_OpeningFcn, ...
'gui_OutputFcn', @qihuo_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before qihuo is made visible.
function qihuo_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to qihuo (see VARARGIN)
% Choose default command line output for qihuo
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
global states
global d
global ll
global lll
global zhsh
global gp
global ad_gp
states=ones(1,10);
d=250;
ll=0;lll=0;
zhsh=[];
gp=[];
% UIWAIT makes qihuo wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = qihuo_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in radiobutton1.function radiobutton1_Callback(hObject, eventdata, handles)% hObject handle to radiobutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton1% --- Executes on button press in radiobutton2.function radiobutton2_Callback(hObject, eventdata, handles)% hObject handle to radiobutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton2% --- Executes on button press in radiobutton3.function radiobutton3_Callback(hObject, eventdata, handles)% hObject handle to radiobutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton3% --- Executes on button press in radiobutton4.function radiobutton4_Callback(hObject, eventdata, handles)% hObject handle to radiobutton4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton4% --- Executes on button press in radiobutton5.function radiobutton5_Callback(hObject, eventdata, handles)% hObject handle to radiobutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton5% --- Executes on button press in radiobutton6.function radiobutton6_Callback(hObject, eventdata, handles)% hObject handle to radiobutton6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)M_A=get(handles.radiobutton6,'Value');
if M_A==1
set(handles.radiobutton3,'Value',1);
set(handles.radiobutton4,'Value',1);
set(handles.radiobutton5,'Value',1);
else
set(handles.radiobutton3,'Value',0);
set(handles.radiobutton4,'Value',0);
set(handles.radiobutton5,'Value',0);
end% Hint: get(hObject,'Value') returns toggle state of radiobutton6% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close
main% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global zhsh
global states
global ad_gp
global gp
global gp_length
global ll
global lll
global d
if isempty(zhsh)
msgbox('请先输入数据');
break;
endswitch states(2)
case 0
msgbox('请选择训练方式');
case 1
s=get(handles.edit1,'string');
kd =str2num(s);
if kd==0
kd=zhsh(1,1);
ll=0;
else
is_ll=0;
for i=1:gp_length
if gp(1,i)==kd
is_ll=1; ll=i;
end
end
end
if is_ll==0
msgbox('起始日期错误,请重新输入,0为从第一个数据开始');
end
s=get(handles.edit2,'string');
d =str2num(s);
if d<5
msgbox('训练时间太短,请重新输入');
else
lll=ll+d;
ld=zhsh(1,lll);
end
case 2
s=get(handles.edit1,'string');
kd =str2num(s);
if kd==0
kd=zhsh(1,1);
ll=0;
else
is_ll=0;
for i=1:gp_length
if gp(1,i)==kd
ll=i;
is_ll=1;
end
end
end
if is_ll==0
msgbox('起始日期错误,请重新输入,0为从第一个数据开始');
end
s=get(handles.edit2,'string');
ld =str2num(s);
if ld==0
ld=zhsh(1,end);
lll=gp_length-ll;
else
for i=1:gp_length
if gp(1,i)==ld
lll=i;
end
end
end
if lll==0
msgbox('结束日期错误,请重新输入,0为从第一个数据开始');
end
end
d=lll-ll;
kd==zhsh(1,ll+1);
ld=zhsh(1,lll);
i=1;
while ad_gp(1)~='.'
ad_gp1(i)=ad_gp(1);
ad_gp(1)=[];
i=i+1;
end
ad_gp1(i)='.'; ad_gp1(i+1)='t'; ad_gp1(i+2)='x'; ad_gp1(i+3)='t';
fid=fopen(ad_gp1,'W');
%——————————计算OBV以及OBV改变量
%obv_zhsh=obv(zhsh(2,:),zhsh(3,:),1);
%obv_gp=obv(gp(2,:),gp(3,:),val);
obv1_zhsh=obv1(zhsh(2,:),zhsh(3,:),zhsh(4,:));
obv1_gp=obv1(gp(2,:),gp(3,:),gp(4,:));
%---------------------------计算变化量b_x和收益率shy_x
shy_zhsh(1)=0; shy_gp(1)=0; b_zhsh(1)=0; b_gp(1)=0;
for i=2:gp_length
shy_zhsh(i)=(zhsh(2,i)-zhsh(2,i-1))/zhsh(2,i-1);
shy_gp(i)=(gp(2,i)-gp(2,i-1))/gp(2,i-1);
b_zhsh(i)=zhsh(2,i)-zhsh(2,i-1);
b_gp(i)=gp(2,i)-gp(2,i-1);
end
%--------------------------计算对数股价d_x和对数收益率db_x
d_zhsh=log(zhsh(2,:)); d_gp=log(gp(2,:));
db_zhsh(1)=0; db_gp(1)=0;
for i=2:gp_length
db_zhsh(i)=d_zhsh(i)-d_zhsh(i-1);
db_gp(i)=d_gp(i)-d_gp(i-1);
end
%--------------建立各种模型
alpha=0.05;
if gp_length<ll+d
d=gp_length-ll;
end
%--------------------计算股票和指数的相关系数
cor=corrcoef(zhsh(2,:),gp(2,:));
ti=['股票和指数的相关系数为:',num2str(cor(1,2))];
disp(ti);
fprintf(fid,'%s\r\n',ti);
M_B=get(handles.radiobutton3,'Value');
C_GP1=get(handles.radiobutton1,'Value');
if M_B&C_GP1
%--1.股票指数模型1:P(t)-P(t-1)=a1(H(t)-H(t-1))
[B_GP1,BINT_GP1,R_GP1,RINT_GP1,STATS_GP1]=regress(b_gp(ll+1:ll+d)',b_zhsh(ll+1:ll+d)',alpha);
nih=B_GP1'*b_zhsh;
nihez_GP1(1)=gp(2,1);
for i=2:gp_length
nihez_GP1(i)=gp(2,i-1)+nih(i);
end
R_GP1=nihez_GP1-gp(2,:);
%-------计算D。W值
S=0; S1=R_GP1(1)^2;
for i=2:gp_length
S=S+R_GP1(i)*R_GP1(i-1);
S1=S1+R_GP1(i)^2;
end
STATS_GP1(4)=2*(1-S/S1);
%-------计算对数似然值
L=-(gp_length)*(1+log(2*pi)+log(R_GP1*R_GP1'/gp_length))/2;
STATS_GP1(5)=L;
%-------计算AIC值
STATS_GP1(6)=2*(-L+3)/gp_length;
%-------计算SC值
STATS_GP1(7)=2*(-L+3*log(gp_length))/gp_length;
%------------计算相对误差
xr_GP1=100*R_GP1'./gp(2,:)';
xr_min=min(xr_GP1);%最小值
xr_max=max(xr_GP1);%最大值
STATS_GP1(8)=xr_min;
STATS_GP1(9)=xr_max;
nu=mean(gp(2,:));
STATS_GP1(10)=dot(gp(2,:)-nihez_GP1,gp(2,:)-nihez_GP1)/dot(gp(2,:)-nu,gp(2,:)-nu);
switch states(3)
case 0
msgbox(' 请选择数据查看方式')
case 1
figure
txt=['P(t)-P(t-1)=',num2str(B_GP1(1)),'*(H(t)-H(t-1))'];
txt1=['训练样本开始时间:',num2str(kd),';结束时间:',num2str(ld),'。共',num2str(d),'天数据'];
txt2=['样本决定系数(R^2):',num2str(STATS_GP1(1)),';AIC值:',num2str(STATS_GP1(4)),';股票和指数相关系数:',num2str(cor(1,2))];
disp('只考虑指数的差分模型为:'); disp(txt);
fprintf(fid,'%s\n','只考虑指数的差分模型为为:',txt);
plot(gp(2,:),'-+');
hold on
plot(nihez_GP1,'r.');
legend('股价真实值','拟合股价');
xlabel(txt);
title('只考虑指数的差分模型')
[h_l,h_w]=size(gp);
h_max=max(gp(2,:));
h_min=min(gp(2,:));
l=max(h_l,h_w);
axis([0,l,h_min,h_max*1.1]);
grid on
set(gca,'xtick',0:round(l/10):l);
format long g
str=gp(1,1:round(l/10):l)';
str(1:2,:)=[];
set(gca,'xticklabel',str)
g_max=max(ylim);
g_min=min(ylim);
text(5,g_min+(g_max-g_min)*0.975,txt1);
text(5,g_min+(g_max-g_min)*0.925,txt2);
hold on
plot(ll+d,gp(2,ll+d),'ko','Markersize',20);
plot(ll,gp(2,ll),'ko','Markersize',20);
figure
plot(xr_GP1);
legend('相对误差','相对误差=100*(拟合值-真实值)/真实值');
xlabel(txt);
title('只考虑指数的差分模型')
hold on
plot(ll+d,xr_GP1(ll+d),'k.','Markersize',20)
plot(ll,xr_GP1(ll),'k.','Markersize',20)
[h_l,h_w]=size(gp);
h_max=max(gp(2,:));
h_min=min(gp(2,:));
l=max(h_l,h_w);
axis([0,l,h_min,h_max*1.1]);
grid on
set(gca,'xtick',0:round(l/10):l);
format long g
set(gca,'xticklabel',str)
figure
normplot(R_GP1);
legend('误差对于正态分布的Q-Q图');
xlabel(txt);
title('只考虑指数的差分模型')
case 2
gp1=gp(:,ll:end);
nihez1_GP1=nihez_GP1(ll:end);
xr1_GP1=xr_GP1(ll:end);
figure
txt=['P(t)-P(t-1)=',num2str(B_GP1(1)),'*(H(t)-H(t-1))'];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -