Problem
The IBKR TWS (Trader Workstation) is a tool for managing stock orders in the stock market, by Interactive Brokers. They provide an API to automate orders, like placing orders, cancelling orders, and more.
I'm creating a program to handle executed orders in my Trader Workstation using the Interactive Brokers Java API.
I'm having trouble detecting when an order fills.
The documentation describes that the execDetails callback (which is an EWrapper method, see code below) is invoked when an order is filled, but I tried using that and the execDetails callback was never invoked (I tested this by logging the reqid int in that callback, and I never got any log).
I have also researched about the completedOrder callback, which I'm not sure if that's the callback that will be invoked when an order is filled, because I tested both callbacks with a simple log, and nothing was outputting in the console.
I don't understand the reqExecutions function and whether I need that. I have already read the documentation on this callback, and I don't understand what I'm doing wrong. I want to know I how can detect when an order fills, or executes in the TWS using their API.
Code
Here is my current code:
import com.ib.client.*;
import java.util.*;
public class Main implements EWrapper {
private static EClientSocket clientSocket;
private static EJavaSignal readerSignal;
public static void main(String[] args) throws InterruptedException, IOException {
readerSignal = new EJavaSignal();
clientSocket = new EClientSocket(this, readerSignal);
clientSocket.eConnect("127.0.0.1", 7497, 0);
}
clientSocket.placeOrder(orderidE, contractFill("CUSIP", "USD", tickSymbol, "STK", "SMART"), orderFill(lmtPriceDouble, actionString, "LMT", "GTC", orderQuantity, account, 0));
//Order executes successfully after sometime
public static Order orderFill(double lmtPrice, String action, String orderType, String tif, int totalQuantity, String account, int clientId){
Order order = new Order();
order.m_lmtPrice = lmtPrice;
order.m_action = action;
order.m_orderType = orderType;
order.m_tif = tif;
order.m_totalQuantity = totalQuantity;
order.m_account = account;
order.m_clientId = clientId;
return order;
}
public static Contract contractFill(String secIdType, String currency, String symbol, String secType, String exchange){
Contract contract = new Contract();
contract.m_secIdType = secIdType;
contract.m_currency = currency;
contract.m_symbol = symbol;
contract.m_secType = secType;
contract.m_exchange = exchange;
return contract;
}
/*Implemented EWrapper methods
...
*/
@Override
public void execDetails(int reqId, Contract contract, Execution execution) {
System.out.println(execution + " " + contract + " " + reqId);
}
@Override
public void execDetailsEnd(int reqId) {
System.out.println(reqId);
}
/*Implemented EWrapper methods
...
*/
@Override
public void completedOrder(Contract contract, Order order, OrderState orderState) {
System.out.println(contract);
System.out.println(order);
System.out.println(orderState);
}
@Override
public void completedOrdersEnd() {
System.out.println("cOE");
}/*Implemented rest of EWrapper methods
...
*/
}
I am placing the orders by hand while this code is running, and the order fills fairly quickly (while the code is running), so the code should detect it, but (my problem -->)none of the callbacks are being invoked. What am I supposed to be doing to detect order executions? (Note: I'm placing the orders by hand and by code in the TWS as of now).