VBA做的移动止损[金字塔论坛]

来源:百度文库 编辑:神马文学网 时间:2024/05/04 15:35:19
VBA做的移动止损  Post By:2010-11-3 9:54:23
Sub APPLICATION_VBAStart()
'设置一个定时器
call Application.SetTimer(2,5000)
End Sub
Sub APPLICATION_Timer(ID)
'得到指定的CTP帐户信息,
dim Cash'现金余额
dim Guaranteemoney'占用保证金
dim Money'当前可用资金
Cash = Order.Account2(3)
Guaranteemoney = Order.Account2(28)
Money=Order.Account2(19)
'MsgBox Cash
'MsgBox Guaranteemoney
'MsgBox Money
'返回持仓数量,成本等
dim BuyHoding
dim BuyCost
dim BuyTodayHoding
dim SellHoding
dim SellCost
dim SellTodayHoding
dim PNL
dim UseMargin
dim Code
dim Market
'取第一个当前默认帐户持仓品种
Result = Order.HoldingInfo2(0,BuyHoding,BuyCost,BuyTodayHoding,SellHoding,SellCost,SellTodayHoding,PNL,UseMargin ,Code,Market)
If Result <> 1 Then
Exit Sub
End If
'显示当前的买入持仓量
MsgBox BuyCost
'msgbox BuyHoding
'MsgBox Market
'MsgBox code
'取指定品种的和约信息
dim Multipliter, MinTick, ShortPercent, LongPercent'该品种的乘数/单位(也就是交易单位),该品种的最小变动单位,该品种的空头保证金,该品种的多头保证金
Result2 = Order.Contract(Code, Market, Multipliter, MinTick, ShortPercent, LongPercent)
If Result2 <> 1 Then
Exit Sub
End If
'MsgBox Market
'MsgBox code
'MsgBox Multipliter
'msgbox MinTick
'MsgBox ShortPercent
'MsgBox LongPercent
'取au1011的最新动态行情数据
'set Report1 = marketdata.GetReportData("SRX00","ZQ")
set Report1 = marketdata.GetReportData(code,Market)
'msgbox Report1.NewPrice '显示最新价
Pre=BuyCost/Multipliter/BuyHoding'买入成本价
NewPrice= Report1.NewPrice'最新价格
ZF = (Pre - NewPrice) /Pre * 100
Highprice=BuyCost
'1///////////////////如果直接下跌的话直接止损
if NewPrice-pre<0 then
If Zf >=5 then'亏损超过5%止损
order.OrderQueue = 1
call order.sell(1,BuyHoding,0,0,"code","Market","",0)
End if
End if
'2//////////////////如果涨了的话实现移动止损
if NewPrice-pre>0 then
if Highprice-NewPrice<0 then
Highprice=NewPrice
else
Highprice=Highprice
End if
End if
if NewPrice-pre>0 and Highprice-NewPrice>0 then
xf = (Highprice - NewPrice) /Highprice * 100
If xf >=5 then'和最高盈利价相比亏损超过5%止损
order.OrderQueue = 1
call order.sell(1,BuyHoding,0,0,"code","Market","",0)
End if
End if
End Sub