// Prerequisites: Variable "session" of type FIXSession.
// All sessions can be enumerated from FIXEngine.Sessions
// First of all, to be safe, acquire the global lock (See FIXOrderManager.SyncRoot).
synchronized (FIXOrderManager.syncRoot) {
FIXSessionOrderManager sessionOM = FIXOrderManager.getSessionOrderManager(session);
FIXMessage newOrder = session.createMessage(MsgTypes.NewOrderSingle); // Or MsgTypes.NewOrderMultileg
// Set symbol, e.g. "IBM"
newOrder.setValue(Tags.Symbol, "IBM");
// Add fields such as "ClOrdID", "TimeInForce", "Price", "OrderQty", "Side", "TransactTime", etc ...
// This example works with FIX versions from 4.1 to 5.0. It is a perfect example of version
// independent order management.
// Sometimes we do need to deal with some special tags. For example: "TransactTime" is usually a
// required field since FIX version 4.2+.
// We directly get long binary of current UTC time to avoid unnecessary object creation.
if (newOrder.getMessageSchema().getFieldByTag(Tags.TransactTime) != null) // Tag defined in message
newOrder.setValue(Tags.TransactTime, DateTime.getUtcNowAsBinary());
try {
// Note: An exception may be thrown out.
// The exception may be caused by application program error (e.g. duplicated ClOrdID);
// or system error (e.g. underlying FIX session connection is down; depends on
// FIXSessionOrderManagerSettings.alwaysSendNewOrder).
// See FIXSessionOrderManager.addNewRegularOrder for more details.
FIXRegularOrder order = sessionOM.addNewRegularOrder(newOrder);
// Event FIXSessionOrderManagerCommonListener.orderAdded will be triggered immediately after the
// method returns, within the same thread context.
} catch (Exception ex) {
// TODO: Your own error handling logic ...
}
}
|