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', ...
    'GWEnvironmentChanged'};

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

Establish the connection to CQG.

startUp(c)
CELStarted
DataConnectionStatusChanged
GWConnectionStatusChanged

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', ...
    'IncorrectSymbol'};

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

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';
realtime(c,instrument)

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

Display cqgDataEZC.

cqgDataEZC(1,1)
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.

cqgDataEZC(1,1).Price
ans = 
    [-2.1475e+09]
    [-2.1475e+09]
    [-2.1475e+09]
    [   660.5000]
    []
    []
    [-2.1475e+09]
    [-2.1475e+09]
    [-2.1475e+09]
    [-2.1475e+09]
    [-2.1475e+09]
    [-2.1475e+09]
    [-2.1475e+09]
    [   660.5000]
    [-2.1475e+09]

Close the CQG Connection

close(c)

See Also

| | | | | | |

Related Examples

More About

External Websites