Create Order Using CQG
This example shows how to connect to CQG® and create a market order.
Connect to CQG
c = cqg;
Establish Event Handlers
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{:})})
endSubscribe to Instrument
Subscribe to a security tied to the EURIBOR.
realtime(c,'F.US.IE')
pause(2)Create CQGInstrument Object
To 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 Up Account Credentials
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 Market Order
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 CQG Connection
close(c)
See Also
cqg | close | createOrder | realtime | startUp
Topics
- Create CQG Orders
- Request CQG Historical Data
- Request CQG Intraday Tick Data
- Request CQG Real-Time Data
- Workflow for CQG