以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://weistock.com/bbs/index.asp)
--  高级功能研发区  (http://weistock.com/bbs/list.asp?boardid=5)
----  VBA做的移动止损  (http://weistock.com/bbs/dispbbs.asp?boardid=5&id=3651)

--  作者:夏小飞
--  发布时间:2010/11/3 9:54:23
--  VBA做的移动止损

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


--  作者:wattwei
--  发布时间:2010/11/3 10:46:02
--  
 不错的尝试。代码中关于order函数调用中code和market的引号是多余的
--  作者:夏小飞
--  发布时间:2010/11/3 10:57:55
--  
哦 谢谢