是否方便給出一個完整可以運行的代碼.
不過從現(xiàn)象看應(yīng)該是取到的history對象為空導致的,你可以調(diào)試一下,看看調(diào)取那個品種的數(shù)據(jù)取不到了
Sub Chashuju2()'盤中數(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
'開盤前8點59分先做一次賬戶檢查
Set mail =
CreateObject("WWSCommon.SmtpMail")
strcon= ""
if
order.Account2(2,"你的ctp賬戶")<>1 then strcon = strcon & "交易帳號未登陸" &
vbCrLf
if application.ReceiveDataStatus = 0 then strcon = strcon &
"金字塔數(shù)據(jù)接收未啟動" & vbCrLf
with mail
.SenderName =
"程序化監(jiān)督"
.SenderAddress = "[email protected]"
if
strcomp(strcon,"")=0 then
.Subject = "盤中檢測已準備就緒" &
cdate(date+time)
strcon = "盤中檢測已準備就緒"
else
.Subject = "盤中檢測異常" & cdate(date+time)
end
if
end with
call
mail.AddReceiver("139","[email protected]")
call
mail.AddTextContent(strcon)
call
mail.Sender("smtp.163.com","[email protected]","123456")
Set mail =
nothing
end if
if cdate(time)<cdate("09:15:00") or
cdate(time)>cdate("15:15:00") then exit
Sub'只在所交易的合約開盤的時間內(nèi)做檢查,我交易合約是股指,所以定這個時間
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 & "交易帳號未登陸" &
vbCrLf
if application.ReceiveDataStatus = 0 then strcon = strcon &
"金字塔數(shù)據(jù)接收未啟動" &
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登陸資金賬號")
If
Result = 1 Then
if Round(BuyHoding-SellHoding)<>Round(Holding)
then
strcon = strcon & code(0) & "持倉不同步,實際持倉" &
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) & " 當天日線缺失" &
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'中午休市時不檢查
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) & " 當天" &
zhouqimin(zhouqi) & "分鐘K線數(shù)目前為" & aa & ",應(yīng)為" & mins &
vbCrLf
elseif aa>Kn then
strcon=strcon & code(pzindex)
& " 當天" & 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'進對已加載的公式檢查狀態(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īng)該在60秒內(nèi),如果你的公式是5分鐘的,那么這個時間要加大
strcon
= strcon & "策略" & i & "已超過" & secs & "秒沒有執(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
'固定在上午和下午開盤后的第一次檢查時發(fā)郵件通知,即使是一切正常時
Set mail =
CreateObject("WWSCommon.SmtpMail")
with mail
.SenderName
= "程序化監(jiān)督"
.SenderAddress = "[email protected]"
.Subject
= "公式已開始運行" & cdate(date+time)
end with
call
mail.AddReceiver("139","[email protected]")
call
mail.AddTextContent("公式已開始運行")
call
mail.Sender("smtp.163.com","[email protected]","123456")
Set mail =
nothing
end if
else
errorcount=errorcount+1
if errorcount<3 or errorcount=5
then'連續(xù)異常時,會有第1、第2、第5次通知;如果出現(xiàn)異常后,又恢復(fù)正常,異常通知次數(shù)會重新計算
Set mail
= CreateObject("WWSCommon.SmtpMail")
with mail
.SenderName = "程序化監(jiān)督"
.SenderAddress =
"[email protected]"
.Subject = "盤中異常通知第" & errorcount & "次 " &
cdate(date+time)
end with
call
mail.AddReceiver("139","[email protected]")
call
mail.AddTextContent(strcon)
call
mail.Sender("smtp.163.com","[email protected]","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
建議你多學習一下調(diào)試方法吧, 將code數(shù)組打印輸出一下你就能看到, 是因為你沒有讀取到品種代碼引起的History 為空導致的