FIX Message | Table of Content | FIX Session |
Manipulating Message Fields |
A FIX message represents a sequence of fields whose values are associated with unique numbers (tags). Such an interpretation treats a message as a collection of values associated with tag numbers. For this reason, the primary approach in handling message fields is similar to managing associative collections.
To associate a field with a value, the Set(Int32, String) method must be used. If there is no field of a given tag number available in the message, this member creates an association. If the field already exists, this member updates its value with the new value.
Note |
---|
It is also possible to get or set fields values using Message’s indexer. |
Also, there are Set methods for each standard type, e.g. Set(Int32, Int32). Such methods transform input value into text-presentation, so the actually-stored value is a text-presentation of input. Therefore, when the message is sent there is no need to serialize such values. If you do not perform a large amount of typed set/get operations with the message object on the critical path, then these methods are the best choice from the performance point of view. In addition, there are similar SetV methods, e.g. SetV(Int32, Int32). Such methods set values as it is without transforming it into text-presentation. Assigning field value via SetV methods saves not only value and its type as well, therefore no parsing is performed during subsequent access to the field via typed Get methods. In this case, when the message is sent there is need to serialize such values, however, if you perform a large amount of typed set/get operations with the message object on the critical path, then these methods are the best choice from the performance point of view.
To get a field value, the Get(Int32) method must be used.
Note |
---|
It is also possible to get or set fields values using Message’s indexer. |
Also, there are typed Get. and TryGet. methods, e.g GetInteger(Int32), TryGetInteger(Int32, Int32). Such methods perform the conversion of stored value to the corresponding type. Typed Get. methods throw an exception if the conversion fails and typed TryGet. methods return false in such cases.
To check the field presence, the Contain(Int32) method must be used.
To remove a field, the Remove(Int32) method must be used.
The following example demonstrates the basic operations over message fields.
string ClOrdID = order[11]; execReport[17] = "Unique identifier of execution message"; order.Remove(57);
Dim ClOrdID As String = order.Get(11) execReport.Set(17, "Unique identifier of execution message") order.Remove(57)
The following interface does not allocate temporary string objects and gives an ability to manipulate fields without a GC pressure:
Please see the "Using GC-free interface" article of the Low Latency Best Practices page for details.
Message class supports the IEnumerable interface, therefore you can use a Message object in the foreach operator to enumerate fields of the message. Additionally, the Fields property exposed by the Message class to obtain the collection of all fields currently available in the message. When you enumerate fields of a message, this does not include fields from a repeating group. To enumerate fields of the repeating group you need to get the corresponding GroupInstance object from the repeating group, please see the FIX Repeating Groups.
The following example dumps all message fields onto the system console.
using FIXForge.NET.FIX; using FIXForge.NET.FIX.FIX44; Message msg = new Message("D", ProtocolVersion.FIX44); foreach(Field field in msg) Console.WriteLine("Tags={0}, value={1}", field.Tag, field.Value);
Imports FIXForge.NET.FIX Imports FIXForge.NET.FIX.FIX44 Dim msg As New Message("D", ProtocolVersion.FIX44) Dim field As Field For Each field In msg Console.WriteLine("Tags={0}, value={1}", field.Tag, field.Value) Next field
In each sub-namespace, which corresponds to certain FIX version (like FIXForge.NET.FIX.FIX44), there is a Tags class defined. This class contains the constants for all-known tag numbers. Additionally, there are classes, which contains the constants for all-known tag values. The usage of all of these constants makes the source code more readable.
using FIXForge.NET.FIX; using FIXForge.NET.FIX.FIX44; Message order = new Message( MsgType.Order_Single, ProtocolVersion.FIX44); order.Set(Tags.ClOrdID, "90001008"); order.Set(Tags.Side, Side.Buy); order.Set(Tags.TimeInForce, TimeInForce.Day);
Imports FIXForge.NET.FIX Imports FIXForge.NET.FIX.FIX44 Dim order As New Message( MsgType.Order_Single, ProtocolVersion.FIX44) order.Set(Tags.ClOrdID, "90001008") order.Set(Tags.Side, Side.Buy) order.Set(Tags.TimeInForce, TimeInForce.Day)