quick fix sample

9
Basic Fix Message Implementation using QuickFix.Net Purpose Purpose of this document is give brief detail about FIX and basic Implementation of messages using QuickFix dotnet 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, dotnet etc. So let’s start with implementation of Fix Messages. I am going to create two application, server and client which we call as FixAcceptor and FixInitiator respectively. Implementation with c# To use QuickFix engine we’ll need to dlls quickfix_net.dll and quickfix_net_message.dll which can 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

Upload: neeraj-kaushik

Post on 26-May-2015

9.991 views

Category:

Technology


3 download

DESCRIPTION

This document contains implementation of QuickFix api.

TRANSCRIPT

Page 1: Quick Fix Sample

Basic Fix Message Implementation using QuickFix.Net

PurposePurpose of this document is give brief detail about FIX and basic Implementation of messages using QuickFix dotnet 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, dotnet etc.

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

Implementation with c#To use QuickFix engine we’ll need to dlls quickfix_net.dll and quickfix_net_message.dll which can 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 configuration and configurations are put into acceptor.cfg which should pretty straight forward configurations like below:

[DEFAULT]ConnectionType=acceptorSocketAcceptPort=5001SocketReuseAddress=YStartTime=00:00:00EndTime=00:00:00

Page 2: Quick Fix Sample

FileLogPath=logFileStorePath=c:\fixfiles

[SESSION]BeginString=FIX.4.2SenderCompID=EXECUTORTargetCompID=CLIENT1DataDictionary=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 is having message specifications according to various specifications versions.

SourceCode

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

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

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

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

Page 3: Quick Fix Sample

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

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

Page 4: Quick Fix Sample

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 configuration and configurations are put into acceptor.cfg which should pretty straight forward configurations like below:

[DEFAULT]ConnectionType=initiatorHeartBtInt=30ReconnectInterval=1FileStorePath=c:\fixfilesFileLogPath=logStartTime=00:00:00EndTime=00:00:00UseDataDictionary=NSocketConnectHost=localhost[SESSION]BeginString=FIX.4.2SenderCompID=CLIENT1

Page 5: Quick Fix Sample

TargetCompID=FixServerSocketConnectPort=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.BegingString: 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 messagesDataDictionary: Path of data dictionary file which is xml format, this file is having message specifications according to various specifications versions.

SourceCode

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

Page 6: Quick Fix Sample

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.

Page 7: Quick Fix Sample

How to Send Order.

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

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

Receive Order Notification in client

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

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()); }

Start Application

1. Run FixAccceptor2. Then Run FixInitiator. This application will send order to FixAcceptor.

Fix Initiator

Page 8: Quick Fix Sample

Fix Acceptor