Showing posts with label QuickFix/J example. Show all posts
Showing posts with label QuickFix/J example. Show all posts

Saturday, December 3, 2011

QuickFix/J based Fix Protocol enabled trading client


Purpose of this post is to make available a simple sample application for the people who either wants to start learning or want to develop of FIX (FIX protocol) based trading client.

FIX Acceptor ( which acts as the Stock Exchange)
FIX Initiator ( which acts as the Trading Client)

Its just a simple client without any GUI.
People who are already aware about the FIX and just want to try the sample app in java can jump directly to the example, rest of all people can take a pain to read the following:

Some jargons about the FIX protocol:
The Financial Information eXchange ("FIX") Protocol 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 ( from Fix Protocol Organization).










Some GYAAN:
If every exchange in the world communicates "ONLY" with their own developed messages (Proprietary Protocol ) than for every exchange there will be need to develop seperately the trading client infrastructure or trading client platform. This is a painful work as to maintain a client with the proprietary messaging or Proprietary Protocol (Message) for every exchange. And keeping the client in the accordance with the exchange notification(s) for upgrades of messages to communicate is a big problem.
And in that case doing a trade with client which supports multiple exchange at the same time seems far from the reality. Of course a client can manage support for few exchange but a large number of exchange will not be possible ( which is the need in the today's world).

With FIX based application its easy to develop OMS ( Order Management System) and EMS ( Execution Management System) faster and scalable. It can be used for Algo Trading but its gonig to be bit slower, so for Algorithmic Trading Proprietary Protocol is a good choice.


A typical FIX message looks like as:
br />
<20111204-11:02:59, FIX.4.2:FixClient8019->FixAcceptor, outgoing>(8=FIX.4.2|9=80|35=A|34=14|49=FixClient8019|52=20111204-11:02:59.353|56=FixAcceptor|98=0|108=60|10=219|)
where every tag has a specific meaning, in the above tags are 8,9,35,34,49,52 etc.

Lets jump to the FIX based Acceptor and Iniator.

FixAcceptor: Which listens on a specific socket and works as a server. It is the Stock Exchange. Number of clients can connect to it. All the properties for the Acceptor is defined in the acceptor.cfg file.
FixInitiator: Which connects to the FixAcceptor on a specific port defined by Acceptor. It is a trading client. Client can connect with different fix implemenation versions which a client and exchange supports. I am using FIX.4.2 version for the example.

Explanation about the .cfg and fix message is beyond the scope of this blog. ha ha ha, i saved some time. Dirty trick to escape but i have no choice as this is a vast subject and neither i have time nor the experities to cover everything.

Create FixAcceptor.java and FixIniator.java either by implementing Application interface or by extending ApplicationAdapter class.

First some snippet from Exchange i.e. FixAcceptor.java

public class FixAcceptor extends MessageCracker implements Application





Start the acceptor based on the configuration defined in acceptor.cfg

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

onMessage(...) : there are many overloaded onMessage() methods are defined in the api, for the purpose of example i have overridden

@Override
public void onMessage(NewOrderSingle order, SessionID sessionID)
 throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
...
}
exchange checks for the match for the requested order by the client and in case of match it process the order and updates the client with the execution report. Have a look in the example to see the implementation.



Now some snippet from Trading Client i.e. FixIniator.java
Here i am extending ApplicationAdapter instead of implementing Application as its done in FixAcceptor.java




public class FixInitator extends ApplicationAdapter
Start the initiatorr based on the configuration defined in initiator.cfg
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();

// prepare order NewOrderSingle order = new NewOrderSingle( ...)
try {
// send order to target which is nothing but the exchange/acceptor
    Session.sendToTarget(order, sessionID);
    } catch (SessionNotFound e) {
    e.printStackTrace();
}

I have overridded method fromAdmin(...) where client will receive from the admin where one of the message is the ExecutionReport which is sent by exchange/acceptor.
@Override
public void fromApp(Message message, SessionID sessionId)

COMPLETE EXAMPLE CAN BE DOWNLOADED FROM HERE

Heroku Custom Trust Store for SSL Handshake

  Working with Heroku for deploying apps (java, nodejs, etc..) is made very easy but while integrating one of the service ho...