是否方便給出一個(gè)完整可以運(yùn)行的代碼.
不過(guò)從現(xiàn)象看應(yīng)該是取到的history對(duì)象為空導(dǎo)致的,你可以調(diào)試一下,看看調(diào)取那個(gè)品種的數(shù)據(jù)取不到了
Sub Chashuju2()'盤(pán)中數(shù)據(jù)檢查
today=Date()
if Weekday(today)=1 or
Weekday(today)=7 or todaystop=1 then Exit Sub'星期6和7不檢查
if
cdate(time)>=cdate("08:59:00") and cdate(time)<=cdate("09:00:00") then
'開(kāi)盤(pán)前8點(diǎn)59分先做一次賬戶檢查
Set mail =
CreateObject("WWSCommon.SmtpMail")
strcon= ""
if
order.Account2(2,"你的ctp賬戶")<>1 then strcon = strcon & "交易帳號(hào)未登陸" &
vbCrLf
if application.ReceiveDataStatus = 0 then strcon = strcon &
"金字塔數(shù)據(jù)接收未啟動(dòng)" & vbCrLf
with mail
.SenderName =
"程序化監(jiān)督"
.SenderAddress = "email@163.com"
if
strcomp(strcon,"")=0 then
.Subject = "盤(pán)中檢測(cè)已準(zhǔn)備就緒" &
cdate(date+time)
strcon = "盤(pán)中檢測(cè)已準(zhǔn)備就緒"
else
.Subject = "盤(pán)中檢測(cè)異常" & cdate(date+time)
end
if
end with
call
mail.AddReceiver("139","13688888888@139.com")
call
mail.AddTextContent(strcon)
call
mail.Sender("smtp.163.com","email@163.com","123456")
Set mail =
nothing
end if
if cdate(time)<cdate("09:15:00") or
cdate(time)>cdate("15:15:00") then exit
Sub'只在所交易的合約開(kāi)盤(pán)的時(shí)間內(nèi)做檢查,我交易合約是股指,所以定這個(gè)時(shí)間
dim code(6)
dim market(6)
dim
zhouqimin(2)
strcon= ""
if application.ReceiveDataStatus = 0 then
application.ReceiveData(1)
Application.PeekAndPump
if
order.Account2(2,"你的ctp賬戶")<>1 then strcon = strcon & "交易帳號(hào)未登陸" &
vbCrLf
if application.ReceiveDataStatus = 0 then strcon = strcon &
"金字塔數(shù)據(jù)接收未啟動(dòng)" &
vbCrLf
code(0)=Document.GetExtString("股指交易合約")
market(0)="ZJ"
Holding = Document.GetExtData("IFHOLDING")
dim
BuyHoding
dim BuyTodayHoding
dim SellHoding
dim
SellTodayHoding
dim BuyCost
dim SellCost
dim PNL
Dim
UseMargin
Result =
Order.HoldingInfoByCode2(code(0),market(0),BuyHoding,BuyCost,BuyTodayHoding,SellHoding,SellCost,SellTodayHoding,PNL,UseMargin,"你的ctp登陸資金賬號(hào)")
If
Result = 1 Then
if Round(BuyHoding-SellHoding)<>Round(Holding)
then
strcon = strcon & code(0) & "持倉(cāng)不同步,實(shí)際持倉(cāng)" &
Round(BuyHoding-SellHoding) & "手,應(yīng)為" & Holding & "手" &
vbCrLf
end if
End If
code(1)=Document.GetExtString("股指主力合約")
market(1)="ZJ"
code(2)="000001"
market(2)="SH"
code(3)="1Z2016"
market(3)="SH"
code(4)="1Z2056"
market(4)="SH"
code(5)="000300"
market(5)="SH"
zhouqimin(0)=1
zhouqimin(1)=5
for
pzindex=0 To 5 step 1
for zhouqi = 0 to 5 step 1
if zhouqi<2 or
zhouqi>4 then
set History =
marketdata.GetHistoryData(code(pzindex),market(pzindex),zhouqi)
Xa=today
set
mkt = marketdata.GetMarketInfo2(market(pzindex))
if zhouqi<2 then
Xa=cdate(today+mkt.opentime-cdate("1975-1-1")+cdate("00:0" &
zhouqimin(zhouqi) &
":00"))
a=History.GetPosFromDate(Xa)
aaa=History.GetPosFromDate(cdate(today+mkt.closetime-cdate("1975-1-1")))
copentime=cdate(mkt.opentime-cdate("1975-1-1"))
if
zhouqi<2 then Kn = mkt.TradeSeconds / 60 /
zhouqimin(zhouqi)
if zhouqi=5 then
if
cdate(time)>cdate(copentime) and History.Date(aaa)<>cdate(today) then
strcon=strcon & code(pzindex) & " 當(dāng)天日線缺失" &
vbCrLf
else
if History.Date(a)<cdate(today) then aa=aaa-a else
aa=aaa-a+1
mins = DateDiff("n",cdate(copentime),cdate(time))
if
mins>-1 and (cdate(time)<cdate("11:30:00") or
cdate(time)>cdate("13:00:00")) then'中午休市時(shí)不檢查
if
cdate(time)<cdate("11:30:00") then
mins = mins \
zhouqimin(zhouqi)+1
elseif cdate(time)>cdate("13:00:00")
then
mins = mins \ zhouqimin(zhouqi)+1-90 \
zhouqimin(zhouqi)
end if
if mins<>aa and aa<Kn
then
strcon=strcon & code(pzindex) & " 當(dāng)天" &
zhouqimin(zhouqi) & "分鐘K線數(shù)目前為" & aa & ",應(yīng)為" & mins &
vbCrLf
elseif aa>Kn then
strcon=strcon & code(pzindex)
& " 當(dāng)天" & zhouqimin(zhouqi) & "分鐘K線數(shù)目前為" & aa & ",應(yīng)為" &
Kn & vbCrLf
end if
end if
end if
end
if
next
next
secs = 100
for i=0 to SigCount-1 step
1
if states(i)=1 and (cdate(time)<cdate("11:30:00") or
cdate(time)>cdate("13:00:00")) then'進(jìn)對(duì)已加載的公式檢查狀態(tài),中午休市不檢查
secs =
DateDiff("s",cdate(newtime(i)),cdate(time))
if cdate(time)>cdate("09:31:00") and secs>60
then '由于我的所有公式均是1分鐘調(diào)用一次VBA函數(shù)READSIG,所以公式最近一次運(yùn)行時(shí)間應(yīng)該在60秒內(nèi),如果你的公式是5分鐘的,那么這個(gè)時(shí)間要加大
strcon
= strcon & "策略" & i & "已超過(guò)" & secs & "秒沒(méi)有執(zhí)行" &
vbCrLf
end if
end if
next
if strcomp(strcon,"")=0
then
errorcount=0
if
(cdate(time)<cdate("09:16:05") or (cdate(time)>cdate("13:00:00") and
cdate(time)<cdate("13:01:05"))) and secs<120 then
'固定在上午和下午開(kāi)盤(pán)后的第一次檢查時(shí)發(fā)郵件通知,即使是一切正常時(shí)
Set mail =
CreateObject("WWSCommon.SmtpMail")
with mail
.SenderName
= "程序化監(jiān)督"
.SenderAddress = "email@163.com"
.Subject
= "公式已開(kāi)始運(yùn)行" & cdate(date+time)
end with
call
mail.AddReceiver("139","13688888888@139.com")
call
mail.AddTextContent("公式已開(kāi)始運(yùn)行")
call
mail.Sender("smtp.163.com","email@163.com","123456")
Set mail =
nothing
end if
else
errorcount=errorcount+1
if errorcount<3 or errorcount=5
then'連續(xù)異常時(shí),會(huì)有第1、第2、第5次通知;如果出現(xiàn)異常后,又恢復(fù)正常,異常通知次數(shù)會(huì)重新計(jì)算
Set mail
= CreateObject("WWSCommon.SmtpMail")
with mail
.SenderName = "程序化監(jiān)督"
.SenderAddress =
"email@163.com"
.Subject = "盤(pán)中異常通知第" & errorcount & "次 " &
cdate(date+time)
end with
call
mail.AddReceiver("139","13688888888@139.com")
call
mail.AddTextContent(strcon)
call
mail.Sender("smtp.163.com","email@163.com","123456")
Set mail =
nothing
end if
end if
End Sub
or pzindex=0 To 5 step 1
for zhouqi = 0 to 5 step 1
if zhouqi<2 or zhouqi>4 then
Application.MsgOut code(pzindex) & "--" & market(pzindex)
set History = marketdata.GetHistoryData(code(pzindex),market(pzindex),zhouqi)
Xa=today
建議你多學(xué)習(xí)一下調(diào)試方法吧, 將code數(shù)組打印輸出一下你就能看到, 是因?yàn)槟銢](méi)有讀取到品種代碼引起的History 為空導(dǎo)致的