Receiving price updates

When we implement an FIX parser and an FIX composer, we know how tedious and time-consuming this process is. If you choose to implement these parts from scratch, you will need to take care of the network connections, the parsing operations, and the part creating the FIX messages. Because we want to focus on creating a trading system that's capable of working quickly, we will want to use a library where all the functions have already been implemented. There are many commercial FIX libraries available, including NYFIX, Aegisfot – Aethna, Reuters – Traid, and Financial Fusion – Trade Force. The one we will use is called the quickfix library.

This library can be downloaded from http://www.quickfixengine.org/.

This library was created in 2000 and is supported by Java, C++, and Python.

The libraries simplify the developer's role by using callbacks. A callback is a computer engineering term and something that we will be using if we have a task that could take some time to finish. In naive code (code without callbacks), we wait for the end of the execution of this task.

If we use a callback system, the following takes place:

  • We start a task and then proceed to the other tasks while this task keeps running.
  • Once that task has finished, it will call a function to leave the program to handle the result of this task. Let's assume that we have a trading system with many tasks.
  • If one of them is to receive price updates from exchanges, we just use a callback that's triggered once a price update has been received and parsed by the system.
  • Once the callback has been called, we will be able to read the specific fields we need in order to proceed with the rest of our system by using this new price update.

The quickfix library gives the developer the ability to implement specific tasks for any messages that are received by the trading system. The following code describes the general structure of a piece of Python code using the quickfix library:

import sys
import time
import quickfix as fix
import quickfix42 as fix42

class Application(fix.Application):
def onCreate(self, sessionID): return
def
onLogon(self, sessionID):
self.sessionID = sessionID
print ("Successful Logon to session '%s'." % sessionID.toString())
return
def
onLogout(self, sessionID): return
def
toAdmin(self, sessionID, message):return
def
fromAdmin(self, sessionID, message):return
def
toApp(self, sessionID, message):
print "Sent the following message: %s" % message.toString()
return
def
fromApp(self, message, sessionID):
print "Received the following message: %s" % message.toString()
return

The code imports the quickfix library and creates a class called Application that's derived from the fix.Application object. Let's go through this now:

  • The onLogon and onLogout functions are callback functions that are called when a logon/logout message (35=A) has been received and parsed by the system. The argument of the onLogon function is the session ID. It is received when a connection has been successfully established between the acceptor and the initiator.
  • The onCreate function is called when a new session is created to initialize a trading session.
  • The toAdmin and toApp functions are used to modify the messages that are sent to the acceptor.
  • The fromAdmin and fromApp functions are called when we receive a message from the acceptor.
  • The incoming code is the minimal code you need to have an FIX application in Python.

Each FIX application has its own config file. By reading the documentation on the quickfix library, you will learn how to configure the application. We are going to comment on a simple configuration example. The quickfix configuration file is divided into several parts. The DEFAULT part configures the main app attributes:

  • The connection type: Initiator or acceptor
  • The reconnection time: 60 seconds (in this config file)
  • SenderCompIT: The identification of the initiator

The SESSION part describes the FIX message format. In this example, the FIX version that's being used is version 4.1. TargetCompID corresponding to the identification of the acceptor is ARCA. The heartbeat interval is set in this file. This sets a heartbeat that checks whether the acceptor is still alive and has been sent. The network connection is established by using a socket. This socket is created based on the IP address (SocketConnectHost) and the port (SocketConnectPort).

We use a dictionary that defines all the mandatory and optional tags for all the message types:

# default settings for sessions
[DEFAULT]
ConnectionType=initiator
ReconnectInterval=60
SenderCompID=TW
# session definition

[SESSION]
# inherit ConnectionType, ReconnectInterval and SenderCompID from default
BeginString=FIX.4.1
TargetCompID=ARCA
StartTime=12:30:00
EndTime=23:30:00
HeartBtInt=20
SocketConnectPort=9823
SocketConnectHost=123.123.123.123
DataDictionary=somewhere/FIX41.xml

For the upcoming code example, we will use some free open source software code from GitHub. It can be found at https://github.com/gloryofrobots/fixsim. This code is a good example of Python code for initiators and acceptors in terms of the price update and order side of things.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset