This example shows how to connect to the IB Trader WorkstationSM, create IB Trader Workstation
IContract
and IComboLegList
objects, and
create a combination order for a calendar spread. A calendar spread is one of many
combination order strategies. This strategy takes advantage of different stock
option expiration dates. This example creates a buy order on a calendar spread for
Google®. For details about IContract
objects,
IComboLegList
objects, and combination orders, see Interactive Brokers API Reference Guide.
This example uses the sample event handler function ibExampleEventHandler
to
handle events associated with creating a combination order. Use this
event handler or write a custom event handler function. For details,
see Writing and Running Custom Event Handler Functions with Interactive Brokers.
To access the code for this example, enter edit IBCombinationOrder.m
.
Connect to the IB Trader Workstation and create connection ib
using
the local host and port number 7496
.
ib = ibtws('',7496);
MATLAB® returns ib
as the connection
to the IB Trader Workstation with the Interactive Brokers® ActiveX® object,
the local host, and the port number that you choose.
IContract
ObjectsCreate the IB Trader Workstation IContract
object ibContract1
.
Here, this object describes the first call option in the calendar
spread. Create an IContract
object with these property
values:
Google symbol.
Stock option.
Expiry date is August 2014.
Strike price is $535.00.
Call option.
Number of shares is 100.
Aggregate exchange.
Primary exchange
USD currency.
Here, EX
is a sample primary exchange
name. Substitute your primary exchange name in ibContract1.primaryExchange
.
ibContract1 = ib.Handle.createContract; ibContract1.symbol = 'GOOG'; ibContract1.secType = 'OPT'; ibContract1.expiry = '201408'; ibContract1.strike = 535; ibContract1.right = 'C'; ibContract1.multiplier = '100'; ibContract1.exchange = 'SMART'; ibContract1.primaryExchange = 'EX'; ibContract1.currency = 'USD';
Request contract details for ibContract1
.
[cd1,ibReqID1] = contractdetails(ib,ibContract1);
cd1
returns the contract details data for ibContract1
. ibReqID1
returns
the request identifier for this contract details request.
Create the IB Trader Workstation IContract
object ibContract2
.
Here, this object describes the second call option in the calendar
spread. Create an IContract
object with these property
values:
Google symbol.
Stock option.
Expiry date is September 2014.
Strike price is $535.00.
Call option.
Number of shares is 100.
Aggregate exchange.
Primary exchange
USD currency.
Here, EX
is a sample primary exchange
name. Substitute your primary exchange name in ibContract2.primaryExchange
.
ibContract2 = ib.Handle.createContract; ibContract2.symbol = 'GOOG'; ibContract2.secType = 'OPT'; ibContract2.expiry = '201409'; ibContract2.strike = 535; ibContract2.right = 'C'; ibContract2.multiplier = '100'; ibContract2.exchange = 'SMART'; ibContract2.primaryExchange = 'EX'; ibContract2.currency = 'USD';
Request contract details for ibContract2
.
[cd2,ibReqID2] = contractdetails(ib,ibContract2);
cd2
returns the contract details data for ibContract2
. ibReqID2
returns
the request identifier for this contract details request.
IComboLegList
ObjectTo define the legs of the combination order, create the IB Trader Workstation IComboLegList
object comboLegs
.
comboLegs = ib.Handle.createComboLegList;
Here, this combination order has two legs. Add the first
leg to comboLegs
. The first leg contains these
property values:
IB Trader Workstation contract identifier for the first contract.
One-to-one leg ratio.
Sell the call option.
Aggregate exchange.
Identify an open or close order based on the parent security.
IB Trader Workstation routes the order without a designated broker.
Blank designated broker.
ibLeg1 = comboLegs.Add; ibLeg1.conId = cd1.summary.conId; ibLeg1.ratio = 1; ibLeg1.action = 'SELL'; ibLeg1.exchange = 'SMART'; ibLeg1.openClose = 0; ibLeg1.shortSaleSlot = 0; ibLeg1.designatedLocation = '';
Add the second leg to comboLegs
. The
second leg contains these property values:
IB Trader Workstation contract identifier for the second contract.
One-to-one leg ratio.
Buy the call option.
Aggregate exchange.
Identify an open or close order based on the parent security.
IB Trader Workstation routes the order without a designated broker.
Blank designated broker.
ibLeg2 = comboLegs.Add; ibLeg2.conId = cd2.summary.conId; ibLeg2.ratio = 1; ibLeg2.action = 'BUY'; ibLeg2.exchange = 'SMART'; ibLeg2.openClose = 0; ibLeg2.shortSaleSlot = 0; ibLeg2.designatedLocation = '';
Create the IB Trader Workstation IContract
object orderContract
for
the combination order. Create an IContract
object
with these property values:
Google symbol
Combination order type BAG
Aggregate exchange
Primary exchange
USD currency
IB Trader Workstation IComboLegList
object comboLegs
Here, EX
is a sample primary exchange
name. Substitute your primary exchange name in orderContract.primaryExchange
.
orderContract = ib.Handle.createContract; orderContract.symbol = 'GOOG'; orderContract.secType = 'BAG'; orderContract.exchange = 'SMART'; orderContract.primaryExchange = 'EX'; orderContract.currency = 'USD'; orderContract.comboLegs = comboLegs;
Create the IB Trader Workstation IOrder
object ibOrder
.
Here, the combination order is a market order to buy one combination
of the two legs.
ibOrder = ib.Handle.createOrder; ibOrder.action = 'BUY'; ibOrder.totalQuantity = 1; ibOrder.orderType = 'MKT';
Request the next valid order identification number id
using orderid
.
id = orderid(ib);
Execute the combination order ibOrder
using
these arguments:
IB Trader Workstation connection ib
Combination order IContract
object orderContract
IB Trader Workstation IOrder
object ibOrder
Order identifier id
Sample event handler ibExampleEventHandler
d = createOrder(ib,orderContract,ibOrder,id,@ibExampleEventHandler)
d = 768413.00
d
returns the unique order identifier for
this combination order.
Close the IB Trader Workstation connection ib
.
close(ib)
close
| contractdetails
| createOrder
| ibtws
| orderid