This example shows how to connect to X_TRADER® and submit orders.
X = xtrdr;
createInstrument(X,'Exchange','CME','Product','2F',... 'ProdType','Future','Contract','Aug13',... 'Alias','SubmitOrderInstrument1')
Register event handlers for the order server. The callback ttorderserverstatus
is
assigned to the event OnExchangeStateUpdate
to
verify that the requested instrument’s exchange order server
is running. Otherwise, no orders can be submitted.
sExchange = X.Instrument.Exchange; registerevent(X.Gate,{'OnExchangeStateUpdate',... @(varargin)ttorderserverstatus(varargin{:},sExchange)})
The OrderSet
object sends orders to X_TRADER.
Set properties of the OrderSet
object and
detail the level of the order status events. Enable order update and
reject (failure) events so you can assign callbacks to handle these
conditions.
createOrderSet(X)
X.OrderSet(1).EnableOrderRejectData = 1;
X.OrderSet(1).EnableOrderUpdateData = 1;
X.OrderSet(1).OrderStatusNotifyMode = 'ORD_NOTIFY_NORMAL';
Set whether the order set checks self-imposed position limits when submitting an order.
X.OrderSet(1).Set('NetLimits',false)
Set a callback to handle the OnOrderFilled
events.
Each time an order is filled (or partially filled), this callback
is invoked.
registerevent(X.OrderSet(1),{'OnOrderFilled',... @(varargin)ttorderevent(varargin{:},X)})
You must first enable order submission before you can submit orders to X_TRADER.
X.OrderSet(1).Open(1)
Build an order profile using an existing instrument. The order
profile contains the settings that define a submitted order. The valid Set
parameters
are shown:
orderProfile = createOrderProfile(X);
orderProfile.Instrument = X.Instrument(1);
orderProfile.Customer = '<Default>';
Create a market order to buy 100 shares.
orderProfile.Set('BuySell','Buy') orderProfile.Set('Qty',100) orderProfile.Set('OrderType','M')
Create a limit order by setting the OrderType
and
limit order price.
orderProfile.Set('OrderType','L') orderProfile.Set('Limit$','127000')
Create a stop market order and set the order restriction to a stop order and a stop price.
orderProfile.Set('OrderType','M') orderProfile.Set('OrderRestr','S') orderProfile.Set('Stop$','129800')
Create a stop limit order and set the order restriction, type, limit price, and stop price.
orderProfile.Set('OrderType','L') orderProfile.Set('OrderRestr','S') orderProfile.Set('Limit$','128000') orderProfile.Set('Stop$','127500')
Check the order server status before submitting the order and add a counter so the example doesn’t delay.
nCounter = 1; while ~exist('bServerUp','var') && nCounter < 20 pause(1) nCounter = nCounter + 1; end
Verify that the exchange’s order server in question is available before submitting the order.
if exist('bServerUp','var') && bServerUp submittedQuantity = X.OrderSet(1).SendOrder(orderProfile); disp(['Quantity Sent: ' num2str(submittedQuantity)]) else disp('Order Server is down. Unable to submit order') end
close(X)
close
| createInstrument
| createOrderProfile
| createOrderSet
| xtrdr