[PEL] 复制代码
//后台加载在股指连续合约上即可。
globalvariable:abb=0;
//在此以双均线模型为例,大家用自己的模型替换双均线模型即可。
variable:hold1=0;
ma1:=ma(c,10);
ma2:=ma(c,20);
if hold1>0 and ma1<ma2 then hold1:=0;
if hold1<0 and ma1>ma2 then hold1:=0;
if hold1=0 and ma1>ma2 then hold1:=1;
if hold1=0 and ma1<ma2 then hold1:=-1;
zongcc:=ref(hold1,1);//最终的模型信号赋值给zongcc
if not(islastbar) or workmode<>1 then exit;
dnsj:=currenttime;
tm:=mod(dnsj,100);
flow:=2;//单次最多下单量
daima:='IF';
ac:='10002351';//改成自己的实盘账号
pinzhong:=daima&numtostrex(newcang,0,2);//参数1 newcang(最新的主力合约)
laststk:=daima&numtostrex(oldcang,0,2);//参数2 oldcang(即将被替代的合约)
xdtm:=30;//每分钟同步的时间点
if abb=2 and tm>=xdtm-3 and tm<xdtm then begin
abb:=0;
tcancelex(1,0,ac,pinzhong);
tcancelex(1,0,ac,laststk);
end
if abb=0 and tm>=xdtm and tm<xdtm+10 then begin
huadian:=1*mindiff;
orderbuy:=dynainfo2(20,pinzhong);//新仓的委买价
ordersell:=dynainfo2(21,pinzhong);//新仓的委卖价
lastorderbuy:=dynainfo2(20,laststk);//旧仓的委买价
lastordersell:=dynainfo2(21,laststk);//旧仓的委卖价
lastbuyhold:=tbuyholdingex(ac,laststk,1);
lastsellhold:=tsellholdingex(ac,laststk,1);
buyhold0:=tbuyholdingex(ac,pinzhong,0);
sellhold0:=tsellholdingex(ac,pinzhong,0);
buyhold:=tbuyholdingex(ac,pinzhong,1);
sellhold:=tsellholdingex(ac,pinzhong,1);
dingdan0:=zongcc-(buyhold-sellhold)-(lastbuyhold-lastsellhold);
dingdan:=sgn(dingdan0)*min(flow,abs(dingdan0));
//新、旧合约合并考虑。要平仓的话,先平旧合约;开新仓的话,开到新合约。
if dingdan>0.5 then begin
pc1:=min(dingdan,lastsellhold);
pc2:=if(sellhold0>0.5,0,min(dingdan-pc1,sellhold));
kc:=dingdan-pc1-pc2;
if pc1>0.5 then begin
if lastorderbuy>1 and lastordersell>1 then tsellshort(1,pc1,lmt,lastorderbuy+huadian,0,ac,laststk),allowrepeat;
end
if orderbuy>1 and ordersell>1 then begin
if pc2>0.5 then tsellshort(1,pc2,lmt,orderbuy+huadian,0,ac,pinzhong),allowrepeat;
if kc>0.5 then tbuy(1,kc,lmt,orderbuy+huadian,0,ac,pinzhong),allowrepeat;
end
end
if dingdan<-0.5 then begin
pc1:=min(-dingdan,lastbuyhold);
pc2:=if(buyhold0>0.5,0,min(-dingdan-pc1,buyhold));
kc:=-dingdan-pc1-pc2;
if pc1>0.5 then begin
if lastorderbuy>1 and lastordersell>1 then tsell(1,pc1,lmt,lastordersell-huadian,0,ac,laststk),allowrepeat;
end
if orderbuy>1 and ordersell>1 then begin
if pc2>0.5 then tsell(1,pc2,lmt,ordersell-huadian,0,ac,pinzhong),allowrepeat;
if kc>0.5 then tbuyshort(1,kc,lmt,ordersell-huadian,0,ac,pinzhong),allowrepeat;
end
end
if dingdan=0 and time>=143000 and (lastbuyhold>0.5 or lastsellhold>0.5) then begin//14:30启动移仓
lastdingdan0:=0-(lastbuyhold-lastsellhold);
lastdingdan:=sgn(lastdingdan0)*min(flow,abs(lastdingdan0));
if lastdingdan<-0.5 then begin
pc2:=-lastdingdan;
if lastorderbuy>1 and lastordersell>1 then tsell(1,pc2,lmt,lastordersell-huadian,0,ac,laststk),allowrepeat;
if orderbuy>1 and ordersell>1 then tbuy(1,pc2,lmt,orderbuy+huadian,0,ac,pinzhong),allowrepeat;
end
if lastdingdan>0.5 then begin
pc2:=lastdingdan;
if lastorderbuy>1 and lastordersell>1 then tsellshort(1,pc2,lmt,lastordersell+huadian,0,ac,laststk),allowrepeat;
if orderbuy>1 and ordersell>1 then tbuyshort(1,pc2,lmt,orderbuy-huadian,0,ac,pinzhong),allowrepeat;
end
if lastdingdan=0 and lastbuyhold>0.5 then begin
pc1:=min(flow,lastbuyhold);
chaju:=lastordersell-lastorderbuy;
xx:=if(chaju>0.5,1,0);
if lastorderbuy>1 and lastordersell>1 then begin
tsellshort(1,pc1,lmt,lastorderbuy+xx*0.2,0,ac,laststk),allowrepeat;
tsell(1,pc1,lmt,lastordersell-xx*0.2,0,ac,laststk),allowrepeat;
end
end
end
abb:=2;
end