Click here to Skip to main content
13,258,269 members (39,350 online)
Click here to Skip to main content
Add your own
alternative version

Stats

81.6K views
3.2K downloads
35 bookmarked
Posted 13 Jan 2011

Fix Message Implementation using QuickFix

, 6 Sep 2011
Rate this:
Please Sign up or sign in to vote.
This article is about how to use QuickFix .NET engine.

Purpose

The purpose of this document is to give brief details about FIX and basic implementation of messages using QuickFix .NET library.

First of all, we need to know what is FIX protocol.

What is FIX Protocol?

It is a series of messaging specifications for the electronic communication of trade-related messages. It has been developed through the collaboration of banks, broker-dealers, exchanges, industry utilities and associations, institutional investors, and information technology providers from around the world. These market participants share a vision of a common, global language for the automated trading of financial instruments.

Most of the exchanges use this standard for communication like sending Order, Executions, MarketData, etc. There are many versions of specifications released by FIX organization like 4.0, 4.2, 5.0, etc.

You can read more about FIX on http://www.fixprotocol.org.

What is QuickFix?

QuickFIX is a free and open source implementation of the FIX protocol in various languages like C++, Java, ruby, .NET, etc.

So let’s start with implementation of Fix Messages. I am going to create two applications, server and client which we call as FixAcceptor and FixInitiator respectively.

Implementation with C#

To use QuickFix engine, we’ll need two DLLs quickfix_net.dll and quickfix_net_message.dll which can be downloaded from QuickFix website.

I created one solution which has two projects FixAcceptor and FixInitiator. FixAcceptor is active as server and FixInitiator is client app.

FixAcceptor

To start FixAcceptor, we need to configure and configurations are put into acceptor.cfg which should be pretty straight forward configurations like below:

[DEFAULT]
ConnectionType=acceptor
SocketAcceptPort=5001
SocketReuseAddress=Y
StartTime=00:00:00
EndTime=00:00:00
FileLogPath=log
FileStorePath=c:\fixfiles
[SESSION]
BeginString=FIX.4.2
SenderCompID=EXECUTOR
TargetCompID=CLIENT1
DataDictionary=c:\user\nek\Code\FIX test App\data Dictionary\FIX42.xml
  • Connection type tells this application will run as Acceptor which is server.
  • SocketAcceptPort: listening port.
  • Session tag: is having configuration for creating session between client application (initiator) and acceptor.
  • BegingString: This sets session will work on which Fix Message specification.
  • SenderCompID: Id of server which will listen and send messages.
  • TargetCompID=Id of client to which server will send messages.
  • DataDictionary: Path of data dictionary file which is XML format, this file has message specifications according to various specifications versions.

Source Code

To start any session with Fix, we need to create Class which should implement QuickFix.Application interface. It has the following methods to be implemented:

public interface Application 
{ 
void fromAdmin(Message __p1, SessionID __p2); 
void fromApp(Message __p1, SessionID __p2); 
void onCreate(SessionID __p1); 
void onLogon(SessionID __p1); 
void onLogout(SessionID __p1); 
void toAdmin(Message __p1, SessionID __p2); 
void toApp(Message __p1, SessionID __p2); 
} 

public override void onMessage(QuickFix42.NewOrderSingle order, SessionID sessionID) 
{ 
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(ordType); 
if (ordType.getValue() != OrdType.LIMIT) 
throw new IncorrectTagValue(ordType.getField()); 
order.get(symbol); 
order.get(side); 
order.get(orderQty); 
order.get(price); 
order.get(clOrdID); 
QuickFix42.ExecutionReport executionReport = new QuickFix42.ExecutionReport 
(genOrderID(), 
genExecID(), 
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); 
executionReport.set(new LastShares(orderQty.getValue())); 
executionReport.set(new LastPx(price.getValue())); 
if (order.isSetAccount()) 
executionReport.set(order.getAccount()); 
try 
{ 
Session.sendToTarget(executionReport, sessionID); 
} 
catch (SessionNotFound) { } 
}
Session.sendToTarget(executionReport, sessionID);

The above statement sends executionReport object to session which is built between client and server.

Start FixAcceptor Application

[STAThread]
static void Main(string[] args)
{
SessionSettings settings = new SessionSettings(@"acceptor.cfg");
FixServerApplication application = new FixServerApplication();
FileStoreFactory storeFactory = new FileStoreFactory(settings);
ScreenLogFactory logFactory = new ScreenLogFactory(settings);
MessageFactory messageFactory = new DefaultMessageFactory();
SocketAcceptor acceptor
= new SocketAcceptor(application, storeFactory, settings, logFactory, messageFactory);
acceptor.start();
Console.WriteLine("press <enter> to quit");
Console.Read();

acceptor.stop();
}

Steps

  1. Create SessionSettings object with config file.
  2. Create object of Application class.
  3. Create Object of SocketAcceptor class by passing SessionSettings.
  4. Run Start method of acceptor object.

Start FixInitiator

To start FixAcceptor, we need to configure and configurations are put into acceptor.cfg which should be pretty straight forward configurations like below:

[DEFAULT]
ConnectionType=initiator
HeartBtInt=30
ReconnectInterval=1
FileStorePath=c:\fixfiles
FileLogPath=log
StartTime=00:00:00
EndTime=00:00:00
UseDataDictionary=N
SocketConnectHost=localhost
[SESSION]
BeginString=FIX.4.2
SenderCompID=CLIENT1
TargetCompID=FixServer
SocketConnectPort=5001
  • Connection type tells this application will run as Acceptor which is server.
  • SocketAcceptPort: listening port.
  • Session tag: is having configuration for creating session between client application (initiator) and acceptor.
  • BeginString: this sets session will work on which Fix Message specification
  • SenderCompID: Id of client which will send messages
  • TargetCompID: Id of server to which server will listen messages
  • DataDictionary: Path of data dictionary file which is xml format, this file is having message specifications according to various specifications versions.

Source Code

To start any session with Fix, we need to create class which should implement QuickFix.Application interface.

public class ClientInitiator : QuickFix.Application 
{ 
public void onCreate(QuickFix.SessionID value) 
{ 
//Console.WriteLine("Message OnCreate" + value.toString()); 
} 
public void onLogon(QuickFix.SessionID value) 
{ 
//Console.WriteLine("OnLogon" + value.toString()); 
} 
public void onLogout(QuickFix.SessionID value) 
{ 
// Console.WriteLine("Log out Session" + value.toString()); 
} 
public void toAdmin(QuickFix.Message value, QuickFix.SessionID session) 
{ 
//Console.WriteLine("Called Admin :" + value.ToString()); 
} 
public void toApp(QuickFix.Message value, QuickFix.SessionID session) 
{ 
// Console.WriteLine("Called toApp :" + value.ToString()); 
} 
public void fromAdmin(QuickFix.Message value, SessionID session) 
{ 
// Console.WriteLine("Got message from Admin" + value.ToString()); 
} 
public void fromApp(QuickFix.Message value, SessionID session) 
{ 
if (value is QuickFix42.ExecutionReport) 
{ 
QuickFix42.ExecutionReport er = (QuickFix42.ExecutionReport)value; 
ExecType et = (ExecType)er.getExecType(); 
if (et.getValue() == ExecType.FILL) 
{ 
//TODO: implement code 
} 
} 
Console.WriteLine("Got message from App" + value.ToString()); 
} 

} 
/// <summary> 
/// The main entry point for the application. 
/// </summary> 
//[STAThread] 
static void Main() 
{ 
ClientInitiator app = new ClientInitiator(); 
SessionSettings settings = new SessionSettings(
    @"c:\users\nek\Code\FIX test App\initiator.cfg"); 
QuickFix.Application application = new ClientInitiator(); 
FileStoreFactory storeFactory = new FileStoreFactory(settings); 
ScreenLogFactory logFactory = new ScreenLogFactory(settings); 
MessageFactory messageFactory = new DefaultMessageFactory(); 
SocketInitiator initiator = new SocketInitiator(application, storeFactory, settings, 
    logFactory, messageFactory); 
initiator.start(); 
Thread.Sleep(3000); 
SessionID sessionID = (SessionID)list[0]; 
QuickFix42.NewOrderSingle order = new QuickFix42.NewOrderSingle(new ClOrdID("DLF"), 
    new HandlInst(HandlInst.MANUAL_ORDER), new Symbol("DLF"), new Side(Side.BUY), 
    new TransactTime(DateTime.Now), new OrdType(OrdType.LIMIT)); 
order.set(new OrderQty(45)); 
order.set(new Price(25.4d)); 
Session.sendToTarget(order, sessionID); 
Console.ReadLine(); 
initiator.stop(); 
}

Steps

  1. Create application class object, i.e., ClientInitiator.
  2. Create object of SessionSettings class.
  3. Create SocketInitiator class.
  4. Run Start method.
  5. Create session id.

How to Send Order

Create order object of NewOrderSingle class. Set type of order, symbol, side, etc., and send order by SendToTarget method.

Receive Order Notification in Client

You can receive sent order acknowledgement in FromApp method in application class.

Start Application

QuickFix42.NewOrderSingle order = new QuickFix42.NewOrderSingle(new ClOrdID("DLF"), 
    new HandlInst(HandlInst.MANUAL_ORDER), new Symbol("DLF"), new Side(Side.BUY), 
    new TransactTime(DateTime.Now), new OrdType(OrdType.LIMIT));

order.set(new OrderQty(45));

order.set(new Price(25.4d));

Session.sendToTarget(order, sessionID); 
public void fromApp(QuickFix.Message value, SessionID session)
{
if (value is QuickFix42.ExecutionReport)
{
QuickFix42.ExecutionReport er = (QuickFix42.ExecutionReport)value;
ExecType et = (ExecType)er.getExecType();
if (et.getValue() == ExecType.FILL)
{
//TODO: implement code
}
}
Console.WriteLine("Got message from App" + value.ToString());

}
  1. Run FixAccceptor.
  2. Then Run FixInitiator. This application will send order to FixAcceptor.

Fix Initiator

fixInitiator.png

Fix Acceptor

FixAcceptor.png

I’ll continue the advanced version of Fix message implementation in next blog.

Please give your valuable feedback.

There is also the need to inherit MessageCracker class which has some virtual methods to handle messages like: the below method invokes when any Order is sent by client then this method sends execution report of this order to client. ExecutionReport can be Filled, Cancelled, etc. Filled Execution Report means order has been successfully executed on exchange.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Neeraj Kaushik1980
Architect Saxo Bank A/S
Denmark Denmark
• Solution Architect /Principle Lead Developer with 12 years of IT experience with more emphasize on Capital Domain and Investment banking domain.
• Strong experience in Continuous Integration, Delivery and DevOps solutions.
• Strong experience in drafting solutions, stakeholder communications and risk management.
• Proved strong coding and designing skills with agile approaches (TDD, XP framework, Pair Programming).
• Delivered many projects with involvement from inception to delivery phase.
• Strong experience in high performance, multithreaded, low latency applications.
• Ability to communicate with the business and technical stake holders effectively.
• Have extensive experience in Capital Market Domain: Front Office & BackOffice (Algorithm Trading tools, messaging framework, Enterprise bus, integration of FIX APIs and many trading APIs).
• Functional knowledge of Portfolio/Wealth Management, Equities, Fixed Income, Derivatives, Forex.
• Practical knowledge of building and practicing agile delivery methodologies (SCRUM, TDD, Kanban).

Technical Skills

• Architectural: Solution Design, Architectural Presentations (Logical, Component, Physical, UML diagrams)
• Languages: C#, C++
• Server Technologies: WCF, Web API,
• Middle Ware: ActiveMQ, RabbitMQ, Enterprise Service Bus
• UI Technologies: Winforms and WPF
• Web Technologies: Asp.Net Mvc, KnockOutJS, JQuery, Advance Java Scripts Concepts
• Databases: Sql Server 2008 +, MySQL
• Tools/Frameworks: TFS, SVN, NUnit, Rhino Mocks, Unity, NAnt, QuickFix/n, Nhibernate, LINQ, JIRA,

Functional Skills

• Wealth Management System, Trade Life Cycle, Trading Components and their integrations
• Working knowledge of Stocks, Bonds, CFDs,Forex, Futures and Options
• Pricing Systems, Market Data Management,
• BackOffice Processes : Settlement Processes, Netting, Tax, Commissions, Corporate Actions Handling,
• Reporting Solutions : OLTP and OLAP Data model designing
• FIX Engine implementation and integration

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionQuickFix with boost::asio Pin
eshustin18-Aug-16 11:18
membereshustin18-Aug-16 11:18 
AnswerRe: QuickFix with boost::asio Pin
Neeraj Kaushik198018-Aug-16 12:24
memberNeeraj Kaushik198018-Aug-16 12:24 
GeneralI am waiting for your next blog Pin
Southmountain23-Jan-16 19:24
memberSouthmountain23-Jan-16 19:24 
GeneralRe: I am waiting for your next blog Pin
Neeraj Kaushik19802-Apr-16 4:26
memberNeeraj Kaushik19802-Apr-16 4:26 
QuestionSocketConnectHost Pin
Member 1197658611-Sep-15 1:12
memberMember 1197658611-Sep-15 1:12 
AnswerRe: SocketConnectHost Pin
Neeraj Kaushik 198016-Sep-15 10:59
memberNeeraj Kaushik 198016-Sep-15 10:59 
QuestionUsername and Password Pin
Archimedes2428-Apr-15 2:18
memberArchimedes2428-Apr-15 2:18 
QuestionHelp with QuoteRequest Pin
Member 136612530-Jun-14 2:44
memberMember 136612530-Jun-14 2:44 
Questionhello,can you help me Pin
zengjunjie23-Apr-14 0:30
memberzengjunjie23-Apr-14 0:30 
QuestionHelp Fix Protocol Pin
Quartana9-Mar-14 23:52
memberQuartana9-Mar-14 23:52 
QuestionExample causes problem for FIXT.1.1 version Pin
shafiqul bashar27-Aug-12 21:03
membershafiqul bashar27-Aug-12 21:03 
AnswerRe: Example causes problem for FIXT.1.1 version Pin
Neeraj Kaushik 198027-Aug-12 21:40
memberNeeraj Kaushik 198027-Aug-12 21:40 
GeneralRe: Example causes problem for FIXT.1.1 version Pin
shafiqul bashar28-Aug-12 1:29
membershafiqul bashar28-Aug-12 1:29 
GeneralRe: Example causes problem for FIXT.1.1 version Pin
inamalvi25-Mar-13 19:42
memberinamalvi25-Mar-13 19:42 
GeneralFIX is connecting on Single Machine only i.e.SocketConnectHost=localhost for connecting in LAN network i used SocketConnectHost=remote but not work Pin
Member 319880029-Feb-12 2:28
memberMember 319880029-Feb-12 2:28 
GeneralRe: FIX is connecting on Single Machine only i.e.SocketConnectHost=localhost for connecting in LAN network i used SocketConnectHost=remote but not work Pin
Neeraj Kaushik 198029-Feb-12 2:52
memberNeeraj Kaushik 198029-Feb-12 2:52 
GeneralRe: FIX is connecting on Single Machine only i.e.SocketConnectHost=localhost for connecting in LAN network i used SocketConnectHost=remote but not work Pin
khokon772-Aug-12 0:15
memberkhokon772-Aug-12 0:15 
GeneralRe: FIX is connecting on Single Machine only i.e.SocketConnectHost=localhost for connecting in LAN network i used SocketConnectHost=remote but not work Pin
Neeraj Kaushik 19802-Aug-12 0:30
memberNeeraj Kaushik 19802-Aug-12 0:30 
GeneralRe: FIX is connecting on Single Machine only i.e.SocketConnectHost=localhost for connecting in LAN network i used SocketConnectHost=remote but not work Pin
khokon772-Aug-12 0:39
memberkhokon772-Aug-12 0:39 
GeneralRe: FIX is connecting on Single Machine only i.e.SocketConnectHost=localhost for connecting in LAN network i used SocketConnectHost=remote but not work Pin
Neeraj Kaushik 19805-Aug-12 19:59
memberNeeraj Kaushik 19805-Aug-12 19:59 
Questiondynamic acceptor issue Pin
GajananPise24-Feb-12 0:06
memberGajananPise24-Feb-12 0:06 
AnswerRe: dynamic acceptor issue Pin
Neeraj Kaushik 198029-Feb-12 3:51
memberNeeraj Kaushik 198029-Feb-12 3:51 
QuestionMarketData Pin
edsoncosta16-Feb-12 1:49
memberedsoncosta16-Feb-12 1:49 
AnswerRe: MarketData Pin
neeraj198016-Feb-12 5:29
memberneeraj198016-Feb-12 5:29 
GeneralRe: MarketData Pin
Member 344990819-Feb-12 8:33
memberMember 344990819-Feb-12 8:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 6 Sep 2011
Article Copyright 2011 by Neeraj Kaushik1980
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid