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 LYNXApi.EWrapper interface and the LYNXApi.EClientSocket

Implementing the EWrapper Interface

The LYNXApi.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.

** Python **

class TestWrapper(wrapper.EWrapper):

** Java **

public class EWrapperImpl implements EWrapper {

The EClientSocket Class

The class used to send messages to TWS is LYNXApi.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 LYNXApi.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 LYNXApi.EClientSocket require a EWrapper implementation so they can processed to meet the needs of the API client.

Another crucial element is the LYNXApi.EReaderSignal object passed to the EClientSocket'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.

** Python **

class TestClient(EClient):
    def __init__(self, wrapper):
        EClient.__init__(self, wrapper)

class TestApp(TestWrapper, TestClient):
    def __init__(self):
        TestClient.__init__(self, wrapper=self)

** Java **

private EReaderSignal readerSignal;
private EClientSocket clientSocket;
protected int currentOrderId = -1;

public EWrapperImpl() {
    readerSignal = new EJavaSignal();
    clientSocket = new EClientSocket(this, readerSignal);

[!NOTE|style:flat] The EReaderSignal class is not used for Python API. The Python Queue module is used for inter-thread communication and data exchange.