每次开仓前加入了资金判断,本地测试下没有问题,您那边再看看
dim num
'每只股票投入资金量
dim money
money = 50000
'最大持有股票数量
dim max_holding
max_holding = 10
Set b = CreateObject("Stock.Block")
call b.open("TEST",1)
'第一个计时器建立头寸
call application.SetTimer(0,2000)
'第二个计时器10秒发单,直到money市值
call application.SetTimer(1,10000)
Sub APPLICATION_Timer(ID)
if ID = 0 then
for i = 0 to b.count-1
buynum = marketdata.GetReportData(b.GetStockCode(i),b.GetStockMarket(i)).SellVolume1*0.5*100
buyprice = marketdata.GetReportData(b.GetStockCode(i),b.GetStockMarket(i)).SellPrice1
Result = Order.HoldingInfoByCode2(b.GetStockCode(i),b.GetStockMarket(i),BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,"")
cond_first = BuyHolding<1 and Result <>1
kcnum = min(money,buynum*buyprice)/buyprice
if num+order.Holding2 + order.OrderNum2 <max_holding and cond_first then
num = num+1
call order.buy(1,kcnum,0,0,b.GetStockCode(i),b.GetStockMarket(i),"",0)
application.MsgOut "第一次开仓数量"&kcnum&"品种代码为"&b.GetStockCode(i)&"现在的num值为"&num
end if
next
end if
if ID = 1 then
for i = 0 to order.Holding2-1
Result = Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin ,Code,Market)
buynum = marketdata.GetReportData(Code,Market).SellVolume1*0.5*100
buyprice = marketdata.GetReportData(Code,Market).SellPrice1
kcnum = min(money-BuyHolding*buyprice,buynum*buyprice)/buyprice
if kcnum>100 then
call order.buy(1,kcnum,0,0,Code,Market,"",0)
application.MsgOut "第二次开仓数量"&kcnum&"品种代码为"&Code&"money的返回值为"&money&"Buyholding置为"&BuyHolding&"buyprice价格为"&buyprice
end if
next
end if
End Sub
'
Sub APPLICATION_VBAEnd()
call application.KillTimer(0)
call application.KillTimer(1)
set b = nothing
End Sub
Sub APPLICATION_VBAStart()
num = 0
End Sub
function min(x,y)
if x<y then
min = x
else
min = y
end if
end function