• Version 1.16.0
Show / Hide Table of Contents

Custom Repeating Group Sample Project

This sample demonstrates how to create a custom message with a custom repeating group.

© Onix Solutions

Source code


using System;
using System.IO;
using NLog.Extensions.Logging;
using OnixS.Fix;
using Tag = OnixS.Fix.Fix44.Tag;

namespace CustomRepeatingGroup
{
    static class CustomRepeatingGroup
    {
        static void Log(string msg)
        {
            Console.Out.WriteLine(msg);
        }

        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");
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static int Main()
        {
            try
            {
                var settings = new EngineSettings
                {
                    Dictionary = "SampleFixDialectDescription.xml",
                    DisableNetworkLevel = true,
                    LicenseStore = GetLicenseStoreFolder(),
                    LoggerProvider = new NLogLoggerProvider()
                };

                Engine.Init(settings);

                var dictionary = ProtocolVersion.Fix44.ToDictionary();

                var customMessage = CreateCustomMessage(dictionary);

                customMessage.Validate();

                string nativeFixMessage = customMessage.ToString();

                Log("FIX message in the native (tag=value) format:\n\n" + nativeFixMessage +
                    "\n\nThe same message in human-readable format:\n\n" + customMessage.ToString(' ', FixStringFormat.TagName));

                var readMessage = ReadFixMessage(nativeFixMessage, dictionary);

                Log("\n\nRead FIX message:\n\n" + readMessage.ToString());

                Engine.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>
        /// Read (parses) the given string that contains the native (tag=value) representation of the FIX message.
        /// </summary>
        /// <param name="nativeFixMsg"></param>
        /// <returns></returns>
        static Message ReadFixMessage(string nativeFixMsg, IMessageInfoDictionary dictionary)
        {
            return Message.Parse(nativeFixMsg, dictionary);
        }

        /// <summary>
        /// Creates a custom FIX message with a custom repeating group.
        /// </summary>
        static Message CreateCustomMessage(IMessageInfoDictionary dictionary)
        {
            var message = new Message("SampleCustomFixMessage", dictionary);

            message.Set(Tag.TargetStrategy, 1)
                   .Set(Tag.EffectiveTime, "20050606-14:00:00")
                   .Set(Tag.ExpireTime , "20050606-18:00:00");

            const int NoStrategyParameters = 957;

            Group group = message.SetGroup(NoStrategyParameters, 2);

            const int StrategyParameterName = 958;
            const int StrategyParameterType = 959;
            const int StrategyParameterValue = 960;

            group.Set(StrategyParameterName, 0, "MinimumVolume")
                .Set(StrategyParameterType, 0, 11)
                .Set(StrategyParameterValue, 0, 11);

            group.Set(StrategyParameterName, 1, "Aggressive")
                 .Set(StrategyParameterType, 1, 13)
                 .Set(StrategyParameterValue, 1, "Y");

            return message;
        }
    }
}

Dialect

<?xml version="1.0" encoding="utf-8" ?>
<Dialect xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://ref.onixs.biz/fix/dialects https://ref.onixs.biz/fix/dialects/dialects-2.19.xsd" xmlns="https://onixs.biz/fix/dialects">

    <FIX version="4.4">
        <Message type="SampleCustomFixMessage">
            <Field tag="847" name="TargetStrategy"/>
            <Field tag="168" name="EffectiveTime"/>
            <Field tag="126" name="ExpireTime"/>
            <Group numberOfInstancesTag="957" name="NoStrategyParameters">
                <Field tag="958" name="StrategyParameterName"/>
                <Field tag="959" name="StrategyParameterType"/>
                <Field tag="960" name="StrategyParameterValue"/>
            </Group>
        </Message>
    </FIX>

</Dialect>
In this article
Back to top Copyright © Onix Solutions.
Generated by DocFX