-- 作者:guotx2010
-- 发布时间: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
|
-- 作者:solarhe2006
-- 发布时间:2012/11/3 11:16:53
--
\'代码中需要用到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 |
|