Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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

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 =

  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        747.65

  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
       2139.00       2568.00        751.29             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 =

     751.83

Close the IB Trader Workstation connection.

 close(ib) 

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 =

  Columns 1 through 5

     736267.00        699.28        720.36        695.10        710.50
     736274.00        710.35        739.20        703.18        732.77
     736281.00        730.00        740.92        711.99        711.99
     736288.00        713.05        757.73        706.00        756.35
     736295.00        755.30        762.70        737.52        748.56
     736302.00        749.33        775.81        740.00        766.15
     736309.00        765.00        768.18        735.57        738.20
     736312.00        738.87        757.77        700.00        747.84

  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
         -1.00         -1.00         -1.00             0
         -1.00         -1.00         -1.00             0
         -1.00         -1.00         -1.00             0
         -1.00         -1.00         -1.00             0
         -1.00             0         -1.00             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 =

     720.36

Close the IB Trader Workstation connection.

 close(ib) 

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 character vector 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 =

  Columns 1 through 5

     736267.00        661.18        738.42        641.64        710.85
     736298.00        712.00        762.71        705.85        742.60
     736312.00        745.50        775.96        724.17        748.73

  Columns 6 through 9

     186268.00     127222.00        692.28             0
     234490.00     160672.00        734.32             0
     151754.00     102702.00        754.11             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 =

     641.64

Close the IB Trader Workstation connection.

 close(ib) 

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

     736267.00        661.18        730.00        641.73        710.81
     736298.00        711.21        762.71        705.85        742.60
     736312.00        747.11        775.96        724.17        748.73

  Columns 6 through 9

     169656.00     125271.00        691.49             0
     210536.00     160260.00        734.41             0
     135075.00     102377.00        753.82             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 =

     641.73

Close the IB Trader Workstation connection.

 close(ib) 

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 character vector 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 =

       9157.00

  Columns 1 through 4

    [1x1 COM.TWS_TwsCtrl_1]    [22.00]    [9157.00]    '20151030'

  Columns 5 through 9

    [661.18]    [730.00]    [641.73]    [710.81]    [169656.00]

  Columns 10 through 14

    [125271.00]    [691.49]    [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 Trader Workstation connection, specified as an IB Trader Workstation connection object created using ibtws.

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.

Start date, specified as a starting date character vector or scalar.

Data Types: double | char

End date, specified as an ending date character vector or scalar.

Data Types: double | char

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

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

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

Event handler, specified as a function handle or a character vector 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

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

Introduced in R2013b

Was this topic helpful?