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


金字塔客服中心 - 专业程序化交易软件提供商金字塔软件高级功能研发区 → 编写了一个套利的vba程序框架模板,感兴趣的朋友可以在此基础上做进一步开发

   

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


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

主题:编写了一个套利的vba程序框架模板,感兴趣的朋友可以在此基础上做进一步开发

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


加好友 发短信
等级:黑侠 帖子:676 积分:2548 威望:0 精华:3 注册:2011/4/13 16:18:50
编写了一个套利的vba程序框架模板,感兴趣的朋友可以在此基础上做进一步开发  发帖心情 Post By:2011/7/4 13:28:54 [只看该作者]

编写了一个套利的vba程序。

  目前的套利程序模板已经包含了价差分析,分批下单,自动追单等基础功能。


  1 将两个品种正确的代码和市场信息填入后,单击“显示品种数据”按钮,
      即可在窗口相应位置出现该品种的最新数据(如图),和两品种的价格差。
        差价值是根据品种1减去品种2的值得出。
  2 在中间的条件区域中填入条件信息。
  3 条件全部设定好以后,在“清空信息”按钮上方出现条件设定的概述(如图),

      检查是否正确后,就可以单击“执行条件”按钮开始执行。
            当满足条件后,就会按照设定进行下单交易。
  4 过程中可以单击“暂停操作”按钮和“手动平仓”按钮。执行相应操作。

            单击后,将程序的状态设为停止,单击“执行条件”后才可以继续进行自动交易。
   
声明:
    交易过程中交易数量为1手。品种1下单1手,后品种2下单1手,为一轮。
    条件设置中“数量”的填入为轮数。
    追单的设置,考虑到有时下单后上时间不交易影响进程,所以程序会自动撤单,
    选中追单则将撤单不上,直到补上撤单后,进行下一轮交易。否则将会漏单。
    还有在“清空信息”按钮上方应藏着操作信息提示,当条件设定完整后,会自动显示。
 其中显示了程序的状态。(如图)
    最下方的空间主要是对下单的时间记录。
   
    应多使用模拟交易多熟悉程序操作。程序如有不足之处,可以提出意见。

 图片点击可在新窗口打开查看

 

下载地址 http://www.weistock.com/download/taoli.frm

 

导入方法:解压缩后,运行金字塔,点击“文件”菜单->导入 ,文件类型选窗体文件

导入taoli.frm窗体

 

关闭金字塔,重新启动

按Alt+F8弹出 执行宏 窗口,下拉菜单选择刚才我们导入的窗体即可运行了。


[本帖被加为精华]
 回到顶部
帅哥哟,离线,有人找我吗?
guotx2010
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:蜘蛛侠 帖子:1366 积分:5210 威望:0 精华:7 注册:2010/12/11 18:00:33
  发帖心情 Post By:2011/7/5 8:25:15 [只看该作者]

是个好东西,学习一下!

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


加好友 发短信
等级:黑侠 帖子:676 积分:2548 威望:0 精华:3 注册:2011/4/13 16:18:50
  发帖心情 Post By:2011/7/7 16:34:10 [只看该作者]

对于外盘发现了一个问题 就是获取外盘的持仓量,为空仓的时候不对,没有判断返回的正确性。所以第一手开仓不行。国内品种交易没问题。


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


加好友 发短信
等级:新手上路 帖子:10 积分:53 威望:0 精华:0 注册:2011/7/12 10:47:57
  发帖心情 Post By:2011/7/14 9:49:17 [只看该作者]

有没有 “品种市场“ 所代表的交易所的代码吗? 如上图 ZQ 是代表郑州吧? 还是有别的意义?

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


加好友 发短信
等级:管理员 帖子:5082 积分:17642 威望:0 精华:6 注册:2010/7/15 9:05:58
  发帖心情 Post By:2011/7/14 9:53:14 [只看该作者]

 ZQ 是代表郑州期货交易所

工具--市场与板块,中 有“品种市场“ 所代表的交易所的代码对照表



金字塔—专业程序化交易量化投资平台

客户服务部

-----------------------------------------------------------

欢迎您参加我公司的技术培训,具体培训需求请发邮件到

service@weistock.com

您的宝贵建议或者投诉,请发往邮箱:weiwei@weistock.com

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


加好友 发短信
等级:新手上路 帖子:10 积分:53 威望:0 精华:0 注册:2011/7/12 10:47:57
  发帖心情 Post By:2011/7/28 10:57:16 [只看该作者]

楼主的VBA 的代码很长,有很多我都看不明白。希望能注释一下。 dim kongjian k dim count count=0 dim pingcang pingcang="" '全局变量 dim exce1 dim report1ComboBox1 dim report2ComboBox1 report1ComboBox1=taoli_report1ComboBox.value report2ComboBox1=taoli_report2ComboBox.value dim report1 dim report2 set report1=nothing set report2=nothing Sub MARKETDATA_ReportNotify(ReportData) '显示自定义的最新价格 display if not report1 is nothing and not report2 is nothing then taoli_Label1.caption=Report1.newprice taoli_Label2.caption=Report2.newprice taoli_chajia.caption= Report1.newprice-Report2.newprice end if '如果有订单为成交,则撤销,以免影响批量下单的进度 if not report1 is nothing and not report2 is nothing then num1=0 '使用于ctp平仓功能变量 num2=0 '使用于ib平仓功能变量 ctp=Order.StockType(report1.Label,report1.marketName) if ctp=1 then num1=Order.OrderInfoByCode2(report1.Label,report1.marketName, OrderID1, ConSign, Filled, R1, Action, OrderType, LmtPrice, Account, Kaiping) num2=Order.OrderInfoByCode2(report2.Label,report2.marketName, OrderID2, ConSign, Filled, R2, Action, OrderType, LmtPrice, Account, Kaiping) elseif ctp=0 then num1=Order.OrderInfoByCode(report1.Label,report1.marketName, OrderID1, ConSign, Filled, R1, Action, OrderType, LmtPrice, Account, Kaiping) num2=Order.OrderInfoByCode(report2.Label,report2.marketName, OrderID2, ConSign, Filled, R2, Action, OrderType, LmtPrice, Account, Kaiping) end if if num1=1 or num2=1 then 'application.MsgOut count&" 检测撤单中 "&OrderID1 count=count+1 if count=5 then num11=0 num12=0 ctp=Order.StockType(report1.Label,report1.marketName) if ctp=1 then num11=Order.OrderInfoByCode2(report1.Label,report1.marketName, OrderID3, ConSign, Filled, R1, Action, OrderType, LmtPrice, Account, Kaiping) num12=Order.OrderInfoByCode2(report2.Label,report2.marketName, OrderID4, ConSign, Filled, R2, Action, OrderType, LmtPrice, Account, Kaiping) elseif ctp=0 then num11=Order.OrderInfoByCode(report1.Label,report1.marketName, OrderID3, ConSign, Filled, R1, Action, OrderType, LmtPrice, Account, Kaiping) num12=Order.OrderInfoByCode(report2.Label,report2.marketName, OrderID4, ConSign, Filled, R2, Action, OrderType, LmtPrice, Account, Kaiping) end if if num11=1 and OrderID1=OrderID3 then call Order.CancelOrder(OrderID3) 'application.MsgOut time()&" "&report1.Label&" "&OrderId1&" 111 chedan"&OrderID3 end if if num12=1 and OrderID2=OrderID4 then call Order.CancelOrder(OrderID4) 'application.MsgOut time()&" "&report2.Label&" "&OrderId2&" 222 chedan"&OrderID4 end if k执行" and not report1 is nothing and taoli_term1price<>"" and taoli_term1num<>"" and taoli_term1cleanprice<>"" then '第一条件的执行 diffbuy1=0 diffsell1=0 BuyHoding1=0 SellHoding1=0 chicang=0 ctp=Order.StockType(report1.Label,report1.marketName) if ctp=1 then call Order.HoldingInfoByCode2(report1.Label,report1.marketName,BuyHoding1,BuyCost,BuyTodayHoding1,SellHoding1,SellCost,SellTodayHoding1,PNL,UseMargin) elseif ctp=0 then call Order.HoldingInfoByCode(report1.Label,report1.marketName, Hold, MktPrice, AvgPrice, MktValue, AgeCost, PNL) if hold>0 then BuyHoding1=Hold end if if hold<0 then sellhoding1="-hold" end="end" if="if" end="end" if="if" diffbuy1="taoli_term1num.value-BuyHoding1 " diffsell1="taoli_term1num.value-SellHoding1 " diff="Report1.newprice-Report2.newprice-taoli_term1price " if="if" instr(taoli_term1ComboBox.value,"大于") <> 0 and diff>0 and k then '下列语句在满足条件1 大于实际差价情况下,只对品种1来判断操作,品种2的操作时事件触发 if instr(report1ComboBox1,"买入") <> 0 and diffbuy1>0 then call Order.Buy(0,1,Report1.SellPrice1,0,Report1.label,Report1.marketname,"",0) k卖出") <> 0 and diffsell1>0 then call Order.BuyShort(0,1,Report1.BuyPrice1,0,Report1.label,Report1.marketname,"",0) k小于") <> 0 and diff<0 and="and" k="k" then '下列语句在满足条件1 大于实际差价情况下,只对品种1来判断操作,品种2的操作时事件触发 if="if" instr(report1ComboBox1,"买入") <> 0 and diffbuy1>0 then call Order.Buy(0,1,Report1.SellPrice1,0,Report1.label,Report1.marketname,"",0) k卖出") <> 0 and diffsell1>0 then call Order.BuyShort(0,1,Report1.BuyPrice1,0,Report1.label,Report1.marketname,"",0) k大于") <> 0 and diff>0 and k and chicang=1 then if BuyHodingclean1>0 then call Order.Sell(0,1,Report1.BuyPrice1,0,report1.Label,report1.marketName,"",0) k",0) k小于") <> 0 and diff<0 and="and" k="k" and="and" chicang="1" then if="if" buyhodingclean1="BuyHodingclean1" >0 then call Order.Sell(0,1,Report1.BuyPrice1,0,report1.Label,report1.marketName,"",0) k",0) k执行" then chicang=0 Buyhoding=0 Sellhoding=0 if not report1 is nothing and not report2 is nothing then ctp=Order.StockType(report1.Label,report1.marketName) if ctp=1 then chicang=Order.HoldingInfoByCode2(report1.Label,report1.marketName,BuyHoding,BuyCost,BuyTodayHoding1,SellHoding,SellCost,SellTodayHoding1,PNL,UseMargin) elseif ctp=0 then Hold=0 chicang=Order.HoldingInfoByCode(report1.Label,report1.marketName, Hold, MktPrice, AvgPrice, MktValue, AgeCost, PNL) if hold>0 then Buyhoding=Hold end if if hold<0 then sellhoding="-hold " end="end" if="if" if="if" hold="0" then buyhoding="Hold " sellhoding="hold " end="end" if end="end" if="if" if="if" buyhoding="Buyhoding" >0 and k and chicang=1 then call Order.Sell(0,1,Report1.BuyPrice1,0,report1.Label,report1.marketName,"",0) kzhixing" and chicang=1 then call Order.Sellshort(0,1,Report1.SellPrice1,0,report1.Label,report1.marketName,"",0) k" end if end if end if end sub '当成交量出现变化时发生的事件处理 Sub ORDER_OrderStatusEx(OrderID,Status,Filled,Remaining,Price,Code,Market,OrderType,Aspect,Kaiping) 'application.MsgOut time()&code&" aspect "&Aspect&" kaiping "&Kaiping&" Status "&Status&Filled if code=UCase(taoli_report1) and status="Filled" then if kaiping=0 and aspect=0 then '品种2开空 taoli_ts.AddItem time()&" 条件 买入 "&Filled&"手 "&Report1.StockName&"价格为"&Report1.SellPrice1 call Order.BuyShort(0,Filled,Report2.BuyPrice1,0,Report2.label,Report2.marketname,"",0) elseif kaiping=0 and aspect=1 then '品种2开多 taoli_ts.AddItem time()&" 条件 卖出 "&Filled&"手 "&Report1.StockName&"价格为"&Report1.BuyPrice1 call Order.Buy(0,Filled,Report2.SellPrice1,0,Report2.label,Report2.marketname,"",0) elseif (kaiping=1 or kaiping=3) and aspect=0 then '品种2平多 taoli_ts.AddItem time()&" 自动平仓 "&Filled&"手 "&Report1.StockName&"价格为"&Report1.BuyPrice1 call Order.Sell(0,Filled,Report2.BuyPrice1,0,report2.Label,report2.marketName,"",0) elseif (kaiping=1 or kaiping=3) and aspect=1 then '品种2平空 taoli_ts.AddItem time()&" 自动平仓 "&Filled&"手 "&Report1.StockName&"价格为"&Report1.BuyPrice1 call Order.Sellshort(0,Filled,Report2.SellPrice1,0,report2.Label,report2.marketName,"",0) end if elseif code=UCase(taoli_report2) and status="Filled" then if kaiping=0 and aspect=0 then taoli_ts.AddItem time()&" 自动 买入 "&Filled&"手 "&Report2.StockName&"价格为"&Report2.BuyPrice1 elseif kaiping=0 and aspect=1 then taoli_ts.AddItem time()&" 自动 卖出 "&Filled&"手 "&Report2.StockName&"价格为"&Report2.SellPrice1 elseif (kaiping=1 or kaiping=3) and aspect=0 then taoli_ts.AddItem time()&" 自动平仓 "&Filled&"手 "&Report2.StockName&"价格为"&Report2.BuyPrice1 elseif (kaiping=1 or kaiping=3) and aspect=1 then taoli_ts.AddItem time()&" 自动平仓 "&Filled&"手 "&Report2.StockName&"价格为"&Report2.SellPrice1 end if k elseif status="Cancelled" and code=UCase(taoli_report2) then ctp=Order.StockType(report1.Label,report1.marketName) if taoli_checkbox1.value and ctp=1 then 'application.MsgOut "report2 追加 aspect "&Aspect&" kaiping "&Kaiping&" Status "&Status if kaiping=0 and aspect=0 then call Order.Buy(0,Remaining,Report2.SellPrice1,0,Report2.label,Report2.marketname,"",0) elseif kaiping=0 and aspect=1 then call Order.BuyShort(0,Remaining,Report2.BuyPrice1,0,Report2.label,Report2.marketname,"",0) elseif (kaiping=1 or kaiping=2) and aspect=0 then 'application.MsgOut "撤销品种2 后追加平空" call Order.Sellshort(0,Remaining,Report2.SellPrice1,0,report2.Label,report2.marketName,"",0) elseif (kaiping=1 or kaiping=2) and aspect=1 then ' application.MsgOut "撤销品种2 后追加平多" call Order.Sell(0,Remaining,Report2.BuyPrice1,0,report2.Label,report2.marketName,"",0) end if elseif not(taoli_checkbox1.value) and ctp=1 then k elseif taoli_checkbox1.value and ctp=0 then if aspect=0 then '品种2开空 call Order.Buy(0,Remaining,Report2.SellPrice1,0,Report2.label,Report2.marketname,"",0) elseif aspect=1 then '品种2开多 call Order.BuyShort(0,Remaining,Report2.BuyPrice1,0,Report2.label,Report2.marketname,"",0) end if elseif not(taoli_checkbox1.value) and ctp=0 then k end if elseif status="Cancelled" and code=UCase(taoli_report1) then k 'application.MsgOut Aspect&" report1撤销 "&Kaiping elseif status="Inactive" and code=UCase(taoli_report1) then k end if End Sub '强制平仓 Sub taoli_clearButton_Click() exce1="" pingcang="执行" k End Sub '调试按钮 Sub taoli_CommandButton1_Click() application.ClearMsg taoli_ts.clear End Sub '下面两个过程是生成操作提示 sub display() Set report1 = marketdata.GetReportData(taoli_report1.text,taoli_report1market) Set report2 = marketdata.GetReportData(taoli_report2.text,taoli_report2market) if taoli_term1price<>"" and taoli_term1num<>"" and taoli_term1cleanprice<>"" then if not report1 is nothing and not report2 is nothing then dim s s="条件设定成立 当差价"&taoli_term1ComboBox.value&taoli_term1price.text&"时,将进行:"&vbCrLf&" " s=s&report1ComboBox1&taoli_term1num.text&"手 "&report1.stockname&" " s=s&report2ComboBox1&taoli_term1num.text&"手 "&report2.stockname if exce1="执行" then taoli_display1.caption=s&" 正在执行" else taoli_display1.caption=s&" 未执行" end if else taoli_display1.caption="条件1设立不足" exce1="" end if end if end sub Sub taoli_executebutton1_Click() '各个填入控件的约束 Set report1 = marketdata.GetReportData(taoli_report1.text,taoli_report1market) if report1 is nothing then msgbox "品种1代码或市场错误" exit sub else 'Application.MsgOut Report1.stockname&" "&Report1.NewPrice end if Set report2 = marketdata.GetReportData(taoli_report2.text,taoli_report2market) if report2 is nothing then msgbox "品种2代码或市场错误" exit sub else 'Application.MsgOut Report2.stockname&" "&Report2.NewPrice end if if taoli_term1price.text="" or not(IsNumeric(taoli_term1price.text)) then msgbox "条件开仓差价未填或不是数字" exit sub end if if taoli_term1num.text="" or not(IsNumeric(taoli_term1num.text)) or CInt(taoli_term1num.text)<0 then if CInt(taoli_term1num.text)<0 then msgbox "条件数量应大于0" exit sub end if msgbox "条件数量未填或不是数字" exit sub end if if taoli_term1cleanprice.text="" or not(IsNumeric(taoli_term1cleanprice.text)) then msgbox "条件平仓差价未填或不是数字" exit sub end if taoli_report1market.enabled=false taoli_report2market.enabled=false report1combobox1="taoli_report1ComboBox.value " report2combobox1="taoli_report2ComboBox.value " exce1="执行" k pingcang="" End Sub Sub taoli_test_Click() Set report1 = marketdata.GetReportData(taoli_report1.text,taoli_report1market) if report1 is nothing then msgbox "品种1代码或市场错误" exit sub else 'Application.MsgOut Report1.stockname&" "&Report1.NewPrice end if Set report2 = marketdata.GetReportData(taoli_report2.text,taoli_report2market) if report2 is nothing then msgbox "品种2代码或市场错误" exit sub else 'call marketdata.RegReportNotify(report2.Label,report2.marketName) ' Application.MsgOut Report2.stockname&" "&Report2.NewPrice end if call marketdata.RegReportNotify(report1.Label,report1.marketName) End Sub Sub taoli_report1_Change() if not report1 is nothing then call marketdata.UnRegReportNotify(report1.Label,report1.marketName) taoli_report1market.enabled=true set report1="nothing " end if End Sub Sub taoli_report2_Change() if not report2 is nothing then call marketdata.UnRegReportNotify(report2.Label,report2.marketName) taoli_report2market.enabled=true set report2="nothing " end if End Sub

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


加好友 发短信
等级:黑侠 帖子:676 积分:2548 威望:0 精华:3 注册:2011/4/13 16:18:50
  发帖心情 Post By:2011/7/28 15:12:41 [只看该作者]

不好意思

程序有些复杂,

因为要完成,一收成交后一手在下单的功能,所以用到了编程中的控制逻辑,如果没有经过编程练习的人,不太容易理解。也不太好讲。


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


加好友 发短信
等级:新手上路 帖子:18 积分:196 威望:0 精华:0 注册:2010/7/7 9:14:06
  发帖心情 Post By:2011/12/16 9:32:39 [只看该作者]

顶楼主!学习了,谢谢,希望有更多好的VBA分享


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


加好友 发短信
等级:论坛游民 帖子:219 积分:1350 威望:0 精华:0 注册:2011/2/27 21:22:43
  发帖心情 Post By:2012/1/9 17:46:32 [只看该作者]

好东西,谢谢!

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


加好友 发短信
等级:新手上路 帖子:27 积分:116 威望:0 精华:0 注册:2012/2/26 19:34:07
  发帖心情 Post By:2012/2/26 22:14:29 [只看该作者]

谢谢搂主的奉献, 我引入以后金字塔出错不能打开, 说是taoli.report1ComboBox 没有define, 不知道其他人有没有这个问题吗?


 回到顶部
总数 31 1 2 3 4 下一页