K Cardinal
K Cardinal

Reputation: 33

Interactive Brokers TWS API - Retrieve Contract Details Error

I am new to using the IB TWS API, and have attempted to start simple by just returning a simple contract ID of a stock. For some reason the contractDetails implements Ewrapper.contractDetails isn't getting hit. From my understanding, and research across several similar requests, it does not appear that there should be an error with the code.

starting with the IB class I have constructed:

Imports IBApi
Public Class IB
    Implements EWrapper

    Public eReaderSignal As EReaderSignal
    Public socketClient As EClientSocket
    Private m_client As EClient

    Private Client_ID As Integer = 0
    Private IP As String = "127.0.0.1"
    Private Port As Integer = 7497

    Public Sub Connect()

        eReaderSignal = New EReaderMonitorSignal
        socketClient = New EClientSocket(Me, eReaderSignal)

        socketClient.eConnect(IP, Port, Client_ID)

    End Sub
    Public Sub Disconnect()

        socketClient.eDisconnect()

    End Sub

    Public ContractID As String
    Public Function Return_ContractID(ByVal Stock As String) As String

        ContractID = String.Empty

        Dim Contract As New Contract
        Contract.Symbol = Stock
        Contract.SecType = "STK"
        Contract.Exchange = "SMART"
        Contract.Currency = "USD"

        socketClient.reqContractDetails(0, Contract)

        Return ContractID

        'End With

    End Function
    Public Sub connectAck() Implements EWrapper.connectAck
        Console.WriteLine("connected")
    End Sub
    Public Sub contractDetails(reqId As Integer, contractDetails As ContractDetails) Implements EWrapper.contractDetails

        Console.WriteLine(reqId.ToString)
        Console.Write(contractDetails.UnderSymbol)
        ContractID = contractDetails.UnderConId

    End Sub
End Class

and called via:

Module Module1

    Sub Main()

        Dim ibClient As New IB
        ibClient.Connect()
        Dim contractID As String = ibClient.Return_ContractID("AAPL")

        ibClient.Disconnect()

    End Sub

End Module

Now the interesting thing is the console will return:

connected

However, the contractDetails never gets hit or writes to console.

Checking the TWS Error log -> everything looks like it is good and the API is returning the following on the request:

-> -- a10-8-0-AAPL-STK--0--SMART-USD-AAPL-NMS-NMS-265598-0.01-100--.....

EDIT When asking IB API Support they had informed me that this is a coding issue and an issue outside of their support.

Upvotes: 0

Views: 629

Answers (1)

K Cardinal
K Cardinal

Reputation: 33

After I did some more digging on TWS API Documentation I realized I missed a crucial step. The API needed event processing. Have fixed by adding this to the connection step:

    Public Sub Connect()

    eReaderSignal = New EReaderMonitorSignal
    socketClient = New EClientSocket(Me, eReaderSignal)
    socketClient.eConnect(IP, Port, Client_ID)

    Dim reader As EReader = New EReader(socketClient, eReaderSignal)
    reader.Start()
    System.Threading.Thread.Sleep(1000) ' Wait for the reader to start

    ' Create a new thread to process events
    Dim eventProcessingThread As New Threading.Thread(Sub()
                                                          While socketClient.IsConnected()
                                                              eReaderSignal.waitForSignal()
                                                              reader.processMsgs()
                                                          End While
                                                      End Sub)

    eventProcessingThread.IsBackground = True
    eventProcessingThread.Start()

End Sub

Upvotes: 1

Related Questions