FIX Prepared Message
Create
A prepared message can be created in three different ways:
- Create a skeleton and set fields
- Parse a raw FIX message (byte array)
- Create a message from existing
FixMessage
To create a FIX prepared message please use the IFixSession.PrepareMessage()
and IFixSession.PrepareMessageFromString()
methods.
Thereafter you can set/change the desired field values of a message and send it to the counter-party many times.
For example:
MessageStructure ms = new MessageStructure();
ms.ReserveString(148, 11);
FixMessage pm = null;
try
{
pm = session.PrepareMessage("B", ms);
}
catch (PreparedMessageException e)
{
//handle error of building prepared message
}
pm.Set(148, "Hello there");
session.SendMessage(pm);
To parse a string containing a raw FIX message
use the IFixSession.PrepareMessageFromString(byte[] message, string type, MessageStructure structure)
method.
For example:
string message = "5=ABRA\u00016=KADABRA\u0001";
MessageStructure ms = new MessageStructure();
ms.Reserve(5,4);
ms.Reserve(6,7);
FixMessage pm = null;
try
{
pm = session.PrepareMessageFromString(message.GetBytes(), "A", ms);
}
catch (PreparedMessageException e)
{
//handle error of building prepared message
}
session.SendMessage(pm);
To create prepared message from FixMessage
object please use
IFixSession.PrepareMessage(FixMessage message, string type, MessageStructure structure)
method:
FixMessage list = new FixMessage();
list.AddTag(1, "TestValue");
MessageStructure ms = new MessageStructure();
ms.Reserve(1, 9);
FixMessage pm = null;
try
{
pm = session.PrepareMessage(list, "A", ms);
}
catch (PreparedMessageException e)
{
//handle error of building prepared message
}
session.SendMessage(pm);
Add field
To add a field to message use the MessageStructure.Reserve(int tagId, int length)
method. Also you can give a hint to
builder about field type with similar methods MessageStructure.ReserveString(int tagId, int length)
and
MessageStructure.ReserveLong(int tagId, int length)
. In the last case builder will be able to prepare more optimized
structure for a new message.
Keep in mind, all tags in message will be in order in which you have reserved it. Length is strictly fixed. If length
of the tag is undetermined, you can use MessageStructure.VariableLength
constant as a length parameter in
Reserve(int,int)
method. In such case length will be auto adjusted. But keep in mind such way is much more slower then
fixed size for field. You will get a best perfomance if you prepared message will have only fixed length fields.
If you reserved 5 bytes, content length of this tag should be equal to 5 bytes too. Otherwise engine will mark the
length for such field as undetermined (MessageStructure.VariableLength
) automatically. But there is an exception from
this rule: if you try to set a numeric value (int, long, double) with a lover length, engine will automatically fill
the tag value with leading "0" to the required length.
Fixed tag value length:
MessageStructure ms = new MessageStructure();
ms.Reserve(1, 9);
ms.ReserveLong(54, 1);
Variable tag value length:
MessageStructure ms = new MessageStructure();
ms.ReserveString(58, MessageStructure.VariableLength);
VariableLength
Set field
To change a field value use FixMessage.Set()
method:
message.Set(148, "Hello there");
message.Set(54, 1);
In case of repeating groups you can use such way to set concrete tag value:
message.Set(58, 1, "Hello there");
message.Set(58, 2, "Hello there again");
Entries in repeating group have a numbers starting from 1.