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.

Request Interactive Brokers Historical Data

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.

Connect to the IB Trader Workstation and Create the IContract Object

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.

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 Interactive Brokers Historical Data

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 Connection

Close the IB Trader Workstation connection ib.

close(ib)

See Also

| | | | |

Related Examples

More About

External Websites

Was this topic helpful?