Main Content

Request CQG Intraday Tick Data

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

Connect to CQG and Define Event Handlers

Create the CQG connection object using cqg.

c = cqg;

Register the sample event handler cqgconnectioneventhandler to track events associated with the 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 structure cqgTickData used for storing intraday tick data.

rawEventNames = {'TicksResolved','TicksAdded'};

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

Request CQG Intraday Tick Data

Pass an additional optional request property by creating the structure x, and setting the optional property. To see only bid tick data, for example, set TickFilter to 'tfBid'.

x.TickFilter = 'tfBid';

TickFilter and SessionsFilter are the only valid additional optional properties for calling timeseries without a timed bar request. For additional property values you can set, see CQG API Reference Guide.

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

instrument = 'XYZ.XYZ';
startdate = now - 2;
enddate = now;

timeseries(c,instrument,startdate,enddate,[],x)
pause(1)

pause causes MATLAB® to wait 1 second before continuing to give time for CQG to subscribe to the instrument. MATLAB writes the variable cqgTickData to the Workspace browser.

Display cqgTickData.

cqgTickData 
cqgTickData = 
              Timestamp: {2x1 cell}
                  Price: [2x1 double]
                 Volume: [2x1 double]
              PriceType: {2x1 cell}
         CorrectionType: {2x1 cell}
    SalesConditionLabel: {2x1 cell}
     SalesConditionCode: [2x1 double]
          ContributorId: {2x1 cell}
      ContributorIdCode: [2x1 double]
            MarketState: {2x1 cell}

Display data in the Timestamp property of cqgTickData.

cqgTickData.Timestamp 
ans = 
    '4/17/2013 2:14:00 PM'
    '4/18/2013 2:14:00 PM'

Request CQG Timed Bar Data

Register an event handler to build and initialize the output data matrix cqgTimedBarData used for storing timed bar data.

aggEventNames = {'TimedBarsResolved','TimedBarsAdded', ...
    'TimedBarsUpdated','TimedBarsInserted', ...
    'TimedBarsRemoved'};

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

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

x.UpdatesEnabled = false;

Request timed bar data for instrument XYZ.XYZ for the last fraction of a day using the additional optional request property x. XYZ.XYZ is a sample instrument name. To request timed bar data for your instrument, substitute the symbol name in instrument.

instrument = 'XYZ.XYZ';
startdate = now - .1;
enddate = now;
intraday = 1;

timeseries(c,instrument,startdate,enddate,intraday,x)
pause(1)

MATLAB writes the variable cqgTimedBarData to the Workspace browser.

Display cqgTimedBarData.

cqgTimedBarData
cqgTimedBarData =
   1.0e+09 *
    0.0007   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475
    0.0007   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475
    0.0007   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475
    0.0007   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475
    0.0007   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475   -2.1475
    ...

cqgTimedBarData returns timed bar data for the specified instrument. The columns of cqgTimedBarData display data corresponding to the timestamp, open price, high price, low price, close price, mid-price, HLC3, average price, and tick volume.

Close the CQG Connection

close(c)

See Also

| | | | | | |

Related Examples

More About

External Websites