到期合約換月自動(dòng)移倉代碼 [金字塔]
- 咨詢內(nèi)容:
'代碼中需要用到Y(jié)cSet.ini文件,自己建立一個(gè)放到c:\,內(nèi)容如下:
[gen]
AccountCount=1
Account1_Code=888888
Account1_Yc=1
有n個(gè)賬戶需要自動(dòng)移倉,則AccountCode=n,注意:多賬戶時(shí)需要機(jī)構(gòu)版,同時(shí)登陸這些賬戶
Account1中的1,是序號(hào),有多個(gè)時(shí),依次遞增,_Code指定該賬戶的賬號(hào),_Yc為1表示要自動(dòng)移倉,0則不要。
'''''以下為代碼
public iMultipliter
public ZhuLiCode,AccountID '主力合約,賬戶
Sub APPLICATION_VBAStart()
Call Application.SetTimer(2,1000*30) '使用定時(shí)器30秒輪詢
End SubSub Application_Timer(ID)
'Application.MsgOut CDate(Time)
if weekday(cdate(date),vbMonday)>=6 then '周六、周日不執(zhí)行
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")) '賬戶數(shù)
For j=1 to TotalAccount
CurrentAccount=Document.GetPrivateProfileString("Gen","Account" & Cstr(j) & "_Code",0,"C:\YcSet.Ini") '本賬戶號(hào)碼
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'獲取合約乘數(shù)
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 SubSub GetAllHolding(sAccount)
dim i,k
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarketOn 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 '自動(dòng)移倉
Next
End If
End Subsub 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 subSub GetContractCode(sMarketCode,sStockPre) '根據(jù)市場編碼取得主力合約編碼
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時(shí)為市價(jià),否則就是傳遞過來的價(jià)
'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) '市價(jià)開多單
Else
Call Order.Buy(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)開多單
End If
End If
End Sub'開空單
Sub KaiKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount) '開空單,nPrice=0時(shí)為市價(jià),否則就是傳遞過來的價(jià)
'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) '市價(jià)開空單
Else
Call Order.BuyShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)開空單
End If
End If
End Sub'平多單
Sub PingDuoDan(nPrice,sCode,sMarket,iOrdVol,sAccount) '平多單,nPrice=0時(shí)為市價(jià),否則就是傳遞過來的價(jià)
If iOrdVol>0 then
If nPrice=0 then
Call Order.Sell(1,iOrdVol,0,0,sCode,sMarket,sAccount,0) '市價(jià)平多單
Else
Call Order.Sell(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)平多單
End If
End If
End Sub
'平空單
Sub PingKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount) '平空單,nPrice=0時(shí)為市價(jià),否則就是傳遞過來的價(jià)
If iOrdVol>0 then
If nPrice=0 then
Call Order.SellShort(1,iOrdVol,0,0,sCode,sMarket,sAccount,0) '市價(jià)平空單
Else
Call Order.SellShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)平空單
End If
End If
End Sub - 金字塔客服:
'代碼中需要用到Y(jié)cSet.ini文件,自己建立一個(gè)放到c:\,內(nèi)容如下:
[gen]
AccountCount=1
Account1_Code=888888
Account1_Yc=1
有n個(gè)賬戶需要自動(dòng)移倉,則AccountCode=n,注意:多賬戶時(shí)需要機(jī)構(gòu)版,同時(shí)登陸這些賬戶
Account1中的1,是序號(hào),有多個(gè)時(shí),依次遞增,_Code指定該賬戶的賬號(hào),_Yc為1表示要自動(dòng)移倉,0則不要。
'''''以下為代碼
public iMultipliter
public ZhuLiCode,AccountID '主力合約,賬戶
Sub APPLICATION_VBAStart()
Call Application.SetTimer(2,1000*30) '使用定時(shí)器30秒輪詢
End SubSub Application_Timer(ID)
'Application.MsgOut CDate(Time)
if weekday(cdate(date),vbMonday)>=6 then '周六、周日不執(zhí)行
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")) '賬戶數(shù)
For j=1 to TotalAccount
CurrentAccount=Document.GetPrivateProfileString("Gen","Account" & Cstr(j) & "_Code",0,"C:\YcSet.Ini") '本賬戶號(hào)碼
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'獲取合約乘數(shù)
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 SubSub GetAllHolding(sAccount)
dim i,k
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarketOn 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 '自動(dòng)移倉
Next
End If
End Subsub 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 subSub GetContractCode(sMarketCode,sStockPre) '根據(jù)市場編碼取得主力合約編碼
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時(shí)為市價(jià),否則就是傳遞過來的價(jià)
'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) '市價(jià)開多單
Else
Call Order.Buy(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)開多單
End If
End If
End Sub'開空單
Sub KaiKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount) '開空單,nPrice=0時(shí)為市價(jià),否則就是傳遞過來的價(jià)
'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) '市價(jià)開空單
Else
Call Order.BuyShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)開空單
End If
End If
End Sub'平多單
Sub PingDuoDan(nPrice,sCode,sMarket,iOrdVol,sAccount) '平多單,nPrice=0時(shí)為市價(jià),否則就是傳遞過來的價(jià)
If iOrdVol>0 then
If nPrice=0 then
Call Order.Sell(1,iOrdVol,0,0,sCode,sMarket,sAccount,0) '市價(jià)平多單
Else
Call Order.Sell(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)平多單
End If
End If
End Sub
'平空單
Sub PingKongDan(nPrice,sCode,sMarket,iOrdVol,sAccount) '平空單,nPrice=0時(shí)為市價(jià),否則就是傳遞過來的價(jià)
If iOrdVol>0 then
If nPrice=0 then
Call Order.SellShort(1,iOrdVol,0,0,sCode,sMarket,sAccount,0) '市價(jià)平空單
Else
Call Order.SellShort(0,iOrdVol,nPrice,0,sCode,sMarket,sAccount,0) '限價(jià)平空單
End If
End If
End Sub - 用戶回復(fù):
感覺有些復(fù)雜,建議是否將自動(dòng)換月的功能加入到軟件中. 比如可以在主連上跑策略,程序自動(dòng)實(shí)現(xiàn)換月.
- 網(wǎng)友回復(fù):
還有人關(guān)注此貼嗎,好像不能映射大連市場的幾組連續(xù)合約,J00和JD00,P00和PP00會(huì)搞混,不知什么原因,請老師解惑2016-09-23 21:00:00.119 【圖表】TA00 運(yùn)行完畢2016-09-23 21:00:00.119 【圖表】MA00 運(yùn)行完畢2016-09-23 21:00:00.119 【圖表】RM00 運(yùn)行完畢2016-09-23 21:00:00.119 【圖表】CF00 運(yùn)行完畢2016-09-23 21:00:01.133 【圖表】FG00 運(yùn)行完畢2016-09-23 21:00:01.133 【圖表】ZC00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】J01 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】M00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】RU00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】Y00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】HC00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】RB00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】AL00 運(yùn)行完畢2016-09-23 21:00:02.147 【圖表】I00 運(yùn)行完畢2016-09-23 21:25:12.401 【下單】J01 價(jià)0.000000 量1 買賣1 類型1 開平1 賬戶600802 Formula 12016-09-23 21:25:12.417 【下單】JD01 價(jià)0.000000 量1 買賣0 類型1 開平0 賬戶600802 Formula 12016-09-23 21:25:12.417 【指令】收到回報(bào)指令 ID = 3042061372016-09-23 21:25:12.433 【回報(bào)】600802 : j1701 - 已報(bào)單 1 價(jià)格:1249.5 平 賣2016-09-23 21:25:12.511 【指令】收到回報(bào)指令 ID = 3042061382016-09-23 21:25:12.511 【回報(bào)】600802 : jd1701 - 已報(bào)單 1 價(jià)格:3378 開 買2016-09-23 21:25:12.511 【指令】收到回報(bào)指令 ID = 3042061372016-09-23 21:25:12.587 【指令】收到回報(bào)指令 ID = 3042061382016-09-23 21:25:12.597 【回報(bào)】600802 : JD01 雞蛋1701 - 已撤單 量:12016-09-23 21:25:12.597 【指令】收到回報(bào)指令 ID = 3042061382016-09-23 21:25:12.597 【指令】收到回報(bào)指令 ID = 3042061372016-09-23 21:25:12.657 【指令】收到成交回報(bào)指令 ORDERID = 3042061372016-09-23 21:25:12.677 【回報(bào)】600802 : j1701 - 已成交 1 價(jià)格:1250.5 平 賣2016-09-23 21:25:12.677 【回報(bào)】600802 : j1701 - 全部成交 12016-09-23 21:27:47.744 【同步】600802 : J01 理論持倉 多1 空0 實(shí)際持倉 多0 空02016-09-23 21:27:47.744 【圖表】J01 理論持倉比實(shí)際持倉大,需要補(bǔ)倉2016-09-23 21:27:47.764 【下單】J01 價(jià)0.000000 量1 買賣0 類型1 開平0 賬戶600802 Formula 12016-09-23 21:27:47.774 【指令】收到回報(bào)指令 ID = 3042061392016-09-23 21:27:47.774 【回報(bào)】600802 : j1701 - 已報(bào)單 1 價(jià)格:1250.0 開 買2016-09-23 21:27:47.774 【指令】收到回報(bào)指令 ID = 3042061392016-09-23 21:27:47.774 【指令】收到回報(bào)指令 ID = 3042061392016-09-23 21:27:47.784 【指令】收到成交回報(bào)指令 ORDERID = 3042061392016-09-23 21:27:47.794 【回報(bào)】600802 : j1701 - 已成交 1 價(jià)格:1249.0 開 買2016-09-23 21:27:47.794 【回報(bào)】600802 : j1701 - 全部成交 12016-09-23 21:31:12.647 【下單】P01 價(jià)0.000000 量1 買賣1 類型1 開平1 賬戶600802 Formula 12016-09-23 21:31:12.647 【下單】PP01 價(jià)0.000000 量1 買賣0 類型1 開平0 賬戶600802 Formula 12016-09-23 21:31:12.663 【指令】收到回報(bào)指令 ID = 3042061402016-09-23 21:31:12.725 【回報(bào)】600802 : p1701 - 已報(bào)單 1 價(jià)格:5706 平 賣2016-09-23 21:31:12.742 【指令】收到回報(bào)指令 ID = 3042061402016-09-23 21:31:12.742 【指令】收到回報(bào)指令 ID = 3042061402016-09-23 21:31:12.793 【指令】收到成交回報(bào)指令 ORDERID = 3042061402016-09-23 21:31:12.803 【回報(bào)】600802 : p1701 - 已成交 1 價(jià)格:5710 平 賣2016-09-23 21:31:12.803 【回報(bào)】600802 : p1701 - 全部成交 12016-09-23 21:31:12.853 【指令】收到回報(bào)指令 ID = 3042061412016-09-23 21:31:12.863 【回報(bào)】600802 : pp1701 - 已報(bào)單 1 價(jià)格:7079 開 買2016-09-23 21:31:12.903 【指令】收到回報(bào)指令 ID = 3042061412016-09-23 21:31:12.913 【回報(bào)】600802 : PP01 聚丙烯1701 - 已撤單 量:12016-09-23 21:31:12.913 【指令】收到回報(bào)指令 ID = 304206141
有思路,想編寫各種指標(biāo)公式,程序化交易模型,選股公式,預(yù)警公式的朋友
可聯(lián)系技術(shù)人員 QQ: 511411198 進(jìn)行 有償 編寫!(不貴!點(diǎn)擊查看價(jià)格!)
相關(guān)文章
-
沒有相關(guān)內(nèi)容