FIX Client Server Complete Application QuickFix
Last tutorial you saw how to create simple FIX client server application using QuickFix. This is advanced version of FIX client server which you can directly implement in your project.
Once gain we will be using QuickFix API which 100% java based free and very easy to use. This API provides everything you need to deal with FIX protocol.
Recap fromm previous tutorial:
- FIX Acceptor (Called as FIX Server) – Server process the FIX message its nothing but exchange you sent your order for processing
- FIX Initiator (Called as FIX Client) – Client are broker who send order to the exachange in FIX format. In this advance tutorial I have used Mediator and adapter desing pattern you shown in below details class diagram:
Main classes shown on above diagram:
- We have main model class name: LifeCycleManagerModel.java which is POJO class to store object data which will be pass to the server for new order.
- PhysicalSettlementAdapter.java interface and it’s implementation class is adapter class which will take LifeCycleManagerModel class as parameter and will convert to NewOrderSingle that will be passed to the exchange for new order
- PhysicalSettlementMediator.java mediator and its implementaion where one method has been exposed and this will work as mediator between client and FIX initiator.
- PhysicalSettlementCollegue.java abstract class and its implementaion is nothing but colloeague of mediator will pass client request to mediator friend to more new order to exchange.
Now let’s jump on actual implementation
- First create maven project name: FIXAcceptorServer which is our dummy exchange server where we will submit our order. Below if final project structure:
- pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wfs.otc.FIXAcceptorServer</groupId> <version>0.0.1</version> <artifactId>FIXAcceptorServer</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>backport-util-concurrent-2.1</groupId> <artifactId>backport-util-concurrent-2.1</artifactId> <version>2.1</version> <scope>system</scope> <systemPath>${basedir}/lib/backport-util-concurrent-2.1.jar</systemPath> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>mina-core-1.1.0-sources</groupId> <artifactId>mina-core-1.1.0-sources</artifactId> <version>1.1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/mina-core-1.1.0-sources.jar</systemPath> </dependency> <dependency> <groupId>mina-core-2.0.9</groupId> <artifactId>mina-core-2.0.9</artifactId> <version>2.0.9</version> <scope>system</scope> <systemPath>${basedir}/lib/mina-core-2.0.9.jar</systemPath> </dependency> <dependency> <groupId>quickfixj-all-1.6.0</groupId> <artifactId>quickfixj-all-1.6.0</artifactId> <version>1.6.0</version> <scope>system</scope> <systemPath>${basedir}/lib/quickfixj-all-1.6.0.jar</systemPath> </dependency> <dependency> <groupId>slf4j-api-1.7.12</groupId> <artifactId>slf4j-api-1.7.12</artifactId> <version>1.7.12</version> <scope>system</scope> <systemPath>${basedir}/lib/slf4j-api-1.7.12.jar</systemPath> </dependency> <dependency> <groupId>slf4j-jdk14-1.7.12</groupId> <artifactId>slf4j-jdk14-1.7.12</artifactId> <version>1.7.12</version> <scope>system</scope> <systemPath>${basedir}/lib/slf4j-jdk14-1.7.12.jar</systemPath> </dependency> </dependencies> </project>
- acceptor.cfg:
[DEFAULT] ConnectionType=acceptor SocketAcceptPort=5001 SocketConnectHost=localhost SocketReuseAddress=Y StartTime=00:00:00 EndTime=00:00:00 FileLogPath=.\log FileStorePath=.\fixfiles\acceptor SocketKeepAlive=Y SocketTcpNoDelay=Y [SESSION] BeginString=FIX.4.2 SenderCompID=FixAcceptor TargetCompID=FixClient8019 DataDictionary=.\config\acceptor\FIX42.xml
- FixAcceptor.java:
package com.javahonk.otc; import java.io.File; import quickfix.Application; import quickfix.DefaultMessageFactory; import quickfix.DoNotSend; import quickfix.FieldNotFound; import quickfix.FileStoreFactory; import quickfix.IncorrectDataFormat; import quickfix.IncorrectTagValue; import quickfix.Message; import quickfix.MessageCracker; import quickfix.RejectLogon; import quickfix.ScreenLogFactory; import quickfix.Session; import quickfix.SessionID; import quickfix.SessionNotFound; import quickfix.SessionSettings; import quickfix.SocketAcceptor; import quickfix.UnsupportedMessageType; import quickfix.field.AvgPx; import quickfix.field.ClOrdID; import quickfix.field.CumQty; import quickfix.field.ExecID; import quickfix.field.ExecTransType; import quickfix.field.ExecType; import quickfix.field.Headline; import quickfix.field.LeavesQty; import quickfix.field.OrdStatus; import quickfix.field.OrdType; import quickfix.field.OrderID; import quickfix.field.OrderQty; import quickfix.field.Price; import quickfix.field.Side; import quickfix.field.Symbol; import quickfix.fix42.ExecutionReport; import quickfix.fix42.NewOrderSingle; import quickfix.fix42.News; public class FixAcceptor extends MessageCracker implements Application { public static void main(String[] args) { try { File file = new File("./config/acceptor/acceptor.cfg"); System.out.println(file.getAbsolutePath()); SessionSettings settings = new SessionSettings("./config/acceptor/acceptor.cfg"); FixAcceptor acceptor = new FixAcceptor(); ScreenLogFactory screenLogFactory = new ScreenLogFactory(settings); DefaultMessageFactory messageFactory = new DefaultMessageFactory(); FileStoreFactory fileStoreFactory = new FileStoreFactory(settings); SocketAcceptor socketAcceptor = new SocketAcceptor(acceptor,fileStoreFactory, settings, screenLogFactory,messageFactory); socketAcceptor.start(); System.out.println("press any key to stop the FIX Acceptor/ Exchange Server..."); System.in.read(); socketAcceptor.stop(); } catch (Exception e) { e.printStackTrace(); } } @Override public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon { System.out.println("fromAdmin " + arg0); } @Override public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { System.out.println("fromApp " + message); crack(message, sessionID); } @Override public void onCreate(SessionID arg0) { } @Override public void onLogon(SessionID sessionID) { System.out.println("onLogon of "+sessionID); } @Override public void onLogout(SessionID arg0) { } @Override public void toAdmin(Message arg0, SessionID arg1) { } @Override public void toApp(Message arg0, SessionID arg1) throws DoNotSend { } //@Override public void onMessage(NewOrderSingle order, SessionID sessionID) throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue { System.out.println("Inside onmessage method"); try { Session.sendToTarget(new News(new Headline("Hello to OTC Expiration")), sessionID); } catch (SessionNotFound e) { e.printStackTrace(); } Symbol symbol = new Symbol(); Side side = new Side(); OrdType ordType = new OrdType(); OrderQty orderQty = new OrderQty(); Price price = new Price(); ClOrdID clOrdID = new ClOrdID(); order.get(symbol); order.get(side); order.get(orderQty); order.get(price); order.get(clOrdID); order.get(ordType); ExecutionReport executionReport = new ExecutionReport( getOrderIDCounter(), getExecutionIDCounter(), new ExecTransType(ExecTransType.NEW), new ExecType( ExecType.FILL), new OrdStatus(OrdStatus.FILLED), symbol, side, new LeavesQty(0), new CumQty(orderQty.getValue()), new AvgPx(price.getValue())); executionReport.set(clOrdID); executionReport.set(orderQty); try { Session.sendToTarget(executionReport, sessionID); System.out.println("NewOrderSingle Execution Completed...."); } catch (SessionNotFound ex) { ex.printStackTrace(); System.out.println("Error during order execution" + ex.getMessage()); } } private int orderIDCounter; private int executionIDCounter; public OrderID getOrderIDCounter() { orderIDCounter++; return new OrderID(String.valueOf(orderIDCounter)); } public ExecID getExecutionIDCounter() { executionIDCounter++; return new ExecID(String.valueOf(executionIDCounter)); } }
Other configuration file you could download and copy from below downalod link.
- Now create maven project name: FIXInitiatorClient which is our actual client which we send real time order to the exchange. Below is final proejct structure:
- pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wfs.otc.FIXInitiatorClient</groupId> <version>0.0.1</version> <artifactId>FIXInitiatorClient</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>activemq-core</groupId> <artifactId>activemq-core</artifactId> <version>5.5.0</version> <scope>system</scope> <systemPath>${basedir}/lib/activemq-core-5.5.0.jar</systemPath> </dependency> <dependency> <groupId>activemq-xmpp</groupId> <artifactId>activemq-xmpp</artifactId> <version>5.5.1</version> <scope>system</scope> <systemPath>${basedir}/lib/activemq-xmpp-5.5.1.jar</systemPath> </dependency> <dependency> <groupId>backport-util-concurrent-2.1</groupId> <artifactId>backport-util-concurrent-2.1</artifactId> <version>2.1</version> <scope>system</scope> <systemPath>${basedir}/lib/backport-util-concurrent-2.1.jar</systemPath> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> <dependency> <groupId>mina-core-1.1.0-sources</groupId> <artifactId>mina-core-1.1.0-sources</artifactId> <version>1.1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/mina-core-1.1.0-sources.jar</systemPath> </dependency> <dependency> <groupId>mina-core-1.1.0</groupId> <artifactId>mina-core-1.1.0</artifactId> <version>1.1.0</version> <scope>system</scope> <systemPath>${basedir}/lib/mina-core-1.1.0.jar</systemPath> </dependency> <dependency> <groupId>quickfixj-all-1.4.0</groupId> <artifactId>quickfixj-all-1.4.0</artifactId> <version>1.4.0</version> <scope>system</scope> <systemPath>${basedir}/lib/quickfixj-all-1.4.0.jar</systemPath> </dependency> <dependency> <groupId>slf4j-api-1.7.12</groupId> <artifactId>slf4j-api-1.7.12</artifactId> <version>1.7.12</version> <scope>system</scope> <systemPath>${basedir}/lib/slf4j-api-1.7.12.jar</systemPath> </dependency> <dependency> <groupId>slf4j-jdk14-1.7.12</groupId> <artifactId>slf4j-jdk14-1.7.12</artifactId> <version>1.7.12</version> <scope>system</scope> <systemPath>${basedir}/lib/slf4j-jdk14-1.7.12.jar</systemPath> </dependency> </dependencies> </project>
- PhysicalSettlementAdapter.java:
package com.javahonk.fix.adapter; import quickfix.fix42.NewOrderSingle; import com.javahonk.fix.model.LifeCycleManagerModel; public interface PhysicalSettlementAdapter { NewOrderSingle convertNewOrderSingle(LifeCycleManagerModel lifeCycleManagerModel); }
- PhysicalSettlementAdapterImpl.java
package com.javahonk.fix.adapter; import java.util.Date; import quickfix.field.ClOrdID; import quickfix.field.HandlInst; import quickfix.field.OrdType; import quickfix.field.OrderQty; import quickfix.field.Price; import quickfix.field.Side; import quickfix.field.Symbol; import quickfix.field.TransactTime; import quickfix.fix42.NewOrderSingle; import com.javahonk.fix.model.LifeCycleManagerModel; public class PhysicalSettlementAdapterImpl implements PhysicalSettlementAdapter{ @Override public NewOrderSingle convertNewOrderSingle(LifeCycleManagerModel lifeCycleManagerModel) { NewOrderSingle order = new NewOrderSingle(new ClOrdID(lifeCycleManagerModel.getOrderID()), new HandlInst(HandlInst.MANUAL_ORDER), new Symbol(lifeCycleManagerModel.getUnderlier()), new Side(Side.BUY), new TransactTime(new Date()), new OrdType(OrdType.MARKET)); order.set(new OrderQty(lifeCycleManagerModel.getQuantity())); order.set(new Price(lifeCycleManagerModel.getUndPrice())); return order; } }
- PhysicalSettlementCollegue.java:
package com.javahonk.fix.mediator; import quickfix.fix42.ExecutionReport; import com.javahonk.fix.model.LifeCycleManagerModel; public abstract class PhysicalSettlementCollegue { protected PhysicalSettlementMediator physicalSettlementMediator; public PhysicalSettlementCollegue(PhysicalSettlementMediator physicalSettlementMediator){ this.physicalSettlementMediator=physicalSettlementMediator; } public abstract ExecutionReport sendPhysicalSettlementOrder(LifeCycleManagerModel lifeCycleManagerModel); }
- PhysicalSettlementCollegueImpl.java:
package com.javahonk.fix.mediator; import quickfix.fix42.ExecutionReport; import com.javahonk.fix.model.LifeCycleManagerModel; public class PhysicalSettlementCollegueImpl extends PhysicalSettlementCollegue{ public PhysicalSettlementCollegueImpl(PhysicalSettlementMediator physicalSettlementMediator) { super(physicalSettlementMediator); } @Override public ExecutionReport sendPhysicalSettlementOrder(LifeCycleManagerModel lifeCycleManagerModel) { return physicalSettlementMediator.sendPhysicalSettlementOrder(lifeCycleManagerModel); } }
- PhysicalSettlementMediator.java:
package com.javahonk.fix.mediator; import quickfix.fix42.ExecutionReport; import com.javahonk.fix.model.LifeCycleManagerModel; public interface PhysicalSettlementMediator { ExecutionReport sendPhysicalSettlementOrder(LifeCycleManagerModel lifeCycleManagerModel); }
- PhysicalSettlementMediatorImpl.java:
package com.wfs.otc.fix.mediator; import java.util.ArrayList; import quickfix.Session; import quickfix.SessionID; import quickfix.fix42.ExecutionReport; import com.javahonk.fix.adapter.PhysicalSettlementAdapter; import com.javahonk.fix.adapter.PhysicalSettlementAdapterImpl; import com.javahonk.fix.model.LifeCycleManagerModel; import com.javahonk.fix.test.FixInitiatorStratup; public class PhysicalSettlementMediatorImpl implements PhysicalSettlementMediator { @Override public ExecutionReport sendPhysicalSettlementOrder(LifeCycleManagerModel lifeCycleManagerModel) { try { ArrayList<SessionID> sessions = FixInitiatorStratup.getFixSession(); PhysicalSettlementAdapter fixMessageAdapter = new PhysicalSettlementAdapterImpl(); SessionID sessionID = sessions.get(0); System.out.println("Sending Order to Server"); Session.sendToTarget(fixMessageAdapter.convertNewOrderSingle(lifeCycleManagerModel), sessionID); Thread.sleep(3000); System.out.println("Stoping socketInitiator"); FixInitiatorStratup.stopFixInitiator(); } catch (Exception e) { e.printStackTrace(); } return FixInitiatorStratup.getExecutionReport(); } }
- LifeCycleManagerModel.java:
package com.javahonk.fix.model; import java.time.LocalDate; import java.util.Objects; public class LifeCycleManagerModel { private String lifeCycleEventID; private LocalDate lifeCycleEventDate; private String holdingID; private String legalEntity; private String accountID; private String customerID; private String strategy; private double quantity; private String smID; private String productID; private String positionAssetType; private String mktTickerSym; private Boolean isListed; private String exchange; private String underlier; private double lastMinusClose; private String mktUndTickerSym; private String assetType; private String description; private String tradeCurrency; private LocalDate expiration; private String expiryTime; private String expirySettleType; private double expiryPrice; private String flagFPV; private double strike; private String type; private double contractSize; private double quantityPhysicalSettle; private double quantityCashSettle; private String contrarySelection; private String processStatus; private String externalSystem; private String refIdDerivative; private String refIdStock; private String executedBy; private String userID; private String batchID; private String undPriceType; private double undPrice; private String orderID; private String riskTransferType; private String riskOrderType; //getter and setter methods }
- Utils.java
package com.javahonk.fix.model; public class Utils { private final static double EPSILON = 0.00001; public static boolean areDoublesEqual(double left, double right) { return Math.abs(left - right) < EPSILON; } }
- FixInitiatorStratup.java:
package com.javahonk.fix.test; import java.util.ArrayList; import quickfix.ApplicationAdapter; import quickfix.ConfigError; import quickfix.DefaultMessageFactory; import quickfix.FieldNotFound; import quickfix.FileStoreFactory; import quickfix.IncorrectDataFormat; import quickfix.IncorrectTagValue; import quickfix.Message; import quickfix.RejectLogon; import quickfix.ScreenLogFactory; import quickfix.SessionID; import quickfix.SessionSettings; import quickfix.SocketInitiator; import quickfix.UnsupportedMessageType; import quickfix.field.ExecType; import quickfix.fix42.ExecutionReport; public class FixInitiatorStratup extends ApplicationAdapter { private SocketInitiator socketInitiator; private static FixInitiatorStratup fixIniator; static ExecutionReport executionReport; public static ArrayList<SessionID> getFixSession() throws ConfigError { fixIniator = new FixInitiatorStratup(); SessionSettings sessionSettings = new SessionSettings("./config/initiator/initiator.cfg"); ApplicationAdapter application = new FixInitiatorStratup(); FileStoreFactory fileStoreFactory = new FileStoreFactory(sessionSettings); ScreenLogFactory screenLogFactory = new ScreenLogFactory(sessionSettings); DefaultMessageFactory defaultMessageFactory = new DefaultMessageFactory(); fixIniator.socketInitiator = new SocketInitiator(application,fileStoreFactory, sessionSettings, screenLogFactory,defaultMessageFactory); fixIniator.socketInitiator.start(); ArrayList<SessionID> sessions = fixIniator.socketInitiator.getSessions(); return sessions; } @Override public void onLogon(SessionID sessionId) { super.onLogon(sessionId); System.out.println("Logon requested by client"); } @Override public void fromAdmin(quickfix.Message message, SessionID sessionId) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon { super.fromAdmin(message, sessionId); System.out.println("Inside fromAdmin"); } @Override public void onCreate(SessionID sessionId) { super.onCreate(sessionId); System.out.println("Inside onCreate"); } @Override protected void finalize() throws Throwable { super.finalize(); if (null != this.socketInitiator) { this.socketInitiator.stop(); } } @Override public void fromApp(Message message, SessionID sessionId) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { if (message instanceof ExecutionReport) { ExecutionReport executionReport = (ExecutionReport) message; FixInitiatorStratup.executionReport = executionReport; try { ExecType executionType = (ExecType) executionReport.getExecType(); System.out.println("Received execution below is response \n"); System.out.println(executionType); System.out.println("Received execution report for the requested order from Exchange \n"); } catch (FieldNotFound e) { e.printStackTrace(); } } } public static void stopFixInitiator(){ fixIniator.socketInitiator.stop(); } public static ExecutionReport getExecutionReport() { return executionReport; } public static void setExecutionReport(ExecutionReport executionReport) { FixInitiatorStratup.executionReport = executionReport; } }
- SendPhysicalSettlementByMediator.java:
package com.javahonk.fix.test; import quickfix.fix42.ExecutionReport; import com.javahonk.fix.mediator.PhysicalSettlementCollegue; import com.javahonk.fix.mediator.PhysicalSettlementCollegueImpl; import com.javahonk.fix.mediator.PhysicalSettlementMediator; import com.javahonk.fix.mediator.PhysicalSettlementMediatorImpl; import com.javahonk.fix.model.LifeCycleManagerModel; public class SendPhysicalSettlementByMediator { public static void main(String[] args) { PhysicalSettlementMediator physicalSettlementMediator = new PhysicalSettlementMediatorImpl(); PhysicalSettlementCollegue fixMessageSenderCollegue = new PhysicalSettlementCollegueImpl(physicalSettlementMediator); LifeCycleManagerModel lifeCycleManagerModel = new LifeCycleManagerModel(); lifeCycleManagerModel.setOrderID("123456"); lifeCycleManagerModel.setUnderlier("AAPL"); lifeCycleManagerModel.setQuantity(123.0); lifeCycleManagerModel.setUndPrice(369.36); ExecutionReport acknowledgement = fixMessageSenderCollegue.sendPhysicalSettlementOrder(lifeCycleManagerModel); System.out.println("Acknowledgement received: --> "+acknowledgement); } }
- initiator.cfg:
[DEFAULT] ConnectionType=initiator HeartBtInt=60 ReconnectInterval=1 FileStorePath=.\fixfiles\initiator FileLogPath=.\log StartTime=00:00:00 EndTime=00:00:00 UseDataDictionary=Y SocketReuseAddress=Y SocketKeepAlive=Y SocketTcpNoDelay=Y SocketConnectHost=localhost [SESSION] BeginString=FIX.4.2 SenderCompID=FixClient8019 TargetCompID=FixAcceptor SocketConnectPort=5001
Thease are main files and other files directly download from bottom link and add to your prject.
- To run this applicaiton first you will have to strat FIXAcceptorServer as java application if all configuration set properly you will below output on console:
- Now start and send order using class: SendPhysicalSettlementByMediator.java as this class also has main method so java start as java application you will below successful order message on the console:
- Also how server processed the message from client you could see on FIXAcceptorServer console as below:
- That’s it.
Note: I am not including Jars of this proejct in download link. You could use maven and QuickFix to download all jar if these don’t works then you could use this tutorial project download link and include all jars.
Download Project: FIXAcceptorServer FIXInitiatorClient