Re: [Quickfix-developers] Getting Started with QuickFix and .NET (an uber-simple example)
Brought to you by:
orenmnero
|
From: John K. <kam...@gm...> - 2007-09-27 10:25:43
|
After some research and massive help from John Haldi, I think I've finally understood the basics of Quickfix and the Fix protocol. Note the word "Basics". I've managed to create a simple vb.net app that can connect to the executor example that ships with the QF documentation. I'll go through this process in this post so that other vb.net newbies can understand. I'll start assuming that whoever is reading this knows the difference between Fix and Quickfix. That is, Fix is a protocol and QuickFix is a set of libraries that can allow your app to use the Fix protocol. If anyone notes something that I've stated wrong, please correct me, I'm a newbie after all. :) In QF there are 2 types of apps, Server and Client, referred to as Acceptor and Initiator. The server/acceptor sits back waits for clients to connect, and the client connects to a server. The vb.net executor example is an Acceptor, there is no vb.net example of an initiator. And thats what I'll create here step by step. Note that the example will create doesnt do anything, it just connects and receives heartbeats. Lets start with the config file. The config file is where the Qf engine will get its settings from, similar to the asp.net web.config file. I wont go into the details of each property (these you can get from the documentation). Below is the config file the initiator/client will use. [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.4 SenderCompID=CLIENT1 TargetCompID=EXECUTOR SocketConnectPort=5001 Save this somewhere say, c:\fixconfig\myconfig.cfg, we'll need it in a moment. QuickFix needs a way to communicate with your app, to do this it needs an object that implements an interface that it knows about namely QuickFix.Application. In the app, we'll create an object that implements this interface, initialise it and wait for QF to send us messages. Ok, moving on, Open VS2005 and Create a vb.net console app, by default the IDE will create for a Module named Module1.vb and the main entry point Sub Main(). Replace the whole Module with this. Module Module1 'QuickFIX engine environment variables Public mySessionSettings As SessionSettings Public myApplication As Application Public myStoreFactory As FileStoreFactory Public myMessageFactory As DefaultMessageFactory Public myLogFactory As ScreenLogFactory Public myInitiator As SocketInitiator Public LoggedOn As Boolean Sub Main() mySessionSettings = New SessionSettings("c:\fixconfig\myconfig.cfg") myApplication = New Application myStoreFactory = New FileStoreFactory(mySessionSettings) myMessageFactory = New DefaultMessageFactory myLogFactory = New ScreenLogFactory(True, True, True) myInitiator = New SocketInitiator(myApplication, myStoreFactory, mySessionSettings, myLogFactory, myMessageFactory) myInitiator.start() Do 'Find something to do while waiting Loop While LoggedOn = False End Sub End Module In Sub main you see we create a couple of objects, first the SessionSettings, this will feed QF our settings, the ones we created earlier. Second is the key object, Application, which implements the QuickFix.Application interface, this is the object that QF will use to send messages to us. We need to create it, so, below the module, paste this. Public Class Application Implements QuickFix.Application Public Sub New() End Sub Public Sub fromAdmin(ByVal Param As QuickFix.Message, ByVal Param1 As QuickFix.SessionID) Implements QuickFix.Application.fromAdmin System.Console.WriteLine("fromAdmin: " & Param.GetType.FullName) End Sub Public Sub fromApp(ByVal Param As QuickFix.Message, ByVal Param1 As QuickFix.SessionID) Implements QuickFix.Application.fromApp System.Console.WriteLine("IN: " & Param.GetType.FullName) End Sub Public Sub onCreate(ByVal Param As QuickFix.SessionID) Implements QuickFix.Application.onCreate System.Console.WriteLine("onCreate") End Sub Public Overloads Sub onMessage(ByVal message As QuickFix44.ExecutionReport, ByVal Param As QuickFix.SessionID) System.Console.WriteLine() End Sub Public Sub onLogon(ByVal Param As QuickFix.SessionID) Implements QuickFix.Application.onLogon System.Console.WriteLine("Logon: " & Param.GetType.FullName) End Sub Public Sub onLogout(ByVal Param As QuickFix.SessionID) Implements QuickFix.Application.onLogout System.Console.WriteLine("Logout") End Sub Public Sub toAdmin(ByVal message As QuickFix.Message, ByVal Param1 As QuickFix.SessionID) Implements QuickFix.Application.toAdmin System.Console.WriteLine("toAdmin: " & message.GetType.FullName) End Sub Public Sub toApp(ByVal Param As QuickFix.Message, ByVal Param1 As QuickFix.SessionID) Implements QuickFix.Application.toApp System.Console.WriteLine("toApp: " & Param.GetType.FullName) End Sub End Class Note that in sub main I have an infinite loop, this means the app will never exit so, if you want it to, change that code so that you exit once you've done all you needed to do. This is all the code you need to AT LEAST connect to executor. The main thing you need to know about, is how to receive messages. This is done by overriding the onMessage method. When you know what type of message you want to receive (like in the case of executor, an ExecutionReport), you just override the method and wait. However, executor will only send you this, when you've submitted a new order using the NewOrderSingle object. >From the example you (the newbie) should easily realise how to do this from a Winforms app, and therefore add some interactivity to your app. Note however that you can't just set text to controls directly, you'll have to use a delegate and the Invoke method of the control. Similar to what you'd do if you were writing a multi-threaded winforms app. I know its not much, but I hope at least vb.net newbies can at least gain something from it. If you feel there is more I should add, just let me know, especially if it adds to the value of this example. On a final note, Qf is extremely powerful, but for newbies like me who don't know c++, it was a hellishly hard just getting these few lines of code to work. It was kind of like learning the theory of relativity in Latin. Thanks John K. -- View this message in context: http://www.nabble.com/Getting-Started-with-QuickFix-and-.NET-tf4508939.html#a12918488 Sent from the QuickFIX - Dev mailing list archive at Nabble.com. |