Skip to content

User Guide

Konstantin Pavlov edited this page Mar 6, 2014 · 1 revision

Configuring Maven dependencies

  1. Download ZIP archive or clone/fork the repository.
  2. Build and install project artifacts to your local maven repository: mvn clean install
  3. Add the dependency to your project
<dependency>
    <groupId>kpavlov.fixio</groupId>
    <artifactId>core</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

You'll also need a slf4j API implementation at runtime, so please add appropriate dependency, e.g.:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.5</version>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

Writing Simple FIX Client

To create a simple FIX client you need to:

  1. Implement FixApplication. You may extend FixApplicationAdapter as a quick start.

  2. Create an instance of FixClient and initialize if with FixApplication you've just created and classpath reference to FIX session settings property file.

  3. Invoke FixClient.connect(host, port) to initiate connection. Method connect(...) returns a ChannelFeature which which will be notified when a channel is closed, so you may invoke the method sync() on it if you wish to wait for connection to be closed.

FixApplication app = new FixApplicationAdapter();
client = new FixClient(app);

// set settings file location related to classpath
client.setSettingsResource("/client.properties");

// connect to specified host and port
ChannelFeature closeFeature = client.connect("localhost", 10201);

// wait until FIX Session is closed
closeFeature.sync();

// Shutdown FIX client
client.disconnect();

Working With FIX Messages

Data in a FIX protocol message is stored in text-encoded fields delimited by separator char with code 01: \u0001. Each field has form tag=value. Tags are positive integers and values are strings.

According to specification, FIX protocol message structure consists of header, body and trailer. Header, body, and trailer contains a sequences of fields. Fields may form a groups called the components or field groups. Component may contain fields and another components, depending on the semantic.

There are two API interfaces to represent FIX messages: FixMessage and FixMessageBuilder.

FixMessage represents received message, whereas FixMessageBuilder represents a message to be sent.

API partially reflects the message structure. There are methods getHeader(), getBody() and getFooter() in FixMessage and FixMessageBuilder.

It is possible to use either numeric tag numbers or tag enum when dealing with message fields. There are a number of methods add(...) and get(...) methods which accepts numeric and enum constants parameters.

Example of using FixMessageBuilder:

FixMessageBuilder userRequest = new FixMessageBuilderImpl(MessageTypes.USER_REQUEST);
userRequest.add(923, "UserRequestID"); //UserRequestID(923)
userRequest.add(FieldType.UserRequestType, 4); //UserRequestType=RequestIndividualUserStatus
userRequest.add(DataType.STRING, 553, "user"); //Username(553)

This example demonstrates different ways of setting field values:

  1. By tag number and value: userRequest.add(923, "UserRequestID");
  2. By enum value: userRequest.add(FieldType.UserRequestType, 4);
  3. By data type and tag number: userRequest.add(DataType.STRING, 553, "user");

The last way of setting field value is applicable for adding custom fields, not specified in standard fix dictionary (5.0.SP2).

To create a FIX message components, also referred as groups, use methods FixMessageBuilder.newGrop(...). These methods returns a new group you'll to add new fields to.

Example of using FixMessageBuilder with groups:

FixMessageBuilder quoteRequest = new FixMessageBuilderImpl(MessageTypes.QUOTE_REQUEST);
quoteRequest.add(FieldType.QuoteReqID, quoteRequestId);
quoteRequest.add(FieldType.ClOrdID, clientOrderId);

Group instrument1 = quoteRequest.newGroup(FieldType.NoRelatedSym, 2); // create group with 2 fields
instrument1.add(FieldType.Symbol, "EUR/USD");
instrument1.add(FieldType.SecurityType, "FOR");

Group instrument2 = quoteRequest.newGroup(FieldType.NoRelatedSym); // create group with unknown number of fields
instrument2.add(FieldType.Symbol, "EUR/CHF");
instrument2.add(FieldType.SecurityType, "FOR");

quoteRequest.add(FieldType.QuoteRequestType, 2); //QuoteRequestType=AUTOMATIC

It is preferable to specify group size to achieve optimal performance and memory usage.

FixApplication

FixApplication interface should be implemented to handle application business logic. It is a callback interface which handles FIX session events, incoming and outgoing messages.

FixApplication has the following methods:

  • to handle session events (onLogon(...) and onLogout(...)),
  • to process incoming messages (onMessage(...))
  • to pre-process outgoing message (beforeSendMessage(...)). You may add custom fields to FixMessageHeader in this method.

Start your with extending FixApplicationAdapter.

Writing Simple FIX Server

To implement FIX server one should follow these steps:

  1. Implement FixApplication. Use FixApplicationAdapter as starting point.
  2. Create FixAuthenticator which is responsible for accepting or rejecting client connections. There is a simple implementation - AcceptAllAuthenticator which accepts all client connections.
  3. Create and start FixServer.

FIX Server example:

FixApplication app = new FixApplicationAdapter();
FixServer server = new FixServer(port, new AcceptAllAuthenticator(), app);
server.start();