QuickFixJ Basic Demo Application
QuickFixJ Basic Demo Application
java package com.fix.initiator; import java.util.ArrayList; import java.util.Date; import java.util.concurrent.CountDownLatch; 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.Session; import quickfix.SessionID; import quickfix.SessionNotFound; import quickfix.SessionSettings; import quickfix.SocketInitiator; import quickfix.UnsupportedMessageType; import quickfix.field.ClOrdID; import quickfix.field.ExecType; 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.ExecutionReport; import quickfix.fix42.NewOrderSingle;
// FIX protocol based client, which acts as the trading client. public class FixInitator extends ApplicationAdapter { private SocketInitiator socketInitiator; private static final CountDownLatch shutdownLatch = new CountDownLatch(1); public static void main(String[] args) throws ConfigError { FixInitator fixIniator = new FixInitator(); SessionSettings sessionSettings = new SessionSettings( "./conf/initiator.cfg"); ApplicationAdapter application = new FixInitator(); 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(); try { Thread.sleep(30000); } catch (InterruptedException e1) { e1.printStackTrace(); } ArrayList<SessionID> sessions = fixIniator.socketInitiator .getSessions(); NewOrderSingle order = new NewOrderSingle(new ClOrdID("MISYS1001"), new HandlInst(HandlInst.MANUAL_ORDER), new Symbol("MISYS"), new Side(Side.BUY), new TransactTime(new Date()), new OrdType( OrdType.LIMIT)); order.set(new OrderQty(45)); order.set(new Price(240.9d)); SessionID sessionID = sessions.get(0); System.out.println("Sending Order to Server"); try { Session.sendToTarget(order, sessionID);
} catch (SessionNotFound e) { e.printStackTrace(); } try { Thread.sleep(30000); } catch (InterruptedException e1) { e1.printStackTrace(); } // try { shutdownLatch.await(); } catch (InterruptedException e1){ // e1.printStackTrace(); } System.out.println("Going to stop socketInitiator"); fixIniator.socketInitiator.stop(); } @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); } @Override public void onCreate(SessionID sessionId) { super.onCreate(sessionId); } @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; try { ExecType executionType = (ExecType) executionReport .getExecType(); System.out.println(executionType); System.out.println("----->>>>>Received execution report for the requested order from Exchange \n"); } catch (FieldNotFound e) { e.printStackTrace(); } } } } CFG FILE:
[DEFAULT] ConnectionType=initiator HeartBtInt=20 ReconnectInterval=1 FileStorePath=c:\fixfi 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
2)Acceptor.java package com; 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 static void main(String[] args) { try { File file = new File("./conf/acceptor.cfg"); System.out.println(file.getAbsolutePath()); SessionSettings settings = new SessionSettings( "./conf/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) { // in case you want to update the db or some other thing which keeps track for the logged in // client that can be done here, for the purpose of the example i have just printed 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 This looks interesting, need to check this, email message -* haha. public void onMessage(Email message, SessionID sessionID) throws * FieldNotFound, UnsupportedMessageType, IncorrectTagValue { * super.onMessage(message, sessionID); } */ @Override public void onMessage(NewOrderSingle order, SessionID sessionID) throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue { // sending some news to the client. try { Session.sendToTarget(new News(new Headline("hello to Fidel..Fixsol")), 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)); } }
CFG FILE
[DEFAULT] ConnectionType=acceptor SocketAcceptPort=5001 SocketConnectHost=localhost SocketReuseAddress=Y StartTime=00:00:00 EndTime=00:00:00 FileLogPath=log FileStorePath=.\fixfiles SocketKeepAlive=Y SocketTcpNoDelay=Y [SESSION] BeginString=FIX.4.2 SenderCompID=FixAcceptor TargetCompID=FixClient8019 DataDictionary=.\FIX42.xml
Common POM.xml for both project same file copy paste in both N change the artifactid
<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>dad</groupId> <artifactId>FixAcceptor</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependencies> <!-- QuickFIX/J dependencies --> <dependency> <groupId>quickfixj</groupId> <artifactId>quickfixj-core</artifactId> <version>1.3.1</version> </dependency>
<dependency> <groupId>quickfixj</groupId> <artifactId>quickfixj-msg-fix40</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>quickfixj</groupId> <artifactId>quickfixj-msg-fix41</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>quickfixj</groupId> <artifactId>quickfixj-msg-fix42</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>quickfixj</groupId> <artifactId>quickfixj-msg-fix43</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>quickfixj</groupId> <artifactId>quickfixj-msg-fix44</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.mina</groupId> <artifactId>mina-core</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.3.0</version> </dependency> </dependencies> </project>