Execution Details
DISCLAIMER / WARNING
Just a heads up, this example will attempt to place an order automatically via the IB API. Please, use your paper trading account details when exploring this example.
See Interactive Brokers paper trading page for additional details
Contents
Initialize session with Trader Workstation
% initialize session with TWS session = TWS.Session.getInstance(); % create local buffer for exec details events [buf,lh] = TWS.initBufferForEvent(TWS.Events.EXECUTIONDETAILS); % connect to TWS session.eClientSocket.eConnect('127.0.0.1',7496,0);
added interface method: TWSNotification notification listener has been added Server Version:71 TWS Time at connection:20141231 11:36:23 EST
Request Order Execution Details
In order to get details about orders placed in Trader Worksstation it is neccessary to auto associate TWS order with the API. Otherwise, only execution details for order placed from the API will be forwarded.
% associate TWS orders with API
session.eClientSocket.reqAutoOpenOrders(true);
If there are multiple traders or many many order then will need to filter exec details
% want all the execution entries so init empty filter
filter = com.ib.client.ExecutionFilter();
Multiple requests for execution details can be active and are differentiated by the reqId. The request ID is just a unique integer identifier.
% create request id for these execution detials.
reqId = 0;
Now, provide request identifier and filter to obtain execution details (default: within last 24 hours)
% request current positions
session.eClientSocket.reqExecutions(0,filter);
Order Placement
to drive this example will place an order via the API
First create a generic contract for SPY
% create a stock contract for symbol SPY contract = com.tws.ContractFactory.GenericStockContract('SPY');
Next, create limit order associated with your account to BUY 100 shares at some price
% create an order with accnt, side, quantity, and price [*USE YOUR IB PAPER TRADING ACCOUNT NUMBER HERE*] order = com.tws.OrderFactory.GenericLimitOrder('DU207406', 'BUY', 100, 208.17);
Finally, place the order using NextOrderId along with the contract and order details above
% place the order with contract and order specifications
session.eClientSocket.placeOrder(8855,contract,order); pause(1);
Processing Execution Details Events
Execution details have a lot of information so make sure to read up on the references listed below.
It is easy to see how many execution details events are in the buffer by calling the size() method. Keep in mind that the buffer is circular with default size of 32. That is, after buffer capacity is exceeded, adding another event will kickout the oldest event to make room for new event.
buf.size()
ans = 27
Pull events from the buffer and put into cell array
% create cell array of ExecutionDetails objects
execEvents = collection2cell(buf);
Print execution details to command window
disp(execEvents{end}.data)
orderId = 8855 clientId = 0 conid = 756733 symbol = SPY secType = STK expiry = null strike = 0.0 right = null multiplier = null exchange = IEX primaryExch = null currency = USD localSymbol = SPY tradingClass = SPY execId = 00018037.54a4090a.01.01 time = 20141231 11:36:24 acctNumber = DU207406 executionExchange = IEX side = BOT shares = 100 price = 207.88 permId = 1012663775 liquidation = 0 cumQty = 100 avgPrice = 207.88 orderRef = null evRule = null evMultiplier = 0.0
See Also
TWSNextOrderIdExample | TWSOpenOrdersExample | TWSPositionsExample
References
Interactive Brokers API:
TWS@Github:
Apache Commons: