欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。


金字塔客服中心 - 专业程序化交易软件提供商金字塔软件高级功能研发区 → 到期合约换月自动移仓代码

   

欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。    


  共有8251人关注过本帖树形打印复制链接

主题:到期合约换月自动移仓代码

帅哥哟,离线,有人找我吗?
guotx2010
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:蜘蛛侠 帖子:1366 积分:5210 威望:0 精华:7 注册:2010/12/11 18:00:33
老师帮忙编写个程序  发帖心情 Post By:2012/11/3 9:06:00 [显示全部帖子]

'代码中需要用到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

 

 

 



查看使用道具详细信息
获赠金币帖,共获得 0 个金币
 回到顶部