【译者注】 IB API 在线文档包含有 C#/Java/CPP/Python 等版本,程序的调用方法基本一致。但因为时间有限,不能翻译全部的版本,而且考虑到适用性和性能,因此在这个版本里,主要围绕着 Java API 进行翻译。您可以从 IB API 在线文档中找到自己熟悉的语言部分,差异不会很大。
http://interactivebrokers.github.io/tws-api/introduction.html
API Software Architecture
【译】 API 软件架构
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
【译】 对EWrapper接口的实现
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 implements 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 构造函数的 IBApi.EReaderSignal。除了Python之外,这个对象在 API 中被用于表示消息队列的状态。(在Python中,队列直接处理这些任务)。我们将在 EReader Thread 章节中详细的讨论这个对象。
private EReaderSignal readerSignal;
private EClientSocket clientSocket;
protected int currentOrderId = -1;
复制代码
public EWrapperImpl()
{
Signal = new EReaderMonitorSignal();
clientSocket = new EClientSocket(this, readerSignal);