您现在的位置:程序化交易>> 期货公式>> 金字塔等>> 金字塔知识>>正文内容

到期合约换月自动移仓代码 [金字塔]

  • 咨询内容:

    '代码中需要用到YcSet.ini文件,自己建立一个放到c:\,内容如下:

    [gen]

    AccountCount=1

    Account1_Code=888888

    Account1_Yc=1

    有n个账户需要自动移仓,则AccountCode=n,注意:多账户时需要机构版,同时登陆这些账户

    Account1中的1,是序号,有多个时,依次递增,_Code指定该账户的账号,_Yc为1表示要自动移仓,0则不要。

     

    '''''以下为代码

    public iMultipliter   

    public ZhuLiCode,AccountID   '主力合约,账户

    Sub APPLICATION_VBAStart()
     Call Application.SetTimer(2,1000*30)  '使用定时器30秒轮询
    End Sub

    Sub Application_Timer(ID)
     'Application.MsgOut CDate(Time)
     if weekday(cdate(date),vbMonday)>=6 then  '周六、周日不执行
      exit sub
     end if 
     '''''''''''''''''
     If CDate(Time)>=cdate("10:0000") then    '10:00-10:10判断是否需要移仓
      If CDate(Time)<=cdate("10:10:00") then
       TotalAccount=Cdbl(Document.GetPrivateProfileString("Gen","AccountCount",0,"C:\YcSet.Ini"))  '账户数
       For j=1 to TotalAccount
        CurrentAccount=Document.GetPrivateProfileString("Gen","Account" & Cstr(j) & "_Code",0,"C:\YcSet.Ini")  '本账户号码
        Yc=Document.GetPrivateProfileString("Gen","Account" & Cstr(j) & "_Yc",0,"C:\YcSet.Ini")  '是否移仓
        if Yc=1 then
         GetAllHolding CurrentAccount
        end if 
       Next 
      end if
     end if  
    End Sub

    '获取合约乘数
    Sub GetContract(sCode,sMarket)
     'Application.MsgOut sCode & "," & sMarket
     Call Order.Contract(sCode,sMarket,Multipliter,MinTick,ShortPercent,LongPercent)
     iMultipliter=Multipliter
     'application.MsgOut iMultipliter
     iMinTick=MinTick
     iShortPercent=ShortPercent
     iLongPercent=LongPercent
    End Sub

    Sub GetAllHolding(sAccount)
      dim i,k
      dim BuyHold
     dim BuyCost
     dim SellHold
     dim SellCost
     dim CurCode
     dim CurMarket

      On Error resume Next
     HoldingCount=Order.Holding2(sAccount)
      If HoldingCount>0 then
       For i=0 to HoldingCount-1
        Call Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,Code,Market,sAccount)
        CurCode=Code
        CurMarket=Market
        BuyHold=BuyHolding
        SellHold=SellHolding
        AutoYiCang CurCode,CurMarket,BuyHold,SellHold,sAccount  '自动移仓
       Next 
      End If 
    End Sub

    sub AutoYiCang(sCode,sMarket,iBuy,iSell,sAcc)
     dim PreCode
     dim i
     
     for i=1 to len(sCode)
      if isnumeric(mid(sCode,i,1)) then
       PreCode=left(sCode,i-1)
       exit for
      end if 
     next
     'application.MsgOut "i:" & i & ",PreCode:" & PreCode & ",sAcc:" & sAcc
     GetContractCode sMarket,PreCode
     if sCode<>ZhuLiCode And left(ZhuLiCode,i-1)=left(sCode,i-1) then  '不是持有主力合约
      if iBuy>0 then
       PingDuoDan 0,sCode,sMarket,iBuy,sAcc
       KaiDuoDan 0,ZhuLiCode,sMarket,iBuy,sAcc
      end if
      if iSell>0 then
       PingKongDan 0,sCode,sMarket,iSell,sAcc
       KaiKongDan 0,ZhuLiCode,sMarket,iSell,sAcc
      end if
     end if
    end sub

    Sub GetContractCode(sMarketCode,sStockPre)    '根据市场编码取得主力合约编码
     If sMarketCode="" then
      exit sub
     End if 
     
     contractvolume = 0
      
     n = marketdata.GetReportCount(sMarketCode)
     For j = 0 To n - 1
      Set report1 = marketdata.GetReportDataByIndex(sMarketCode, j)
      suffixlabel = Right(report1.Label, 2)
      If sStockPre=left(report1.Label,len(sStockPre)) then
       If suffixlabel = "00" Then
        ZhuLiVol=report1.volume
       End If
     
       If cdbl(suffixlabel) >= 1 And cdbl(suffixlabel) <= 12 Then
        If report1.volume = ZhuLiVol Then
         ZhuLiCode = report1.Label
         'application.MsgOut ZhuLiCode
         exit for
        End If
       End If
      End if 
     Next
    End Sub

    '开多单
    Sub KaiDuoDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '开多单,nPrice=0时为市价,否则就是传递过来的价
     'application.MsgOut "iOrdVol:" & iOrdVol & "," & sCode & "," & sMarket & "," & sAccount & "," & nPrice
     If iOrdVol>0 then
      If nPrice=0 then
       'application.MsgOut "sCode,sMarket,AccountID:" & sCode & "," & sMarket & "," & AccountID
       Call Order.Buy(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)     '市价开多单
      Else
       Call Order.Buy(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价开多单
      End If 
     End If
    End Sub

    '开空单
    Sub KaiKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '开空单,nPrice=0时为市价,否则就是传递过来的价
     'application.MsgOut "iOrdVol:" & iOrdVol & "," & sCode & "," & sMarket & "," & sAccount & "," & nPrice
     If iOrdVol>0 then
      If nPrice=0 then
       Call Order.BuyShort(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)    '市价开空单
      Else
       Call Order.BuyShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价开空单
      End If 
     End If
    End Sub

    '平多单
    Sub PingDuoDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '平多单,nPrice=0时为市价,否则就是传递过来的价
     If iOrdVol>0 then
      If nPrice=0 then
       Call Order.Sell(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)    '市价平多单
      Else
       Call Order.Sell(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价平多单
      End If 
     End If
    End Sub
    '平空单
    Sub PingKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '平空单,nPrice=0时为市价,否则就是传递过来的价
     If iOrdVol>0 then
      If nPrice=0 then
       Call Order.SellShort(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)    '市价平空单
      Else
       Call Order.SellShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价平空单
      End If 
     End If
    End Sub

     

     

     

     

  • 金字塔客服:

    '代码中需要用到YcSet.ini文件,自己建立一个放到c:\,内容如下:

    [gen]

    AccountCount=1

    Account1_Code=888888

    Account1_Yc=1

    有n个账户需要自动移仓,则AccountCode=n,注意:多账户时需要机构版,同时登陆这些账户

    Account1中的1,是序号,有多个时,依次递增,_Code指定该账户的账号,_Yc为1表示要自动移仓,0则不要。

     

    '''''以下为代码

    public iMultipliter   

    public ZhuLiCode,AccountID   '主力合约,账户

    Sub APPLICATION_VBAStart()
     Call Application.SetTimer(2,1000*30)  '使用定时器30秒轮询
    End Sub

    Sub Application_Timer(ID)
     'Application.MsgOut CDate(Time)
     if weekday(cdate(date),vbMonday)>=6 then  '周六、周日不执行
      exit sub
     end if 
     '''''''''''''''''
     If CDate(Time)>=cdate("10:0000") then    '10:00-10:10判断是否需要移仓
      If CDate(Time)<=cdate("10:10:00") then
       TotalAccount=Cdbl(Document.GetPrivateProfileString("Gen","AccountCount",0,"C:\YcSet.Ini"))  '账户数
       For j=1 to TotalAccount
        CurrentAccount=Document.GetPrivateProfileString("Gen","Account" & Cstr(j) & "_Code",0,"C:\YcSet.Ini")  '本账户号码
        Yc=Document.GetPrivateProfileString("Gen","Account" & Cstr(j) & "_Yc",0,"C:\YcSet.Ini")  '是否移仓
        if Yc=1 then
         GetAllHolding CurrentAccount
        end if 
       Next 
      end if
     end if  
    End Sub

    '获取合约乘数
    Sub GetContract(sCode,sMarket)
     'Application.MsgOut sCode & "," & sMarket
     Call Order.Contract(sCode,sMarket,Multipliter,MinTick,ShortPercent,LongPercent)
     iMultipliter=Multipliter
     'application.MsgOut iMultipliter
     iMinTick=MinTick
     iShortPercent=ShortPercent
     iLongPercent=LongPercent
    End Sub

    Sub GetAllHolding(sAccount)
      dim i,k
      dim BuyHold
     dim BuyCost
     dim SellHold
     dim SellCost
     dim CurCode
     dim CurMarket

      On Error resume Next
     HoldingCount=Order.Holding2(sAccount)
      If HoldingCount>0 then
       For i=0 to HoldingCount-1
        Call Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,Code,Market,sAccount)
        CurCode=Code
        CurMarket=Market
        BuyHold=BuyHolding
        SellHold=SellHolding
        AutoYiCang CurCode,CurMarket,BuyHold,SellHold,sAccount  '自动移仓
       Next 
      End If 
    End Sub

    sub AutoYiCang(sCode,sMarket,iBuy,iSell,sAcc)
     dim PreCode
     dim i
     
     for i=1 to len(sCode)
      if isnumeric(mid(sCode,i,1)) then
       PreCode=left(sCode,i-1)
       exit for
      end if 
     next
     'application.MsgOut "i:" & i & ",PreCode:" & PreCode & ",sAcc:" & sAcc
     GetContractCode sMarket,PreCode
     if sCode<>ZhuLiCode And left(ZhuLiCode,i-1)=left(sCode,i-1) then  '不是持有主力合约
      if iBuy>0 then
       PingDuoDan 0,sCode,sMarket,iBuy,sAcc
       KaiDuoDan 0,ZhuLiCode,sMarket,iBuy,sAcc
      end if
      if iSell>0 then
       PingKongDan 0,sCode,sMarket,iSell,sAcc
       KaiKongDan 0,ZhuLiCode,sMarket,iSell,sAcc
      end if
     end if
    end sub

    Sub GetContractCode(sMarketCode,sStockPre)    '根据市场编码取得主力合约编码
     If sMarketCode="" then
      exit sub
     End if 
     
     contractvolume = 0
      
     n = marketdata.GetReportCount(sMarketCode)
     For j = 0 To n - 1
      Set report1 = marketdata.GetReportDataByIndex(sMarketCode, j)
      suffixlabel = Right(report1.Label, 2)
      If sStockPre=left(report1.Label,len(sStockPre)) then
       If suffixlabel = "00" Then
        ZhuLiVol=report1.volume
       End If
     
       If cdbl(suffixlabel) >= 1 And cdbl(suffixlabel) <= 12 Then
        If report1.volume = ZhuLiVol Then
         ZhuLiCode = report1.Label
         'application.MsgOut ZhuLiCode
         exit for
        End If
       End If
      End if 
     Next
    End Sub

    '开多单
    Sub KaiDuoDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '开多单,nPrice=0时为市价,否则就是传递过来的价
     'application.MsgOut "iOrdVol:" & iOrdVol & "," & sCode & "," & sMarket & "," & sAccount & "," & nPrice
     If iOrdVol>0 then
      If nPrice=0 then
       'application.MsgOut "sCode,sMarket,AccountID:" & sCode & "," & sMarket & "," & AccountID
       Call Order.Buy(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)     '市价开多单
      Else
       Call Order.Buy(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价开多单
      End If 
     End If
    End Sub

    '开空单
    Sub KaiKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '开空单,nPrice=0时为市价,否则就是传递过来的价
     'application.MsgOut "iOrdVol:" & iOrdVol & "," & sCode & "," & sMarket & "," & sAccount & "," & nPrice
     If iOrdVol>0 then
      If nPrice=0 then
       Call Order.BuyShort(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)    '市价开空单
      Else
       Call Order.BuyShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价开空单
      End If 
     End If
    End Sub

    '平多单
    Sub PingDuoDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '平多单,nPrice=0时为市价,否则就是传递过来的价
     If iOrdVol>0 then
      If nPrice=0 then
       Call Order.Sell(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)    '市价平多单
      Else
       Call Order.Sell(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价平多单
      End If 
     End If
    End Sub
    '平空单
    Sub PingKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount)  '平空单,nPrice=0时为市价,否则就是传递过来的价
     If iOrdVol>0 then
      If nPrice=0 then
       Call Order.SellShort(1,iOrdVol,0,0,sCode,sMarket,sAccount,0)    '市价平空单
      Else
       Call Order.SellShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0)   '限价平空单
      End If 
     End If
    End Sub

     

  • 用户回复: 感觉有些复杂,建议是否将自动换月的功能加入到软件中. 比如可以在主连上跑策略,程序自动实现换月.

     

  • 网友回复: 还有人关注此贴吗,好像不能映射大连市场的几组连续合约,J00和JD00,P00和PP00会搞混,不知什么原因,请老师解惑2016-09-23 21:00:00.119    【图表】TA00 运行完毕2016-09-23 21:00:00.119    【图表】MA00 运行完毕2016-09-23 21:00:00.119    【图表】RM00 运行完毕2016-09-23 21:00:00.119    【图表】CF00 运行完毕2016-09-23 21:00:01.133    【图表】FG00 运行完毕2016-09-23 21:00:01.133    【图表】ZC00 运行完毕2016-09-23 21:00:02.147    【图表】J01 运行完毕2016-09-23 21:00:02.147    【图表】M00 运行完毕2016-09-23 21:00:02.147    【图表】RU00 运行完毕2016-09-23 21:00:02.147    【图表】Y00 运行完毕2016-09-23 21:00:02.147    【图表】HC00 运行完毕2016-09-23 21:00:02.147    【图表】RB00 运行完毕2016-09-23 21:00:02.147    【图表】AL00 运行完毕2016-09-23 21:00:02.147    【图表】I00 运行完毕2016-09-23 21:25:12.401    【下单】J01 价0.000000 量1 买卖1 类型1 开平1 账户600802 Formula 12016-09-23 21:25:12.417    【下单】JD01 价0.000000 量1 买卖0 类型1 开平0 账户600802 Formula 12016-09-23 21:25:12.417    【指令】收到回报指令 ID = 3042061372016-09-23 21:25:12.433    【回报】600802 : j1701 - 已报单 1 价格:1249.5 平 卖2016-09-23 21:25:12.511    【指令】收到回报指令 ID = 3042061382016-09-23 21:25:12.511    【回报】600802 : jd1701 - 已报单 1 价格:3378 开 买2016-09-23 21:25:12.511    【指令】收到回报指令 ID = 3042061372016-09-23 21:25:12.587    【指令】收到回报指令 ID = 3042061382016-09-23 21:25:12.597    【回报】600802 : JD01 鸡蛋1701 - 已撤单 量:12016-09-23 21:25:12.597    【指令】收到回报指令 ID = 3042061382016-09-23 21:25:12.597    【指令】收到回报指令 ID = 3042061372016-09-23 21:25:12.657    【指令】收到成交回报指令 ORDERID = 3042061372016-09-23 21:25:12.677    【回报】600802 : j1701 - 已成交 1 价格:1250.5 平 卖2016-09-23 21:25:12.677    【回报】600802 : j1701 - 全部成交 12016-09-23 21:27:47.744    【同步】600802 : J01 理论持仓 多1 空0 实际持仓 多0 空02016-09-23 21:27:47.744    【图表】J01 理论持仓比实际持仓大,需要补仓2016-09-23 21:27:47.764    【下单】J01 价0.000000 量1 买卖0 类型1 开平0 账户600802 Formula 12016-09-23 21:27:47.774    【指令】收到回报指令 ID = 3042061392016-09-23 21:27:47.774    【回报】600802 : j1701 - 已报单 1 价格:1250.0 开 买2016-09-23 21:27:47.774    【指令】收到回报指令 ID = 3042061392016-09-23 21:27:47.774    【指令】收到回报指令 ID = 3042061392016-09-23 21:27:47.784    【指令】收到成交回报指令 ORDERID = 3042061392016-09-23 21:27:47.794    【回报】600802 : j1701 - 已成交 1 价格:1249.0 开 买2016-09-23 21:27:47.794    【回报】600802 : j1701 - 全部成交 12016-09-23 21:31:12.647    【下单】P01 价0.000000 量1 买卖1 类型1 开平1 账户600802 Formula 12016-09-23 21:31:12.647    【下单】PP01 价0.000000 量1 买卖0 类型1 开平0 账户600802 Formula 12016-09-23 21:31:12.663    【指令】收到回报指令 ID = 3042061402016-09-23 21:31:12.725    【回报】600802 : p1701 - 已报单 1 价格:5706 平 卖2016-09-23 21:31:12.742    【指令】收到回报指令 ID = 3042061402016-09-23 21:31:12.742    【指令】收到回报指令 ID = 3042061402016-09-23 21:31:12.793    【指令】收到成交回报指令 ORDERID = 3042061402016-09-23 21:31:12.803    【回报】600802 : p1701 - 已成交 1 价格:5710 平 卖2016-09-23 21:31:12.803    【回报】600802 : p1701 - 全部成交 12016-09-23 21:31:12.853    【指令】收到回报指令 ID = 3042061412016-09-23 21:31:12.863    【回报】600802 : pp1701 - 已报单 1 价格:7079 开 买2016-09-23 21:31:12.903    【指令】收到回报指令 ID = 3042061412016-09-23 21:31:12.913    【回报】600802 : PP01 聚丙烯1701 - 已撤单 量:12016-09-23 21:31:12.913    【指令】收到回报指令 ID = 304206141

 

有思路,想编写各种指标公式,程序化交易模型,选股公式,预警公式的朋友

可联系技术人员 QQ: 511411198  点击这里给我发消息进行 有偿 编写!不贵!点击查看价格!


【字体: 】【打印文章】【查看评论

相关文章

    没有相关内容