Documentation

history

Request Interactive Brokers historical data

Syntax

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

Description

example

d = history(ib,ibContract,startdate,enddate) requests Interactive Brokers® historical data using the IB Trader WorkstationSM connection ib and IB Trader Workstation IContract object ibContract to signify the instrument. history requests data from startdate through enddate. The default tick type is 'TRADES' and default period is '1 day'.

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.

example

d = history(ib,ibContract,startdate,enddate,ticktype,period,tradehours) requests Interactive Brokers historical data using the flag tradehours to include all data or only data within regular trading hours.

example

d = history(ib,ibContract,startdate,enddate,ticktype,period,tradehours,eventhandler) requests Interactive Brokers historical data using an event handler function eventhandler. Use the sample event handler ibExampleEventHandler or write a custom event handler function.

Examples

collapse all

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

To request historical data, set up the IB Trader Workstation connection ib using ibtws. 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 data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request the last 5 days of historical data using ib and 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:

  • 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

Display the open price for the most recent record 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 the IB Trader Workstation connection ib using ibtws. 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 data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request the last 50 days of historical data using ib, ibContract, and these arguments:

  • Start date is 50 days ago.

  • End date is the current moment.

  • Tick type is 'BID'.

  • Bar size is '1W'.

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

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 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

Display the high price for the most recent record 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 the IB Trader Workstation connection ib using ibtws. 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 data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request the last 50 days of historical data using ib, ibContract, and these arguments:

  • Start date is 50 days ago.

  • End date is the current moment.

  • The empty string denotes the default tick type 'TRADES'.

  • Bar size is '1M'.

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

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 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

Display the low price for the most recent record in matrix d.

d(1,4)
ans =
    780

Close the IB Trader Workstation connection.

 close(ib) 

Request Interactive Brokers Historical Data Within Regular Trading Hours

To request historical data, set up the IB Trader Workstation connection ib using ibtws. 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 data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request the last 50 days of historical data using ib, ibContract, and these arguments:

  • Start date is 50 days ago.

  • End date is the current moment.

  • The empty string denotes the default tick type 'TRADES'.

  • Bar size is '1M'.

  • Within regular trading hours.

startdate = floor(now)-50;
enddate = floor(now);
ticktype = '';
period = '1M';
tradehours = true;

d = history(ib,ibContract,startdate,enddate,ticktype,period,...
            tradehours) 
d =

  Columns 1 through 5

     735805.00        591.25        599.55        585.21        588.85
     735812.00        587.50        592.45        562.80        565.90
     735819.00        568.85        575.32        560.00        568.45
     ...

  Columns 6 through 9

         -1.00         -1.00         -1.00             0
         -1.00         -1.00         -1.00             0
         -1.00         -1.00         -1.00             0
         ...

d returns the historical data for 50 days.

Each row of d contains historical data for 1 month.

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

Display the low price for the most recent record in matrix d.

d(1,4)
ans =
    585.21

Close the IB Trader Workstation connection.

 close(ib) 

Request Interactive Brokers Historical Data Using an Event Handler

To request historical data, set up the IB Trader Workstation connection ib using ibtws. 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 data about a security to process transactions. For details about creating this object, see Interactive Brokers API Reference Guide.

Request the last 50 days of historical data using ib, ibContract, and these arguments:

  • Start date is 50 days ago.

  • End date is the current moment.

  • The empty string denotes the default tick type 'TRADES'.

  • Bar size is '1M'.

  • Within regular trading hours.

  • Sample event handler function ibExampleEventHandler.

Use ibExampleEventHandler or write a custom event handler function.

startdate = floor(now)-50;
enddate = floor(now);
ticktype = '';
period = '1M';
tradehours = true;
eventhandler = 'ibExampleEventHandler';

d = history(ib,ibContract,startdate,enddate,ticktype,period,...
            tradehours,eventhandler) 
d =

       1576.00

  Columns 1 through 4

    [1x1 COM.TWS_TwsCtrl_1]    [22.00]    [1576.00]    '20140718'

  Columns 5 through 10

    [582.50]    [596.76]    [568.51]    [594.94]    [-1.00]    [-1.00]

  Columns 11 through 14

    [-1.00]    [0]    [1x1 struct]    'historicalData'
  ...

d is the request identifier.

After d, ibExampleEventHandler streams historical data to the Command Window. The columns are:

  • Interactive Brokers ActiveX® object

  • Event identifier

  • Request identifier

  • Date

  • Open price

  • High price

  • Low price

  • Close price

  • Volume

  • Bar count

  • Weighted average price

  • Flag indicating if there are gaps in the bar

  • Structure that repeats the contents of the columns

  • Event type

Close the IB Trader Workstation connection.

 close(ib) 

Related Examples

Input Arguments

collapse 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. Create this object by calling the Interactive Brokers API function createContract. For details about createContract and the attributes that 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 preceding enumerated strings. The Interactive Brokers API predetermines these strings to denote tick values to collect.

period — Bar size'1 day' (default) | '1W' | '1M'

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

tradehours — Trading hoursfalse (default) | true

Trading hours, specified as the logical true or false. When this flag is set to true, this function returns data only within regular trading hours. Otherwise, this function returns all data.

Data Types: logical

eventhandler — Event handlerfunction handle | string

Event handler, specified as a function handle or a string to identify an event handler function that processes the returned data. Use the sample event handler or write a custom event handler function. For details, see Writing and Running Custom Event Handler Functions with Interactive Brokers.

Example: @eventhandler

Data Types: function_handle | char

Output Arguments

collapse all

d — Interactive Brokers historical datamatrix | double

Interactive Brokers historical data, returned as a matrix with these columns:

  • 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

When using an event handler function, d is a double denoting the request identifier.

More About

collapse all

Tips

  • ibBuiltInErrMsg appears in the MATLAB® workspace. Check the status of connection and function execution by displaying the contents of this variable. ibBuiltInErrMsg contains messages related to:

    • Connection

    • Information resulting from executing functions

    • Errors

Was this topic helpful?