VS2015

源码下载地址:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.ClickDim ierr As Long = 0Dim serr As String = "sf111111111111111111111111"AddHandler tVQhtICtpClientAPI.OnEventsInfo, AddressOf OnMyEventsInfo 'OnEventsInfo(int InfoID, BSTR MsgInfo)AddHandler tVQhtICtpClientAPI.OnMarketData, AddressOf OnMyMarketDataAddHandler tVQhtICtpClientAPI.OnTradeConnected, AddressOf MyOnTdForntConnectdAddHandler tVQhtICtpClientAPI.OnTradeDisconnected, AddressOf MyOnTradeDisconnectedAddHandler tVQhtICtpClientAPI.OnMDConnected, AddressOf MyOnMDConnectedAddHandler tVQhtICtpClientAPI.OnMDDisconnected, AddressOf MyOnMDDisconnectedAddHandler tVQhtICtpClientAPI.OnOrder, AddressOf MyOnOrderAddHandler tVQhtICtpClientAPI.OnTrade, AddressOf MyOnTradeAddHandler tVQhtICtpClientAPI.OnOrderActionFailed, AddressOf MyOnOrderActionFailedAddHandler tVQhtICtpClientAPI.OnOrderCanceled, AddressOf MyOnOrderCanceledAddHandler tVQhtICtpClientAPI.OnOutPosition, AddressOf MyOnOutPositionAddHandler tVQhtICtpClientAPI.OnInstrumentStatus, AddressOf MyOnInstrumentStatusAddHandler tVQhtICtpClientAPI.OnGetPositionDetail, AddressOf MyOnGetPositionDetailAddHandler tVQhtICtpClientAPI.OnGetInstrument, AddressOf MyOnGetInstrumentAddHandler tVQhtICtpClientAPI.OnRspError, AddressOf MyOnRspErrorAddHandler tVQhtICtpClientAPI.OnRspQryTrade, AddressOf MyOnRspQryTradeAddHandler tVQhtICtpClientAPI.OnInitFinished, AddressOf MyOnInitFinishedAddHandler tVQhtICtpClientAPI.OnPushCancleOrder, AddressOf OnPushCancleOrderAddHandler tVQhtICtpClientAPI.OnDepthMarketData, AddressOf OnDepthMarketDataAddHandler tVQhtICtpClientAPI.OnRspQryOrder, AddressOf OnRspQryOrderAddHandler tVQhtICtpClientAPI.OnOutProgress, AddressOf OnOutProgressAddHandler tVQhtICtpClientAPI.OnOutExchangeTradeTimes, AddressOf MyOnOutExchangeTradeTimestVQhtICtpClientAPI.SetOrderCloseMode(0, ierr) '// 设置平仓时的优先顺序 : 0 先平昨再平今  1 先平今再平昨  默认为0 tVQhtICtpClientAPI.Login("config.ini", Me.textBox5.Text.ToString(), Me.textBox6.Text.ToString(), ierr, serr)Me.textBox1.AppendText("Loginid:" + ierr.ToString() + " Loginfo:" + serr.ToString() + tVQhtICtpClientAPI.GetAPIVersion() + vbCrLf)End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs)Dim i As IntegerFor i = 0 To 63tVQhtICtpClientAPI.SubscribeMD(MyInstr(i).InstrumentID)Me.textBox1.AppendText("SubscribeMD:" + MyInstr(i).InstrumentID + vbCrLf)NexttVQhtICtpClientAPI.SubscribeMD(Me.textBox3.Text.ToString())End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles button4.ClickMyUseVar.iBeginTest = 0If (Me.rb0.Checked = True) ThenDim ProductClass, MaxMarketOrderVolume, MinMarketOrderVolume,MaxLimitOrderVolume, MinLimitOrderVolume, IsTrading, OptionsType, CombinationType As IntegerDim LongMarginRatio, ShortMarginRatio, Multiply, PriceUnit, MarginRate As DoubleDim ExchangeID, InstrumentName, ExchangeInstID, ProductID, ExpireDate As StringtVQhtICtpClientAPI.GetInstrumentInfo(Me.textBox3.Text.ToString(), ProductClass, MaxMarketOrderVolume, MinMarketOrderVolume, MaxLimitOrderVolume, MinLimitOrderVolume,IsTrading, OptionsType, CombinationType, LongMarginRatio, ShortMarginRatio, Multiply, PriceUnit, MarginRate, ExchangeID, InstrumentName, ExchangeInstID, ProductID,ExpireDate)Me.textBox1.AppendText(String.Format("查询合约:{0} 产品类型:{1} 市价单最大下单量:{2} 市价单最小下单量:{3} 限价单最大下单量:{4} 限价单最小下单量:{5} " +"当前是否交易:{6} 期权类型:{7} 组合类型:{8} 多头保证金率:{9:f} 空头保证金率:{10:f} 合约数量乘数:{11:f} 最小变动价位:{12:f} 保证金率:{13:f} 交易所代码:{14} " +"合约在交易所的代码:{15} 合约名称:{16} 产品代码:{17} 到期日:{18}", Me.textBox3.Text.ToString(), ProductClass, MaxMarketOrderVolume, MinMarketOrderVolume,MaxLimitOrderVolume, MinLimitOrderVolume, IsTrading, OptionsType, CombinationType, LongMarginRatio, ShortMarginRatio, Multiply, PriceUnit,MarginRate, ExchangeID, InstrumentName, ExchangeInstID, ProductID, ExpireDate) + vbCrLf)ElseIf (Me.rb1.Checked = True) ThentVQhtICtpClientAPI.GetPositionDetail(Me.textBox3.Text.ToString())Me.textBox1.AppendText(String.Format("查询:{0} 持仓", MyUseVar.sInstrumentID) + vbCrLf)ElseIf (Me.rb2.Checked = True) ThenDim PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit As DoubletVQhtICtpClientAPI.GetAccount(PreBalance, Balance, PreMargin, Available,WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit)Me.textBox1.AppendText(String.Format("静态权益:{0:f} 结算准备金:{1:f} 占用保证金:{2:f} " +"可用资金:{3:f} 可取资金:{4:f} 交易所保证金:{5:f} 持仓盈亏:{6:f} 平仓盈亏:{7:f}",PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, PositionProfit, CloseProfit) + vbCrLf)ElseIf (Me.rb3.Checked = True) ThentVQhtICtpClientAPI.GetTradeInfo(Me.textBox3.Text.ToString())Me.textBox1.AppendText(String.Format("查询:{0} 成交信息", Me.textBox3.Text.ToString()) + vbCrLf)ElseIf (Me.rb4.Checked = True) ThentVQhtICtpClientAPI.SettlementInfoConfirm()Me.textBox1.AppendText(String.Format("确认结算单 !", Me.textBox3.Text.ToString()) + vbCrLf)ElseIf (Me.rb5.Checked = True) ThenElseIf (Me.rb6.Checked = True) ThenDim vOrderId, vIsCanC As IntegervOrderId = CInt(Me.textBox3.Text)tVQhtICtpClientAPI.IsOrderOpen(vOrderId, vIsCanC)Me.textBox1.AppendText(vMyTimeStr + " 订单:" + vOrderId.ToString() +" 是否可撤:" + vIsCanC.ToString() + " (0 :不可撤 1:可撤)" + vbCrLf)ElseIf (Me.rb7.Checked = True) ThenDim vd1, vd2 As IntegerDim lpp, spp, lprice, bidprice, sprice, askprice As DoubletVQhtICtpClientAPI.GetPositionPlus(Me.textBox3.Text.ToString(),MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen,vd1, lprice, lpp,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen,vd2, sprice, spp, bidprice, askprice)Me.textBox1.AppendText(String.Format("{0}  多头仓位:{1} 昨仓:{2} 冻结仓位:{3} 开仓挂起:{4} 持仓价格:{5:f} 持仓盈亏:{6:f} " +"BidPrice:{7:f} 空头仓位:{8} 昨仓:{9} 冻结仓位:{10} 开仓挂起:{11} 持仓价格:{12:f} 持仓盈亏:{13:f} AskPrice:{14:f}",Me.textBox3.Text.ToString(),MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen,vd1, lprice, lpp, bidprice,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen,vd2, sprice, spp, askprice) + vbCrLf)'多头仓位:IF2301 昨仓:1 冻结仓位:1 开仓挂起:0 持仓价格:0.00 持仓盈亏:3947.40 BidPrice:-120.00 空头仓位:3947 昨仓:1 冻结仓位:1 开仓挂起:0 持仓价格:0.00 持仓盈亏:3947.40 AskPrice:-120.00ElseIf (Me.rb8.Checked = True) ThenDim getflag As IntegertVQhtICtpClientAPI.GetHisOrderList("", getflag) '//查询历史订单信息 不为空查询指定合约。' //tVQhtICtpClientAPI.GeMeOrderList("IF2111")//查询历史订单信息 为空查询所有。ElseIf (Me.rb9.Checked = True) ThenDim YdClose, YdSettlement, Open, Last, Bid1, Ask1, Highest, Lowest, TradedAmmount As DoubleDim Bid1Volume, Ask1Volume, TradedVolume As IntegertVQhtICtpClientAPI.GetMarketData(Me.textBox3.Text.ToString(), YdClose, YdSettlement, Open, Last, Bid1, Ask1, Bid1Volume, Ask1Volume, Highest, Lowest,TradedVolume, TradedAmmount)Me.textBox1.AppendText(String.Format("{0} YdClose:{1:F2} YdSettlement:{2:F2} Open:{3:F2} " +"Last:{4:F2} Bid1:{5:F2} Ask1:{6:F2} Bid1Volume:{7} Ask1Volume:{8} " +"Highest:{9:F2} Lowest:{10:F2} TradedVolume:{11} TradedAmmount:{12:F2}\r\n",MyUseVar.sInstrumentID, YdClose, YdSettlement, Open, Last, Bid1, Ask1, Bid1Volume,Ask1Volume, Highest, Lowest, TradedVolume, TradedAmmount))ElseIf (Me.rb10.Checked = True) ThentVQhtICtpClientAPI.PrintOrderInfo()Me.textBox1.AppendText(String.Format("输出信息到:{0} 目录的{0}.txt文件中完成!{1}", Me.textBox5.Text.ToString(), vbCrLf))ElseIf (Me.rb11.Checked = True) ThenDim OpenRatioByVolume, OpenRatioByMoney, CloseRatioByMoney, CloseRatioByVolume, CloseTodayRatioByMoney, CloseTodayRatioByVolume As DoubletVQhtICtpClientAPI.GetInstrumentRatio(Me.textBox3.Text.ToString(), OpenRatioByMoney, OpenRatioByVolume, CloseRatioByMoney,CloseRatioByVolume, CloseTodayRatioByMoney, CloseTodayRatioByVolume)Me.textBox1.AppendText(String.Format("{0} OpenRatioByMoney:{1} OpenRatioByVolume:{2} CloseRatioByMoney:{3} CloseRatioByVolume:{4} " +"CloseTodayRatioByMoney :{5} CloseTodayRatioByVolume:{6}vbCrLf", Me.textBox3.Text.ToString(), OpenRatioByMoney,OpenRatioByVolume, CloseRatioByMoney, CloseRatioByVolume, CloseTodayRatioByMoney, CloseTodayRatioByVolume))ElseIf (Me.rb12.Checked = True) ThenDim PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit As DoubletVQhtICtpClientAPI.GetRealAccount(PreBalance, Balance, PreMargin, Available, WithdrawQuota,ExchangeMargin, CloseProfit, PositionProfit)Me.textBox1.AppendText(String.Format("静态权益:{0} 结算保证金:{1} 占用保证金:{2} 可用资金:{3} 可取资金:{4} 交易所保证金:{5} 平仓盈亏:{6} 持仓盈亏:{7}vbCrLf",PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit))ElseIf (Me.rb13.Checked = True) ThenDim lsp, ssp As DoubletVQhtICtpClientAPI.GetRealPosition(Me.textBox3.Text.ToString(), MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion,MyUseVar.MyPostion.LongFrozen, lsp, MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion,MyUseVar.MyPostion.ShortFrozen, ssp)Me.textBox1.AppendText(String.Format("{0} 多头仓位:{1} 昨仓:{2} 冻结仓位:{3} 持仓价格:{4} 空头仓位:{5} 昨仓:{6} 冻结仓位:{7} 持仓价格:{8}vbCrLf",Me.textBox3.Text.ToString(), MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen, lsp,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen, ssp))ElseIf (Me.rb14.Checked = True) ThenDim vOrderId, vIsCanC As IntegerTryvOrderId = Convert.ToInt32(Me.textBox7.Text.ToString())Catch ex As ExceptionvOrderId = 0End TrytVQhtICtpClientAPI.CancelOrder(vOrderId, vIsCanC)Me.textBox1.AppendText(vMyTimeStr + " 订单:" + vOrderId.ToString() + " 撤单状态:" + vIsCanC.ToString() + " 【0:撤单成功 其它:撤单不成功】vbCrLf")ElseIf (Me.rb15.Checked = True) ThenDim IsOk, Counts, Period As IntegerPeriod = 1tVQhtICtpClientAPI.CreateBars(Me.textBox3.Text.ToString(), Period, Counts, IsOk)Me.textBox1.AppendText(String.Format(" 合约:{0} 创建{1}分钟周期BARS完成:{2} 【1:创建成功 2:此周期已创建 -1:创建不成功】当前分钟计数器:{3}vbCrLf",Me.textBox3.Text.ToString(), Period, IsOk, Counts))If (IsOk = 2) ThentVQhtICtpClientAPI.DeleteBars(Me.textBox3.Text.ToString(), Period, IsOk)Me.textBox1.AppendText(String.Format(" 合约:{0} 删除{1}分钟周期BARS完成:{2} 【1:创建成功 2:此周期已创建 -1:创建不成功】vbCrLf",Me.textBox3.Text.ToString(), Period, IsOk))End IfElseIf (Me.rb17.Checked = True) ThenDim vopen, vhight, vclose, vlow As DoubleDim vmaxi, tmppe, counts, vvloumes As IntegerDim vuptime As StringtVQhtICtpClientAPI.GetBars(Me.textBox3.Text.ToString(), tmppe, 0, vmaxi, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime)Me.textBox1.AppendText(String.Format("合约{0} K线数据 周期:{1} 计数器:{2} 长度:{3} 序号:{4} open:{5} vclose:{6} vhight:{7} vlow:{8} vvloumes:{9} counts:{10} vuptime:{11}vbCrLf",Me.textBox3.Text.ToString(), tmppe, counts, vmaxi, 0, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime))If (vmaxi > 1) ThenDim vl As Integer = vmaxiFor i As Integer = 1 To vltVQhtICtpClientAPI.GetBars(Me.textBox3.Text.ToString(), tmppe, i, vmaxi, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime)Me.textBox1.AppendText(String.Format("合约{0} K线数据 周期:{1} 计数器:{2} 长度:{3} 序号:{4} open:{5} vclose:{6} vhight:{7} vlow:{8} vvloumes:{9} counts:{10} vuptime:{11}vbCrLf",Me.textBox3.Text.ToString(), tmppe, counts, vmaxi, i, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime))NextEnd IfElseIf (Me.rb18.Checked = True) ThenDim vinfoid As Integer = 0Dim vinfo As String = ""Me.textBox1.AppendText("==========CZCE======添加合约苹果2305交易时间段=========================" + vbCrLf) ''CZCEtVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP2305", 9, 0, 75, 0, vinfoid, vinfo)   ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP2305", 9, 0, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP2305", 10, 30, 60, 1, vinfoid, vinfo)  ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP2305", 10, 30, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP2305", 13, 30, 90, 9, vinfoid, vinfo)    ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP2305", 13, 30, vinfo, vinfoid) + vbCrLf)Me.textBox1.AppendText("==========SHFE======添加苹果所有合约交易时间段=========================" + vbCrLf) ''CZCEtVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP999", 9, 0, 75, 0, vinfoid, vinfo)   ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP999", 9, 0, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP999", 10, 30, 60, 1, vinfoid, vinfo)  ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP999", 10, 30, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP999", 13, 30, 90, 9, vinfoid, vinfo)    ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP999", 13, 30, vinfo, vinfoid) + vbCrLf)Me.textBox1.AppendText("============删除合约苹果2305交易时间段============================" + vbCrLf)tVQhtICtpClientAPI.DeleteExchangeTradeTimes("CZCE", "AP2305", vinfoid, vinfo)Me.textBox1.AppendText(String.Format("删除{0} 合约{1}交易时间 {2}", "CZCE", "AP2305", vinfo) + vbCrLf)Me.textBox1.AppendText("==========SHFE======螺纹合约交易时间段=========================" + vbCrLf)tVQhtICtpClientAPI.GetOutExchangeTradeTimes("SHFE", "rb9999")Me.textBox1.AppendText("================获取全部交易所全部合约交易时间段=========================" + vbCrLf) ''CZCEtVQhtICtpClientAPI.GetOutExchangeTradeTimes("", "")End If 'Me.Rb0.CheckedEnd Sub 'Button4_Click、、
''===============================显示时间=========================================================Private Delegate Sub DelegateSetTimeLabel1(vts As String)Private Sub SetTimeLabel1(vts As String) ''显示时间Me.label1.Text = "时间:" + vtsEnd Sub''========================================================================================'====================刷新账户资金状况.代码==================================Private Delegate Sub DelegateSetAccoutTable2()Private Sub InvokeSetAccoutTable2()Me.BeginInvoke(New DelegateSetAccoutTable2(AddressOf SetAccoutTable2)) ''显示账户资金状况End SubPrivate Sub SetAccoutTable2()Dim PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit As DoubletVQhtICtpClientAPI.GetAccount(PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit)Me.dataGridView2.Rows(0).Cells(0).Value = PreBalance.ToString()  '//静态权益Me.dataGridView2.Rows(0).Cells(1).Value = Balance.ToString()  '// 结算保证金Me.dataGridView2.Rows(0).Cells(2).Value = PreMargin.ToString()  '//占用保证金Me.dataGridView2.Rows(0).Cells(3).Value = Available.ToString()  '//可用资金Me.dataGridView2.Rows(0).Cells(4).Value = WithdrawQuota.ToString()  '//可取资金Me.dataGridView2.Rows(0).Cells(5).Value = ExchangeMargin.ToString()  '//交易所保证金Me.dataGridView2.Rows(0).Cells(6).Value = PositionProfit.ToString()  '//持仓盈亏或浮动盈亏Me.dataGridView2.Rows(0).Cells(7).Value = CloseProfit.ToString()  '//平仓盈亏End Sub''========================================================================================'====================刷新持仓盈利==================================Private Delegate Sub DelegateSetPositionsTable4()Private Sub InvokeSetPositionsTable4()Me.BeginInvoke(New DelegateSetPositionsTable4(AddressOf SetPositionsTable4)) ''显示账户资金状况End SubPrivate Sub SetPositionsTable4()Dim vd1, vd2 As IntegerDim lpp, spp, lprice, bidprice, sprice, askprice As DoubleFor i As Integer = 0 To MyPositionlist.Count - 1If (MyPositionlist(i).买卖 = " 卖") ThenTrytVQhtICtpClientAPI.GetPositionPlus(MyPositionlist(i).合约名称.ToString(),MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen,vd1, lprice, lpp,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen,vd2, sprice, spp, bidprice, askprice)MyPositionlist(i).BidPrice = bidpriceMyPositionlist(i).AskPrice = askpriceMyPositionlist(i).持仓盈亏 = sppMyPositionlist(i + 1).BidPrice = bidpriceMyPositionlist(i + 1).AskPrice = askpriceMyPositionlist(i + 1).持仓盈亏 = lppCatch ex As ExceptionEnd TryEnd IfNextMe.dataGridView4.DataSource = vbNull  '必须加此代码,否则表格不显示。Me.dataGridView4.DataSource = MyPositionlistEnd Sub''========================================================================================Private Delegate Sub DelegateSetAccountPositionTable()Private Sub InvokeSetAccountPositionTable()While (True) ''while 循环一定要放在线程直接调用的过程中。放在其它过程中 程序CPU占有率高或程序卡顿、崩溃。Thread.Sleep(1000)Dim vsec As Integer = DateTime.Now.SecondIf (vsec <> MyUseVar.RefSec) ThenMyUseVar.RefSec = vsecvMyTimeStr = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")Me.BeginInvoke(New DelegateSetTimeLabel1(AddressOf SetTimeLabel1), vMyTimeStr) ''显示时间Dim vt As New Thread(New ThreadStart(AddressOf InvokeSetAccoutTable2))  ''显示账户资金状况vt.IsBackground = Truevt.Start()Dim vt1 As New Thread(New ThreadStart(AddressOf InvokeSetPositionsTable4))  ''刷新持仓盈利vt1.IsBackground = Truevt1.Start()End IfEnd WhileEnd Sub'=====================================================================Public Sub MyOnInitFinished() '//初始化完成后回报。Me.textBox1.AppendText("MyOnInitFinished OK!" + vbCrLf)Dim vt1 As New Thread(New ThreadStart(AddressOf InvokeSetAccountPositionTable))vt1.IsBackground = Truevt1.Start()End Sub
''//#==================循环开平仓操作=================================Private Delegate Sub DelegateOpenCloseDoit()Private Sub InvokeOpenCloseDoit()While (True) ''while 循环一定要放在线程直接调用的过程中。放在其它过程中 程序CPU占有率高或程序卡顿、崩溃。Thread.Sleep(MyUseVar.Interval)Me.BeginInvoke(New DelegateOpenCloseDoit(AddressOf OpenCloseDoit)) ''OpenCloseDoitEnd WhileEnd SubPrivate Function GetRandom(vb As Integer, ve As Integer) As IntegerDim vrand As Random = New Random(Guid.NewGuid().GetHashCode())Return vrand.Next(vb, ve)End FunctionPrivate Sub OpenCloseDoit()If (MyUseVar.vloop = 0) Then ''获取持仓仓位 判断是否有可撤订单Me.textBox4.AppendText("======TickCount:" + System.Environment.TickCount.ToString() + "=======================" + vbCrLf)Dim vd1, vd2 As DoubletVQhtICtpClientAPI.GetPosition(MyUseVar.sInstrumentID, MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen, vd1,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen, vd2)Me.textBox4.AppendText(" Tick:" + System.Environment.TickCount.ToString() + " 多头仓位:" + MyUseVar.MyPostion.LongPostion.ToString() +" 昨仓:" + MyUseVar.MyPostion.LongYDPostion.ToString() + " 冻结仓位:" + MyUseVar.MyPostion.LongFrozen.ToString() +" 空头仓位:" + MyUseVar.MyPostion.ShortPostion.ToString() + " 昨仓:" + MyUseVar.MyPostion.ShortYDPostion.ToString() +" 冻结仓位:" + MyUseVar.MyPostion.ShortFrozen.ToString() + vbCrLf)MyUseVar.vIsCanC = 0MyUseVar.vIsCanC1 = 0MyUseVar.vIsCanC2 = 0MyUseVar.vIsCanC3 = 0If (MyUseVar.MyPostion.LongOrder > 0 Or MyUseVar.MyPostion.LongOrder1 > 0 Or MyUseVar.MyPostion.ShortOrder > 0 Or MyUseVar.MyPostion.ShortOrder1 > 0) ThenIf (MyUseVar.MyPostion.LongOrder > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC)Me.textBox4.AppendText(String.Format("LongOrder订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC, vbCrLf))End IfIf (MyUseVar.MyPostion.LongOrder1 > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1)Me.textBox4.AppendText(String.Format("LongOrder1订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1, vbCrLf))End IfIf (MyUseVar.MyPostion.ShortOrder > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2)Me.textBox4.AppendText(String.Format("ShortOrder订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2, vbCrLf))End IfIf (MyUseVar.MyPostion.ShortOrder1 > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3)Me.textBox4.AppendText(String.Format("ShortOrder1订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3, vbCrLf))End IfEnd IfIf (MyUseVar.vIsCanC = 1 Or MyUseVar.vIsCanC1 = 1 Or MyUseVar.vIsCanC2 = 1 Or MyUseVar.vIsCanC3 = 1) ThenMyUseVar.vloop = 1ElseMyUseVar.vloop = 4End IfEnd If '结束vloop=0 步骤 获取持仓仓位 判断是否有可撤订单'==========================================If (MyUseVar.vloop = 1) Then ''有订单可撤时 等待时间MyUseVar.vWaitInCancle = MyUseVar.vWaitInCancle + 1If (MyUseVar.vWaitInCancle >= MyUseVar.vWaitCancleInt) ThenMyUseVar.vWaitInCancle = 0MyUseVar.vloop = 2End IfEnd If '结束vloop=1 步骤 有订单可撤时 等待时间'==========================================If (MyUseVar.vloop = 2) Then ''有订单可撤 撤单If (MyUseVar.vIsCanC = 1) Then ' #longorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC)Me.textBox4.AppendText(String.Format("LongOrder订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC))End IfIf (MyUseVar.vIsCanC1 = 1) Then ' #longorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1)Me.textBox4.AppendText(String.Format("LongOrder1订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1))End IfIf (MyUseVar.vIsCanC2 = 1) Then ' #Shortorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2)Me.textBox4.AppendText(String.Format("ShortOrder订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2))End IfIf (MyUseVar.vIsCanC3 = 1) Then ' #Shortorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3)Me.textBox4.AppendText(String.Format("ShortOrder1订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3))End IfMyUseVar.vloop = 3End If  '结束vloop=2步骤 有订单可撤 撤单'==========================================If (MyUseVar.vloop = 3) Then ''有订单可撤时 等待时间MyUseVar.vWaitInCancle = MyUseVar.vWaitInCancle + 1If (MyUseVar.vWaitInCancle >= MyUseVar.vWaitCancleInt) ThenMyUseVar.vWaitInCancle = 0MyUseVar.vloop = 4End IfEnd If  '结束vloop=3步骤 有订单可撤时 等待时间'==========================================If (MyUseVar.vloop = 4) Then ''#循环开平仓开始 random.randint(1,10)'#=========================多头开平仓操作=================================Dim vrandtvol As IntegerIf (MyUseVar.MyPostion.LongPostion > 0 And MyUseVar.MyPostion.LongPostion < MyUseVar.iVolT) Then'=============================If (MyUseVar.MyPostion.LongOrderFlag = 1) Then '上次平仓vrandtvol = GetRandom(1, MyUseVar.MyPostion.LongPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Sell(MyUseVar.sInstrumentID, vrandtvol, vMyBidPrice, MyUseVar.MyPostion.LongOrder, MyUseVar.MyPostion.LongOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" LongOrder1:" + MyUseVar.MyPostion.LongOrder1.ToString() + " 继续平多头仓位:" + vrandtvol.ToString() + vbCrLf)Elsevrandtvol = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Buy(MyUseVar.sInstrumentID, vrandtvol, vMyAskPrice, MyUseVar.MyPostion.LongOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" 继续多头买开仓:" + vrandtvol.ToString() + vbCrLf)End If'=============================Else'=============================If (MyUseVar.MyPostion.LongPostion >= MyUseVar.iVolT) ThenDim vrandtvol1 As Integer = GetRandom(1, MyUseVar.MyPostion.LongPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Sell(MyUseVar.sInstrumentID, vrandtvol1, vMyBidPrice, MyUseVar.MyPostion.LongOrder, MyUseVar.MyPostion.LongOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" LongOrder1:" + MyUseVar.MyPostion.LongOrder1.ToString() + " 平多头多余仓位:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.LongOrderFlag = 1End IfIf (MyUseVar.MyPostion.LongPostion <= 0) Then '无持仓时Dim vrandtvol1 As Integer = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Buy(MyUseVar.sInstrumentID, vrandtvol1, vMyAskPrice, MyUseVar.MyPostion.LongOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" 多头买开仓:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.LongOrderFlag = 0End If'=============================End If '================结束多头开平仓操作======================================'#=========================空头开平仓操作=================================If (MyUseVar.MyPostion.ShortPostion > 0 And MyUseVar.MyPostion.ShortPostion < MyUseVar.iVolT) Then'=============================If (MyUseVar.MyPostion.ShortOrderFlag = 1) Then '上次平仓vrandtvol = GetRandom(1, MyUseVar.MyPostion.ShortPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Cover(MyUseVar.sInstrumentID, vrandtvol, vMyAskPrice, MyUseVar.MyPostion.ShortOrder, MyUseVar.MyPostion.ShortOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" ShortOrder1:" + MyUseVar.MyPostion.ShortOrder1.ToString() + " 继续平空头仓位:" + vrandtvol.ToString() + vbCrLf)Elsevrandtvol = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Short(MyUseVar.sInstrumentID, vrandtvol, vMyBidPrice, MyUseVar.MyPostion.ShortOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" 继续空头卖开仓:" + vrandtvol.ToString() + vbCrLf)End If'=============================Else'=============================If (MyUseVar.MyPostion.ShortPostion >= MyUseVar.iVolT) ThenDim vrandtvol1 As Integer = GetRandom(1, MyUseVar.MyPostion.ShortPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Cover(MyUseVar.sInstrumentID, vrandtvol1, vMyAskPrice, MyUseVar.MyPostion.ShortOrder, MyUseVar.MyPostion.ShortOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" ShortOrder1:" + MyUseVar.MyPostion.ShortOrder1.ToString() + " 平空头多余仓位:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.ShortOrderFlag = 1End IfIf (MyUseVar.MyPostion.ShortPostion <= 0) ThenDim vrandtvol1 As Integer = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Short(MyUseVar.sInstrumentID, vrandtvol1, vMyBidPrice, MyUseVar.MyPostion.ShortOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" 空头卖开仓:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.ShortOrderFlag = 0End If'=============================End If  '================结束空头开平仓操作======================================MyUseVar.vloop = 5End If  '结束vloop=4步骤 循环开平仓开始'==========================================If (MyUseVar.vloop = 5) Then ''#下订单后等待时间MyUseVar.vWaitInOrder = MyUseVar.vWaitInOrder + 1If (MyUseVar.vWaitInOrder >= MyUseVar.vWaitOrderInt) ThenMyUseVar.vWaitInOrder = 0MyUseVar.vloop = 0Me.textBox4.AppendText("===================================================================================" + vbCrLf)End IfEnd IfEnd SubPrivate Sub SetCloseMode()Dim vm, vsetok As IntegerIf (radioButton8.Checked) Thenvm = 0Elsevm = 1End IftVQhtICtpClientAPI.SetOrderCloseMode(vm, vsetok)Me.textBox1.AppendText(String.Format("设置平仓模式为:{0} 【0:先平昨再平今 1:先平今再平昨  默认为0】," +"设置成功于否标志:{1}(0:设置成功 1:设置不成功{2}", vm, vsetok, vbCrLf))End SubPrivate Sub radioButton7_CheckedChanged(sender As Object, e As EventArgs) Handles radioButton7.CheckedChangedSetCloseMode()End SubPrivate Sub radioButton8_CheckedChanged(sender As Object, e As EventArgs) Handles radioButton8.CheckedChangedSetCloseMode()End Sub''=====================================================================================================================================Private Sub button8_Click(sender As Object, e As EventArgs) Handles button8.ClickMyUseVar.iBeginTest = 1 - MyUseVar.iBeginTestIf (MyUseVar.iBeginTest = 1) Thenbutton8.Text = "暂停开平仓循环测试"OpenCloseThread = New Thread(New ThreadStart(AddressOf InvokeOpenCloseDoit))  ''OpenCloseDoitOpenCloseThread.IsBackground = TrueOpenCloseThread.Start()Elsebutton8.Text = "开始开平仓循环测试"OpenCloseThread.Abort()End IfEnd Sub

VS2015

源码下载地址:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.ClickDim ierr As Long = 0Dim serr As String = "sf111111111111111111111111"AddHandler tVQhtICtpClientAPI.OnEventsInfo, AddressOf OnMyEventsInfo 'OnEventsInfo(int InfoID, BSTR MsgInfo)AddHandler tVQhtICtpClientAPI.OnMarketData, AddressOf OnMyMarketDataAddHandler tVQhtICtpClientAPI.OnTradeConnected, AddressOf MyOnTdForntConnectdAddHandler tVQhtICtpClientAPI.OnTradeDisconnected, AddressOf MyOnTradeDisconnectedAddHandler tVQhtICtpClientAPI.OnMDConnected, AddressOf MyOnMDConnectedAddHandler tVQhtICtpClientAPI.OnMDDisconnected, AddressOf MyOnMDDisconnectedAddHandler tVQhtICtpClientAPI.OnOrder, AddressOf MyOnOrderAddHandler tVQhtICtpClientAPI.OnTrade, AddressOf MyOnTradeAddHandler tVQhtICtpClientAPI.OnOrderActionFailed, AddressOf MyOnOrderActionFailedAddHandler tVQhtICtpClientAPI.OnOrderCanceled, AddressOf MyOnOrderCanceledAddHandler tVQhtICtpClientAPI.OnOutPosition, AddressOf MyOnOutPositionAddHandler tVQhtICtpClientAPI.OnInstrumentStatus, AddressOf MyOnInstrumentStatusAddHandler tVQhtICtpClientAPI.OnGetPositionDetail, AddressOf MyOnGetPositionDetailAddHandler tVQhtICtpClientAPI.OnGetInstrument, AddressOf MyOnGetInstrumentAddHandler tVQhtICtpClientAPI.OnRspError, AddressOf MyOnRspErrorAddHandler tVQhtICtpClientAPI.OnRspQryTrade, AddressOf MyOnRspQryTradeAddHandler tVQhtICtpClientAPI.OnInitFinished, AddressOf MyOnInitFinishedAddHandler tVQhtICtpClientAPI.OnPushCancleOrder, AddressOf OnPushCancleOrderAddHandler tVQhtICtpClientAPI.OnDepthMarketData, AddressOf OnDepthMarketDataAddHandler tVQhtICtpClientAPI.OnRspQryOrder, AddressOf OnRspQryOrderAddHandler tVQhtICtpClientAPI.OnOutProgress, AddressOf OnOutProgressAddHandler tVQhtICtpClientAPI.OnOutExchangeTradeTimes, AddressOf MyOnOutExchangeTradeTimestVQhtICtpClientAPI.SetOrderCloseMode(0, ierr) '// 设置平仓时的优先顺序 : 0 先平昨再平今  1 先平今再平昨  默认为0 tVQhtICtpClientAPI.Login("config.ini", Me.textBox5.Text.ToString(), Me.textBox6.Text.ToString(), ierr, serr)Me.textBox1.AppendText("Loginid:" + ierr.ToString() + " Loginfo:" + serr.ToString() + tVQhtICtpClientAPI.GetAPIVersion() + vbCrLf)End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs)Dim i As IntegerFor i = 0 To 63tVQhtICtpClientAPI.SubscribeMD(MyInstr(i).InstrumentID)Me.textBox1.AppendText("SubscribeMD:" + MyInstr(i).InstrumentID + vbCrLf)NexttVQhtICtpClientAPI.SubscribeMD(Me.textBox3.Text.ToString())End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles button4.ClickMyUseVar.iBeginTest = 0If (Me.rb0.Checked = True) ThenDim ProductClass, MaxMarketOrderVolume, MinMarketOrderVolume,MaxLimitOrderVolume, MinLimitOrderVolume, IsTrading, OptionsType, CombinationType As IntegerDim LongMarginRatio, ShortMarginRatio, Multiply, PriceUnit, MarginRate As DoubleDim ExchangeID, InstrumentName, ExchangeInstID, ProductID, ExpireDate As StringtVQhtICtpClientAPI.GetInstrumentInfo(Me.textBox3.Text.ToString(), ProductClass, MaxMarketOrderVolume, MinMarketOrderVolume, MaxLimitOrderVolume, MinLimitOrderVolume,IsTrading, OptionsType, CombinationType, LongMarginRatio, ShortMarginRatio, Multiply, PriceUnit, MarginRate, ExchangeID, InstrumentName, ExchangeInstID, ProductID,ExpireDate)Me.textBox1.AppendText(String.Format("查询合约:{0} 产品类型:{1} 市价单最大下单量:{2} 市价单最小下单量:{3} 限价单最大下单量:{4} 限价单最小下单量:{5} " +"当前是否交易:{6} 期权类型:{7} 组合类型:{8} 多头保证金率:{9:f} 空头保证金率:{10:f} 合约数量乘数:{11:f} 最小变动价位:{12:f} 保证金率:{13:f} 交易所代码:{14} " +"合约在交易所的代码:{15} 合约名称:{16} 产品代码:{17} 到期日:{18}", Me.textBox3.Text.ToString(), ProductClass, MaxMarketOrderVolume, MinMarketOrderVolume,MaxLimitOrderVolume, MinLimitOrderVolume, IsTrading, OptionsType, CombinationType, LongMarginRatio, ShortMarginRatio, Multiply, PriceUnit,MarginRate, ExchangeID, InstrumentName, ExchangeInstID, ProductID, ExpireDate) + vbCrLf)ElseIf (Me.rb1.Checked = True) ThentVQhtICtpClientAPI.GetPositionDetail(Me.textBox3.Text.ToString())Me.textBox1.AppendText(String.Format("查询:{0} 持仓", MyUseVar.sInstrumentID) + vbCrLf)ElseIf (Me.rb2.Checked = True) ThenDim PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit As DoubletVQhtICtpClientAPI.GetAccount(PreBalance, Balance, PreMargin, Available,WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit)Me.textBox1.AppendText(String.Format("静态权益:{0:f} 结算准备金:{1:f} 占用保证金:{2:f} " +"可用资金:{3:f} 可取资金:{4:f} 交易所保证金:{5:f} 持仓盈亏:{6:f} 平仓盈亏:{7:f}",PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, PositionProfit, CloseProfit) + vbCrLf)ElseIf (Me.rb3.Checked = True) ThentVQhtICtpClientAPI.GetTradeInfo(Me.textBox3.Text.ToString())Me.textBox1.AppendText(String.Format("查询:{0} 成交信息", Me.textBox3.Text.ToString()) + vbCrLf)ElseIf (Me.rb4.Checked = True) ThentVQhtICtpClientAPI.SettlementInfoConfirm()Me.textBox1.AppendText(String.Format("确认结算单 !", Me.textBox3.Text.ToString()) + vbCrLf)ElseIf (Me.rb5.Checked = True) ThenElseIf (Me.rb6.Checked = True) ThenDim vOrderId, vIsCanC As IntegervOrderId = CInt(Me.textBox3.Text)tVQhtICtpClientAPI.IsOrderOpen(vOrderId, vIsCanC)Me.textBox1.AppendText(vMyTimeStr + " 订单:" + vOrderId.ToString() +" 是否可撤:" + vIsCanC.ToString() + " (0 :不可撤 1:可撤)" + vbCrLf)ElseIf (Me.rb7.Checked = True) ThenDim vd1, vd2 As IntegerDim lpp, spp, lprice, bidprice, sprice, askprice As DoubletVQhtICtpClientAPI.GetPositionPlus(Me.textBox3.Text.ToString(),MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen,vd1, lprice, lpp,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen,vd2, sprice, spp, bidprice, askprice)Me.textBox1.AppendText(String.Format("{0}  多头仓位:{1} 昨仓:{2} 冻结仓位:{3} 开仓挂起:{4} 持仓价格:{5:f} 持仓盈亏:{6:f} " +"BidPrice:{7:f} 空头仓位:{8} 昨仓:{9} 冻结仓位:{10} 开仓挂起:{11} 持仓价格:{12:f} 持仓盈亏:{13:f} AskPrice:{14:f}",Me.textBox3.Text.ToString(),MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen,vd1, lprice, lpp, bidprice,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen,vd2, sprice, spp, askprice) + vbCrLf)'多头仓位:IF2301 昨仓:1 冻结仓位:1 开仓挂起:0 持仓价格:0.00 持仓盈亏:3947.40 BidPrice:-120.00 空头仓位:3947 昨仓:1 冻结仓位:1 开仓挂起:0 持仓价格:0.00 持仓盈亏:3947.40 AskPrice:-120.00ElseIf (Me.rb8.Checked = True) ThenDim getflag As IntegertVQhtICtpClientAPI.GetHisOrderList("", getflag) '//查询历史订单信息 不为空查询指定合约。' //tVQhtICtpClientAPI.GeMeOrderList("IF2111")//查询历史订单信息 为空查询所有。ElseIf (Me.rb9.Checked = True) ThenDim YdClose, YdSettlement, Open, Last, Bid1, Ask1, Highest, Lowest, TradedAmmount As DoubleDim Bid1Volume, Ask1Volume, TradedVolume As IntegertVQhtICtpClientAPI.GetMarketData(Me.textBox3.Text.ToString(), YdClose, YdSettlement, Open, Last, Bid1, Ask1, Bid1Volume, Ask1Volume, Highest, Lowest,TradedVolume, TradedAmmount)Me.textBox1.AppendText(String.Format("{0} YdClose:{1:F2} YdSettlement:{2:F2} Open:{3:F2} " +"Last:{4:F2} Bid1:{5:F2} Ask1:{6:F2} Bid1Volume:{7} Ask1Volume:{8} " +"Highest:{9:F2} Lowest:{10:F2} TradedVolume:{11} TradedAmmount:{12:F2}\r\n",MyUseVar.sInstrumentID, YdClose, YdSettlement, Open, Last, Bid1, Ask1, Bid1Volume,Ask1Volume, Highest, Lowest, TradedVolume, TradedAmmount))ElseIf (Me.rb10.Checked = True) ThentVQhtICtpClientAPI.PrintOrderInfo()Me.textBox1.AppendText(String.Format("输出信息到:{0} 目录的{0}.txt文件中完成!{1}", Me.textBox5.Text.ToString(), vbCrLf))ElseIf (Me.rb11.Checked = True) ThenDim OpenRatioByVolume, OpenRatioByMoney, CloseRatioByMoney, CloseRatioByVolume, CloseTodayRatioByMoney, CloseTodayRatioByVolume As DoubletVQhtICtpClientAPI.GetInstrumentRatio(Me.textBox3.Text.ToString(), OpenRatioByMoney, OpenRatioByVolume, CloseRatioByMoney,CloseRatioByVolume, CloseTodayRatioByMoney, CloseTodayRatioByVolume)Me.textBox1.AppendText(String.Format("{0} OpenRatioByMoney:{1} OpenRatioByVolume:{2} CloseRatioByMoney:{3} CloseRatioByVolume:{4} " +"CloseTodayRatioByMoney :{5} CloseTodayRatioByVolume:{6}vbCrLf", Me.textBox3.Text.ToString(), OpenRatioByMoney,OpenRatioByVolume, CloseRatioByMoney, CloseRatioByVolume, CloseTodayRatioByMoney, CloseTodayRatioByVolume))ElseIf (Me.rb12.Checked = True) ThenDim PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit As DoubletVQhtICtpClientAPI.GetRealAccount(PreBalance, Balance, PreMargin, Available, WithdrawQuota,ExchangeMargin, CloseProfit, PositionProfit)Me.textBox1.AppendText(String.Format("静态权益:{0} 结算保证金:{1} 占用保证金:{2} 可用资金:{3} 可取资金:{4} 交易所保证金:{5} 平仓盈亏:{6} 持仓盈亏:{7}vbCrLf",PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit))ElseIf (Me.rb13.Checked = True) ThenDim lsp, ssp As DoubletVQhtICtpClientAPI.GetRealPosition(Me.textBox3.Text.ToString(), MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion,MyUseVar.MyPostion.LongFrozen, lsp, MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion,MyUseVar.MyPostion.ShortFrozen, ssp)Me.textBox1.AppendText(String.Format("{0} 多头仓位:{1} 昨仓:{2} 冻结仓位:{3} 持仓价格:{4} 空头仓位:{5} 昨仓:{6} 冻结仓位:{7} 持仓价格:{8}vbCrLf",Me.textBox3.Text.ToString(), MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen, lsp,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen, ssp))ElseIf (Me.rb14.Checked = True) ThenDim vOrderId, vIsCanC As IntegerTryvOrderId = Convert.ToInt32(Me.textBox7.Text.ToString())Catch ex As ExceptionvOrderId = 0End TrytVQhtICtpClientAPI.CancelOrder(vOrderId, vIsCanC)Me.textBox1.AppendText(vMyTimeStr + " 订单:" + vOrderId.ToString() + " 撤单状态:" + vIsCanC.ToString() + " 【0:撤单成功 其它:撤单不成功】vbCrLf")ElseIf (Me.rb15.Checked = True) ThenDim IsOk, Counts, Period As IntegerPeriod = 1tVQhtICtpClientAPI.CreateBars(Me.textBox3.Text.ToString(), Period, Counts, IsOk)Me.textBox1.AppendText(String.Format(" 合约:{0} 创建{1}分钟周期BARS完成:{2} 【1:创建成功 2:此周期已创建 -1:创建不成功】当前分钟计数器:{3}vbCrLf",Me.textBox3.Text.ToString(), Period, IsOk, Counts))If (IsOk = 2) ThentVQhtICtpClientAPI.DeleteBars(Me.textBox3.Text.ToString(), Period, IsOk)Me.textBox1.AppendText(String.Format(" 合约:{0} 删除{1}分钟周期BARS完成:{2} 【1:创建成功 2:此周期已创建 -1:创建不成功】vbCrLf",Me.textBox3.Text.ToString(), Period, IsOk))End IfElseIf (Me.rb17.Checked = True) ThenDim vopen, vhight, vclose, vlow As DoubleDim vmaxi, tmppe, counts, vvloumes As IntegerDim vuptime As StringtVQhtICtpClientAPI.GetBars(Me.textBox3.Text.ToString(), tmppe, 0, vmaxi, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime)Me.textBox1.AppendText(String.Format("合约{0} K线数据 周期:{1} 计数器:{2} 长度:{3} 序号:{4} open:{5} vclose:{6} vhight:{7} vlow:{8} vvloumes:{9} counts:{10} vuptime:{11}vbCrLf",Me.textBox3.Text.ToString(), tmppe, counts, vmaxi, 0, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime))If (vmaxi > 1) ThenDim vl As Integer = vmaxiFor i As Integer = 1 To vltVQhtICtpClientAPI.GetBars(Me.textBox3.Text.ToString(), tmppe, i, vmaxi, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime)Me.textBox1.AppendText(String.Format("合约{0} K线数据 周期:{1} 计数器:{2} 长度:{3} 序号:{4} open:{5} vclose:{6} vhight:{7} vlow:{8} vvloumes:{9} counts:{10} vuptime:{11}vbCrLf",Me.textBox3.Text.ToString(), tmppe, counts, vmaxi, i, vopen, vclose, vhight, vlow, vvloumes, counts, vuptime))NextEnd IfElseIf (Me.rb18.Checked = True) ThenDim vinfoid As Integer = 0Dim vinfo As String = ""Me.textBox1.AppendText("==========CZCE======添加合约苹果2305交易时间段=========================" + vbCrLf) ''CZCEtVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP2305", 9, 0, 75, 0, vinfoid, vinfo)   ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP2305", 9, 0, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP2305", 10, 30, 60, 1, vinfoid, vinfo)  ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP2305", 10, 30, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP2305", 13, 30, 90, 9, vinfoid, vinfo)    ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP2305", 13, 30, vinfo, vinfoid) + vbCrLf)Me.textBox1.AppendText("==========SHFE======添加苹果所有合约交易时间段=========================" + vbCrLf) ''CZCEtVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP999", 9, 0, 75, 0, vinfoid, vinfo)   ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP999", 9, 0, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP999", 10, 30, 60, 1, vinfoid, vinfo)  ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP999", 10, 30, vinfo, vinfoid) + vbCrLf)tVQhtICtpClientAPI.AppendExchangeTradeTimes("CZCE", "AP999", 13, 30, 90, 9, vinfoid, vinfo)    ''开盘时间段 标志位是 0 收盘时间段标志位是9 其他为1Me.textBox1.AppendText(String.Format("添加:{0} 合约:{1}交易时间段:{2}:{3}:{4}  vinfoid::{5}", "CZCE", "AP999", 13, 30, vinfo, vinfoid) + vbCrLf)Me.textBox1.AppendText("============删除合约苹果2305交易时间段============================" + vbCrLf)tVQhtICtpClientAPI.DeleteExchangeTradeTimes("CZCE", "AP2305", vinfoid, vinfo)Me.textBox1.AppendText(String.Format("删除{0} 合约{1}交易时间 {2}", "CZCE", "AP2305", vinfo) + vbCrLf)Me.textBox1.AppendText("==========SHFE======螺纹合约交易时间段=========================" + vbCrLf)tVQhtICtpClientAPI.GetOutExchangeTradeTimes("SHFE", "rb9999")Me.textBox1.AppendText("================获取全部交易所全部合约交易时间段=========================" + vbCrLf) ''CZCEtVQhtICtpClientAPI.GetOutExchangeTradeTimes("", "")End If 'Me.Rb0.CheckedEnd Sub 'Button4_Click、、
''===============================显示时间=========================================================Private Delegate Sub DelegateSetTimeLabel1(vts As String)Private Sub SetTimeLabel1(vts As String) ''显示时间Me.label1.Text = "时间:" + vtsEnd Sub''========================================================================================'====================刷新账户资金状况.代码==================================Private Delegate Sub DelegateSetAccoutTable2()Private Sub InvokeSetAccoutTable2()Me.BeginInvoke(New DelegateSetAccoutTable2(AddressOf SetAccoutTable2)) ''显示账户资金状况End SubPrivate Sub SetAccoutTable2()Dim PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit As DoubletVQhtICtpClientAPI.GetAccount(PreBalance, Balance, PreMargin, Available, WithdrawQuota, ExchangeMargin, CloseProfit, PositionProfit)Me.dataGridView2.Rows(0).Cells(0).Value = PreBalance.ToString()  '//静态权益Me.dataGridView2.Rows(0).Cells(1).Value = Balance.ToString()  '// 结算保证金Me.dataGridView2.Rows(0).Cells(2).Value = PreMargin.ToString()  '//占用保证金Me.dataGridView2.Rows(0).Cells(3).Value = Available.ToString()  '//可用资金Me.dataGridView2.Rows(0).Cells(4).Value = WithdrawQuota.ToString()  '//可取资金Me.dataGridView2.Rows(0).Cells(5).Value = ExchangeMargin.ToString()  '//交易所保证金Me.dataGridView2.Rows(0).Cells(6).Value = PositionProfit.ToString()  '//持仓盈亏或浮动盈亏Me.dataGridView2.Rows(0).Cells(7).Value = CloseProfit.ToString()  '//平仓盈亏End Sub''========================================================================================'====================刷新持仓盈利==================================Private Delegate Sub DelegateSetPositionsTable4()Private Sub InvokeSetPositionsTable4()Me.BeginInvoke(New DelegateSetPositionsTable4(AddressOf SetPositionsTable4)) ''显示账户资金状况End SubPrivate Sub SetPositionsTable4()Dim vd1, vd2 As IntegerDim lpp, spp, lprice, bidprice, sprice, askprice As DoubleFor i As Integer = 0 To MyPositionlist.Count - 1If (MyPositionlist(i).买卖 = " 卖") ThenTrytVQhtICtpClientAPI.GetPositionPlus(MyPositionlist(i).合约名称.ToString(),MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen,vd1, lprice, lpp,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen,vd2, sprice, spp, bidprice, askprice)MyPositionlist(i).BidPrice = bidpriceMyPositionlist(i).AskPrice = askpriceMyPositionlist(i).持仓盈亏 = sppMyPositionlist(i + 1).BidPrice = bidpriceMyPositionlist(i + 1).AskPrice = askpriceMyPositionlist(i + 1).持仓盈亏 = lppCatch ex As ExceptionEnd TryEnd IfNextMe.dataGridView4.DataSource = vbNull  '必须加此代码,否则表格不显示。Me.dataGridView4.DataSource = MyPositionlistEnd Sub''========================================================================================Private Delegate Sub DelegateSetAccountPositionTable()Private Sub InvokeSetAccountPositionTable()While (True) ''while 循环一定要放在线程直接调用的过程中。放在其它过程中 程序CPU占有率高或程序卡顿、崩溃。Thread.Sleep(1000)Dim vsec As Integer = DateTime.Now.SecondIf (vsec <> MyUseVar.RefSec) ThenMyUseVar.RefSec = vsecvMyTimeStr = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")Me.BeginInvoke(New DelegateSetTimeLabel1(AddressOf SetTimeLabel1), vMyTimeStr) ''显示时间Dim vt As New Thread(New ThreadStart(AddressOf InvokeSetAccoutTable2))  ''显示账户资金状况vt.IsBackground = Truevt.Start()Dim vt1 As New Thread(New ThreadStart(AddressOf InvokeSetPositionsTable4))  ''刷新持仓盈利vt1.IsBackground = Truevt1.Start()End IfEnd WhileEnd Sub'=====================================================================Public Sub MyOnInitFinished() '//初始化完成后回报。Me.textBox1.AppendText("MyOnInitFinished OK!" + vbCrLf)Dim vt1 As New Thread(New ThreadStart(AddressOf InvokeSetAccountPositionTable))vt1.IsBackground = Truevt1.Start()End Sub
''//#==================循环开平仓操作=================================Private Delegate Sub DelegateOpenCloseDoit()Private Sub InvokeOpenCloseDoit()While (True) ''while 循环一定要放在线程直接调用的过程中。放在其它过程中 程序CPU占有率高或程序卡顿、崩溃。Thread.Sleep(MyUseVar.Interval)Me.BeginInvoke(New DelegateOpenCloseDoit(AddressOf OpenCloseDoit)) ''OpenCloseDoitEnd WhileEnd SubPrivate Function GetRandom(vb As Integer, ve As Integer) As IntegerDim vrand As Random = New Random(Guid.NewGuid().GetHashCode())Return vrand.Next(vb, ve)End FunctionPrivate Sub OpenCloseDoit()If (MyUseVar.vloop = 0) Then ''获取持仓仓位 判断是否有可撤订单Me.textBox4.AppendText("======TickCount:" + System.Environment.TickCount.ToString() + "=======================" + vbCrLf)Dim vd1, vd2 As DoubletVQhtICtpClientAPI.GetPosition(MyUseVar.sInstrumentID, MyUseVar.MyPostion.LongPostion, MyUseVar.MyPostion.LongYDPostion, MyUseVar.MyPostion.LongFrozen, vd1,MyUseVar.MyPostion.ShortPostion, MyUseVar.MyPostion.ShortYDPostion, MyUseVar.MyPostion.ShortFrozen, vd2)Me.textBox4.AppendText(" Tick:" + System.Environment.TickCount.ToString() + " 多头仓位:" + MyUseVar.MyPostion.LongPostion.ToString() +" 昨仓:" + MyUseVar.MyPostion.LongYDPostion.ToString() + " 冻结仓位:" + MyUseVar.MyPostion.LongFrozen.ToString() +" 空头仓位:" + MyUseVar.MyPostion.ShortPostion.ToString() + " 昨仓:" + MyUseVar.MyPostion.ShortYDPostion.ToString() +" 冻结仓位:" + MyUseVar.MyPostion.ShortFrozen.ToString() + vbCrLf)MyUseVar.vIsCanC = 0MyUseVar.vIsCanC1 = 0MyUseVar.vIsCanC2 = 0MyUseVar.vIsCanC3 = 0If (MyUseVar.MyPostion.LongOrder > 0 Or MyUseVar.MyPostion.LongOrder1 > 0 Or MyUseVar.MyPostion.ShortOrder > 0 Or MyUseVar.MyPostion.ShortOrder1 > 0) ThenIf (MyUseVar.MyPostion.LongOrder > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC)Me.textBox4.AppendText(String.Format("LongOrder订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC, vbCrLf))End IfIf (MyUseVar.MyPostion.LongOrder1 > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1)Me.textBox4.AppendText(String.Format("LongOrder1订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1, vbCrLf))End IfIf (MyUseVar.MyPostion.ShortOrder > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2)Me.textBox4.AppendText(String.Format("ShortOrder订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2, vbCrLf))End IfIf (MyUseVar.MyPostion.ShortOrder1 > 0) ThenMe.tVQhtICtpClientAPI.IsOrderOpen(MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3)Me.textBox4.AppendText(String.Format("ShortOrder1订单:{0} 是否可撤:{1} (0 :不可撤 1:可撤){2}", MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3, vbCrLf))End IfEnd IfIf (MyUseVar.vIsCanC = 1 Or MyUseVar.vIsCanC1 = 1 Or MyUseVar.vIsCanC2 = 1 Or MyUseVar.vIsCanC3 = 1) ThenMyUseVar.vloop = 1ElseMyUseVar.vloop = 4End IfEnd If '结束vloop=0 步骤 获取持仓仓位 判断是否有可撤订单'==========================================If (MyUseVar.vloop = 1) Then ''有订单可撤时 等待时间MyUseVar.vWaitInCancle = MyUseVar.vWaitInCancle + 1If (MyUseVar.vWaitInCancle >= MyUseVar.vWaitCancleInt) ThenMyUseVar.vWaitInCancle = 0MyUseVar.vloop = 2End IfEnd If '结束vloop=1 步骤 有订单可撤时 等待时间'==========================================If (MyUseVar.vloop = 2) Then ''有订单可撤 撤单If (MyUseVar.vIsCanC = 1) Then ' #longorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC)Me.textBox4.AppendText(String.Format("LongOrder订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.LongOrder, MyUseVar.vIsCanC))End IfIf (MyUseVar.vIsCanC1 = 1) Then ' #longorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1)Me.textBox4.AppendText(String.Format("LongOrder1订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.LongOrder1, MyUseVar.vIsCanC1))End IfIf (MyUseVar.vIsCanC2 = 1) Then ' #Shortorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2)Me.textBox4.AppendText(String.Format("ShortOrder订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.ShortOrder, MyUseVar.vIsCanC2))End IfIf (MyUseVar.vIsCanC3 = 1) Then ' #Shortorder可撤tVQhtICtpClientAPI.CancelOrder(MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3)Me.textBox4.AppendText(String.Format("ShortOrder1订单:{0} 空头撤单 状态:{1}【0:撤单成功 其它:撤单不成功】vbCrLf",MyUseVar.MyPostion.ShortOrder1, MyUseVar.vIsCanC3))End IfMyUseVar.vloop = 3End If  '结束vloop=2步骤 有订单可撤 撤单'==========================================If (MyUseVar.vloop = 3) Then ''有订单可撤时 等待时间MyUseVar.vWaitInCancle = MyUseVar.vWaitInCancle + 1If (MyUseVar.vWaitInCancle >= MyUseVar.vWaitCancleInt) ThenMyUseVar.vWaitInCancle = 0MyUseVar.vloop = 4End IfEnd If  '结束vloop=3步骤 有订单可撤时 等待时间'==========================================If (MyUseVar.vloop = 4) Then ''#循环开平仓开始 random.randint(1,10)'#=========================多头开平仓操作=================================Dim vrandtvol As IntegerIf (MyUseVar.MyPostion.LongPostion > 0 And MyUseVar.MyPostion.LongPostion < MyUseVar.iVolT) Then'=============================If (MyUseVar.MyPostion.LongOrderFlag = 1) Then '上次平仓vrandtvol = GetRandom(1, MyUseVar.MyPostion.LongPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Sell(MyUseVar.sInstrumentID, vrandtvol, vMyBidPrice, MyUseVar.MyPostion.LongOrder, MyUseVar.MyPostion.LongOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" LongOrder1:" + MyUseVar.MyPostion.LongOrder1.ToString() + " 继续平多头仓位:" + vrandtvol.ToString() + vbCrLf)Elsevrandtvol = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Buy(MyUseVar.sInstrumentID, vrandtvol, vMyAskPrice, MyUseVar.MyPostion.LongOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" 继续多头买开仓:" + vrandtvol.ToString() + vbCrLf)End If'=============================Else'=============================If (MyUseVar.MyPostion.LongPostion >= MyUseVar.iVolT) ThenDim vrandtvol1 As Integer = GetRandom(1, MyUseVar.MyPostion.LongPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Sell(MyUseVar.sInstrumentID, vrandtvol1, vMyBidPrice, MyUseVar.MyPostion.LongOrder, MyUseVar.MyPostion.LongOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" LongOrder1:" + MyUseVar.MyPostion.LongOrder1.ToString() + " 平多头多余仓位:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.LongOrderFlag = 1End IfIf (MyUseVar.MyPostion.LongPostion <= 0) Then '无持仓时Dim vrandtvol1 As Integer = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Buy(MyUseVar.sInstrumentID, vrandtvol1, vMyAskPrice, MyUseVar.MyPostion.LongOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " LongOrder:" + MyUseVar.MyPostion.LongOrder.ToString() +" 多头买开仓:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.LongOrderFlag = 0End If'=============================End If '================结束多头开平仓操作======================================'#=========================空头开平仓操作=================================If (MyUseVar.MyPostion.ShortPostion > 0 And MyUseVar.MyPostion.ShortPostion < MyUseVar.iVolT) Then'=============================If (MyUseVar.MyPostion.ShortOrderFlag = 1) Then '上次平仓vrandtvol = GetRandom(1, MyUseVar.MyPostion.ShortPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Cover(MyUseVar.sInstrumentID, vrandtvol, vMyAskPrice, MyUseVar.MyPostion.ShortOrder, MyUseVar.MyPostion.ShortOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" ShortOrder1:" + MyUseVar.MyPostion.ShortOrder1.ToString() + " 继续平空头仓位:" + vrandtvol.ToString() + vbCrLf)Elsevrandtvol = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Short(MyUseVar.sInstrumentID, vrandtvol, vMyBidPrice, MyUseVar.MyPostion.ShortOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" 继续空头卖开仓:" + vrandtvol.ToString() + vbCrLf)End If'=============================Else'=============================If (MyUseVar.MyPostion.ShortPostion >= MyUseVar.iVolT) ThenDim vrandtvol1 As Integer = GetRandom(1, MyUseVar.MyPostion.ShortPostion) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Cover(MyUseVar.sInstrumentID, vrandtvol1, vMyAskPrice, MyUseVar.MyPostion.ShortOrder, MyUseVar.MyPostion.ShortOrder1)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" ShortOrder1:" + MyUseVar.MyPostion.ShortOrder1.ToString() + " 平空头多余仓位:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.ShortOrderFlag = 1End IfIf (MyUseVar.MyPostion.ShortPostion <= 0) ThenDim vrandtvol1 As Integer = GetRandom(1, MyUseVar.iVolT) '  # 产生随机开平仓手数量tVQhtICtpClientAPI.Short(MyUseVar.sInstrumentID, vrandtvol1, vMyBidPrice, MyUseVar.MyPostion.ShortOrder)Me.textBox4.AppendText(vMyTimeStr + " Tick:" + System.Environment.TickCount.ToString() + " ShortOrder:" + MyUseVar.MyPostion.ShortOrder.ToString() +" 空头卖开仓:" + vrandtvol1.ToString() + vbCrLf)MyUseVar.MyPostion.ShortOrderFlag = 0End If'=============================End If  '================结束空头开平仓操作======================================MyUseVar.vloop = 5End If  '结束vloop=4步骤 循环开平仓开始'==========================================If (MyUseVar.vloop = 5) Then ''#下订单后等待时间MyUseVar.vWaitInOrder = MyUseVar.vWaitInOrder + 1If (MyUseVar.vWaitInOrder >= MyUseVar.vWaitOrderInt) ThenMyUseVar.vWaitInOrder = 0MyUseVar.vloop = 0Me.textBox4.AppendText("===================================================================================" + vbCrLf)End IfEnd IfEnd SubPrivate Sub SetCloseMode()Dim vm, vsetok As IntegerIf (radioButton8.Checked) Thenvm = 0Elsevm = 1End IftVQhtICtpClientAPI.SetOrderCloseMode(vm, vsetok)Me.textBox1.AppendText(String.Format("设置平仓模式为:{0} 【0:先平昨再平今 1:先平今再平昨  默认为0】," +"设置成功于否标志:{1}(0:设置成功 1:设置不成功{2}", vm, vsetok, vbCrLf))End SubPrivate Sub radioButton7_CheckedChanged(sender As Object, e As EventArgs) Handles radioButton7.CheckedChangedSetCloseMode()End SubPrivate Sub radioButton8_CheckedChanged(sender As Object, e As EventArgs) Handles radioButton8.CheckedChangedSetCloseMode()End Sub''=====================================================================================================================================Private Sub button8_Click(sender As Object, e As EventArgs) Handles button8.ClickMyUseVar.iBeginTest = 1 - MyUseVar.iBeginTestIf (MyUseVar.iBeginTest = 1) Thenbutton8.Text = "暂停开平仓循环测试"OpenCloseThread = New Thread(New ThreadStart(AddressOf InvokeOpenCloseDoit))  ''OpenCloseDoitOpenCloseThread.IsBackground = TrueOpenCloseThread.Start()Elsebutton8.Text = "开始开平仓循环测试"OpenCloseThread.Abort()End IfEnd Sub