是否方便给出一个完整可以运行的代码.
不过从现象看应该是取到的history对象为空导致的,你可以调试一下,看看调取那个品种的数据取不到了
Sub Chashuju2()'盘中数据检查
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 &
"金字塔数据接收未启动" & vbCrLf
with mail
.SenderName =
"程序化监督"
.SenderAddress = "email@163.com"
if
strcomp(strcon,"")=0 then
.Subject = "盘中检测已准备就绪" &
cdate(date+time)
strcon = "盘中检测已准备就绪"
else
.Subject = "盘中检测异常" & 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'只在所交易的合约开盘的时间内做检查,我交易合约是股指,所以定这个时间
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 &
"金字塔数据接收未启动" &
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) & "手,应为" & 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线数目前为" & aa & ",应为" & mins &
vbCrLf
elseif aa>Kn then
strcon=strcon & code(pzindex)
& " 当天" & zhouqimin(zhouqi) & "分钟K线数目前为" & aa & ",应为" &
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'进对已加载的公式检查状态,中午休市不检查
secs =
DateDiff("s",cdate(newtime(i)),cdate(time))
if cdate(time)>cdate("09:31:00") and secs>60
then '由于我的所有公式均是1分钟调用一次VBA函数READSIG,所以公式最近一次运行时间应该在60秒内,如果你的公式是5分钟的,那么这个时间要加大
strcon
= strcon & "策略" & i & "已超过" & secs & "秒没有执行" &
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
'固定在上午和下午开盘后的第一次检查时发邮件通知,即使是一切正常时
Set mail =
CreateObject("WWSCommon.SmtpMail")
with mail
.SenderName
= "程序化监督"
.SenderAddress = "email@163.com"
.Subject
= "公式已开始运行" & cdate(date+time)
end with
call
mail.AddReceiver("139","13688888888@139.com")
call
mail.AddTextContent("公式已开始运行")
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'连续异常时,会有第1、第2、第5次通知;如果出现异常后,又恢复正常,异常通知次数会重新计算
Set mail
= CreateObject("WWSCommon.SmtpMail")
with mail
.SenderName = "程序化监督"
.SenderAddress =
"email@163.com"
.Subject = "盘中异常通知第" & 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
建议你多学习一下调试方法吧, 将code数组打印输出一下你就能看到, 是因为你没有读取到品种代码引起的History 为空导致的