以文本方式查看主题 - 金字塔客服中心 - 专业程序化交易软件提供商 (http://www.weistock.com/bbs/index.asp) -- 高级功能研发区 (http://www.weistock.com/bbs/list.asp?boardid=5) ---- 编写了一个套利的vba程序框架模板,感兴趣的朋友可以在此基础上做进一步开发 (http://www.weistock.com/bbs/dispbbs.asp?boardid=5&id=7088) |
-- 作者:26327756l -- 发布时间:2011/7/4 13:28:54 -- 编写了一个套利的vba程序框架模板,感兴趣的朋友可以在此基础上做进一步开发 编写了一个套利的vba程序。 目前的套利程序模板已经包含了价差分析,分批下单,自动追单等基础功能。
检查是否正确后,就可以单击“执行条件”按钮开始执行。 单击后,将程序的状态设为停止,单击“执行条件”后才可以继续进行自动交易。
下载地址 http://www.weistock.com/download/taoli.frm
导入方法:解压缩后,运行金字塔,点击“文件”菜单->导入 ,文件类型选窗体文件 导入taoli.frm窗体
关闭金字塔,重新启动 按Alt+F8弹出 执行宏 窗口,下拉菜单选择刚才我们导入的窗体即可运行了。 |
-- 作者:guotx2010 -- 发布时间:2011/7/5 8:25:15 -- 是个好东西,学习一下! |
-- 作者:26327756l -- 发布时间:2011/7/7 16:34:10 -- 对于外盘发现了一个问题 就是获取外盘的持仓量,为空仓的时候不对,没有判断返回的正确性。所以第一手开仓不行。国内品种交易没问题。 |
-- 作者:abqqe -- 发布时间:2011/7/14 9:49:17 -- 有没有 “品种市场“ 所代表的交易所的代码吗? 如上图 ZQ 是代表郑州吧? 还是有别的意义? |
-- 作者:fly -- 发布时间:2011/7/14 9:53:14 -- ZQ 是代表郑州期货交易所 工具--市场与板块,中 有“品种市场“ 所代表的交易所的代码对照表 |
-- 作者:abqqe -- 发布时间: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 -- 发布时间:2011/7/28 15:12:41 -- 不好意思 程序有些复杂, 因为要完成,一收成交后一手在下单的功能,所以用到了编程中的控制逻辑,如果没有经过编程练习的人,不太容易理解。也不太好讲。 |
-- 作者:light -- 发布时间:2011/12/16 9:32:39 -- 顶楼主!学习了,谢谢,希望有更多好的VBA分享 |
-- 作者:longbow -- 发布时间:2012/1/9 17:46:32 -- 好东西,谢谢! |
-- 作者:carpediem -- 发布时间:2012/2/26 22:14:29 -- 谢谢搂主的奉献, 我引入以后金字塔出错不能打开, 说是taoli.report1ComboBox 没有define, 不知道其他人有没有这个问题吗? |