股指套利 MATLAB源程序

股指套利 MATLAB源程序

Embed below code to your site

包括Arbitrage(全样本)算法源程序,股指数据分析源程序 1 (ProAnalysis),期指合约矩阵分析源程序(MatAnalysis)


Code=load('d:\code.txt'); %输入股票代码;
P=xlsread('d:\QZ300.xls','C2:C301'); %输入当前沪深300价格数列;
Q=load('d:\QZ300.txt')/100; %输入当前沪深300权重数据;
sumBuy=0;
sumSell=0; %初始化下一交易日应卖出股票总量;

for n=1:300 %初始化数组;
N(n,1)=n;
X(n,1)=n;
intN(n,1)=n;
end

X=5000*300*Q; %一张期指合约对应各股票权重;

for i=1:300 %依次计算应买入股票数量;
N(i,1)=X(i,1)/P(i,1);
RawN(i,1)=N(i,1);
intN(i,1)=round(N(i,1)); %四舍五入,以保证买入股票为100整数倍;

if mod(intN(i,1),100)<=10 && Q(i,1)<0.001 %如果买入股票数量对100取余结果小于10 并且其权重小于 千分之一;

tmp1=intN(i,1); %则舍去余数,只购买1手整数倍;
intN(i,1)=intN(i,1)-mod(intN(i,1),100);
fprintf('股票 %i数量权重为.3f 应买入数量为 .3f ',Code(i,1),RawN(i,1),intN(i,1))
fprintf('丢失股票数量为 %i ; 其权重为%8.4f\n',mod(tmp1,100),Q(i,1))

else %其它情况,则购买对100取整数部分加100股;
tmp=intN(i,1);
intN(i,1)=intN(i,1)-mod(intN(i,1),100)+100; %计算下一交易日应卖掉数量;
nextDaySell=100-mod(tmp,100);
sumSell=sumSell+nextDaySell; %自加求和;
fprintf('股票 %i数量权重为.3f 应买入数量为 .3f ',Code(i,1),RawN(i,1),intN(i,1))
fprintf('下一交易日应卖出股票数量为 %i\n',nextDaySell)

end
sumBuy=sumBuy+intN(i,1);


end
fprintf(' 下一交易日应卖掉股票总数为%i\n',sumSell)
fprintf(' 下一交易日应买入股票总数为%i\n',sumBuy)


-----内含下列附件-----

% Part 1 理论期指计算
disp('Part 1 股指合约矩阵及理论期指计算')
sheet=input('please enter the sheet.No: ');
switch (sheet)
case 1
V=xlsread('d:\matrix .xls',1,'I12:J16') % 输入当前股指值(value)矩阵;
B=xlsread('d:\matrix .xls',1,'I4:M8') % 输入当前股指比值(ratio)矩阵;
case 2
V=xlsread('d:\matrix .xls',2,'I12:J16')
B=xlsread('d:\matrix .xls',2,'I4:M8')
case 3
V=xlsread('d:\matrix .xls',3,'I12:J16')
B=xlsread('d:\matrix .xls',3,'I4:M8')

otherwise
disp('null!')
end
Crt=input('输入首个当前合约: ','s');
r=0.08; %连续复利的无风险利率;
q=0.03; %红利收益率;
t=date; %当前交易日期;
ETC_S=10; %股票交易成本;
ETC_F=4700*300*2*0.00005; %期货交易成本;
ETC=ETC_S+ETC_F; %总交易成本;
% liquidity=f(currency,inflation,interest,CPI)
% AV=Hs*( f(enthusiasm,liquidity,time) ) AV:ajustment value

Hs=V(1,1); %当前沪深300指数值;


switch (Crt) %选择执行多分支语句;
case ('IF0801')

T1=datestr('18-Jan-2008'); %合约到期日;
T2=datestr('15-Feb-2008');
T3=datestr('21-Mar-2008');
T6 =datestr('20-Jun-2008');

IF01=Hs*exp((r-q)*(daysact(t,T1)/365))+ETC; %求各合约理论期指;
IF02=Hs*exp((r-q)*(daysact(t,T2)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF01;IF02;IF03;IF06] %输出理论合约值;
Rac=eye(5); %建立单位阵 ,用理论合约值填充为比值矩阵;
Rac(1,2)=IF01/Hs;
Rac(1,3)=IF02/Hs;
Rac(1,4)=IF03/Hs;
Rac(1,5)=IF06/Hs;
Rac(2,3)= IF02/IF01;
Rac(2,4)= IF03/IF01;
Rac(2,5)= IF06/IF01;
Rac(3,4)= IF03/IF02;
Rac(3,5)= IF06/IF02;
Rac(4,5)= IF06/IF03;
disp('理论比值矩阵')
Rac %输出比值理论矩阵;

case ('IF0802')

T2=datestr('15-Feb-2008');
T3=datestr('21-Mar-2008');
T6 =datestr('20-Jun-2008');
T9=datestr('19-Sep-2008');

IF02=Hs*exp((r-q)*(daysact(t,T2)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF02;IF03;IF06;IF09]
Rac=eye(5);
Rac(1,2)=IF02/Hs;
Rac(1,3)=IF03/Hs;
Rac(1,4)=IF06/Hs;
Rac(1,5)=IF09/Hs;

Rac(2,3)= IF03/IF02;
Rac(2,4)= IF06/IF02;
Rac(2,5)= IF09/IF02;

Rac(3,4)= IF06/IF03;
Rac(3,5)= IF09/IF03;

Rac(4,5)= IF09/IF06;
disp('理论比值矩阵')
Rac

case ('IF0803')
T3=datestr('21-Mar-2008');
T4=datestr('18-Apr-2008');
T6=datestr('20-Jun-2008');
T9=datestr('19-Sep-2008');

IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
IF04=Hs*exp((r-q)*(daysact(t,T4)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF03;IF04;IF06;IF09]
Rac=eye(5);

Rac(1,2)=IF03/Hs;
Rac(1,3)=IF04/Hs;
Rac(1,4)=IF06/Hs;
Rac(1,5)=IF09/Hs;

Rac(2,3)= IF04/IF03;
Rac(2,4)= IF06/IF03;
Rac(2,5)= IF09/IF03;

Rac(3,4)= IF06/IF04;
Rac(3,5)= IF09/IF04;

Rac(4,5)= IF09/IF06;
disp('理论比值矩阵')
Rac
case ('IF0804')

T4=datestr('18-Apr-2008');
T5=datestr('16-May-2008');
T6 =datestr('20-Jun-2008');
T9=datestr('19-Sep-2008');

IF04=Hs*exp((r-q)*(daysact(t,T4)/365))+ETC;
IF05=Hs*exp((r-q)*(daysact(t,T5)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF04;IF05;IF06;IF09]
Rac=eye(5);
Rac(1,2)=IF04/Hs;
Rac(1,3)=IF05/Hs;
Rac(1,4)=IF06/Hs;
Rac(1,5)=IF09/Hs;

Rac(2,3)= IF05/IF04;
Rac(2,4)= IF06/IF04;
Rac(2,5)= IF09/IF04;

Rac(3,4)= IF06/IF05;
Rac(3,5)= IF09/IF05;

Rac(4,5)= IF09/IF06;
disp('理论比值矩阵')
Rac
case ('IF0805')

T5=datestr('16-May-2008');
T6 =datestr('20-Jun-2008');
T9=datestr('19-Sep-2008');
T12=datestr('19-Dec-2008');

IF05=Hs*exp((r-q)*(daysact(t,T5)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF05;IF06;IF09;IF12]
Rac=eye(5);

Rac(1,2)=IF05/Hs;
Rac(1,3)=IF06/Hs;
Rac(1,4)=IF09/Hs;
Rac(1,5)=IF12/Hs;

Rac(2,3)= IF06/IF05;
Rac(2,4)= IF09/IF05;
Rac(2,5)= IF12/IF05;

Rac(3,4)= IF09/IF06;
Rac(3,5)= IF12/IF06;

Rac(4,5)= IF12/IF09;
disp('理论比值矩阵')
Rac

case ('IF0806')

T6 =datestr('20-Jun-2008');
T7=datestr('18-Jul-2008');
T9=datestr('19-Sep-2008');
T12=datestr('19-Dec-2008');

IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
IF07=Hs*exp((r-q)*(daysact(t,T7)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF06;IF07;IF09;IF12]
Rac=eye(5);
Rac(1,2)=IF06/Hs;
Rac(1,3)=IF07/Hs;
Rac(1,4)=IF09/Hs;
Rac(1,5)=IF12/Hs;

Rac(2,3)= IF07/IF06;
Rac(2,4)= IF09/IF06;
Rac(2,5)= IF12/IF06;

Rac(3,4)= IF09/IF07;
Rac(3,5)= IF12/IF07;

Rac(4,5)= IF12/IF09;
disp('理论比值矩阵')
Rac
case ('IF0807')

T7=datestr('18-Jul-2008');
T8 =datestr('15-Aug-2008');
T9=datestr('19-Sep-2008');
T12=datestr('19-Dec-2008');

IF07=Hs*exp((r-q)*(daysact(t,T7)/365))+ETC;
IF08=Hs*exp((r-q)*(daysact(t,T8)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF07;IF08;IF09;IF12]
Rac=eye(5);


Rac(1,2)=IF07/Hs;
Rac(1,3)=IF08/Hs;
Rac(1,4)=IF09/Hs;
Rac(1,5)=IF12/Hs;

Rac(2,3)= IF08/IF07;
Rac(2,4)= IF09/IF07;
Rac(2,5)= IF12/IF07;

Rac(3,4)= IF09/IF08;
Rac(3,5)= IF12/IF08;

Rac(4,5)= IF12/IF09;
disp('理论比值矩阵')
Rac
case ('IF0708')

T8=datestr('17-Aug-2007');
T9=datestr('21-Sep-2007');
T12=datestr('21-Dec-2007');
T3 =datestr('21-Mar-2008');

IF08=Hs*exp((r-q)*(daysact(t,T8)/365))+ETC;
IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF08;IF09;IF12;IF03]
Rac=eye(5);
Rac(1,2)=IF08/Hs;
Rac(1,3)=IF09/Hs;
Rac(1,4)=IF12/Hs;
Rac(1,5)=IF03/Hs;
Rac(2,3)= IF09/IF08;
Rac(2,4)= IF12/IF08;
Rac(2,5)= IF03/IF08;
Rac(3,4)= IF12/IF09;
Rac(3,5)= IF03/IF09;
Rac(4,5)= IF03/IF12;
disp('理论比值矩阵')
Rac

case ('IF0709')

T9=datestr('21-Sep-2007');
T10=datestr('17-Oct-2007');
T12=datestr('21-Dec-2007');
T3 =datestr('21-Mar-2008');

IF09=Hs*exp((r-q)*(daysact(t,T9)/365))+ETC;
IF10=Hs*exp((r-q)*(daysact(t,T10)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF09;IF10;IF12;IF03]
Rac=eye(5);
Rac(1,2)=IF09/Hs;
Rac(1,3)=IF10/Hs;
Rac(1,4)=IF12/Hs;
Rac(1,5)=IF03/Hs;
Rac(2,3)= IF10/IF09;
Rac(2,4)= IF12/IF09;
Rac(2,5)= IF03/IF09;

Rac(3,4)= IF12/IF10;
Rac(3,5)= IF03/IF10;
Rac(4,5)= IF03/IF12;
disp('理论比值矩阵')
Rac

case ('IF0710')

T10=datestr('17-Oct-2007');
T11=datestr('16-Nov-2007');
T12=datestr('21-Dec-2007');
T3 =datestr('21-Mar-2008');

IF10=Hs*exp((r-q)*(daysact(t,T10)/365))+ETC;
IF11=Hs*exp((r-q)*(daysact(t,T11)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF10;IF11;IF12;IF03]
Rac=eye(5);
Rac(1,2)=IF10/Hs;
Rac(1,3)=IF11/Hs;
Rac(1,4)=IF12/Hs;
Rac(1,5)=IF03/Hs;

Rac(2,3)= IF11/IF10;
Rac(2,4)= IF12/IF10;
Rac(2,5)= IF03/IF10;

Rac(3,4)= IF12/IF11;
Rac(3,5)= IF03/IF11;

Rac(4,5)= IF03/IF12;
disp('理论比值矩阵')
Rac
case ('IF0711')

T11=datestr('16-Nov-2007');
T12=datestr('21-Dec-2007');
T3 =datestr('21-Mar-2008');
T6=datestr('20-Jun-2008');

IF11=Hs*exp((r-q)*(daysact(t,T11)/365))+ETC;
IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF11;IF12;IF03;IF06;]
Rac=eye(5);
Rac(1,2)=IF11/Hs;
Rac(1,3)=IF12/Hs;
Rac(1,4)=IF03/Hs;
Rac(1,5)=IF06/Hs;

Rac(2,3)= IF12/IF11;
Rac(2,4)= IF03/IF11;
Rac(2,5)= IF06/IF11;

Rac(3,4)= IF03/IF12;
Rac(3,5)= IF06/IF12;

Rac(4,5)= IF06/IF03;
disp('理论比值矩阵')
Rac
case ('IF0712')

T12=datestr('21-Dec-2007');
T1=datestr('18-Jan-2008');
T3 =datestr('21-Mar-2008');
T6=datestr('20-Jun-2008');

IF12=Hs*exp((r-q)*(daysact(t,T12)/365))+ETC;
IF01=Hs*exp((r-q)*(daysact(t,T1)/365))+ETC;
IF03=Hs*exp((r-q)*(daysact(t,T3)/365))+ETC;
IF06=Hs*exp((r-q)*(daysact(t,T6)/365))+ETC;
disp('各合约理论期指')
Vac=[Hs;IF12;IF01;IF03;IF06]
Rac=eye(5);
Rac(1,2)=IF12/Hs;
Rac(1,3)=IF01/Hs;
Rac(1,4)=IF03/Hs;
Rac(1,5)=IF06/Hs;

Rac(2,3)= IF01/IF12;
Rac(2,4)= IF03/IF12;
Rac(2,5)= IF06/IF12;

Rac(3,4)= IF03/IF01;
Rac(3,5)= IF06/IF01;

Rac(4,5)= IF06/IF03;
disp('理论比值矩阵')
Rac
otherwise
disp('错误输入!请您返回重新运行:');

end


% Part 2 真正有效检查套利机会
disp('Part 2 开始检查异常值');

A =xlsread('d:\matrix .xls',sheet,'B4:F8') % 输入初值对称矩阵;
B =xlsread('d:\matrix .xls',sheet,'I4:M8') % 输入终值对称矩阵;
C=B-A %求变化矩阵;

for i=1:4 % 是正向市场还是反向市场;
for j=i+1:5
if C(i,j)>0
continue
else
fprintf('invert market [%i %i]\n',i,j)
end

end
end
L=abs(C(1,2))-abs(C(1,1)); % 设置同行比值差变量差L并赋初值;
for i=1:4 % rows 观看特殊值;
for j=i:4
if abs(C(i,j))<abs(C(i,j+1))
continue % 进行下一次迭代;
else

N=abs(C(i,j+1))-abs(C(i,j)); %设置当前循环同行比值变量差 N;


if N-L<0 %比较当前循环同行差值与上循环值;
if i==1 %如果是第一行;
fprintf('EXCEPTION! ROW %i \n',i),
Coordinate=[i,j], %显示异常数据行列坐标;
fprintf(' The data is : %8.4f 高估,存期现套利机会\n\n', C(i,j)); %提示有期现套利机会;
else

fprintf('EXCEPTION! ROW %i \n',i),
Coordinate=[i,j], %显示异常数据行列坐标;
fprintf(' The data is : %8.4f 存跨期套利机会\n\n', C(i,j)); %提示有跨期套利机会;
end

else
fprintf('EXCEPTION! ROW %i \n',i),
Coordinate=[i,j+1], %显示异常数据行列坐标;
fprintf(' The data is : %8.4f \n\n', C(i,j+1));

end

L=N; % 当前差值迭代;

end
end
end

for i=1:4 % columns 观看特殊值;
for j=i+1:5
if abs(C(i,j))>abs(C(i+1,j))
continue % 进行下一次迭代;
else
fprintf('EXCEPTION! COLUMN %i\n',j),
Coordinate=[i+1,j], %显示异常数据行列坐标;
fprintf(' The data is : %8.4f\n\n', C(i+1,j));

end
end
end


-----内含下列附件-----

x = input('输入股指套利数据路径并读取至matlab: '); % 导入外部excel文件;
% xlsread语句;

disp('进行正态分布假设检验分析')
number=length(x); %求数据量;

if (number>30) %条件执行语句
[h,p,j,cv]=jbtest(x) % 大样本进行jb检验
if (h==1)
disp('作直方图与核密度分布图 ')
figure(1);
subplot(2,1,1)
histfit(x)
title('histogram with Norm Distribution curve')
[f,y]=ksdensity(x);
figure(1);
subplot(2,1,2)
plot(y,f)
title('kernel density distritution ')

else
disp('perfect,Normal Distritution!'),
histfit(x);
end

else
[h,p,l,cv]=lillietest(x) % 小样本进行lilieforse检验
if (h==1)
disp('作直方图与核密度分布图')
figure(1);
subplot(2,1,1)
histfit(x)
title('histogram with Norm Distribution curve')
[f,y]=ksdensity(x);
figure(1);
subplot(2,1,2)
plot(y,f)
title('kernel density distritution ')

else
disp('perfect,Normal Distritution!'),
histfit(x);
end
end

fprintf('求特征参数 \n\n ') %注: 所有数据与函数显著水平设为0.05,即置信度为95%
%求特征参数
mu = mean(x); %求期 望(1阶中心矩,度量分布中心位置);
sigma = std(x); %求标准差
variance= var(x); %求方 差(2阶中心矩,度量取值变化程度);
itsSkewness = skewness(x); %求偏斜度(3阶中心矩,度量关于均值对称程度);
itsKurtosis = kurtosis(x); %求峰 值(4阶中心矩,度量尾部厚度,极端值情况);
Max = max(x); %求最大值;
Min = min(x); %求最小值;
s = range(x); %求极差;

disp('价差将扩大或缩小之概率 ')
if number>=30 && itsKurtosis-3>1

if x(number)<=mu-sigma*itsKurtosis^1/4
disp('The probability of SPREAD_WIDEN is')
normspec([x(number),Inf],mu,sigma)
elseif x(number)>mu+sigma*itsKurtosis^1/4
disp('The probability of SPREAD_SHORTEN is')
normspec([-Inf,x(number)],mu,sigma)
else
fprintf('Temporarily steady,balance will be broke! \n\n')
end
elseif number>=30 && itsKurtosis-3<=1
if x(number)<=mu-sigma
disp('The probability of SPREAD_WIDEN is')
normspec([x(number),Inf],mu,sigma)
elseif x(number)>mu+sigma
disp('The probability of SPREAD_SHORTEN is')
normspec([-Inf,x(number)],mu,sigma)
else
fprintf('Temporarily steady,balance will be broke! \n\n')
end

else
disp('insufficient data,deduce nothing!')

end

fprintf('当前值= .5f\n',x(number)) % 格式化输出
fprintf('期望= .5f\n',mu)
fprintf('标准差= .5f\n',sigma)
fprintf('方 差= .5f\n',variance)
fprintf('偏斜度= .5f\n',itsSkewness)
fprintf('峰值 = .5f\n',itsKurtosis)
fprintf('超出峰度 = .5f\n',itsKurtosis-3)
fprintf('4次方根峰值=.5f\n',itsKurtosis^1/4)

fprintf('最大值= .5f\n',Max)
fprintf('最小值= .5f\n',Min)
fprintf('极 差= .5f\n',s)
fprintf('数据个数= .5f\n',number)

disp('显示附有指数移动平均线,MACD,RSI的价差图')
figure(2);
subplot(3,1,1)
movavg(x,30,120,'e')
title('价差变动走势')
text(number,mu,' \leftarrow mu','FontSize',12,'Color','c')

subplot(3,1,2)
plot(macd(x))
title('价差MACD图')

subplot(3,1,3)
plot(rsindex(x))
title('价差RSI图')

发现了错别字? 请选中并且点击Ctrl+Enter发送!