Main Content

Request CQG Real-Time Data

This example shows how to connect to CQG®, define event handlers, and request current 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 for the connection status.

eventNames = {'CELStarted','DataError','IsReady', ...
    'DataConnectionStatusChanged','GWConnectionStatusChanged', ...

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

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 the API configuration properties, see CQG API Reference Guide.

Establish the connection to CQG.


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

Register an event handler to track events associated with the CQG instrument subscription.

streamEventNames = {'InstrumentSubscribed','InstrumentChanged', ...

for i = 1:length(streamEventNames)
  registerevent(c.Handle,{streamEventNames{i}, ...

Request CQG Real-Time Data

With the connection established, subscribe to the instrument. The instrument name must be formatted in the CQG long symbol view. For example, to subscribe to a security tied to corn, enter the following. (F.US.EZC is a sample instrument name. To request real-time data for your instrument, substitute this sample name with the name of your instrument.)

instrument = 'F.US.EZC';

MATLAB® writes the structure variable cqgDataEZC to the Workspace browser.

Display cqgDataEZC.

ans = 
              Price: {15x1 cell}
             Volume: {15x1 cell}
    ServerTimestamp: {15x1 cell}
          Timestamp: {15x1 cell}
               Type: {15x1 cell}
               Name: {15x1 cell}
            IsValid: {15x1 cell}
         Instrument: {15x1 cell}
          HasVolume: {15x1 cell}

cqgDataEZC returns the current quotes for the security.

Display data in the Price property of cqgDataEZC.

ans = 
    [   660.5000]
    [   660.5000]

Close the CQG Connection


See Also

| | | | | | |

Related Examples

More About

External Websites