\'///////////////////////////////////////////////////////////////////////////////////////
dim OrderID, ConSign, Filled, Remaining, Action, OrderType, LmtPrice, Account, Kaiping, Code, Market
dim CodeT1,MarketT1
dim CodeT2,MarketT2
dim submitted09
dim submitted11
dim tradeing09,tradeing11,interestarbitrage
tradeing09=0
tradeing11=0
submitted09=0
submitted11=0
interestarbitrage=0\'套利开始符
orderid09=0
orderid11=0
\'//////////////////////////////////////////////////////////////////////////////////////////////
Sub APPLICATION_VBAStart()
\'设置一个定时器
call Application.SetTimer(2,1000)
End Sub
\'//////////////////////////////////////////////////////////////////////////////////////////////
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 and interestarbitrage=0 then\'当套利开始符=0时
orderid09= Order.Buy(0,1,Report1.SellPrice1,0,"CF09","ZQ","",0)
orderid11= Order.BuyShort(0,1,Report2.BuyPrice1,0,"CF11","ZQ","",0)
interestarbitrage=1\'套利开始进行
end if
\'当差价小于1000时进行套利平仓
Diff = Report1.BuyPrice1 - Report2.SellPrice1
if diff < 1000 and BuyHoding1 > 0 and interestarbitrage=1 then\'当套利开始符=1时
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
\'//////////////////////////////////////////////////////////////////////
Sub ORDER_OrderStatus(OrderID, Status, Filled, Remaining, Price, Code, Market)
Set Report1 = marketdata.GetReportData("AU09","SQ")
Set Report2 = marketdata.GetReportData("AU11","SQ")
if Status="Submitted" and tradeing09=0 and tradeing11=0 then
msgbox "已提交"
if Code="AU09"and Market="SQ" then
submitted09=1\'已经提交订单,在下面的if里处理已经提交的订单
end if
if Code="AU11"and Market="SQ" then
submitted11=1
end if
end if
if Status="Tradeing" or Status="Filled" then
if Code="AU09"and Market="SQ" and submitted09=0 then
msgbox "09已成交"
tradeing09=1
end if
if Code="AU11"and Market="SQ" and submitted11=0 then
msgbox "11已成交"
tradeing11=1
end if
if Code="AU09"and Market="SQ" and submitted09=1 then
msgbox "09已成交"
tradeing09=1
end if
if Code="AU11"and Market="SQ" and submitted11=1 then
msgbox "11已成交"
tradeing11=1
end if
if tradeing09=1 and tradeing11=1 then
msgbox "套利已完成"
end if
if tradeing09=1 or tradeing11=1 then
if tradeing11=1 then\'09没有交易
Set Report1 = marketdata.GetReportData("AU11","SQ")
Set Re1= Order.Sell(0,1,Report1.SellPrice1,0,"AU11","SQ","",0)
If Re1 = 0 Then
msgbox "11没有平成功"
end if
end if
\'/////////////////////////////////////////
if tradeing09=1 then\'11没有交易
Set Report2 = marketdata.GetReportData("AU09","SQ")
\'Set Re2=
call Order.Sell(0,1,Report2.SellPrice1,0,"AU09","SQ","",0)
\'If Re2 =0 Then
\'msgbox "09没有平成功"
\'end if
end if
end if
msgbox "已成交"
end if
if Status="Cancelled" then
msgbox "已扯单"
end if
if Status="Inactive" then
msgbox "本次委托无效"
end if
end sub
dim Timecount09,Timecount11
Timecount09=0
Timecount11=0
Sub APPLICATION_Timer(ID)
if submitted09=1 and tradeing09=0 and interestarbitrage=1 then\'当套利开始符=1时
Timecount09=Timecount09+1
if Timecount09>5 then
CancelOrder(orderid09)
end if
end if
if submitted11=1 and tradeing11=0 and interestarbitrage=1 then\'当套利开始符=1时
Timecount11=Timecount11+1
if Timecount11>5 then
CancelOrder(orderid11)
end if
end if
End Sub