Saturday, 18 June 2016

VB Trading Platform Tutorial 3 Streaming Data

This is the 3rd installment in my Trading platform creation using Visual Basic.
I am using Visual Studio 2012
In this video:
  • you will add some textboxes, comboboxes, labels, numberupdown, to your form
  • request and receive live streaming data bid, ask, and last prices
    The videos are best viewed at the highest quality and full screen.






TradingPlatformLatest














Copy the highlighted text and past it into the code in Visual Studio


 Private Sub cbSymbol_KeyDown(sender As Object, e As KeyEventArgs) Handles cbSymbol.KeyDown
     Try  
       AxTws1.reqAccountUpdates(0, "DU132564") ' added this  
       If e.KeyCode = Keys.Enter Then  
         ' Cancels market data from previous request  
         AxTws1.cancelMktData(0)  
         ' removes the anoying beep sound every time you hit enter.  
         e.SuppressKeyPress = True  
         ' Create a Contract object to hold contract details  
         Dim ContractInfo As TWSLib.IContract  
         ' Connect to the IB Data Server  
         ' The First parameter is the IP address of the server. Leave it blank  
         ' The Second parameter is the network port. Used the default of 7496  
         ' The third parameter is the client application identifier  
         ' Create a new TagValueList object (for API version 9.71)   
         Dim mktDataOptions As TWSLib.ITagValueList  
         ' Create a new empty contract to represent the data we will request  
         ContractInfo = AxTws1.createContract()  
         ' Now fill the ContractInfo object with the necessary information   
         ContractInfo.conId = 0       ' identifier  
         ' Stock symbol  
         ContractInfo.symbol = cbSymbol.Text  
         ' Security Type: Stock=STK, Option=OPT, Future=FUT, etc.  
         ContractInfo.secType = "STK"  
         ' The destination of order or request. "SMART" =IB order router  
         ContractInfo.exchange = "SMART"  
         ' The primary exchange where the instrument trades.   
         ContractInfo.primaryExchange = "NASDAQ"  
         ' The currency USD or GBP or CAD or EUR, etc.  
         ContractInfo.currency = "USD"  
         ' Make the request for streaming market data  
         '  TickerID, ContractInfo, genericTicks and Snapshot (0 for streaming)  
         AxTws1.reqMktDataEx(0, ContractInfo, "", 0)
         ' AxTws1.reqMktDataEx(0, ContractInfo, "", 0, mktDataOptions)  
         'Selects all text in the combo box  
         cbSymbol.SelectAll()  
         AxTws1.reqAccountUpdates(1, "DU132564") ' added this  
         Timer1.Start() ' added this  
       End If  
     Catch ex As Exception  
       MsgBox("Nothing in combobox" & vbCrLf & ex.Message)  
     End Try  
   End Sub  




tickPrice Event

 Private Sub AxTws1_tickPrice(sender As Object, e As AxTWSLib._DTwsEvents_tickPriceEvent) Handles AxTws1.tickPrice 
     ' Call method handles market data Price change events  
     ' When the call method is activated, price changes  
     ' appear in the Events objects named "e"   
     ' Properties of e are as follows:  
     ' e.id       The id that was set in the call to reqMktData  
     ' e.price      The lastest Price  
     ' e.tickType    The tick type 1 = bid, 2 = ask,   
     ' 4 = last, 6 = high, 7 = low, 9 = close   
     ' e.canAutoExecute A flag 1= the order can be automatically executed  
     ' If this is a last price change, then display it in the text box  
     If (e.tickType = 4) Then  
       ' Adds the latest price to the list box  
       'lbPrice.Items.Add("price = " & e.price)  
       tbLast.Text = e.price.ToString("0.00") 'formats to 2 decimals  
     End If  
     If (e.tickType = 2) Then  
       'Adds the Ask price to the text box   
       tbAsk.Text = e.price.ToString("0.00") 'formats to 2 decimals  
     End If  
     If (e.tickType = 1) Then  
       'Adds the Bid price to the text box  
       tbBid.Text = e.price.ToString("0.00") 'formats to 2 decimals  
     End If 
   End Sub  

KeyPress Event

 Private Sub cbSymbol_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cbSymbol.KeyPress
     ' when key is press changes it to uppercase  
     If Char.IsLetter(e.KeyChar) Then  
       e.KeyChar = Char.ToUpper(e.KeyChar)  
     End If      
   End Sub  







No comments:

Post a Comment