欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。


金字塔客服中心 - 专业程序化交易软件提供商金字塔软件高级功能研发区 → VBA做的移动止损

   

欢迎使用金字塔普通技术服务论坛,您可以在相关区域发表技术支持贴。
我司技术服务人员将优先处理 VIP客服论坛 服务贴,普通区问题处理速度慢,请耐心等待。谢谢您对我们的支持与理解。    


  共有4790人关注过本帖树形打印复制链接

主题:VBA做的移动止损

帅哥哟,离线,有人找我吗?
夏小飞
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:新手上路 帖子:45 积分:270 威望:0 精华:0 注册:2010/10/14 16:38:31
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


 回到顶部
帅哥哟,离线,有人找我吗?
wattwei
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:论坛游侠 帖子:574 积分:3167 威望:0 精华:0 注册:2009/10/10 11:13:01
  发帖心情 Post By:2010/11/3 10:46:02 [只看该作者]

 不错的尝试。代码中关于order函数调用中code和market的引号是多余的

 回到顶部
帅哥哟,离线,有人找我吗?
夏小飞
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:新手上路 帖子:45 积分:270 威望:0 精华:0 注册:2010/10/14 16:38:31
  发帖心情 Post By:2010/11/3 10:57:55 [只看该作者]

哦 谢谢

 回到顶部