Main Content

Submit X_TRADER Orders

This example shows how to connect to X_TRADER® and submit orders.

Connect to X_TRADER

X = xtrdr;

Create an Instrument

createInstrument(X,'Exchange','CME','Product','2F',...
                'ProdType','Future','Contract','Aug13',...
                'Alias','SubmitOrderInstrument1')

Register Event Handlers

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)})

Create an Order Set

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 Position Limit Checks

Set whether the order set checks self-imposed position limits when submitting an order.

X.OrderSet(1).Set('NetLimits',false)

Set a Callback Function

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)})

Enable Order Submission

You must first enable order submission before you can submit orders to X_TRADER.

X.OrderSet(1).Open(1)

Build an Order Profile

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>';

Sample: Create a Market Order

Create a market order to buy 100 shares.

orderProfile.Set('BuySell','Buy')
orderProfile.Set('Qty',100)
orderProfile.Set('OrderType','M')

Sample: Create a Limit Order

Create a limit order by setting the OrderType and limit order price.

orderProfile.Set('OrderType','L')
orderProfile.Set('Limit$','127000')

Sample: Create a Stop Market Order

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')

Sample: Create a Stop Limit Order

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

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 the Order Server Availability

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 the Connection

close(X)

See Also

| | | |

Related Examples

More About