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