Today I will show you how to submit an order to Interactive Brokers TWS directly from your Matlab work-space. In order to be able to do this you have to link Matlab to IB TWS first. Then, type in the following code to connect your Matlab station to IB TWS.
ib = ibtws('',7496);
Once connected, the next step is defining the IB contract that we would like to trade. We will build the contract by using the minimum required information which is the contract symbol, security type, currency and routing destination.
ibContract = ib.Handle.createContract; ibContract.symbol = 'AAPL'; ibContract.secType = 'STK'; ibContract.exchange = 'SMART'; ibContract.currency = 'USD';
The next step is to configure the parameters for the orders we prepare for submission:
ibLMTOrder = ib.Handle.createOrder; ibLMTOrder.action = 'BUY'; ibLMTOrder.totalQuantity = 100; ibLMTOrder.orderType = 'LMT'; ibLMTOrder.lmtPrice = 113; ibLMTOrder.account = 'DUxxxxx6';
In this case we will submit a buy limit order @ 113 USD for 100 Apple* shares on account number DUxxxxx6. After the order is configured, we can also set a unique order identifier, and send the orders to Interactive Brokers.
id = orderid(ib); result = createOrder(ib, ibContract, ibLMTOrder, id)
The Matlab Trading Toolbox function that submits the order is called createOrder. The function creates an IB Trader Workstation ibLMTOrder over the IB Trader Workstation connection ib for the ibContract with a unique order identifier id and returns the order result containing data about the completed order.
result = STATUS: 'PreSubmitted' FILLED: 0 REMAINING: 100 AVG_FILL_PRICE: 0 PERM_ID: '326506298' PARENT_ID: 0 LAST_FILL_PRICE: 0 CLIENT_ID: 0 WHY_HELD: ''
Your Matlab work-space will show the following.
The code will generate and transmit the order from your trader workstation similar to the screenshot below.
* The tickers shown in examples are random, for exercising purposes only and do not represent any advise to invest in them nor any other kind of advertising.