Main Content

Request CQG Historical Data

This example shows how to connect to CQG®, define event handlers, and request historical data.

Connect to CQG

Create the CQG connection object using cqg.

c = cqg;

Define Event Handlers

Register the sample event handler cqgconnectioneventhandler to track events associated with connection status.

eventNames = {'CELStarted','DataError','IsReady', ...
    'DataConnectionStatusChanged'};

for i = 1:length(eventNames)
  registerevent(c.Handle,{eventNames{i}, ...
    @(varargin)cqgconnectioneventhandler(varargin{:})})
end

cqgconnectioneventhandler is assigned to the events in eventNames.

Set the API configuration properties. For example, to set the time zone to Eastern Time, enter the following.

c.APIConfig.TimeZoneCode = 'tzEastern';

c.APIConfig is a CQG configuration object. For details about setting API configuration properties, see CQG API Reference Guide.

Create the CQG connection.

startUp(c)
CELStarted
DataConnectionStatusChanged

The connection event handler displays event names for a successful CQG connection.

Register an event handler to build and initialize the output data matrix cqgHistoryData.

histEventNames = {'ExpressionResolved','ExpressionAdded', ...
    'ExpressionUpdated'};

for i = 1:length(histEventNames)
  registerevent(c.Handle,{histEventNames{i}, ...
    @(varargin)cqgexpressioneventhandler(varargin{:})})
end

Pass an Additional Optional Request Property

Pass an additional optional request property by creating the structure x and setting the optional property.

x.UpdatesEnabled = false;

For additional optional properties you can set, see CQG API Reference Guide.

Request CQG Historical Data

Request daily data for instrument XYZ.XYZ for the last 10 days using the additional optional request property x. XYZ.XYZ is a sample instrument name. To request historical data for your instrument, substitute the symbol name in instrument.

instrument = {'Close(XYZ.XYZ)','Open(XYZ.XYZ)'};
startdate = floor(now) - 10;
enddate = floor(now);
period = 'hpDaily';

history(c,instrument,startdate,enddate,period,x)
pause(1)

MATLAB® writes the variable cqgHistoryData to the Workspace browser.

Display cqgHistoryData.

cqgHistoryData
cqgHistoryData =
   1.0e+05 *
    7.3533    0.0063    0.0063
    7.3533    0.0064    0.0064
    7.3533    0.0065    0.0065
    7.3534    0.0065    0.0065
    7.3534    0.0066    0.0066
    7.3534    0.0065    0.0065
    7.3534    0.0066    0.0066
    7.3534    0.0066    0.0066
    7.3534    0.0064    0.0064

Each row in cqgHistoryData represents data for 1 day. The columns in cqgHistoryData show the numerical representation of the timestamp, the close price, and the open price for the instrument during the day.

Close the CQG Connection

close(c)

See Also

| | | | | | |

Related Examples

More About

External Websites