• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Programming the API: Architecture

EClientSocket and EWrapper Classes

Once the TWS is up and running and actively listening for incoming connections we are ready to write our code. This brings us to the TWS API's two major classes: the IBApi.EWrapper interface and the IBApi.EClientSocket

Implementing the EWrapper Interface

The IBApi.EWrapper interface is the mechanism through which the TWS delivers information to the API client application. By implementing this interface the client application will be able to receive and handle the information coming from the TWS. For further information on how to implement interfaces, refer to your programming language's documentation.

  • public class EWrapperImpl : EWrapper
  • public class EWrapperImpl implements EWrapper {
  • Public Class EWrapperImpl
    Implements EWrapper
  • class TestCppClient : public EWrapper
  • 1 class TestWrapper(wrapper.EWrapper):

The EClientSocket Class

The class used to send messages to TWS is IBApi.EClientSocket. Unlike EWrapper, this class is not overriden as the provided functions in EClientSocket are invoked to send messages to TWS. To use EClientSocket, first it may be necessary to implement the IBApi.EWrapper interface as part of its constructor parameters so that the application can handle all returned messages. Messages sent from TWS as a response to function calls in IBApi.EClientSocket require a EWrapper implementation so they can processed to meet the needs of the API client.

Another crucial element is the IBApi.EReaderSignal object passed to theEClientSocket's constructor. With the exception of Python, this object is used in APIs to signal a message is ready for processing in the queue. (In Python the Queue class handles this task directly). We will discuss this object in more detail in the The EReader Thread section.

  • EClientSocket clientSocket;
    public readonly EReaderSignal Signal;
    public EWrapperImpl()
    Signal = new EReaderMonitorSignal();
    clientSocket = new EClientSocket(this, Signal);
  • private EReaderSignal readerSignal;
    private EClientSocket clientSocket;
    protected int currentOrderId = -1;
    public EWrapperImpl() {
    readerSignal = new EJavaSignal();
    clientSocket = new EClientSocket(this, readerSignal);
  • Public eReaderSignal As EReaderSignal
    Public socketClient As EClientSocket
    Sub New()
    eReaderSignal = New EReaderMonitorSignal
    socketClient = New EClientSocket(Me, eReaderSignal)
    End Sub
  • EReaderOSSignal m_osSignal;
    EClientSocket * const m_pClient;
    TestCppClient::TestCppClient() :
    m_osSignal(2000)//2-seconds timeout
    , m_pClient(new EClientSocket(this, &m_osSignal))
    , m_state(ST_CONNECT)
    , m_sleepDeadline(0)
    , m_orderId(0)
    , m_extraAuth(false)
  • 1 class TestClient(EClient):
    2  def __init__(self, wrapper):
    3  EClient.__init__(self, wrapper)
    1 class TestApp(TestWrapper, TestClient):
    2  def __init__(self):
    3  TestWrapper.__init__(self)
    4  TestClient.__init__(self, wrapper=self)
    Note: The EReaderSignal class is not used for Python API. The Python Queue module is used for inter-thread communication and data exchange.