FIX Message
A FIX message represents a sequence of fields whose values are associated with unique numbers (tags).
A common way of presenting a FIX message is called the tag-value FIX message format. In this format, all fields are presented
as tag=value
pairs and are delimited by the special SOH
ASCII symbol (it's code is 0x01
).
The following image shows the structure of FIX message in the tag-value format:
Message Class
The Engine exposes the Message class to encapsulate all services related to handling FIX messages.
Constructing Blank Message
To create a new FIX Message object, use one of the Message constructors. It initializes a message of the specified type and the FIX protocol version with a few service fields.
For example:
var order = new Message(MsgType.NewOrderSingle, ProtocolVersion.Fix44);
Note
When a Message instance is created with the specified message type, then the corresponding internal data structure is associated with this instance, so you need to create a new message when you want to change the message type.
Parsing Raw (Tag-Value) Format
Several Message.Parse(..) methods are available to construct a Message instance from the raw (tag-value) representation.
For example:
string rawFixString = "8=FIX.4.0\u00019=86\u000135=D\u000149=0\u000156=0\u000134=1\u000152=99990909-17:17:17\u000111=90001008\u000121=1\u000155=IBM\u000154=1\u000138=10\u000140=1\u000159=0\u000110=191\u0001";
var order = Message.Parse(rawFixString);
Converting To Raw (Tag-Value) Format
Several overloads of the ToString() method are available to build a tag-value presentation of a Message instance.
For example:
// Default presentation.
Console.WriteLine(order.ToString());
// Presentation delimited with spaces instead of SOH symbol.
Console.WriteLine(order.ToString(' '));
// More advanced presentation which allows selecting which parts of fields information will be included.
Console.WriteLine(order.ToString(' ', FixStringFormat.TagName | FixStringFormat.TagNumber));
Validating Message
In addition to the presence of a particular field in the message of a specific type, the FIX protocol also defines whether the presence of fields are strictly required in the message, whether they are required under certain conditions, or whether they are optional.
To check for the presence of all required fields use the Validate(MessageValidationFlags) method.
Message Life Cycle
Sometimes there is a need to reuse a Message instance right after it was sent to a session. As soon as the message was sent, the Engine will not use it later, so it can be used again; for example, it could be sent to other sessions.
For example:
public static void Send(Message message, List<Session> sessions)
{
foreach (var session in sessions)
{
session.Send(message);
}
}
Note
Please note that a Message instance is not thread-safe, so it cannot be used in one thread while it is used in another.