API Documentation

FixMessage

class pyfixmsg.fixmessage.FixMessage(*args, **kwargs)[source]

Bases: pyfixmsg.fixmessage.FixFragment

Simple dictionary-like object, for use with FIX raw messages. Note that the tags are converted (when possible) to integers, and that the values are kept as strings. The default separator is ;, but can be specified. Check the definition of load_fix() for details.

Example:
>>> fix = FixMessage()
>>> fix.load_fix(line)
>>> #print(fix)
{6: '0', 8: 'FIX.4.2',
10: '100', 10481: 'A', 14: '0',
15: 'EUR', 10641: 'blabla',
18: '1', 21: '2', 22: '5',
151: '1',
11951: 'HOOF7M0f4BGJ0rkaNTkkeAA',
....

FixMessage also have a time attribute, a direction attribute (inbound : 0, outbound : 1) and a recipient which is rather where it’s been received from or sent to. FixMessages sort by default on time, and will be considered equal if the dictionary values are the same AND the time is the same.

This FixMessage is eager : it will parse the whole fix and store it locally. It is significantly faster in most usage patterns that we observed.

useful shortcut methods :

fix.tag_exact(tag, value)
fix.tag_iexact(tag, value)
fix.tag_contains(tag, value)
fix.tag_icontains(tag, value)
fix.tag_match_regex(tag, value)

Note : the tag_* methods don’t support repeating groups

FragmentType

alias of FixFragment

__init__(*args, **kwargs)[source]

The constructor uses the dict() signature unmodified. You can set the following manually or through a factory function:

  • self.process an opaque value (to store the process that received or processed the message, defaults to empty string).
  • self.separator the default separator to use when parsing messages. Defaults to ';'
  • self.time the time the message has been created or received. Defaults to datetime.utcnow()
  • self.recipient opaque value (to store for whom the message was intended)
  • self.direction Whether the message was received (0), sent (1) or unknown (None)
  • self.typed_values Whether the values in the message are typed. Defaults to False
  • self.raw_message If constructed by class method from_buffer, keep the original format
  • self.codec Default Codec to use to parse message. Defaults to a naive codec that doesn’t support repeating groups
apply(update)[source]

equivalent to update() but if any value in the update dictionary is None and the tag is present in the current message, that tag is removed. Note: naive about repeating groups

Parameters:update (dict) – map of values to update the state with.
calculate_checksum()[source]

calculates the standard fix checksum

checksum(value=None)[source]

FIX checksum

copy()[source]

Copy interface without using the copy module

fix

Legacy compatibility, will be removed shortly

classmethod from_buffer(msg_buffer, fix_codec)[source]

Create a FixMessage from a buffer and a codec

Parameters:
  • msg_buffer (str) – a buffer as a string
  • fix_codec (Codec) – an object with static encode() and decode() calls
Returns:

a FixMessage object

Return type:

FixMessage

from_wire(msg, codec=None)[source]

Extract from a wire representation according to a codec

get_raw_message()[source]

Return the original string from which the fix message was constructed

load_fix(string, process=None, separator=';')[source]

Parses a FIX message from a string using default codecs and separators.

Parameters:
  • string (bytes) – the string containing the FIX message to be parsed
  • process (unicode) – Optional originator of the FIX message
  • separator (unicode) – Character delimiting “tag=val” pairs. Optional. By default this is a ‘;’ character. Specify pyfixmsg.SEPARATOR when parsing standard FIX.
Returns:

A parsed fix message

Return type:

FixMessage

output_fix(separator=';', calc_checksum=True, remove_length=False)[source]

ouputs itself as a vanilla FIX message. This forces the output to String fix but tries to reuse the spec from the current codec

set_len_and_chksum()[source]

Assign length and checksum based on current contents

set_or_delete(tag, value)[source]

Sets the tag if value is neither None or the empty string. Deletes the tag otherwise. Only works on top-level tags (not inside repeating groups)

tag_contains(tag, value, case_insensitive=False)[source]

Returns True if self[tag] contains value. Returns False otherwise, or if the tag doesnt exist This is a string string comparison

tag_exact(tag, value, case_insensitive=False)[source]

Returns True if self[tag] has the exact value. Returns False if the tag doesnt exist or is not exact

tag_exact_dict(dictionary)[source]

check that all the keys and values of the passed dict are present and identical in the fixmsg

tag_ge(tag, value)[source]

Test tag is greater or equal to value. Uses decimal comparison if possible. Returns False if tag absent

tag_gt(tag, value)[source]

Test tag is greater than value. Uses decimal comparison if possible. Returns False if tag absent

tag_icontains(tag, value)[source]

case-insensitive version of tag_contains

tag_iexact(tag, value)[source]

Returns True if self[tag] has the exact value (case insensitive). Returns False if the tag doesnt exist or is not exact

tag_in(tag, values)[source]

returns True if self[tag] is in values, false otherwise or if the tag doesnt exist

tag_le(tag, value)[source]

Test tag is smaller or equal value. Uses decimal comparison if possible. Returns False if tag absent

tag_lt(tag, value)[source]

Test tag is smaller than value. Uses decimal comparison if possible. Returns False if tag absent

tag_match_regex(tag, regex)[source]

returns True of self[tag] matches regex, false otherwise or if the tag doesnt exist

tags

Note: this property is there to replace a self reference that existed before.

Deprecated.

to_wire(codec=None)[source]

Return wire representation according to a codec

update_all(tag, value)[source]

this will force a tag (that already exists!) to a value at all appearances

class pyfixmsg.fixmessage.FixFragment(*args, **kwargs)[source]

Bases: dict

Type designed to hold a collection of fix tags and values. This type is used directly for the contents of repeating groups. Whole fix messages are parsed from their wire representation to instances of the FixMessage type which inherits from this type.

__init__(*args, **kwargs)[source]

FixFragment constructor.

all_tags()[source]

Returns a list of all the tag keys in this message, including flattened tags that are only present in repeating groups. The same tag will not appear twice in the list.

Returns:A list of tag keys (usually strings or ints)
Return type:list
anywhere(tag)[source]

returns true if the tag is in the message or anywhere inside any contained repeating group

find_all(tag)[source]

Generator. Find all instances of the tag in the message or inside repeating groups and returns the path to them one at a time.

Example, navigate all paths for a given tag:
>>> for path in msg.find_all(self, tag):
...   # path here is a list of ints or str keys
...   path_msg = msg
...   for key in path:
...     path_msg = path_msg[key]
...     # [...] do something at each level in the path
...   path_msg[tag] = # [...] do something with the last level of the path
Returns:a generator of paths where each path is a list of string or integer indices into the message
Return type:Generator of list of int or str
classmethod from_dict(tags_dict)[source]

Create a FixMessage from a dictionary.

Parameters:tags_dict (dict of int to str or int or float or long) – dictionary of FIX tags to values
Returns:a FixMessage object
Return type:FixMessage
length()[source]

Length of the body of the message in bytes

Repeating groups

class pyfixmsg.RepeatingGroup(*args, **kwargs)[source]

Bases: list

Implementation of repeating groups for pyfixmsg.FixMessage. The repeating group will look like {opening_tag:[FixMessage,FixMessage]} in the fix message a repeating group behaves like a list. You can add two repeating groups, or append a FixMessage to one.

__init__(*args, **kwargs)[source]

Maintains list’s signature unchanged.

Sets * self.number_tag (the tag that contains the number of elements in the group) * self.first_tag (the first repeated tag) * self.standard (reserved)

all_tags()[source]

Returns a list of all the tag keys in any member of this repeating group, The same tag will not appear twice in the generated sequence. The count tag for the repeating group is not included, it is considered as part of the parent message. Order is not guaranteed. @return: A list of tag keys (usually strings or ints) @rtype: C{list}

classmethod create_repeating_group(tag, standard=True, first_tag=None)[source]

creates a group with member. Can’t use __init__ as it would mean overriding the list __init__ which sounds dangerous

entry_tag

returns the entry tag for the group and its value as a tuple

find_all(tag)[source]

Generator. Find all instances of the tag in the message or inside repeating groups and returns the path to them one at a time.

Example, navigate all paths for a given tag:
>>> for path in msg.find_all(self, tag):
...   # path here is a list of ints or str keys
...   path_msg = msg
...   for key in path:
...     path_msg = path_msg[key]
...     # [...] do something at each level in the path
...   path_msg[tag] = # [...] do something with the last level of the path

@return: a generator of paths where each path is a list of string or integer indices into the message @rtype: Generator of C{list} of C{int} or C{str}

length()[source]

Length of the body of the message in bytes

class pyfixmsg.RepeatingGroupFactory(tag, standard=True, first_tag=None)[source]

Bases: object

An easy way to create a repeating group for a given tag, without having to define all the tags yourself, takes the standard ones

__init__(tag, standard=True, first_tag=None)[source]

x.__init__(…) initializes x; see help(type(x)) for signature

get_r_group(*fix_messages)[source]

factory method. I’m not familiar with the factory design pattern, it shows ;-)

Codec

class pyfixmsg.codecs.stringfix.Codec(spec=None, no_groups=False, fragment_class=<type 'dict'>, decode_as=None, decode_all_as_347=False)[source]

Bases: object

FIX codec. Initialise with a FixSpec to support repeating groups.

This class is used to transform the serialised FIX message into an instance of fragment_class, default dict Tags are assumed to be all of type int, repeating groups are lists of fragment_class

Values can either bytes or unicode or a mix thereof, depending on the constructor arguments.

__init__(spec=None, no_groups=False, fragment_class=<type 'dict'>, decode_as=None, decode_all_as_347=False)[source]
Parameters:
  • spec – the FixSpec instance to use to parse messages. If spec is not defined repeating groups will not be parsed correctly, and the logic to handle encoded tags will not be functional.
  • no_groups – set to True to ignore repeating groups
  • fragment_class – Which dict-like object to return when parsing messages. Also sets the type of members of repeating groups
  • decode_as – what encoding to decode all tags. Defaults to None, which returns the raw byte strings. setting to a non-None value makes both non-numerical tags and values to be unicode, using this value for decode.
  • decode_all_as_347 – whether to trust tag 347 to decode all other tags or only the Encoded* ones. If set to False, use 347 normally for Encoded* tags, respect decode_as for all other tags. If 347 is not present on the message, the values are left encoded.
parse(buff, delimiter='=', separator='\x01')[source]

Parse a FIX message. The FIX message is expected to be a bytestring and the output is a dictionary-like object which type is determined by the fragment_class constructor argument and which keys are int and values unicode. Note that if there is a non-int tag in the message, this will be stored as a key in the original format (i.e. bytestring)

Parameters:
  • buff (bytestr or unicode) – Buffer to parse
  • delimiter (unicode) – A character that separate key and values inside the FIX message. Generally ‘=’. Note the type: because of the way the buffer is tokenised, this needs to be unicode (or str in python 2.7*).
  • separator (unicode) – A character that separate key+value pairs inside the FIX message. Generally ‘’. See type observations above.
serialise(msg, separator='\x01', delimiter='=', encoding=None)[source]

Serialise a message into a bytestring.

Parameters:
  • msg (dict-like interface) – the message to serialse
  • delimiter – as in parse()
  • separator – as in parse()
  • encoding (str) – encoding mode

FixTag

class pyfixmsg.reference.FixTag(name, tag, tagtype=None, values=())[source]

Bases: object

Fix tag representation. A fix tag has name, tag (number), type and valid values (enum)

__init__(name, tag, tagtype=None, values=())[source]
Parameters:
  • name (str) – Tag name
  • tag (int) – Tag number
  • tagtype (str) – Type as in quickfix’s XML reference documents
  • values (tuple((str, str)) with the first element of each tuple being the value of the enum, the second the name of the value.) – The valid enum values
add_enum_value(name, value)[source]

Add a value to the tag’s enum values.

del_enum_value(name=None, value=None)[source]

Delete a value from the tag’s enum values. Specify name or value using keyword arguments. If specifying both, they must both match the known name and value otherwise ValueError is raised.

enum_by_name(name)[source]

Retrieve an enum value by name

enum_by_value(value)[source]

Retrieve an enum value by value

FixSpec

class pyfixmsg.reference.FixSpec(xml_file, eager=False)[source]

Bases: object

A python-friendly representation of a FIX spec. This class is built from an XML file sourced from Quickfix (http://www.quickfixengine.org/).

It contains the Message Types supported by the specification, as a map (FixSpec.msg_types) of message type value (‘D’, ‘6’, ‘8’, etc..) to MessageType class, and all the fields supported in the spec as a TagReference instance (FixSpec.tags) which can be accessed by tag name or number.

__init__(xml_file, eager=False)[source]
Parameters:
  • xml_file (str) – path to a quickfix specification xml file
  • eager (bool) – whether to eagerly populate tags maps for speedy lookup or only on first access