Documentation Center

  • Trial Software
  • Product Updates

history

Request Interactive Brokers historical data

Syntax

  • d = history(ib,ibContract,startdate,enddate) example
  • d = history(ib,ibContract,startdate,enddate,ticktype,period) example

Description

example

d = history(ib,ibContract,startdate,enddate) requests Interactive Brokers® historical data between startdate and enddate for default tick type 'TRADES' and default period of '1 day' over the IB Trader WorkstationSM connection ib using the IB Trader Workstation contract object ibContract to signify the instrument. history returns a matrix d with Interactive Brokers historical data.

example

d = history(ib,ibContract,startdate,enddate,ticktype,period) requests Interactive Brokers historical data for a specific type of market data tick ticktype and bar size period.

Examples

expand all

Request Interactive Brokers Historical Data with TRADES Default Tick Type and 1-Day Default Period

To request historical data, set up a connection ib using ibtws and create an IB Trader Workstation IContract object ibContract as shown in Request Interactive Brokers Historical Data. An IContract object is an Interactive Brokers object for containing the necessary data about a security to process transactions. See Interactive Brokers API Reference Guide to learn more about creating this object.

Request the last 5 days of historical data using ibContract.

startdate = floor(now) - 5;
enddate = floor(now);

d = history(ib,ibContract,startdate,enddate) 
d =
   1.0e+05 *
    7.3534    0.0079    0.0080    0.0078    0.0078    0.2386    0.1727    0.0079         0
    7.3534    0.0078    0.0080    0.0078    0.0079    0.1669    0.1075    0.0079         0
    7.3534    0.0079    0.0079    0.0078    0.0078    0.1982    0.1420    0.0078         0
    7.3534    0.0079    0.0080    0.0076    0.0078    0.3188    0.2239    0.0077         0
    7.3534    0.0078    0.0080    0.0077    0.0080    0.5568    0.3723    0.0079         0

d returns the historical data for 5 days. When ticktype and period are not specified as input arguments, history returns historical data using the default ticktype of 'TRADES' and the default period of '1 day'.

Each row of d contains historical data for 1 day. The columns in matrix d are a numeric representation of a date, open price, high price, low price, close price, volume, bar count, weighted average price, and flag indicating if there are gaps in the bar.

Display the open price in matrix d.

d(1,2)
ans =
    790.0000

Close the IB Trader Workstation connection.

 close(ib); 

Request Interactive Brokers Historical Data with BID Tick Type and 1-Week Period

To request historical data, set up a connection ib using ibtws and create an IB Trader Workstation IContract object ibContract as shown in Request Interactive Brokers Historical Data. An IContract object is an Interactive Brokers object for containing the necessary data about a security to process transactions. See Interactive Brokers API Reference Guide to learn more about creating this object.

Request the last 50 days of historical data using ibContract with the 'BID' tick type and a bar size of '1 week'.

startdate = floor(now) - 50;
enddate = floor(now);
ticktype = 'BID';
period = '1 week';

d = history(ib,ibContract,startdate,enddate,ticktype,period)  
d =
   1.0e+05 *
    7.3529    0.0080    0.0081    0.0078    0.0081   -0.0000   -0.0000   -0.0000         0
    7.3530    0.0080    0.0084    0.0080    0.0083   -0.0000   -0.0000   -0.0000         0
    7.3531    0.0082    0.0084    0.0081    0.0081   -0.0000   -0.0000   -0.0000         0
    7.3532    0.0080    0.0083    0.0079    0.0081   -0.0000   -0.0000   -0.0000         0
    7.3532    0.0081    0.0082    0.0079    0.0079   -0.0000   -0.0000   -0.0000         0
    7.3533    0.0079    0.0081    0.0078    0.0078   -0.0000   -0.0000   -0.0000         0
    7.3534    0.0078    0.0079    0.0077    0.0079   -0.0000   -0.0000   -0.0000         0
    7.3534    0.0079    0.0080    0.0076    0.0080   -0.0000   -0.0000   -0.0000         0

d returns the historical data for 50 days.

Each row of d contains historical data for 1 week. The columns in object d are a numeric representation of a date, open price, high price, low price, close price, volume, bar count, weighted average price, and flag indicating if there are gaps in the bar.

Display the high price in matrix d.

d(1,3)
ans =
    810

Close the IB Trader Workstation connection.

 close(ib); 

Request Interactive Brokers Historical Data with TRADES Default Tick Type and 1-Month Period

To request historical data, set up a connection ib using ibtws and create an IB Trader Workstation IContract object ibContract as shown in Request Interactive Brokers Historical Data. An IContract object is an Interactive Brokers object for containing the necessary data about a security to process transactions. See Interactive Brokers API Reference Guide to learn more about creating this object.

Request the last 50 days of historical data using ibContract with the 'TRADES' default tick type as denoted by the empty string and a bar size of '1 month'.

startdate = floor(now) - 50;
enddate = floor(now);
ticktype = '';
period = '1 month';

d = history(ib,ibContract,startdate,enddate,ticktype,period) 
d =
   1.0e+05 *
    7.3529    0.0079    0.0081    0.0078    0.0080    1.9128    1.3384    0.0080         0
    7.3532    0.0080    0.0084    0.0079    0.0079    4.0250    2.6757    0.0082         0
    7.3534    0.0079    0.0081    0.0076    0.0080    3.6047    2.4843    0.0079         0

d returns the historical data for 50 days.

Each row of d contains historical data for 1 month. The columns in object d are a numeric representation of a date, open price, high price, low price, close price, volume, bar count, weighted average price, and flag indicating if there are gaps in the bar.

Display the low price in matrix d.

d(1,4)
ans =
    780

Close the IB Trader Workstation connection.

 close(ib); 

Input Arguments

expand all

ib — IB Trader Workstation connectionconnection object

IB Trader Workstation connection, specified as an IB Trader Workstation connection object created using ibtws.

ibContract — IB Trader Workstation contractIContract object

IB Trader Workstation contract, specified as an IB Trader Workstation IContract object. This object is the instrument or security used in the order transaction. This object is created by calling the Interactive Brokers API function createContract. To learn more about createContract and the attributes you can set, see Interactive Brokers API Reference Guide.

startdate — Start datedate string | date scalar

Start date, specified as a starting date string or scalar.

Data Types: double | char

enddate — End datedate string | date scalar

End date, specified as an ending date string or scalar.

Data Types: double | char

ticktype — Types of market data ticks'TRADES' (default) | 'MIDPOINT' | 'BID' | 'ASK' | 'BID_ASK' | 'HISTORICAL_VOLATILITY' | 'OPTION_IMPLIED_VOLATILITY'

Types of market data ticks, specified as one of the above enumerated strings predetermined by the Interactive Brokers API that denote tick values to collect.

period — Bar size'1 day' (default) | '1 week' | '1 month'

Bar size, specified as one of the above enumerated strings predetermined by the Interactive Brokers API that denote the periodicity for collecting data.

Output Arguments

expand all

d — Interactive Brokers historical datamatrix

Interactive Brokers historical data, returned as a matrix with nine columns: numeric representation of a date, open price, high price, low price, close price, volume, bar count, weighted average price, and flag indicating if there are gaps in the bar.

See Also

| | | |

Was this topic helpful?