This example shows how to connect to CQG® and create a market order.
c = cqg;
Start the CQG session. Set up event handlers for instrument subscription, orders, and associated events.
startUp(c) streamEventNames = {'InstrumentSubscribed', ... 'InstrumentChanged','IncorrectSymbol'}; for i = 1:length(streamEventNames) registerevent(c.Handle,{streamEventNames{i}, ... @(varargin)cqgrealtimeeventhandler(varargin{:})}) end orderEventNames = {'AccountChanged','OrderChanged','AllOrdersCanceled'}; for i = 1:length(orderEventNames) registerevent(c.Handle,{orderEventNames{i}, ... @(varargin)cqgordereventhandler(varargin{:})}) end
Subscribe to a security tied to the EURIBOR.
realtime(c,'F.US.IE')
pause(2)
CQGInstrument
ObjectTo use the instrument for creating an order, import the instrument
name cqgInstrumentName
into the current MATLAB® workspace.
Then, create the CQGInstrument
object cqgInst
.
cqgInstrumentName = evalin('base','cqgInstrument'); cqgInst = c.Handle.Instruments.Item(cqgInstrumentName);
Set the CQG flags to enable account information retrieval.
c.Handle.set('AccountSubscriptionLevel','aslNone'); c.Handle.set('AccountSubscriptionLevel','aslAccountUpdatesAndOrders'); pause(2) accountHandle = c.Handle.Accounts.ItemByIndex(0);
Create a market order that buys one share of the subscribed
security cqgInst
using the account credentials accountHandle
.
orderType = 1; % Market order flag quantity = 1; % Positive quantity is Buy, negative is Sell oMarket = createOrder(c,cqgInst,orderType,accountHandle,quantity); oMarket.Place
close(c)
close
| cqg
| createOrder
| realtime
| startUp