Skip to main content

aiopyfix

Project description

AIOPyFIX Build Status

Open Source implementation of a FIX (Financial Information eXchange) Engine implemented in Python asyncio

See here [http://fixprotocol.org/] for more information on what FIX is.

Installation

This package requires Python 3.6 to run.

Install in the normal python way

pip install aiopyfix

or from source

python setup.py install

and it should install with no errors

Usage

Using the module should be simple. There is an examples directory, which is the probably best place to start.

Session Setup

Either you can create a FIXClient or a FIXServer. The Client initiates the connection and also initaiates the Logon sequence, a Server would sit there waiting for inbound connections, and expect a Logon message to be sent.

self.client = FIXClient("aiopyfix.FIX44", "TARGET", "SENDER")

# tell the client to start the connection sequence
await self.client.start('localhost', int("9898"), loop)

The argument "aiopyfix.FIX44" specified the module which is used as the protocol, this is dynamically loaded, to you can create and specify your own if required.

If you want to do something useful, other than just watching the session level bits work, you'll probably want to register for connection status changes (you'll need to do this be fore starting the event loop);

self.client.addConnectionListener(self.onConnect, ConnectionState.CONNECTED)
self.client.addConnectionListener(self.onDisconnect, ConnectionState.DISCONNECTED)

The implementatino of thouse methods would be something like this;

async def onConnect(self, session):
    logging.info("Established connection to %s" % (session.address(), ))
    session.addMessageHandler(self.onLogin, MessageDirection.INBOUND, self.client.protocol.msgtype.LOGON)

async def onDisconnect(self, session):
    logging.info("%s has disconnected" % (session.address(), ))
    session.removeMsgHandler(self.onLogin, MessageDirection.INBOUND, self.client.protocol.msgtype.LOGON)

in the code above, we are registering to be called back whenever we receive (MessageDirection.INBOUND) a logon request MsgType[35]=A on that session.

That is pretty much it for the session setup.

Message construction and sending

Constructing a message is simple, and is just a matter of adding the fields you require. The session level tags will be added when the message is encoded by the codec. Setting any of the following session tags will result in the tag being duplicated in the message

  • BeginString
  • BodyLength
  • MsgType
  • MsgSeqNo
  • SendingTime
  • SenderCompID
  • TargetCompID
  • CheckSum

Example of building a simple message

async def sendOrder(self, connectionHandler):
    self.clOrdID = self.clOrdID + 1
    # get the codec we are currently using for this session
    codec = connectionHandler.codec

    # create a new message
    msg = FIXMessage(codec.protocol.msgtype.NEWORDERSINGLE)

    # ...and add some data to it
    msg.setField(codec.protocol.fixtags.Price, random.random() * 1000)
    msg.setField(codec.protocol.fixtags.OrderQty, int(random.random() * 10000))
    msg.setField(codec.protocol.fixtags.Symbol, "VOD.L")
    msg.setField(codec.protocol.fixtags.SecurityID, "GB00BH4HKS39")
    msg.setField(codec.protocol.fixtags.SecurityIDSource, "4")
    msg.setField(codec.protocol.fixtags.Symbol, "VOD.L")
    msg.setField(codec.protocol.fixtags.Account, "TEST")
    msg.setField(codec.protocol.fixtags.HandlInst, "1")
    msg.setField(codec.protocol.fixtags.ExDestination, "XLON")
    msg.setField(codec.protocol.fixtags.Side, int(random.random() * 2))
    msg.setField(codec.protocol.fixtags.ClOrdID, str(self.clOrdID))
    msg.setField(codec.protocol.fixtags.Currency, "GBP")

    # send the message on the session
    await connectionHandler.sendMsg(codec.pack(msg, connectionHandler.session))

A message (which is a subclass of FIXContext) can also hold instances of FIXContext, these will be treated as repeating groups. For example

msg = FIXMessage(codec.protocol.msgtype.NEWORDERSINGLE)
msg.setField(codec.protocol.fixtags.Symbol, "VOD.L")
msg.setField(codec.protocol.fixtags.SecurityID, "GB00BH4HKS39")
msg.setField(codec.protocol.fixtags.SecurityIDSource, "4")

rptgrp1 = FIXContext()
rptgrp1.setField(codec.protocol.fixtags.PartyID, "It's Me")
rptgrp1.setField(codec.protocol.fixtags.PartyIDSource, "1")
rptgrp1.setField(codec.protocol.fixtags.PartyRole, "2")

msg.addRepeatingGroup(codec.protocol.fixtags.NoPartyIDs, rptgrp1)

rptgrp2 = FIXContext()
rptgrp2.setField(codec.protocol.fixtags.PartyID, "Someone Else")
rptgrp2.setField(codec.protocol.fixtags.PartyIDSource, "2")
rptgrp2.setField(codec.protocol.fixtags.PartyRole, "8")
msg.addRepeatingGroup(codec.protocol.fixtags.NoPartyIDs, rptgrp2)

This will result in a message like the following

8=FIX.4.4|9=144|35=D|49=sender|56=target|34=1|52=20150619-11:08:54.000|55=VOD.L|48=GB00BH4HKS39|22=4|453=2|448=It's Me|447=1|452=2|448=Someone Else|447=2|452=8|10=073|

To send the message you need a handle on the session you want to use, this is provided to you in the callback methods. e.g. in the code above we registered for Logon callbacks using

session.addMessageHandler(self.onLogin, MessageDirection.INBOUND, self.client.protocol.msgtype.LOGON)

the signature for the callback is something like;

async def onLogin(self, connectionHandler, msg):
    logging.info("Logged in")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiopyfix-0.4.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

aiopyfix-0.4-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file aiopyfix-0.4.tar.gz.

File metadata

  • Download URL: aiopyfix-0.4.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.0

File hashes

Hashes for aiopyfix-0.4.tar.gz
Algorithm Hash digest
SHA256 0bcfacd4c67cb0ee72b1c10f519f0a6375f54c74a985264e24ab01a7247ddcac
MD5 1b82f930616dc18384e2003c02377ab8
BLAKE2b-256 677c0ff6d0631fdde57ee0aaca5ebc20411aef858d605d5bf26dbfe53c591cba

See more details on using hashes here.

File details

Details for the file aiopyfix-0.4-py3-none-any.whl.

File metadata

  • Download URL: aiopyfix-0.4-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.0

File hashes

Hashes for aiopyfix-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 87fdab680ad623a48feef40e66f75aa27f3f31c9a658627b7b0feb477d6041a7
MD5 b4afe37fb809bc8ed6fb4e22cfef26b5
BLAKE2b-256 d0c8b25658100ad203c41729fc9d40ff1bbcb95d23038c1b631c0fc350e74607

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page