www.gusucode.com > trading工具箱matlab源码程序 > trading/trading/@ibtws/createOrder.m
function d = createOrder(c,s,order,orderid,callback) %CREATEORDER Create Interactive Brokers Trader Workstation order. % CREATEORDER(C,S,ORDER,ORDERID) creates Interactive Brokers Trader % Workstation orders given the connection handle, C, contract, S, % order information, ORDER, and unique order identifier, ORDERID. % The user can specify an event handler, CALLBACK. If no event handler is % specified, the data will be returned as the first output argument % For example, % % ibContract = ib.Handle.createContract; % ibContract.symbol = 'XYZ'; % ibContract.secType = 'STK'; % ibContract.exchange = 'SMART'; % ibContract.currency = 'USD'; % % ibOrder = ib.Handle.createOrder; % ibOrder.action = 'BUY'; % ibOrder.totalQuantity = 2; % ibOrder.orderType = 'MKT'; % d = ib.createOrder(ibContract,ibOrder,1) % % To see all of the fields associated with the Interactive Brokers % contract object, type % % fieldnames(ibContract) % % To see all of the fields associated with the Interactive Brokers % order object, type % % fieldnames(ibOrder) % % See also IBTWS, CLOSE, GETDATA, HISTORY, TIMESERIES. % Copyright 2013-2014 The MathWorks, Inc. % Define event handlers for error and order status events eventNames = {'orderStatus'}; if nargin < 5 for i = 1:length(eventNames) c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInOrderEventHandler(varargin{:},c)}) end elseif ~isempty(callback) for i = 1:length(eventNames) c.Handle.registerevent({eventNames{i},callback}) end end % Submit the order for execution % IB market data request API call c.DataRequest = true; c.Handle.placeOrderEx(orderid,s,order) % If user defined event handler, do not loop on DataRequest property if nargin > 4 d = orderid; return end while c.DataRequest drawnow end % Return data to method workspace and convert dates to date numbers try d = evalin('base','ibBuiltInOrderData'); catch d = evalin('base','ibBuiltInErrMsg'); end % Clear temporary variables evalin('base','clear ibBuiltInOrderData'); function ibBuiltInOrderEventHandler(varargin) %IBBUILTINORDEREVENTHANDLER Interactive Brokers' Trader Workstation order example event handler. % Copyright 2012-2015 The MathWorks, Inc. persistent ibBuiltInOrderData % Process event based on identifier switch varargin{end-1} case 'orderStatus' % Order status % Get order data ibBuiltInOrderData.STATUS = varargin{13}.status; ibBuiltInOrderData.FILLED = varargin{13}.filled; ibBuiltInOrderData.REMAINING = varargin{13}.remaining; ibBuiltInOrderData.AVG_FILL_PRICE = varargin{13}.avgFillPrice; ibBuiltInOrderData.PERM_ID = num2str(varargin{13}.permId); ibBuiltInOrderData.PARENT_ID = varargin{13}.parentId; ibBuiltInOrderData.LAST_FILL_PRICE = varargin{13}.lastFillPrice; ibBuiltInOrderData.CLIENT_ID = varargin{13}.clientId; ibBuiltInOrderData.WHY_HELD = varargin{13}.whyHeld; % Assign error to base workspace assignin('base','ibBuiltInOrderData',ibBuiltInOrderData); % Clear persistent variables and event listeners clear ibBuiltInOrderData evtListeners = varargin{1}.eventlisteners; i = strcmp(evtListeners(:,1),'orderStatus'); varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}}); varargin{end}.DataRequest = false; %#ok return end