Documentation Center

  • Trial Software
  • Product Updates

Create CQG Order

This example shows how to connect to CQG®, define the event handlers, subscribe to the security, define the account handle, and submit orders for execution.

Create the CQG connection.

Create the CQG connection object using cqg.

c = cqg;

Define event handlers.

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

eventNames = {'CELStarted','DataError','IsReady',...
					 'DataConnectionStatusChanged',...
					 'GWConnectionStatusChanged',...
					 'GWEnvironmentChanged'};
for i = 1:length(eventNames)
  registerevent(c.Handle,{eventNames{i},...
						@(varargin)cqgconnectioneventhandler(varargin{:})});
end

The example event handler cqgconnectioneventhandler is assigned to the events in eventNames.

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

c.APIConfig.TimeZoneCode = 'tzEastern';

c.APIConfig is a CQG configuration object. For more information about setting 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 a CQG instrument subscription.

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

Register an event handler to track events associated with a CQG order and account.

orderEventNames = {'AccountChanged','OrderChanged','AllOrdersCanceled'};
for i = 1:length(orderEventNames)
  registerevent(c.Handle,{orderEventNames{i},...
						@(varargin)cqgordereventhandler(varargin{:})});
end

Subscribe to the CQG instrument.

With the connection established, subscribe to the CQG instrument. The instrument must be successfully subscribed first before it is available for transactions. You must format the instrument name in the CQG long symbol view. For example, to subscribe to a security tied to the EURIBOR, type the following.

realtime(c,'F.US.IE')
pause(2)
F.US.IEK13 subscribed

pause causes MATLAB® to wait 2 seconds before continuing to give time for CQG to subscribe to the instrument.

Create the CQG instrument object.

To use the instrument in createOrder, import the name of the instrument cqgInstrumentName into the current MATLAB workspace. Then, create the CQGInstrument object cqgInst.

cqgInstrumentName = evalin('base','cqgInstrument');
cqgInst = c.Handle.Instruments.Item(cqgInstrumentName);

Set up account credentials.

Set the CQG flags to enable account information retrieval.

set(c.Handle,'AccountSubscriptionLevel','aslNone')
set(c.Handle,'AccountSubscriptionLevel','aslAccountUpdatesAndOrders')
pause(2)
ans =
    AccountChanged

The CQG API shows that account information changed.

Set up the CQG account credentials.

Retrieve the CQGAccount object into accountHandle to use your account information in createOrder. For more information about creating a CQGAccount object, see CQG API Reference Guide.

accountHandle = c.Handle.Accounts.ItemByIndex(0);

Create CQG market, limit, stop, and stop limit orders.

Create a market order that buys one share of the subscribed security cqgInst using the account credentials accountHandle.

quantity = 1;

oMarket = createOrder(c,cqgInst,1,accountHandle,quantity);
oMarket.Place
ans =
    OrderChanged

The CQGOrder object oMarket contains the order. The CQG API executes the market order using the CQG API function Place. After execution, the order status changes.

To use a string for the security, subscribe to the security 'EZC' as shown above. Then, create a market order that buys one share of the security 'EZC' using the defined account credentials accountHandle.

cqgInstrumentName = 'EZC';
quantity = 1;

oMarket = createOrder(c,cqgInstrumentName,1,accountHandle,quantity);
oMarket.Place
ans =
    OrderChanged

The CQGOrder object oMarket contains the order. The CQG API executes the market order using the CQG API function Place. After execution, the order status changes.

To create a limit order, you can use the bid price. Extract the CQG bid object qtBid from the previously defined CQGInstrument object cqgInst. For more information about the CQGInstrument object, see CQG API Reference Guide.

qtBid = cqgInst.get('Bid');

Create a limit order that buys one share of the previously subscribed security cqgInst using the previously defined account credentials accountHandle and qtBid for the limit price.

quantity = 1;
limitprice = qtBid.get('Price');

oLimit = createOrder(c,cqgInst,2,accountHandle,quantity,limitprice);
oLimit.Place
ans =
    OrderChanged

The CQGOrder object oLimit contains the order. The CQG API executes the limit order using the CQG API function Place. After execution, the order status changes.

To create a stop order, you can use the trade price. Extract the CQG trade object qtTrade from the previously defined CQGInstrument object cqgInst.

qtTrade = cqgInst.get('Trade');

Create a stop order that buys one share of the previously subscribed security cqgInst using the previously defined account credentials accountHandle and qtTrade for the stop price.

quantity = 1;
stopprice = qtTrade.get('Price');

oStop = createOrder(c,cqgInst,3,accountHandle,quantity,stopprice);
oStop.Place
ans =
    OrderChanged

The CQGOrder object oStop contains the order. The CQG API executes the stop order using the CQG API function Place. After execution, the order status changes.

To create a stop limit order, use both the bid and trade prices defined above. Create a stop limit order that buys one share of the subscribed security cqgInst using the defined account credentials accountHandle.

quantity = 1;

oStopLimit = createOrder(c,cqgInst,4,accountHandle,quantity,...
									limitprice,stopprice);
oStopLimit.Place
ans =
    OrderChanged

The CQGOrder object oStopLimit contains the order. The CQG API executes the stop limit order using the CQG API function Place. After execution, the order status changes.

Close the CQG connection.

shutDown(c);

See Also

| | | | | | |

Related Examples

More About

External Web Sites

Was this topic helpful?