Repeating Groups
Fields with the same tag number can appear multiple times in the same message within a so-called repeating group.
The following diagram shows the general structure of a FIX message with a repeating group:
Number Of Instances Field
Each repeating group starts from the field that identifies the number of
repeated instances within the repeating group (so-called number of instances field). This field must immediately
precede the repeating group contents. In the referenced example, the number of repeated instances is defined by the NoRoutingIDs
field.
Changing the value of this field affects the repeating group length.
Removing this field from a message or parent repeating group removes all entries of the repeating group.
Leading Field
Each instance of a repeating group has a selected field that identifies the beginning of a new instance (so-called leading field). This field must occur before any other fields from a single repeating group instance.
In other words, these fields separate repeated instances from each other. In the referenced example, this is the RoutingType
field.
Repeating Group Structure
- In a correctly defined FIX message, all repeating group fields follow one another. The appearance of an arbitrary field that does not belong to the repeating group (according to the FIX specification) automatically indicates the end of the repeating group. Therefore, the further appearance of fields from the repeating group is disallowed by the FIX Standard.
- It is possible for a repeating group to appear inside of another repeating group (so-called nested repeating group). In this case, all instances of the inner repeating group belong to the single instance of the outer repeating group.
- The number of repeating group instances that are defined by the leading field value must correspond to the actual number of instances.
Group class
The Group class encapsulates a repeating group.
Adding Group to Message
To create a new repeating group or modify the number of repeated instances, use the SetGroup(int, int) method.
For example:
// Create a 'Market Data Request' message.
var request = new Message(MsgType.MarketDataRequest, ProtocolVersion.Fix44);
// Sets up regular fields.
request.Set(Tag.MDReqID, "ABSD");
request.Set(Tag.SubscriptionRequestType, 1);
request.Set(Tag.MarketDepth, 1);
request.Set(Tag.MDUpdateType, 1);
request.Set(Tag.AggregatedBook, "N");
// Creates a repeating group (NoMDEntryTypes) with two instances.
Group mdEntryTypes = request.SetGroup(Tag.NoMDEntryTypes, 2);
Note
Setting a non-zero integer value to the number of instances field implicitly changes the length of the repeating group object.
Accessing Repeating Group
To obtain a reference to the Group object that represents an existing repeating group, use the GetGroup(int) method.
Group relatedSymbols = request.GetGroup(Tag.NoRelatedSym);
Accessing Repeating Group Fields
The Group class works with fields, as well as with nested repeating groups in the same manner as the Message class. The only difference in accessing field values is the availability of an additional parameter that defines the index of the repeated instance. Indexing starts from zero.
For example:
// Set fields in the first repeated instance.
mdEntryTypes.Set(Tag.MDEntryType, 0, "EntryType_0");
// .. and the same for the second repeated instance.
mdEntryTypes.Set(Tag.MDEntryType, 1, "EntryType_1");
The Group class also supports the IEnumerable
interface; therefore a Group object could be used in the foreach
operator to enumerate repeated instances.
For example:
foreach (GroupInstance instance in mdEntryTypes)
{
string type = instance.Get(Tag.MDEntryType);
}
Removing Repeating Group from Message
To remove a repeating group, use the Remove(int) method with the number of instances field tag.
Detected Ill-Formed Repeating Group
In a well-formed repeating group, the declared number of repeated instances (defined by the number of instances field) should be equal to the actual number of instances.
By default, the FIX Engine reports an error if the incoming message does not meet this requirement. This behavior could be changed via the ValidateRepeatingGroupEntryCount property.
Note
It is not recommended to set the ValidateRepeatingGroupEntryCount properties to false
because this can lead to data loss during the message parsing without any notification about an error.
By default, FIX Engine does not check that each repeating group instance starts with the correct leading field. This behavior could be changed via the ValidateRepeatingGroupLeadingField property.
It is also possible to detect the ill-formed repeating group using the Validate(MessageValidationFlags) method.
For example:
request.Validate(MessageValidationFlags.ValidateRepeatingGroupEntryCount | MessageValidationFlags.ValidateRepeatingGroupLeadingField);
See Also
- FIX Repeating Group introduction
- FIX Dictionary
- The RepeatingGroup sample from the FIX Engine distribution package