Michelle Queen
Michelle Queen

Reputation: 249

How to parse repeating groups efficiently in Java with the FIX protocol?

I am interested in parsing FIX messages with repeating groups. Can someone provide examples of how this is done in the API level with the major FIX engines and discuss how this can be done efficiently in the implementation level?

As an example, I am providing a FIX repeating group:

453 NoPartyIDs
448 PartyID
447 PartyIDSource
452 PartyRole

453=3 {448=a&447=1&452=11, 448=b&447=2&452=22, 448=c&447=3&452=33}  

Upvotes: 3

Views: 4409

Answers (1)

rdalmeida
rdalmeida

Reputation: 1853

So repeating groups are not cheap to parse and it is a known weakness of the FIX protocol. Fortunately most of the time sensitive FIX messages (like ExecutionReport) will not contain any repeating group. It gets even worse when you have repeating groups inside repeating groups.

The trick is to try to parse repeating groups only for those message types that you know can contain repeating groups, ignoring repeating groups for all other message types when it comes to parsing.

Below an example of how you can parse repeating groups with CoralFIX:

    FixParser parser = new FixParser();

    // define a repeating group for a message type:
    parser.addGroupTemplate(QuoteStatusRequest, NoRelatedSym, 
                            Symbol, FutSettDate, SecurityDesc);

    FixMessage fixMsg = parser.parse(byteBufferWithFixMessage);

    fixGroup = fixMsg.getGroup(NoRelatedSym);

    System.out.println(fixGroup.getNumberOfElements()); // => 4

    // you can also print the whole fix group for debugging purposes:
    System.out.println(fixGroup);
    // Symbol(55)=AAPL FutSettDate(64)=1 SecurityDesc(107)=blah1 | Symbol(55)=IBM FutSettDate(64)=2 SecurityDesc(107)=blah2 | Symbol(55)=LDK FutSettDate(64)=3 SecurityDesc(107)=blah3 | Symbol(55)=FB FutSettDate(64)=4 SecurityDesc(107)=blah4

    System.out.println();

    Iterator<FixGroupElement> iter = fixGroup.iterator();

    while(iter.hasNext()) {

        FixGroupElement elem = iter.next();

        // fetch fix tag values as usual:
        System.out.println(elem.getString(Symbol));
        System.out.println(elem.getInt("FutSettDate"));
        System.out.println(elem.getString(107));

        // you can also print the whole element for debugging purposes:
        System.out.println(elem);
        System.out.println();
    }
}

So you can see above that we are explicitly indicating to the parser that the QuoteStatusRequest message type can contain a repeating group defined by tag NoRelatedSym and the fix tags Symbol, FutSettDate and SecurityDesc. Other message types won't have any overhead in the parser due to this repeating group. You can read more about the source code above here.

Now when it comes to implementation details, you need recursion, especially to handle repeating groups inside repeating groups. Recursion makes the implementation much more clear to follow and understand. This can also be done by iteration but the code gets too much complicated for just a little bit more of performance (nanos). It would make more difference if you had too many repeating groups inside repeating groups. I particularly have only seen 2 levels of nesting when it comes to FIX repeating groups, but the protocol allows any level of nesting.

Disclaimer: I am one of the developers of CoralFIX.

Upvotes: 2

Related Questions