Main Content

CreateCQGOrders

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 theCQGConnection

Create the CQG connection object usingcqg.

c = cqg;

Define Event Handlers

Register the sample event handlercqgconnectioneventhandlerto track events associated with the connection status.

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

cqgconnectioneventhandleris assigned to the events ineventNames.

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

c.APIConfig.TimeZoneCode ='tzEastern';

c.APIConfigis a CQG configuration object. For details about setting API configuration properties, seeCQG 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'};fori = 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'};fori = 1:length(orderEventNames) registerevent(c.Handle,{orderEventNames{i},...@(varargin)cqgordereventhandler(varargin{:})})end

Subscribe to theCQGInstrument

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, enter the following.

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

pausecauses MATLAB®等待2秒时间在继续之前给for CQG to subscribe to the instrument.

Create the CQG instrument object.

To use the instrument increateOrder, import the name of the instrumentcqgInstrumentNameinto the current MATLAB workspace. Then, create theCQGInstrumentobjectcqgInst.

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 theCQGAccountobject intoaccountHandleto use your account information increateOrder. For details about creating aCQGAccountobject, seeCQG API Reference Guide.

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

CreateCQGMarket, Limit, Stop, and Stop Limit Orders

Create a market order that buys one share of the subscribed securitycqgInstusing the account credentialsaccountHandle.

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

TheCQGOrderobjectoMarketcontains the order. The CQG API executes the market order using the CQG API functionPlace. After execution, the order status changes.

To use a character vector 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 credentialsaccountHandle.

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

TheCQGOrderobjectoMarketcontains the order. The CQG API executes the market order using the CQG API functionPlace. After execution, the order status changes.

To create a limit order, you can use the bid price. Extract the CQG bid objectqtBidfrom the previously definedCQGInstrumentobjectcqgInst. For details about theCQGInstrumentobject, seeCQG API Reference Guide.

qtBid = cqgInst.get('Bid');

Create a limit order that buys one share of the previously subscribed securitycqgInstusing the previously defined account credentialsaccountHandleandqtBidfor the limit price.

quantity = 1; limitprice = qtBid.get('Price'); oLimit = createOrder(c,cqgInst,2,accountHandle,quantity,limitprice); oLimit.Place
ans = OrderChanged

TheCQGOrderobjectoLimitcontains the order. The CQG API executes the limit order using the CQG API functionPlace. After execution, the order status changes.

To create a stop order, you can use the trade price. Extract the CQG trade objectqtTradefrom the previously definedCQGInstrumentobjectcqgInst.

qtTrade = cqgInst.get('Trade');

Create a stop order that buys one share of the previously subscribed securitycqgInstusing the previously defined account credentialsaccountHandleandqtTradefor the stop price.

quantity = 1; stopprice = qtTrade.get('Price'); oStop = createOrder(c,cqgInst,3,accountHandle,quantity,stopprice); oStop.Place
ans = OrderChanged

TheCQGOrderobjectoStopcontains the order. The CQG API executes the stop order using the CQG API functionPlace. 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 securitycqgInstusing the defined account credentialsaccountHandle.

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

TheCQGOrderobjectoStopLimitcontains the order. The CQG API executes the stop limit order using the CQG API functionPlace. After execution, the order status changes.

Close theCQGConnection

shutDown(c)

See Also

|||||||

Related Examples

More About

External Websites