金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 132|回复: 0

【阿火秘笈】股指期货后台自动锁仓和换月移仓的代码,实现全自动化交易。

[复制链接]

1

主题

1

帖子

1

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2024-11-13
曾用名:
发表于 2024-11-26 15:15 | 显示全部楼层 |阅读模式
本帖最后由 技术006 于 2024-12-3 21:44 编辑

由于股指期货日内平仓手续费高昂,且基本每个月要移仓,在此分享本人使用多年的全自动化锁仓和移仓的后台代码。感兴趣的塔友们可根据此编程思路自行调整(注意:完全吸收理解的基础上,再去调整):
[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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 微信登录

本版积分规则

手机版|小黑屋|上海金之塔信息技术有限公司 ( 沪ICP备13035422号 )

GMT+8, 2024-12-22 00:59 , Processed in 0.202653 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表