This example shows how to connect to the IB Trader WorkstationSM, create an IB Trader Workstation
IContract
object, and request historical data. For details about
the IContract
object, see Interactive Brokers API Reference Guide. To access the code for
this example, enter edit IBHistoricalDataWorkflow.m
.
IContract
ObjectConnect 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.
Create the IB Trader Workstation IContract
object ibContract
.
Here, this object describes a security with these property values:
XYZ
symbol
Stock security type
Aggregate exchange
Primary exchange
USD currency
XYZ
is a sample symbol name and EX
is
a sample primary exchange name. To create orders for your security,
substitute the symbol name in ibContract.symbol
and
primary exchange name in ibContract.primaryExchange
.
ibContract = ib.Handle.createContract; ibContract.symbol = 'XYZ'; ibContract.secType = 'STK'; ibContract.exchange = 'SMART'; ibContract.primaryExchange = 'EX'; ibContract.currency = 'USD'
ibContract = Interface.Tws_ActiveX_Control_module.IContract
Request the last 5 days of historical data using ibContract
.
startdate = floor(now) - 5; enddate = floor(now); d = history(ib,ibContract,startdate,enddate)
d = Columns 1 through 5 736308.00 751.83 755.85 743.83 749.46 736309.00 742.69 745.71 736.75 738.20 736312.00 743.08 748.73 724.17 748.48 736313.00 752.50 758.08 744.43 750.45 Columns 6 through 9 12513.00 9107.00 751.28 0 15984.00 11121.00 740.39 0 17125.00 11355.00 736.61 0 1935.00 2371.00 751.67 0
d
contains the historical data for 5 days.
Each row of d
contains historical data for
1 day. The columns in matrix d
are:
Numeric representation of a date
Open price
High price
Low price
Close price
Volume
Bar count
Weighted average price
Flag indicating if there are gaps in the bar
Close the IB Trader Workstation connection ib
.
close(ib)
close
| createOrder
| getdata
| history
| ibtws
| timeseries