Rss & SiteMap

金字塔客服中心 - 专业程序化交易软件提供商 http://www.weistock.com/bbs/

专业程序化软件提供商
共12 条记录, 每页显示 10 条, 页签: [1] [2]
[浏览完整版]

标题:VBA套利模型范例

1楼
admin 发表于:2010/7/14 15:02:03

首先我们建立一个TLStart的宏,然后再响应品种报表数据变化事件,代码如下:

Sub TLStart()
 '注册CF09和CF11品种
 call marketdata.RegReportNotify("CF09","ZQ")
 call marketdata.RegReportNotify("CF11","ZQ")
End Sub

'响应注册的品种行情变化通知
Sub MARKETDATA_ReportNotify(ReportData)
 '得到这两个品种的行情报价
 Set Report1 = marketdata.GetReportData("CF09","ZQ")
 Set Report2 = marketdata.GetReportData("CF11","ZQ")
 
 '得到品种的持仓量等信息
 dim BuyHoding1
 dim BuyHlding2
 dim BuyTodayHoding1
 dim BuyTodayHoding2
 dim SellHoding1
 dim SellHoding2
 dim SellTodayHoding1
 dim SellTodayHoding2
 dim BuyCost
 dim SellCost
 dim PNL
 Dim UseMargin

 '取指定持仓品种信息
 call Order.HoldingInfoByCode2("CF09","ZQ",BuyHoding1,BuyCost,BuyTodayHoding1,SellHoding1,SellCost,SellTodayHoding1,PNL,UseMargin)
 call Order.HoldingInfoByCode2("CF11","ZQ",BuyHoding2,BuyCost,BuyTodayHoding2,SellHoding2,SellCost,SellTodayHoding2,PNL,UseMargin)
 
 '当差价出现大于1800时进行套利开仓
 '假设是09买 11卖
 Diff = Report1.SellPrice1 - Report2.BuyPrice1 '分别取卖价和买价计算差价 
 if Diff > 1800 and BuyHoding1 = 0 then
  call Order.Buy(0,1,Report1.SellPrice1,0,"CF09","ZQ","",0)
  call Order.BuyShort(0,1,Report2.BuyPrice1,0,"CF11","ZQ","",0)
 end if
 
 '当差价小于1000时进行套利平仓
 Diff = Report1.BuyPrice1 - Report2.SellPrice1
 if diff < 1000 and BuyHoding1 > 0 then
  call Order.Sell(0,1,Report1.BuyPrice1,0,"CF09","ZQ","",0)
  call Order.Sellshort(0,1,Report2.SellPrice1,0,"CF11","ZQ","",0)
 end if
End Sub

 

代码编写完毕后Alt+F8,然后选择我们刚才建立的TLStart宏名即可

[此贴子已经被作者于2010-7-14 21:54:24编辑过]
2楼
msedu 发表于:2010/7/14 21:22:37
太好了,非常感谢老大,如果能,多一些这样的范例就好了,:-)
3楼
lfxuezz 发表于:2010/11/3 17:04:50
这种举例的形式好!学习中。。。
4楼
Morgan 发表于:2010/11/4 11:38:11
dim BuyHlding2少个O
5楼
kyle 发表于:2011/6/3 10:45:15

建立建立TLStart宏后要怎么操作

6楼
王锋 发表于:2011/6/3 13:07:26
Alt+F8就可以执行
7楼
sunset0920 发表于:2012/7/5 11:11:22

好像 不能运行提示1024 usemargin 有错??

“缺少语句”

8楼
sunset0920 发表于:2012/7/5 11:30:46
图片点击可在新窗口打开查看
9楼
王锋 发表于:2012/7/5 11:58:08

要专业版才行的

10楼
guotx2010 发表于:2012/7/5 20:08:10

Sub TLStart()
 '注册CF09和CF11品种
 call marketdata.RegReportNotify("CF09","ZQ")
 call marketdata.RegReportNotify("CF11","ZQ")

End Sub

'响应注册的品种行情变化通知
Sub MARKETDATA_ReportNotify(ReportData)
 '得到这两个品种的行情报价
 Set Report1 = marketdata.GetReportData("CF09","ZQ")
 Set Report2 = marketdata.GetReportData("CF11","ZQ")


这里的获取行情信息,是不是重复了,既然已经使用RegReportNotify注册了品种,就可以在 MarketData_ReportNotity事件中获取价格了,所以,只需要判断ReportData返回的品种代码就可以知道是哪个品种的价格。

如:

if ReportData.label="CF09" then

    CF09Price=ReportData.BuyPrice1

else

    CF11Price=ReportData.SellPrice1

end if

Diff=CF09Price-CF11Price





共12 条记录, 每页显示 10 条, 页签: [1] [2]


Powered By Dvbbs Version 8.3.0
Processed in 0.02344 s, 2 queries.