www.gusucode.com > trading工具箱matlab源码程序 > trading/tradingdemos/CQGOrderWorkflow.m

    %% CQG Order Workflow
%Create connection and API handles
c = cqg;

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

%Set API configuration properties
c.APIConfig.TimeZoneCode = 'tzEastern';

%Start the CQG session
c.startUp;

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

% Default event handlers for order and associated events
orderEventNames = {'AccountChanged','OrderChanged','AllOrdersCanceled'};
for i = 1:length(orderEventNames)
  c.Handle.registerevent({orderEventNames{i},@(varargin)cqgordereventhandler(varargin{:})});
end

%% Orders submitted using subscribed instruments
% Subscribe to a security
c.realtime('F.US.EP')
pause(2)

% Get instrument handle, note that the Instrument name was returned by the
% realtime event handler for OnInstrumentSubscribed
% cqgInstrument is returned to the base workspace by the example event
% handler cqgrealtimeeventhandler
cqgInstrumentName = evalin('base','cqgInstrument');
cqgInst = c.Handle.Instruments.Item(cqgInstrumentName);
qtBid = cqgInst.get('Bid');
qtAsk = cqgInst.get('Ask');
qtTrade = cqgInst.get('Trade');

% Set CQG flag enabling account id retrieval and get account id handle
% AccountSubscriptionLevel must be set to aslNone and then
% aslAccountUpdatesAndOrders
c.Handle.set('AccountSubscriptionLevel','aslNone')
c.Handle.set('AccountSubscriptionLevel','aslAccountUpdatesAndOrders')
pause(2)
accountHandle = c.Handle.Accounts.ItemByIndex(0);

% Create market order
orderType = 1;  % Market order flag
quantity = 1;   % Positive quantity is Buy, negative is Sell
oMarket = c.createOrder(cqgInst,orderType,accountHandle,quantity);
oMarket.Place

% Create limit order
orderType = 2;   % Limit order flag
quantity = 1;    % Positive quantity is Buy, negative is Sell
oLimit = c.createOrder(cqgInst,orderType,accountHandle,quantity,qtBid.get('Price'));
oLimit.Place

% Create stop order
orderType = 3;   % Stop order flag
quantity = 1;  
oStop = c.createOrder(cqgInst,orderType,accountHandle,quantity,qtTrade.get('Price'));
oStop.Place

% Create stop limit order
orderType = 4;
quantity = 1;
oStopLimit = c.createOrder(cqgInst,orderType,accountHandle,quantity,qtBid.get('Price'),qtTrade.get('Price'));
oStopLimit.Place

%% Orders submitted using instrument names

% Create market order for ELX Corn
orderType = 1;  % Market order flag
quantity = 1;   % Positive quantity is Buy, negative is Sell
oMarket = c.createOrder('F.US.CLE',orderType,accountHandle,quantity);
oMarket.Place

% Close the CQG session
c.shutDown;