Your SlideShare is downloading. ×
Quick Fix Sample
Quick Fix Sample
Quick Fix Sample
Quick Fix Sample
Quick Fix Sample
Quick Fix Sample
Quick Fix Sample
Quick Fix Sample
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Quick Fix Sample

10,116

Published on

This document contains implementation of QuickFix api.

This document contains implementation of QuickFix api.

Published in: Technology, Business
1 Comment
6 Likes
Statistics
Notes
  • Great Article. Many thanks for sharing.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
10,116
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
223
Comments
1
Likes
6
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Basic Fix Message Implementation using QuickFix.Net&lt;br /&gt;Purpose&lt;br /&gt;Purpose of this document is give brief detail about FIX and basic Implementation of messages using QuickFix dotnet library.&lt;br /&gt;First of All we need to know what is FIX protocol.&lt;br /&gt;What is FIX protocol?&lt;br /&gt;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. &lt;br /&gt;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. &lt;br /&gt;You can read more about FIX on http://www.fixprotocol.org.&lt;br /&gt;What is QuickFix?&lt;br /&gt;QuickFIX is a free and open source implementation of the FIX protocol in various languages like c++, java, ruby, dotnet etc.&lt;br /&gt;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.&lt;br /&gt;Implementation with c#&lt;br /&gt;To use QuickFix engine we’ll need to dlls quickfix_net.dll and quickfix_net_message.dll which can downloaded from QuickFix website.&lt;br /&gt;I created one solution which has two projects FixAcceptor and FixInitiator. FixAcceptor is active as server and FixInitiator is client app. &lt;br /&gt;FixAcceptor&lt;br /&gt;To start FixAcceptor we need to configuration and configurations are put into acceptor.cfg which should pretty straight forward configurations like below:&lt;br /&gt;[DEFAULT]&lt;br /&gt;ConnectionType=acceptor&lt;br /&gt;SocketAcceptPort=5001&lt;br /&gt;SocketReuseAddress=Y&lt;br /&gt;StartTime=00:00:00&lt;br /&gt;EndTime=00:00:00&lt;br /&gt;FileLogPath=log&lt;br /&gt;FileStorePath=c:fixfiles&lt;br /&gt;[SESSION]&lt;br /&gt;BeginString=FIX.4.2&lt;br /&gt;SenderCompID=EXECUTOR&lt;br /&gt;TargetCompID=CLIENT1&lt;br /&gt;DataDictionary=c:usernekCodeFIX test Appdata DictionaryFIX42.xml&lt;br /&gt;Connection type tells, this application will run as Acceptor which is server.&lt;br /&gt;SocketAcceptPort: listening port.&lt;br /&gt;Session tag: is having configuration for creating session between client application (initiator) and acceptor.&lt;br /&gt;BegingString: this sets session will work on which Fix Message specification,&lt;br /&gt;SenderCompID:Id of server which will listen and send messages.&lt;br /&gt;TargetCompID=Id of client to which server will send messages.&lt;br /&gt;DataDictionary: Path of data dictionary file which is xml format, this file is having message specifications according to various specifications versions.&lt;br /&gt;SourceCode&lt;br /&gt;To start any session with Fix, we need to create Class which should implement QuickFix.Application interface. It has following methods to be implement:&lt;br /&gt;public interface Application&lt;br /&gt; {&lt;br /&gt; void fromAdmin(Message __p1, SessionID __p2);&lt;br /&gt; void fromApp(Message __p1, SessionID __p2);&lt;br /&gt; void onCreate(SessionID __p1);&lt;br /&gt; void onLogon(SessionID __p1);&lt;br /&gt; void onLogout(SessionID __p1);&lt;br /&gt; void toAdmin(Message __p1, SessionID __p2);&lt;br /&gt; void toApp(Message __p1, SessionID __p2);&lt;br /&gt; }&lt;br /&gt;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.&lt;br /&gt;public override void onMessage(QuickFix42.NewOrderSingle order, SessionID sessionID)&lt;br /&gt; {&lt;br /&gt; Symbol symbol = new Symbol();&lt;br /&gt; Side side = new Side();&lt;br /&gt; OrdType ordType = new OrdType();&lt;br /&gt; OrderQty orderQty = new OrderQty();&lt;br /&gt; Price price = new Price();&lt;br /&gt; ClOrdID clOrdID = new ClOrdID();&lt;br /&gt; order.get(ordType);&lt;br /&gt; if (ordType.getValue() != OrdType.LIMIT)&lt;br /&gt; throw new IncorrectTagValue(ordType.getField());&lt;br /&gt; order.get(symbol);&lt;br /&gt; order.get(side);&lt;br /&gt; order.get(orderQty);&lt;br /&gt; order.get(price);&lt;br /&gt; order.get(clOrdID);&lt;br /&gt; QuickFix42.ExecutionReport executionReport = new QuickFix42.ExecutionReport&lt;br /&gt; (genOrderID(),&lt;br /&gt; genExecID(),&lt;br /&gt; new ExecTransType(ExecTransType.NEW),&lt;br /&gt; new ExecType(ExecType.FILL),&lt;br /&gt; new OrdStatus(OrdStatus.FILLED),&lt;br /&gt; symbol,&lt;br /&gt; side,&lt;br /&gt; new LeavesQty(0),&lt;br /&gt; new CumQty(orderQty.getValue()),&lt;br /&gt; new AvgPx(price.getValue()));&lt;br /&gt; executionReport.set(clOrdID);&lt;br /&gt; executionReport.set(orderQty);&lt;br /&gt; executionReport.set(new LastShares(orderQty.getValue()));&lt;br /&gt; executionReport.set(new LastPx(price.getValue()));&lt;br /&gt; if (order.isSetAccount())&lt;br /&gt; executionReport.set(order.getAccount());&lt;br /&gt; try&lt;br /&gt; {&lt;br /&gt; Session.sendToTarget(executionReport, sessionID);&lt;br /&gt; }&lt;br /&gt; catch (SessionNotFound) { }&lt;br /&gt;}&lt;br /&gt;Session.sendToTarget(executionReport, sessionID);&lt;br /&gt;Above statement sends executionReport object to session which is built between client and server.&lt;br /&gt;Start FixAcceptor Application&lt;br /&gt;[STAThread]&lt;br /&gt; static void Main(string[] args)&lt;br /&gt; {&lt;br /&gt; SessionSettings settings = new SessionSettings(@&quot;acceptor.cfg&quot;);&lt;br /&gt; FixServerApplication application = new FixServerApplication();&lt;br /&gt; FileStoreFactory storeFactory = new FileStoreFactory(settings);&lt;br /&gt; ScreenLogFactory logFactory = new ScreenLogFactory(settings);&lt;br /&gt; MessageFactory messageFactory = new DefaultMessageFactory();&lt;br /&gt; SocketAcceptor acceptor&lt;br /&gt; = new SocketAcceptor(application, storeFactory, settings, logFactory, messageFactory);&lt;br /&gt; acceptor.start();&lt;br /&gt; Console.WriteLine(&quot;press &lt;enter&gt; to quit&quot;);&lt;br /&gt; Console.Read();&lt;br /&gt; acceptor.stop();&lt;br /&gt;}&lt;br /&gt;Steps:&lt;br /&gt;<ul><li>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.</li></ul>Start FixInitiator&lt;br /&gt;To start FixAcceptor we need to configuration and configurations are put into acceptor.cfg which should pretty straight forward configurations like below:&lt;br /&gt;[DEFAULT]&lt;br /&gt;ConnectionType=initiator&lt;br /&gt;HeartBtInt=30&lt;br /&gt;ReconnectInterval=1&lt;br /&gt;FileStorePath=c:fixfiles&lt;br /&gt;FileLogPath=log&lt;br /&gt;StartTime=00:00:00&lt;br /&gt;EndTime=00:00:00&lt;br /&gt;UseDataDictionary=N&lt;br /&gt;SocketConnectHost=localhost&lt;br /&gt;[SESSION]&lt;br /&gt;BeginString=FIX.4.2&lt;br /&gt;SenderCompID=CLIENT1&lt;br /&gt;TargetCompID=FixServer&lt;br /&gt;SocketConnectPort=5001&lt;br /&gt;Connection type tells, this application will run as Acceptor which is server.&lt;br /&gt;SocketAcceptPort: listening port.&lt;br /&gt;Session tag: is having configuration for creating session between client application (initiator) and acceptor.&lt;br /&gt;BegingString: this sets session will work on which Fix Message specification,&lt;br /&gt;SenderCompID: Id of client which will send messages.&lt;br /&gt;TargetCompID: Id of server to which server will listen messages&lt;br /&gt;DataDictionary: Path of data dictionary file which is xml format, this file is having message specifications according to various specifications versions.&lt;br /&gt;SourceCode&lt;br /&gt;To start any session with Fix, we need to create Class which should implement QuickFix.Application interface.&lt;br /&gt;public class ClientInitiator : QuickFix.Application&lt;br /&gt; {&lt;br /&gt; public void onCreate(QuickFix.SessionID value)&lt;br /&gt; {&lt;br /&gt; //Console.WriteLine(&quot;Message OnCreate&quot; + value.toString());&lt;br /&gt; }&lt;br /&gt; public void onLogon(QuickFix.SessionID value)&lt;br /&gt; {&lt;br /&gt; //Console.WriteLine(&quot;OnLogon&quot; + value.toString());&lt;br /&gt; }&lt;br /&gt; public void onLogout(QuickFix.SessionID value)&lt;br /&gt; {&lt;br /&gt; // Console.WriteLine(&quot;Log out Session&quot; + value.toString());&lt;br /&gt; }&lt;br /&gt; public void toAdmin(QuickFix.Message value, QuickFix.SessionID session)&lt;br /&gt; {&lt;br /&gt; //Console.WriteLine(&quot;Called Admin :&quot; + value.ToString());&lt;br /&gt; }&lt;br /&gt; public void toApp(QuickFix.Message value, QuickFix.SessionID session)&lt;br /&gt; {&lt;br /&gt; // Console.WriteLine(&quot;Called toApp :&quot; + value.ToString());&lt;br /&gt; }&lt;br /&gt; public void fromAdmin(QuickFix.Message value, SessionID session)&lt;br /&gt; {&lt;br /&gt; // Console.WriteLine(&quot;Got message from Admin&quot; + value.ToString());&lt;br /&gt; }&lt;br /&gt; public void fromApp(QuickFix.Message value, SessionID session)&lt;br /&gt; {&lt;br /&gt; if (value is QuickFix42.ExecutionReport)&lt;br /&gt; {&lt;br /&gt; QuickFix42.ExecutionReport er = (QuickFix42.ExecutionReport)value;&lt;br /&gt; ExecType et = (ExecType)er.getExecType();&lt;br /&gt; if (et.getValue() == ExecType.FILL)&lt;br /&gt; {&lt;br /&gt; //TODO: implement code&lt;br /&gt; }&lt;br /&gt; }&lt;br /&gt; Console.WriteLine(&quot;Got message from App&quot; + value.ToString());&lt;br /&gt; }&lt;br /&gt; }&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt; /// The main entry point for the application.&lt;br /&gt; /// &lt;/summary&gt;&lt;br /&gt; //[STAThread]&lt;br /&gt; static void Main()&lt;br /&gt; {&lt;br /&gt; ClientInitiator app = new ClientInitiator();&lt;br /&gt; SessionSettings settings = new SessionSettings(@&quot;c:usersnekCodeFIX test Appinitiator.cfg&quot;);&lt;br /&gt; QuickFix.Application application = new ClientInitiator();&lt;br /&gt; FileStoreFactory storeFactory = new FileStoreFactory(settings);&lt;br /&gt; ScreenLogFactory logFactory = new ScreenLogFactory(settings);&lt;br /&gt; MessageFactory messageFactory = new DefaultMessageFactory();&lt;br /&gt; SocketInitiator initiator = new SocketInitiator(application, storeFactory, settings, logFactory, messageFactory);&lt;br /&gt; initiator.start();&lt;br /&gt; Thread.Sleep(3000);&lt;br /&gt; SessionID sessionID = (SessionID)list[0]; QuickFix42.NewOrderSingle order = new QuickFix42.NewOrderSingle(new ClOrdID(&quot;DLF&quot;), new HandlInst(HandlInst.MANUAL_ORDER), new Symbol(&quot;DLF&quot;), new Side(Side.BUY), new TransactTime(DateTime.Now), new OrdType(OrdType.LIMIT));&lt;br /&gt; order.set(new OrderQty(45));&lt;br /&gt; order.set(new Price(25.4d));&lt;br /&gt; Session.sendToTarget(order, sessionID);&lt;br /&gt; Console.ReadLine();&lt;br /&gt; initiator.stop();&lt;br /&gt; }&lt;br /&gt;Steps:&lt;br /&gt;<ul><li>Create application class object i.e. ClientInitiator.
  • 5. Create object of SessionSettings class.
  • 6. Create SocketInitiator class.
  • 7. Run Start method.
  • 8. Create session id.</li></ul>How to Send Order.&lt;br /&gt;Create order object of NewOrderSingle class. Set type of order, symbol,side etc and send order by SendToTarget method.&lt;br /&gt; QuickFix42.NewOrderSingle order = new QuickFix42.NewOrderSingle(new ClOrdID(&quot;DLF&quot;), new HandlInst(HandlInst.MANUAL_ORDER), new Symbol(&quot;DLF&quot;), new Side(Side.BUY), new TransactTime(DateTime.Now), new OrdType(OrdType.LIMIT));&lt;br /&gt; order.set(new OrderQty(45));&lt;br /&gt; order.set(new Price(25.4d));&lt;br /&gt; Session.sendToTarget(order, sessionID);&lt;br /&gt;Receive Order Notification in client&lt;br /&gt;You can receive sent order acknowledgement in FromApp method in application class.&lt;br /&gt;public void fromApp(QuickFix.Message value, SessionID session)&lt;br /&gt; {&lt;br /&gt; if (value is QuickFix42.ExecutionReport)&lt;br /&gt; {&lt;br /&gt; QuickFix42.ExecutionReport er = (QuickFix42.ExecutionReport)value;&lt;br /&gt; ExecType et = (ExecType)er.getExecType();&lt;br /&gt; if (et.getValue() == ExecType.FILL)&lt;br /&gt; {&lt;br /&gt; //TODO: implement code&lt;br /&gt; }&lt;br /&gt; }&lt;br /&gt; Console.WriteLine(&quot;Got message from App&quot; + value.ToString());&lt;br /&gt; }&lt;br /&gt;Start Application&lt;br /&gt;<ul><li>Run FixAccceptor
  • 9. Then Run FixInitiator. This application will send order to FixAcceptor.</li></ul>Fix Initiator&lt;br /&gt;Fix Acceptor&lt;br /&gt;

×