• 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, string advancedOrderRejectJson)
    {
    if (!Util.StringIsEmpty(advancedOrderRejectJson))
    {
    Console.WriteLine("Error. Id: " + id + ", Code: " + errorCode + ", Msg: " + errorMsg + ", AdvancedOrderRejectJson: " + advancedOrderRejectJson + "\n");
    }
    else
    {
    Console.WriteLine("Error. Id: " + id + ", Code: " + errorCode + ", Msg: " + errorMsg + "\n");
    }
    }
  • public class EWrapperImpl implements EWrapper {
    ...
    @Override
    public void error(int id, int errorCode, String errorMsg, String advancedOrderRejectJson) {
    String str = "Error. Id: " + id + ", Code: " + errorCode + ", Msg: " + errorMsg;
    if (advancedOrderRejectJson != null) {
    str += (", AdvancedOrderRejectJson: " + advancedOrderRejectJson);
    }
    System.out.println(str + "\n");
    }
  • Public Class EWrapperImpl
    Implements EWrapper
    ...
    Public Sub [error](id As Integer, errorCode As Integer, errorMsg As String, advancedOrderRejectJson As String) Implements IBApi.EWrapper.error
    If advancedOrderRejectJson <> "" Then
    Console.WriteLine("Error - Id [" & id & "] ErrorCode [" & errorCode & "] ErrorMsg [" & errorMsg & "] AdvancedOrderRejectJson [" & advancedOrderRejectJson & "]")
    Else
    Console.WriteLine("Error - Id [" & id & "] ErrorCode [" & errorCode & "] ErrorMsg [" & errorMsg & "]")
    End If
    End Sub
  • class TestCppClient : public EWrapper
    {
    ...
    void TestCppClient::error(int id, int errorCode, const std::string& errorString, const std::string& advancedOrderRejectJson)
    {
    if (!advancedOrderRejectJson.empty()) {
    printf("Error. Id: %d, Code: %d, Msg: %s, AdvancedOrderRejectJson: %s\n", id, errorCode, errorString.c_str(), advancedOrderRejectJson.c_str());
    } else {
    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, advancedOrderRejectJson = ""):
    2  super().error(reqId, errorCode, errorString, advancedOrderRejectJson)
    3  if advancedOrderRejectJson:
    4  print("Error. Id:", reqId, "Code:", errorCode, "Msg:", errorString, "AdvancedOrderRejectJson:", advancedOrderRejectJson)
    5  else:
    6  print("Error. Id:", reqId, "Code:", errorCode, "Msg:", errorString)
    7 

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