Implementing-Fix-Protocol

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.

LanguageLibraryComments
Java/ScalaQuickFix/JA Java implementation of the FIX protocol
C++QuickFIXA C++ library for implementing FIX protocol
PythonQuickFIX (Python)Python bindings for the QuickFIX FIX engine
C# (.NET)QuickFIX/nA .NET implementation of the QuickFIX FIX engine
JavaScript (Node.js)fixparserA Node.js library for working with FIX protocol
GoquickfixgoA lightweight Go library for FIX protocol
PerlProtocol::FIXA Perl module for working with FIX protocol
HaskellfixhsA 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.

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

[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
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
@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
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
@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.