Search code examples
quickfixfix-protocol

Quickfix Float Fields


I would like to know the right way to read float fields using Quickfix (python). I was getting a string then casting to float.

For instance:

>>> m = fix.Message()
>>> m.setField(fix.BidPx(1.12))
>>> m.getField(fix.BidPx()).getString()
'1.12'
>>> float(m.getField(fix.BidPx()).getString())
1.12

The way above works fine for floats with less then 15 digits of precision. But I got the following error for float numbers with more the 15 digits of precision:

>>> m = fix.Message()
>>> m.setField(fix.BidPx(1.123456789123456))
>>> m.getField(fix.BidPx()).getString()
'\x00\xe1}\xf5\x82U\x00\x0078912346'

>>> float(m.getField(fix.BidPx()).getString())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:

Solution

  • I am not sure if that sample works, may be you should explain how you import "fix". Any way, this sample works with python 3.7 and quickfix 1.15.1

    >>> import quickfix as fix
    >>> m = fix.Message()
    >>> m.setField(fix.BidPx(1.123456789123456789123456789))
    >>> m.getField(fix.BidPx().getField())
    '1.12345678912346'
    >>>
    

    if you need more precision in the float number, you can do

    >>> m.setField(fix.StringField(fix.BidPx().getField(),"1.123456789123456789123456789"))
    >>> m.getField(fix.BidPx().getField())
    '1.123456789123456789123456789'
    >>>
    

    I hope I've helped