• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Error Handling

When a client application sends a message to TWS which requires a response which has an expected response (i.e. placing an order, requesting market data, subscribing to account updates, etc.), TWS will almost either always 1) respond with the relevant data or 2) send an error message to IBApi::EWrapper::error.

  • Exceptions when no response can occur: If a market data request is made during a competing session (when a paper user is logged into simultaneously on a different computer from the associated live user) before TWS v972, there will not be a message returned from TWS. Also, if a request is made prior to full establishment of connection (denoted by a returned 2104 or 2106 error code "Data Server is Ok"), there may not be a response from the request.

Error messages sent by the TWS are handled by the IBApi.EWrapper.error method. The IBApi.EWrapper.error event contains the originating request Id (or the orderId in case the error was raised when placing an order), a numeric error code and a brief description. It is important to keep in mind that this function is used for true error messages as well as notifications that do not mean anything is wrong.

API Error Messages when TWS is not set to the English Language

  • Currently on the Windows platform, error messages are sent using Latin1 encoding. If TWS is launched in a non-Western language, it is recommended to enable the setting at Global Configuration -> API -> Settings to "Show API error messages in English".
  • public class EWrapperImpl : EWrapper
    public virtual void error(int id, int errorCode, string errorMsg)
    Console.WriteLine("Error. Id: " + id + ", Code: " + errorCode + ", Msg: " + errorMsg + "\n");
  • public class EWrapperImpl implements EWrapper {
    public void error(int id, int errorCode, String errorMsg) {
    System.out.println("Error. Id: " + id + ", Code: " + errorCode + ", Msg: " + errorMsg + "\n");
  • Public Class EWrapperImpl
    Implements EWrapper
    Public Sub [error](id As Integer, errorCode As Integer, errorMsg As String) Implements IBApi.EWrapper.error
    Console.WriteLine("Error - Id [" & id & "] ErrorCode [" & errorCode & "] ErrorMsg [" & errorMsg & "]")
    End Sub
  • class TestCppClient : public EWrapper
    void TestCppClient::error(int id, int errorCode, const std::string& errorString)
    printf( "Error. Id: %d, Code: %d, Msg: %s\n", id, errorCode, errorString.c_str());
  • 1 class TestWrapper(wrapper.EWrapper):
    1  def error(self, reqId: TickerId, errorCode: int, errorString: str):
    2  super().error(reqId, errorCode, errorString)
    3  print("Error. Id:", reqId, "Code:", errorCode, "Msg:", errorString)

For a list containing all available error messages from the TWS, see Message Codes