← All articles

Implementing FIX Protocol

The second post in our FIX series moves from how the protocol works to code. We list FIX libraries for different languages, then set up QuickFIX/J in Java: the Maven dependency, the configuration file, logging on and off, and sending and receiving an order.

A NewOrderSingle FIX message — tag=value fields 35=D, 55=AAPL, 54=1, 38=100 — sent from a Java/QuickFIX server, transmitted, and parsed by the counterparty trading system.
A NewOrderSingle message: the same tag=value fields, unchanged from send to parse.

Introduction

In our previous blog, we looked at what FIX protocol is, who uses it, it’s architecture and how it works. We also looked at the FIX message structure, it’s advantages and it’s challenges as well. Now, in this blog, let’s look at how to implement FIX.

FIX Libraries

Below are some of the FIX libraries for different languages.

Language Library Comments
Java/Scala QuickFix/J A Java implementation of the FIX protocol
C++ QuickFIX A C++ library for implementing FIX protocol
Python QuickFIX (Python) Python bindings for the QuickFIX FIX engine
C# (.NET) QuickFIX/n A .NET implementation of the QuickFIX FIX engine
JavaScript (Node.js) fixparser A Node.js library for working with FIX protocol
Go quickfixgo A lightweight Go library for FIX protocol
Perl Protocol::FIX A Perl module for working with FIX protocol
Haskell fixhs A Haskell library for working with FIX protocol

FIX Implementation in Java

Maven Dependency

First, we need to add the below maven dependency to our Java project.

XML
<dependency>
    <groupId>org.quickfixj</groupId>
    <artifactId>quickfixj-core</artifactId>
    <version>2.0.1</version>
</dependency>

Configuration File

Below is an example of a quickfix configuration file (quickfix.cfg)

Config
[DEFAULT]
# Connection settings
StartTime=00:00:00
EndTime=23:59:59
SocketConnectHost=localhost/Client or Vendor IP
SocketConnectPort=5000
ReconnectInterval=60
HeartBtInt=30
[SESSION]
# FIX version and sender/target comp IDs
BeginString=FIX.4.4
SenderCompID=YOUR_SENDER_ID
TargetCompID=TARGET_ID

Starting the session

Java
import quickfix.*;
import quickfix.fix44.NewOrderSingle;
import quickfix.field.Symbol;
public class SimpleFixReceiver implements quickfix.Application {
    public static void main(String[] args) throws Exception {
        // Set up the session settings (use your own config)
        SessionSettings settings = new SessionSettings("quickfix.cfg");
        // Create the application instance
        SimpleFixReceiver app = new SimpleFixReceiver();
        // Set up the FIX initiator (client)
        Initiator initiator = new SocketInitiator(app, new MessageStoreFactory(settings), new MessageFactory(), settings);
        // Start the FIX session
        initiator.start();
    }
}

Login/Logout

Java
@Override
public void onLogon(SessionID sessionID) {
    System.out.println("Logon to FIX session: " + sessionID);
}
@Override
public void onLogout(SessionID sessionID) {
    System.out.println("Logout from FIX session: " + sessionID);
}

Sending the Order

Java
public void sendOrder(SessionID sessionID) {
    // The sessionID is the unique identifier for this communication channel
    // It is a combination of SenderCompID and TargetCompID along with other details
    try {
        // Create a NewOrderSingle message
        NewOrderSingle order = new NewOrderSingle(
            new ClOrdID("12345"), // ClOrdID (Client Order ID)
            new Side(Side.BUY), // Side (Buy)
            new Price(100.25), // Price
            new OrderQty(100), // Order quantity
            new Symbol("AAPL") // Symbol
        );
        // Send the order to the target session using the sessionID
        Session.sendToTarget(order, sessionID);
        // Session.sendToTarget(order, sessionID) method in QuickFIX/J is used to send a
        // FIX message to the target system (or counterparty) that corresponds to the specified sessionID
        System.out.println("Sent NewOrderSingle: " + order);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Receiving the Order

Java
@Override
public void onMessage(NewOrderSingle message, SessionID sessionID) {
    // Parse the symbol (tag 55) from NewOrderSingle message
    try {
        String symbol = message.get(new Symbol()).getValue();
        System.out.println("Received FIX message with Symbol: " + symbol);
    } catch (FieldNotFound e) {
        e.printStackTrace();
    }
}

Conclusion

In this blog, we looked at different FIX library implementations in different languages and an example of how to send and receive a basic FIX message in Java.