在2012年,我开始涉足股指期货套利交易。
在尚未购买一家名为fansoft公司的股指套利软件之前,我只能使用最简单和原始的手段,比如说,我借助于matlab,编写简单的分析程序,以寻找套利的机会和评估风险及利润空间。
(仅为证明我并未虚构此书,你不必细看。)
简单列一些片段如下,以帮助您了解一个股票期指套利交易员的交易动机和所关注的指标:
下面包括的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 d(intn(i,1),100)<=10 && q(i,1)<0.001 %如果买入股票数量对100取余结果小于10 并且其权重小于 千分之一;
tmp1=intn(i,1); %则舍去余数,只购买1手整数倍;
intn(i,1)=intn(i,1)-d(intn(i,1),100);
fprintf('股票 %i数量权重为%11.3f 应买入数量为 %11.3f ',code(i,1),rawn(i,1),intn(i,1))
fprintf('丢失股票数量为 %i ; 其权重为%8.4f\n',d(tmp1,100),q(i,1))
else %其它情况,则购买对100取整数部分加100股;
tmp=intn(i,1);
intn(i,1)=intn(i,1)-d(intn(i,1),100)+100; %计算下一交易日应卖掉数量;
nextdaysell=100-d(tmp,100);
sumsell=sumsell+nextdaysell; %自加求和;
fprintf('股票 %i数量权重为%11.3f 应买入数量为 %11.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') % 输入当前股指值(va露e)矩阵;
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 va露e
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 % co露mns 观看特殊值;
for j=i+1:5
if abs(c(i,j))>abs(c(i+1,j))
continue % 进行下一次迭代;
else
fprintf('exception! co露mn %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('当前值= %12.5f\n',x(number)) % 格式化输出
fprintf('期望= %12.5f\n',mu)
fprintf('标准差= %12.5f\n',sigma)
fprintf('方 差= %12.5f\n',variance)
fprintf('偏斜度= %12.5f\n',itsskewness)
fprintf('峰值 = %12.5f\n',itskurtosis)
fprintf('超出峰度 = %12.5f\n',itskurtosis-3)
fprintf('4次方根峰值=%12.5f\n',itskurtosis^1/4)
fprintf('最大值= %12.5f\n',max)
fprintf('最小值= %12.5f\n',min)
fprintf('极 差= %12.5f\n',s)
fprintf('数据个数= %12.5f\n',number)
disp('显示附有指数移动平均线,macd,rsi的价差图')
figure(2);
subplot(3,1,1)
vavg(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图')
但是,正如我曾经强调过的:直觉和经验,远比数学和逻辑重要。
如果你不相信,请去问问那些顶尖的交易员们。