function GetHoldStr(sAccount)
dim i
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarket
On Error resume Next
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
call order.sell(1,BuyHold,0,0,CurCode,CurMarket,sAccount,0)
end if
if SellHold>0 then
call order.sellshort(1,SellHold,0,0,CurCode,CurMarket,sAccount,0)
end if
Next
End If
End function
[此贴子已经被作者于2013-5-27 11:07:34编辑过]
'此模块中sAccount为传递账号,对按照某个时间(比如15:30)平全部仓位的用户可以直接调用此模块,用call语句就行。可以直接放在模块中
'也可以复制代码到大家做需要的窗体中进行调用。
第一种:
'创建了一个窗体,加上一个按钮来触发,导入附件后就能使用
Sub UserForm1_CommandButton1_Click()
'输入账号
call getholdstr(803156)
End Sub
'该模块主要用来保存宏主执行主函数,不要拿做他用
function GetHoldStr(sAccount)
'定义所需变量
dim i
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarket
On Error resume Next
'索取持仓合约数
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
call order.sell(1,BuyHold,0,0,CurCode,CurMarket,sAccount,0)
end if
'取得空头持仓,按照事件返回的手数和相关信息进行平空
if SellHold>0 then
call order.sellshort(1,SellHold,0,0,CurCode,CurMarket,sAccount,0)
end if
Next
End If
End function
第二种 适合用vba做日内的,
'大家也可把function函数直接放到大家已经编辑好的代码中加上定时器来读取时间 (定时器到收盘15:00进行全平操作)
sub application_vbastart()
'设置定时器,每秒刷新
call application.SetTimer(1,1000)
end sub
sub application_timer(id)
application.MsgOut(right(now,8))
if id=1 then
'输入自己要日内平仓时间 ,now函数返回的时间是有日期的 我们用right方法去掉日期。就顺利取得时分秒,^,^
if right(now,8)>="13:23:30" then
'输入自己的账号
call GetHoldStr(888)
end if
end if
end sub
sub application_vbaend()
'删除定时器,释放资源
call application.KillTimer(1)
end sub
'插入模块
function GetHoldStr(sAccount)
'定义所需变量
dim i
dim BuyHold
dim BuyCost
dim SellHold
dim SellCost
dim CurCode
dim CurMarket
On Error resume Next
'索取持仓合约数
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
call order.sell(1,BuyHold,0,0,CurCode,CurMarket,sAccount,0)
end if
'取得空头持仓,按照事件返回的手数和相关信息进行平空
if SellHold>0 then
call order.sellshort(1,SellHold,0,0,CurCode,CurMarket,sAccount,0)
end if
Next
End If
End function
看来我那个帖子已经深入人心了,还能做举一反三,进行修改了,扩展了,本人深感荣幸。希望有更多的人能熟练掌握vba开发。
是啊 版主的帖子帮助了很多人了,对你的贡献表示感谢