Open Orders
Contents
Initialize session with Trader Workstation
% get TWS session instance session = TWS.Session.getInstance(); % create local buffer for open order and order status events [buf,lh] = TWS.initBufferForEvent( ... { ... TWS.Events.OPENORDER ,... TWS.Events.ORDERSTATUS ... } ... ); % connect to TWS session.eClientSocket.eConnect('127.0.0.1',7496,0);
added interface method: TWSNotification notification listener has been added Server Version:71 TWS Time at connection:20150102 09:59:41 EST
Requesting Open Orders
% request all open orders and order status
session.eClientSocket.reqAllOpenOrders();
Process OpenOrder and OrderStatus Events
After requesting all open orders there are two callbacks which can be triggered
The EWrapper::openOrder callback is fired once for each open order. That is, there is one OpenOrderEvent delivered for each open order.
The EWrapper::orderStatus callback is invoked whenever the status of an order changes and is also fired after reconnecting to TWS if the client has any open orders.
If not feeling particularly adventurous, simply print out the events to the screen
cellfun(@(e)disp(e.data),collection2cell(buf))
open order: orderId=0 action=BUY quantity=100 conid=756733 symbol=SPY secType=STK expiry=null strike=0.0 right=? multiplier=null exchange=SMART primaryExch=null currency=USD localSymbol=SPY tradingClass=SPY type=LMT lmtPrice=206.26 auxPrice=0.0 TIF=DAY localSymbol=SPY client Id=0 parent Id=0 permId=1831429808 outsideRth=false hidden=false discretionaryAmt=0.0 displaySize=0 triggerMethod=0 goodAfterTime=null goodTillDate=null faGroup=null faMethod=null faPercentage=null faProfile=null shortSaleSlot=0 designatedLocation=null exemptCode=-1 ocaGroup=null ocaType=3 rule80A=null allOrNone=false minQty= percentOffset= eTradeOnly=false firmQuoteOnly=false nbboPriceCap= optOutSmartRouting=false auctionStrategy=0 startingPrice= stockRefPrice= delta= stockRangeLower= stockRangeUpper= volatility= volatilityType=0 deltaNeutralOrderType=None deltaNeutralAuxPrice= deltaNeutralConId=0 deltaNeutralSettlingFirm=null deltaNeutralClearingAccount=null deltaNeutralClearingIntent=null deltaNeutralOpenClose=? deltaNeutralShortSale=false deltaNeutralShortSaleSlot=0 deltaNeutralDesignatedLocation=null continuousUpdate=0 referencePriceType=0 trailStopPrice= trailingPercent= scaleInitLevelSize= scaleSubsLevelSize= scalePriceIncrement= scalePriceAdjustValue= scalePriceAdjustInterval= scaleProfitOffset= scaleAutoReset=false scaleInitPosition= scaleInitFillQty= scaleRandomPercent=false hedgeType=null hedgeParam=null account=DU207406 settlingFirm=null clearingAccount=null clearingIntent=IB notHeld=false whatIf=false status=Submitted initMargin=1.7976931348623157E308 maintMargin=1.7976931348623157E308 equityWithLoan=1.7976931348623157E308 commission= minCommission= maxCommission= commissionCurrency=null warningText=null order status: orderId=0 clientId=0 permId=1831429808 status=Submitted filled=0 remaining=100 avgFillPrice=0.0 lastFillPrice=0.0 parent Id=0 whyHeld=null
On the other hand, if feeling zesty, probe each event for class type and data type
% get the events from the buffer and convert to cell array events = collection2cell(buf); % do some important analysis for i = 1:numel(events) % get the i'th event e = events{i}; % figure out the class type of the event fprintf('Event class type is: %s \n',class(e)); % figure out the class type of the data enclosed fprintf('Event data type is: %s with fields: \n',class(e.data)); % display fields associated with the event.data type disp(fields(e.data)); end
Event class type is: com.tws.Handler$OpenOrderEvent Event data type is: com.tws.OpenOrder with fields: 'orderId' 'contract' 'order' 'orderState' Event class type is: com.tws.Handler$OrderStatusEvent Event data type is: com.tws.OrderStatus with fields: 'orderId' 'status' 'filled' 'remaining' 'avgFillPrice' 'permId' 'parentId' 'lastFillPrice' 'clientId' 'whyHeld'
Note that the com.tws.OpenOrder fields contract, order, and orderState correspond to API objects:
Working with heterogeneous event queue is no problem. Use switch to disposition different events based on class type
for i = 1:numel(events) % get the i'th event e = events{i}; switch class(e) % process OpenOrder events case 'com.tws.Handler$OpenOrderEvent' fprintf('OpenOrder: action=%s\n',char(e.data.order.m_action)); % process OrderStatus events case 'com.tws.Handler$OrderStatusEvent' fprintf('OrderStatus: %s\n',char(e.data.status)); % default case otherwise fprintf( ... [ ... 'The major difference between a thing that might go wrong ' ... 'and a thing that cannot possibly go wrong is ' ... 'that when a thing that cannot possibly go wrong goes wrong '... 'it usually turns out to be impossible to get at or repair\n'... ] ... ); end end
OpenOrder: action=BUY OrderStatus: Submitted
See Also
TWSOrderPlacementExample | TWSExecutionDetailsExample
References
Interactive Brokers API:
TWS@Github:
Apache Commons:
Wikipedia: