Documentation Center

  • Trial Software
  • Product Updates

X_TRADER Order Submission

This example shows how to connect to X_TRADER® and submit an order.

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;
X.Gate.registerevent({'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.

X.OrderSet(1).registerevent({'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

Was this topic helpful?