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 handlercqgconnectioneventhandler
to 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
cqgconnectioneventhandler
is 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.APIConfig
is 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
pause
causes MATLAB®等待2秒时间在继续之前给for CQG to subscribe to the instrument.
Create the CQG instrument object.
To use the instrument increateOrder
, import the name of the instrumentcqgInstrumentName
into the current MATLAB workspace. Then, create theCQGInstrument
objectcqgInst
.
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 theCQGAccount
object intoaccountHandle
to use your account information increateOrder
. For details about creating aCQGAccount
object, 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 securitycqgInst
using the account credentialsaccountHandle
.
quantity = 1; oMarket = createOrder(c,cqgInst,1,accountHandle,quantity); oMarket.Place
ans = OrderChanged
TheCQGOrder
objectoMarket
contains 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
TheCQGOrder
objectoMarket
contains 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 objectqtBid
from the previously definedCQGInstrument
objectcqgInst
. For details about theCQGInstrument
object, seeCQG API Reference Guide.
qtBid = cqgInst.get('Bid');
Create a limit order that buys one share of the previously subscribed securitycqgInst
using the previously defined account credentialsaccountHandle
andqtBid
for the limit price.
quantity = 1; limitprice = qtBid.get('Price'); oLimit = createOrder(c,cqgInst,2,accountHandle,quantity,limitprice); oLimit.Place
ans = OrderChanged
TheCQGOrder
objectoLimit
contains 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 objectqtTrade
from the previously definedCQGInstrument
objectcqgInst
.
qtTrade = cqgInst.get('Trade');
Create a stop order that buys one share of the previously subscribed securitycqgInst
using the previously defined account credentialsaccountHandle
andqtTrade
for the stop price.
quantity = 1; stopprice = qtTrade.get('Price'); oStop = createOrder(c,cqgInst,3,accountHandle,quantity,stopprice); oStop.Place
ans = OrderChanged
TheCQGOrder
objectoStop
contains 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 securitycqgInst
using the defined account credentialsaccountHandle
.
quantity = 1; oStopLimit = createOrder(c,cqgInst,4,accountHandle,quantity,...limitprice,stopprice); oStopLimit.Place
ans = OrderChanged
TheCQGOrder
objectoStopLimit
contains 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
cqg
|close
|createOrder
|history
|timeseries
|startUp
|shutDown
|realtime
Related Examples
- 创建啊rder Using CQG
- Request CQG Historical Data
- Request CQG Real-Time Data
- Request CQG Intraday Tick Data