Search code examples
javaquickfixj

QuickFIX/J Group vs Component


What is the difference, correct or best code to do it?
Both version of code work good but I think that the components, maybe, has some useful thing that I can't understand.

First:

MarketDataRequest m = new MarketDataRequest();
quickfix.fix50sp2.component.MDReqGrp cmp = new quickfix.fix50sp2.component.MDReqGrp();
quickfix.fix50sp2.MarketDataRequest.NoMDEntryTypes group = new quickfix.fix50sp2.MarketDataRequest.NoMDEntryTypes();

  cmp.addGroup(group);
  m.set(cmp);

Second:

MarketDataRequest m = new MarketDataRequest();
quickfix.fix50sp2.MarketDataRequest.NoMDEntryTypes group = new quickfix.fix50sp2.MarketDataRequest.NoMDEntryTypes();

  m.addGroup(group)

Solution

  • It does not make a big difference for the example that you have given. You only have to make one more indirection to add the component to the message instead of adding the repeating group directly.

    However, there are components which contain more than just one repeating group. Take a look at the InstrmtMDReqGrp for example which contains some sub components and repeating groups.

    One advantage of using components over single fields/groups shows when wanting to copy fields. You could then use the component's method copyFrom(FieldMap fields) or copyTo(FieldMap fields) respectively. These methods will copy all of the component's fields and groups in one go.

    Example:

        @Test
        public void testComponent() throws Exception {
            final Instrument instrument = new Instrument();
            instrument.set(new Symbol("DELL"));
            instrument.set(new CountryOfIssue("USA"));
            instrument.set(new SecurityType(SecurityType.COMMON_STOCK));
    
            final quickfix.fix44.NewOrderSingle newOrderSingle = new quickfix.fix44.NewOrderSingle();
            newOrderSingle.set(instrument);
    
            final quickfix.fix44.ExecutionReport executionReport = new quickfix.fix44.ExecutionReport();
            executionReport.setComponent(newOrderSingle.getInstrument());
    
            System.out.println("NOS: " + newOrderSingle.toString().replace('\001', '|'));
            System.out.println("ER:  " + executionReport.toString().replace('\001', '|'));
        }
    

    Output:

    NOS: 8=FIX.4.4|9=28|35=D|55=DELL|167=CS|470=USA|10=233|
    ER:  8=FIX.4.4|9=28|35=8|55=DELL|167=CS|470=USA|10=221|