function YiJianPinCangAll(sAccount)
dim i
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarket
' msgbox "当前持仓数:"&Order.Holding2(sAccount)
while ( Order.Holding2(Account))
On Error resume Next
HoldStr=""
HoldingCount=Order.Holding2(sAccount)
If HoldingCount>0 then
For i=0 to HoldingCount-1
Call Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,Code,Market,sAccount)
' CurCode=Code
' CurMarket=Market
BuyHold=BuyHolding '持仓品种买入持仓总量
SellHold=SellHolding '持仓品种卖出持仓总量
HoldStr=HoldStr & CurCode
if BuyHold>0 then
HoldStr=HoldStr & " 多单:+" & BuyHold & "手"
call Sell(1,abs(BuyHolding),0,0,Code,Market,sAccount,0) '平多
end if
if SellHold>0 then
HoldStr=HoldStr & " 空单:-" & SellHold & "手"
call SellShort(1,abs(SellHolding),0,0,Code,Market,sAccount,0)
end if
HoldStr=HoldStr & " ,"
Next
HoldStr=Left(HoldStr,len(HoldStr)-1) '去掉了尾部,号
End If
' GetHoldStr=HoldStr
wend
DayClose_Label_ZhangHuJieGuo.Caption= "当前品种"&HoldStr &"平仓后,当前持仓数:"&Order.Holding2(sAccount)
End function
一个品种都平不了仓
好象没反应一样
获取指定者账户的所有持仓合约,保存到一个字符串中。
此例需要解决的问题:首先要获取账户的总持仓数,需要用到Holding2属性,然后要逐一循环取出每一个持仓合约的持仓量,需要用到HoldingInfo2方法。
function GetHoldStr(sAccount)
dim i
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarket
On Error resume Next
HoldStr=""
HoldingCount=Order.Holding2(sAccount)
If HoldingCount>0 then
For i=0 to HoldingCount-1
Call Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,Code,Market,sAccount)
CurCode=Code
CurMarket=Market
BuyHold=BuyHolding
SellHold=SellHolding
HoldStr=HoldStr & CurCode
if BuyHold>0 then
HoldStr=HoldStr & ":+" & BuyHold
end if
if SellHold>0 then
HoldStr=HoldStr & ":-" & SellHold
end if
HoldStr=HoldStr & ","
Next
HoldStr=Left(HoldStr,len(HoldStr)-1)
End If
GetHoldStr=HoldStr
End function
本代码使用函数方式,传递一个账户号码,就可以获取该账户的持仓信息,调用方法是:hold=GetHoldStr("88888888")。
我就是按照上面的代码做的,就是多加了一个平仓语句,能读出帐户信息,但平不了仓,我是用按扭调用这个函数的
if BuyHold>0 then
HoldStr=HoldStr & " 多单:+" & BuyHold & "手"
call Sell(1,abs(BuyHolding),0,0,Code,Market,sAccount,0) '平多 平不了
end if
if SellHold>0 then
HoldStr=HoldStr & " 空单:-" & SellHold & "手"
call SellShort(1,abs(SellHolding),0,0,Code,Market,sAccount,0)'平空 平不了
end if
function YiJianPinCangAll(sAccount)
dim i
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarket
' msgbox "当前持仓数:"&Order.Holding2(sAccount)
Application.MsgOut "111111"
while ( Order.Holding2(Account))
On Error resume Next
HoldStr=""
HoldingCount=Order.Holding2(sAccount)
Application.MsgOut "222222"
If HoldingCount>0 then
For i=0 to HoldingCount-1
Application.MsgOut "33333"
Call Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,Code,Market,sAccount)
' CurCode=Code
' CurMarket=Market
BuyHold=BuyHolding '持仓品种买入持仓总量
SellHold=SellHolding '持仓品种卖出持仓总量
HoldStr=HoldStr & CurCode
if BuyHold>0 then
HoldStr=HoldStr & " 多单:+" & BuyHold & "手"
call Sell(1,abs(BuyHolding),0,0,Code,Market,sAccount,0) '平多
Application.MsgOut "444444"
end if
if SellHold>0 then
HoldStr=HoldStr & " 空单:-" & SellHold & "手"
call SellShort(1,abs(SellHolding),0,0,Code,Market,sAccount,0)
Application.MsgOut "55555"
end if
HoldStr=HoldStr & " ,"
Next
HoldStr=Left(HoldStr,len(HoldStr)-1) '去掉了尾部,号
End If
' GetHoldStr=HoldStr
wend
DayClose_Label_ZhangHuJieGuo.Caption= "当前品种"&HoldStr &"平仓后,当前持仓数:"&Order.Holding2(sAccount)
Application.MsgOut "666666"
End function
将上述加上调试日志的代码运行后,把运行结果贴过来