Repeating Group Sample
Source code
using System;
using System.Diagnostics;
using System.IO;
using NLog.Extensions.Logging;
using OnixS.Fix;
namespace RepeatingGroup
{
static class RepeatingGroupSample
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static int Main()
{
try
{
var settings = new EngineSettings()
{
LicenseStore = GetLicenseStoreFolder(),
LoggerProvider = new NLogLoggerProvider()
};
Engine.Init(settings);
Message marketDataRequest = CreateMarketDataRequestMessage();
Log("Created FIX message in the native (tag=value) format:\n\n" + marketDataRequest);
Log("\nThe same message in human-readable format:\n\n" + marketDataRequest.ToString(' ', FixStringFormat.TagName));
ProcessMarketDataRequestMessage(marketDataRequest);
Message order = CreateMessageWithNestedRepeatingGroups();
ProcessMessageWithNestedRepeatingGroups(order);
Engine.Instance.Shutdown();
}
catch(Exception ex)
{
Log("Exception: " + ex);
return 1;
}
finally
{
// From https://github.com/NLog/NLog/wiki/Tutorial:
// NET Application running on Mono / Linux are required to stop threads / timers before entering application shutdown phase.
// Failing to do this will cause unhandled exceptions and segmentation faults, and other unpredictable behavior.
NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers
}
return 0;
}
/// <summary>
/// Creates a Market Data Request (MsgType=V) message.
/// </summary>
static Message CreateMarketDataRequestMessage()
{
var request = new Message(OnixS.Fix.Fix44.MsgType.MarketDataRequest, Engine.Instance.MessageInfoDictionaryManager[ProtocolVersion.Fix44]);
request.Set(OnixS.Fix.Fix44.Tag.MDReqID, "1")
.Set(OnixS.Fix.Fix44.Tag.SubscriptionRequestType, OnixS.Fix.Fix44.SubscriptionRequestType.SnapshotUpdate)
.Set(OnixS.Fix.Fix44.Tag.MarketDepth, "0")
.Set(OnixS.Fix.Fix44.Tag.MDUpdateType, OnixS.Fix.Fix44.MDUpdateType.Full);
Group entryTypes = request.SetGroup(OnixS.Fix.Fix44.Tag.NoMDEntryTypes, 3);
entryTypes.Set(OnixS.Fix.Fix44.Tag.MDEntryType, 0, OnixS.Fix.Fix44.MDEntryType.Bid);
entryTypes.Set(OnixS.Fix.Fix44.Tag.MDEntryType, 1, OnixS.Fix.Fix44.MDEntryType.Offer);
entryTypes.Set(OnixS.Fix.Fix44.Tag.MDEntryType, 2, OnixS.Fix.Fix44.MDEntryType.Trade);
Group symbols = request.SetGroup(OnixS.Fix.Fix44.Tag.NoRelatedSym, 2);
symbols.Set(OnixS.Fix.Fix44.Tag.Symbol, 0, "EUR/USD");
symbols.Set(OnixS.Fix.Fix44.Tag.Symbol, 1, "GPS/USD");
request.Validate();
return request;
}
static void ProcessMarketDataRequestMessage(Message request)
{
Debug.Assert(OnixS.Fix.Fix44.MsgType.MarketDataRequest == request.Type);
Group marketDataEntryTypes = request.GetGroup(OnixS.Fix.Fix44.Tag.NoMDEntryTypes);
Console.WriteLine("\nProcessing " + request.ToString(' ') + "\n");
for(int index = 0; index < marketDataEntryTypes.NumberOfInstances; ++index)
{
Console.WriteLine("Market Data Entry # " + index + " : MDEntryType = " + marketDataEntryTypes.Get(OnixS.Fix.Fix44.Tag.MDEntryType, index));
}
}
static Message CreateMessageWithNestedRepeatingGroups()
{
var order = new Message(OnixS.Fix.Fix44.MsgType.NewOrderSingle, Engine.Instance.MessageInfoDictionaryManager[ProtocolVersion.Fix50]);
order.Set(OnixS.Fix.Fix44.Tag.ClOrdID, "ClOrdID value");
Group partiesGroup = order.SetGroup(OnixS.Fix.Fix44.Tag.NoPartyIDs, 1);
partiesGroup.Set(OnixS.Fix.Fix44.Tag.PartyID, 0, "Party ID value");
// Nested repeating group:
Group subIDs = partiesGroup.SetGroup(OnixS.Fix.Fix44.Tag.NoPartySubIDs, 0, 3);
subIDs.Set(OnixS.Fix.Fix44.Tag.PartySubID, 0, "PartySubID value0");
subIDs.Set(OnixS.Fix.Fix44.Tag.PartySubID, 1, "PartySubID value1");
subIDs.Set(OnixS.Fix.Fix44.Tag.PartySubID, 2, "PartySubID value2");
Console.WriteLine("\n\nMessage with nested repeating group:\n" + order);
return order;
}
static void ProcessMessageWithNestedRepeatingGroups(Message order)
{
Debug.Assert(OnixS.Fix.Fix44.MsgType.NewOrderSingle == order.Type);
Group partiesGroup = order.GetGroup(OnixS.Fix.Fix44.Tag.NoPartyIDs);
Console.WriteLine("\nProcessing " + order.ToString(' ') + "\n");
for (int index = 0; index < partiesGroup.NumberOfInstances; ++index)
{
Group partiesSubGroup = partiesGroup.GetGroup(OnixS.Fix.Fix44.Tag.NoPartySubIDs, index);
for (int nestedIndex = 0; nestedIndex < partiesSubGroup.NumberOfInstances; ++nestedIndex)
{
Console.WriteLine("Parties SubGroup # " + nestedIndex +
" : PartySubID = " + partiesSubGroup.Get(OnixS.Fix.Fix44.Tag.PartySubID, nestedIndex));
}
}
}
private static string GetLicenseStoreFolder()
{
string path = Path.Join(AppContext.BaseDirectory, "../../../../../license");
if (Directory.Exists(path))
return path;
// expecting it run after dotnet publish using default paths
return Path.Join(AppContext.BaseDirectory, "../../../../../../license");
}
private static void Log(string msg)
{
Console.Out.WriteLine(msg);
}
}
}