问题:1老合约数据可以拷贝的数组,再从数组可以拷贝到History11(marketdate)中——采用application.MsgOut cstr(History11.Count)显示出count数量不为0,等于老合约的数据量。
2、但是再在Application_Timer(ID)调用新合约1分钟数据为0!
3、单独不做全市场扫描,只将一个股票采用上述代码,可以完整的建立新的股票代码,即直接把变量laoheyue赋值为恒量“000722”这种方式,则不会导致上面1、2点问题。
'___________________________目标合约、代码设置______________________
'###################################################################
dim oldContinueArray(8000)'老合约代码
dim oldMarketArray(8000)'老市场代码
dim newContinueArray(8000)'新合约代码
dim newMarketArray(8000)'老市场代码
dim pk
pk=0
dim xinheyue
xinheyue = ""
dim xinshichang
xinshichang = ""
dim laoheyue
laoheyue = "" '老合约代码
dim laoshichang
laoshichang = "" '老合约市场
dim laoheyueStartDate
laoheyueStartDate = laoheyue+"StartDate" '老合约的开始日期
dim laoheyueDataCount
laoheyueDataCount = laoheyue+"DataCount" '老合约的数
dim newKOpen'新合约的K的Open
newKOpen=0
dim newKClose'新合约的K的Close
newKClose=0
dim newKHigh'新合约的K的High
newKHigh=0
dim newKLow'新合约的K的Low
newKLow=0
dim newKVolume'新合约的K的High
newKVolume=0
dim mxVolume'新合约的K的High
mxVolume=0
dim newKDate'新合约的K的Low
newKDate=0
dim newkOpenInt'新合约的K的High
newkOpenInt=0
'___________________________目标合约、代码设置______________________
'###################################################################
dim endminutdate
endminutdate=0
dim PKOpen
dim PKClose
dim PKHigh
dim PKLow
dim PKVolume
dim PKDate
dim PKOpenInt
'________________公式专用________
'================================
'================================
'该函数查找主力合约
Sub SearchStart()
application.MsgOut Date &" " &Time& "检索股票名称及代码"
Dim marketName
marketName=Array("SZ","SH")'此处只查上海及深圳A股"SZ","SH"。不查期货市场"SQ","DQ","ZQ","ZJ",
'找到股票的代码及名称
'dim labels
dim markets
For j=0 To UBound(marketName)
n=marketData.GetReportCount(marketName(j))
markets=marketName(j)
For i=0 To n-1
Set reportData=marketdata.GetReportDataByIndex(marketName(j),i)
if IsNumeric(Left(reportData.Label,1))=true then
if (Cint(Left(reportData.Label,1))=0 or Cint(Left(reportData.Label,1))=3) and j=0 then‘股票代码起始为0的主板股票或起始代码为3的创业版股票
laoheyue=Trim(reportData.Label)'股票的老代码
laoshichang=Trim(marketName(j))'股票所在市场的老代码
xinheyue=9000000+laoheyue'股票的新代码
oldContinueArray(pk)=Trim(laoheyue)'股票的老代码——赋值给数组保存
oldMarketArray(pk)=Trim(laoshichang)'股票所在市场的老代码——赋值给数组保存
newMarketArray(pk)=Trim(laoshichang)'股票在市场的的新代码——赋值给数组保存
newContinueArray(pk)=Trim(xinheyue)
'股票的新代码——赋值给数组保存
if j<UBound(marketName) and i<n-1 then
pk=pk+1
end if
end if
Next
Next
End Sub
sub cushihua()
Call SearchStart()'只能一次
for y=0 to pk
laoheyue=oldContinueArray(y)'股票的老代码
laoshichang=oldMarketArray(y)'股票的老市场名称
xinheyue=newContinueArray(y)'股票的新名称
xinshichang=newMarketArray(y)'股票的新市场名称
'Application.MsgOut laoheyue +"laoheyue"+xinheyue +"xinheyue"+xinmingcheng +"xinmingcheng"+xinheyue +"xinheyue"
if laoheyue = "000722" then
Call lishishuju()'只能一次
end if
'-------------------------------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------------
'Call addtodaydata()'只能一次
next
end sub
Sub lishishuju()
'查询品种是否存在,不存在则新建
cunzai = marketdata.IsExistStock(xinheyue,xinshichang)
if cunzai = 0 then
call marketdata.AddStock(xinheyue,xinshichang, xinmingcheng)
application.MsgOut "新K线【创建成功】!"
shanchu = 0
else
'删除历史数据,创建第一根K线
shanchu = 1
application.MsgOut "等价K线【已经存在】!"
end if
if shanchu = 1 then
Set History1 =marketdata.GetHistoryData(xinheyue,xinshichang,0)
if History1.Count>1 then'!!!!特别注意不能全部清空数组,否则为《空数组》,会导致错误及意外!!!
for j = 1 to History1.Count-1
History1.RemoveAt(j)
next
end if
call History1.SaveData(xinheyue,xinshichang,1)
call MarketData.DestroyHistoryData
application.MsgOut "新K线【历史数据初始化】成功!"
end if
'+++++++++++++++++++更新新合约的历史数据++++++++++++++++++++++++++
Set PKOpen=CreateObject("Stock.Array")
Set PKClose=CreateObject("Stock.Array")
Set PKHigh=CreateObject("Stock.Array")
Set PKLow=CreateObject("Stock.Array")
Set PKVolume=CreateObject("Stock.Array")
Set PKDate=CreateObject("Stock.Array")
Set PKOpenInt=CreateObject("Stock.Array")
Set History00 = marketdata.GetHistoryData(laoheyue,laoshichang,0)'取老合约1分钟数据,将其拷贝到数组,以便拷贝到新合约之中
h00=History00.Count-1
for i = 0 to H00
PKOpen.AddBack(History00.Open(i))
PKClose.AddBack(History00.close(i))
PKHigh.AddBack(History00.high(i))
PKLow.AddBack(History00.low(i))
PKVolume.AddBack(History00.Volume(i))
PKDate.AddBack(History00.date(i))
PKOpenInt.AddBack(History00.OpenInt(i))
call MarketData.DestroyHistoryData
Next
Set History11=marketdata.GetHistoryData(xinheyue,xinshichang,0)'取老合约1分钟数据
for i = 0 to PKOpen.Count-1
'#############加载历史数据给等价k新合约的newkopen、newkhigh、newklow、newkVolume
'#############加载历史数据给等价k新合约的newkclose。newkclose在abs(Tclose-newkopen) > qj成立时生成
newkhigh=PKHigh.GetAt(i)
newklow=PKLow.GetAt(i)
newkVolume=PKVolume.GetAt(i)
newkDate=PKDate.GetAt(i)
newkopen=PKOpen.GetAt(i)
newkHigh=Thigh
newkLow=Tlow
newkOpenInt=PKOpenInt.GetAt(i)
'新合约数据转存到marketdate之中
h11c=History11.Count
call History11.InsertAt(h11c)
History11.Open(h11c)=newkOpen
History11.Close(h11c)=newkClose
History11.High(h11c)=newkHigh
History11.Low(h11c)=newkLow
History11.Volume(h11c)=newkVolume
History11.Date(h11c)=newkDate
History11.OpenInt(h11c)=newkOpenInt
call History11.SaveData(xinheyue,xinshichang,1)
'application.MsgOut cstr(History11.Open(i))+"dddddd=====新K线======" + cstr(i)+ "------------OPEN==========" +cstr(PKOpen.GetAt(i))
next
application.MsgOut cstr(History11.Count) ’备注:History12.Count的结果是拷贝成功,count数与原数据一致!!!
MarketData.DestroyHistoryData
Set PKOpen=nothing
Set PKClose=nothing
Set PKHigh=nothing
Set PKLow=nothing
Set PKVolume=nothing
Set PKDate=nothing
Set PKOpenInt=nothing
End Sub
Sub APPLICATION_VBAStart()
call Application.SetTimer(0,1000) '创建一个0号定时器,间隔时间1秒
End Sub
Sub APPLICATION_VBAend()
call Application.KillTimer(0) '创建一个0号定时器,间隔时间1秒
End Sub
Sub APPLICATION_Timer(ID)
if ID = 0 then
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~全市场扫描~~~~~~~~~~~~~~~~~~~~~~~~~~~
'########################################################################
for k=0 to PK
laoheyue = oldContinueArray(k)
laoshichang = oldMarketArray(k)
xinshichang = newMarketArray(k)
xinheyue = newContinueArray(k)
'~~~~~~~~~~~~~~~~~~~~~~~以下开始交易~~~~~~~~~~~~~~~~~~~~~~~~~~~
if laoheyue="000722" then
Set History12 = marketdata.GetHistoryData("9000722","SZ",0)
Application.MsgOut cstr(History12.Count)+"--------000722=========="+cstr(xinheyue)+cstr(xinshichang) 备注:History12.Count的结果是0,为何?
for i= h12-1 to 0
datex=History12.Date(i)
datei=History12.date(i-1)
if DateDiff("d",date1x,datei)>=1 then
N=h12-i+1
Application.MsgOut cstr(N)+"=============000722=========="
exit for
end if
Next
'_______________________以上开始交易___________________________
'~~~~~~~~~~~~~~~~~~~~~~~以下开始数据刷新~~~~~~~~~~~~~~~~~~~~~~~
'Call RefreshData()
'_______________________以上开始数据刷新_______________________
next
'########################################################################
end if
end sub