以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://weistock.com/bbs/index.asp)
--  公式模型编写问题提交  (http://weistock.com/bbs/list.asp?boardid=4)
----  [原创]主力跟踪交易(大部分品种都可以)  (http://weistock.com/bbs/dispbbs.asp?boardid=4&id=3250)

--  作者:明心
--  发布时间:2010/10/14 14:36:14
--  [原创]主力跟踪交易(大部分品种都可以)

说明:这个是用RB作为代表的,只要是1——12合约的品种,只要直接用代码替换RB即可,如“CU”替换“RB”,其他的都不用动的。

具体后台交易的时候,最好监控相对应的品种指数,如这里监控RB指数。

 

input:账户(0,0,999999999999);
input:手数(1,1,1000);
      
//螺纹合约信息
//rb01
rb01close:="rb01$close";
rb01OPENINT:="rb01$OPENINT";
//rb02
rb02close:="rb02$close";
rb02OPENINT:="rb02$OPENINT";
//rb03
rb03close:="rb03$close";
rb03OPENINT:="rb03$OPENINT";
//rb04
rb04close:="rb04$close";
rb04OPENINT:="rb04$OPENINT";
//rb05
rb05close:="rb05$close";
rb05OPENINT:="rb05$OPENINT";
//rb06
rb06close:="rb06$close";
rb06OPENINT:="rb06$OPENINT";
//rb07
rb07close:="rb07$close";
rb07OPENINT:="rb07$OPENINT";
//rb08
rb08close:="rb08$close";
rb08OPENINT:="rb08$OPENINT";
//rb09
rb09close:="rb09$close";
rb09OPENINT:="rb09$OPENINT";
//rb10
rb10close:="rb10$close";
rb10OPENINT:="rb10$OPENINT";
//rb11
rb11close:="rb11$close";
rb11OPENINT:="rb11$OPENINT";
//rb12
rb12close:="rb12$close";
rb12OPENINT:="rb12$OPENINT";
//判断主力合约
maxOPENINT:=
MAX(rb01OPENINT ,
MAX(rb02OPENINT ,
MAX(rb03OPENINT ,
MAX(rb04OPENINT ,
MAX(rb05OPENINT ,
MAX(rb06OPENINT ,
MAX(rb07OPENINT ,
MAX(rb08OPENINT ,
MAX(rb09OPENINT ,
MAX(rb10OPENINT ,
MAX(rb11OPENINT ,
rb12OPENINT) ) ) ) ) ) ) ) ) ) );
yf:=if(rb01OPENINT=maxOPENINT,1,
if(rb02OPENINT=maxOPENINT,2,
if(rb03OPENINT=maxOPENINT,3,
if(rb04OPENINT=maxOPENINT,4,
if(rb05OPENINT=maxOPENINT,5,
if(rb06OPENINT=maxOPENINT,6,
if(rb07OPENINT=maxOPENINT,7,
if(rb08OPENINT=maxOPENINT,8,
if(rb09OPENINT=maxOPENINT,9,
if(rb10OPENINT=maxOPENINT,10,
if(rb11OPENINT=maxOPENINT,11,
12))))))))))),LINETHICK0;
//主力合约代码字符串
if yf<10 then
   begin
     dm:=NUMTOSTR(yf,0);
     hy:=STRCAT(\'0\',dm);
   end
else
     hy:=NUMTOSTR(yf,0);
     
zlhy:=STRCAT(\'RB\',hy);
DRAWTEXTEX(close>0,0,2,0,zlhy);

//买卖价格
mxclose:=if(rb01OPENINT=maxOPENINT,rb01close,
if(rb02OPENINT=maxOPENINT,rb02close,
if(rb03OPENINT=maxOPENINT,rb03close,
if(rb04OPENINT=maxOPENINT,rb04close,
if(rb05OPENINT=maxOPENINT,rb05close,
if(rb06OPENINT=maxOPENINT,rb06close,
if(rb07OPENINT=maxOPENINT,rb07close,
if(rb08OPENINT=maxOPENINT,rb08close,
if(rb09OPENINT=maxOPENINT,rb09close,
if(rb10OPENINT=maxOPENINT,rb10close,
if(rb11OPENINT=maxOPENINT,rb11close,
rb12close))))))))))),LINETHICK0;

//策略
dt:=cross(ma(close,10),ma(close,30));
kt:=cross(ma(close,30),ma(close,10));

//----------------交易指令开始---------------//
//没有持有该品种时——买卖指令
if TSELLHOLDINGEX(账户,zlhy,1)=0 and  TBUYHOLDINGEX(账户,zlhy,1)=0 then
    begin
        TBUY(dt,手数,lmt,(mxclose+MINDIFF*10),账户,zlhy);
        TBUYSHORT(kt,手数,lmt,(mxclose+MINDIFF*10),账户,zlhy);
    end
        
//持有该品种多单时——买卖指令
if TSELLHOLDINGEX(账户,zlhy,1)=0 and  TBUYHOLDINGEX(账户,zlhy,1)>0 then
    begin
        TSELL(kt,TBUYHOLDINGEX(账户,zlhy,1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
        if  TBUYHOLDINGEX(账户,zlhy,1)<手数  then  TBUY(dt,(手数-TBUYHOLDINGEX(账户,zlhy,1)),lmt,(mxclose+MINDIFF*10),账户,zlhy);
    end

//持有该品种空单时——买卖指令
if TSELLHOLDINGEX(账户,zlhy,1)>0 and  TBUYHOLDINGEX(账户,zlhy,1)=0 then
    begin
        TSELLSHORT(dt,TBUYHOLDINGEX(账户,zlhy,1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
        if TSELLHOLDINGEX(账户,zlhy,1)<手数  then  TBUYSHORT(kt,(手数-TBUYHOLDINGEX(账户,zlhy,1)),lmt,(mxclose-MINDIFF*10),账户,zlhy);        
    end

//多空单子同时持有——买卖指令
if TSELLHOLDINGEX(账户,zlhy,1)>0 and  TBUYHOLDINGEX(账户,zlhy,1)>0 then
     begin
         TSELL(kt,TBUYHOLDINGEX(账户,zlhy,1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
         TSELLSHORT(dt,TSELLHOLDINGEX(账户,zlhy,1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
         if TBUYHOLDINGEX(账户,zlhy,1)<手数  then  TBUY(dt,(手数-TBUYHOLDINGEX(账户,zlhy,1)),lmt,(mxclose+MINDIFF*10),账户,zlhy);         
         if TSELLHOLDINGEX(账户,zlhy,1)<手数  then  TBUYSHORT(kt,(手数-TSELLHOLDINGEX(账户,zlhy,1)),lmt,(mxclose-MINDIFF*10),账户,zlhy);
     end

[此贴子已经被作者于2010-10-14 14:40:33编辑过]

--  作者:董小球
--  发布时间:2010/10/14 14:44:23
--  
不错 这个加精~~~感谢明心
--  作者:明心
--  发布时间:2010/10/14 14:46:59
--  
交易指令部分好像还有点问题,稍后补充一下。不好意思。刚写好,就发上来了,还没有来得及完善呢。呵呵
--  作者:fly
--  发布时间:2010/10/14 15:05:57
--  
感谢共享,支持加精
--  作者:明心
--  发布时间:2010/10/14 23:53:54
--  

从交易指令开始的部分换成以下内容,就可以在图表上显示了。

buy(dt);sell(kt);buyshort(kt);sellshort(dt);

 

图表交易中好像不能指定品种交易的把。

[此贴子已经被作者于2010-10-14 23:57:58编辑过]

--  作者:明心
--  发布时间:2010/10/15 11:26:14
--  

交易指令补充如下(跟在交易指令模块后面即可):

//-----------换合约开始-----------------//
//rb01
if rb01OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb01\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb01\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb01\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb01\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb01\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb01\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb01\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb01\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end


//rb02
if rb02OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb02\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb02\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb02\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb02\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb02\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb02\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb02\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb02\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
//rb03
if rb03OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb03\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb03\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb03\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb03\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb03\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb03\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb03\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb03\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end

//rb04
if rb04OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb04\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb04\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb04\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb04\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb04\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb04\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb04\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb04\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
   
 //rb05
if rb05OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb05\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb05\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb05\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb05\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb05\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb05\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb05\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb05\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
    
》》》》由于字数限制,下面部分请看下帖。 


--  作者:明心
--  发布时间:2010/10/15 11:26:27
--  
//rb06
if rb06OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb06\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb06\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb06\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb06\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb06\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb06\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb06\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb06\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   

 //rb07
if rb07OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb07\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb07\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb07\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb07\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb07\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb07\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb07\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb07\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
 //rb08
if rb08OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb08\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb08\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb08\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb08\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb08\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb08\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb08\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb08\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
   
 //rb09
if rb09OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb09\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb09\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb09\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb09\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb09\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb09\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb09\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb09\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
   
 //rb10
if rb10OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb10\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb10\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb10\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb10\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb10\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb10\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb10\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb10\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
   
 //rb11
if rb11OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb11\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb11\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb11\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb11\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb11\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb11\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb11\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb11\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end
   
   
//rb12
if rb12OPENINT<>maxOPENINT then
   begin
        if TBUYHOLDINGEX(账户,\'rb12\',1)>0 then
           begin
                TSELL(close>0,TBUYHOLDINGEX(账户,\'rb12\',1),lmt,(mxclose-MINDIFF*10),账户,\'rb12\');
                TBUY(close>0,TBUYHOLDINGEX(账户,\'rb12\',1),lmt,(mxclose+MINDIFF*10),账户,zlhy);
           end
          
         if TSELLHOLDINGEX(账户,\'rb12\',1)>0 then
           begin
                TSELLshort(close>0,TSELLHOLDINGEX(账户,\'rb12\',1),lmt,(mxclose+MINDIFF*10),账户,\'rb12\');
                TBUYshort(close>0,TSELLHOLDINGEX(账户,\'rb12\',1),lmt,(mxclose-MINDIFF*10),账户,zlhy);
           end
    end


--  作者:QJ
--  发布时间:2011/6/28 3:39:33
--  

顶起