• Version 1.16.0
Show / Hide Table of Contents

Buy Side Sample Project

This sample connects to the pre-defined host and port (acts as a FIX Initiator). When the session is established, the 'SingleOrder - New'(MsgType='D') FIX message is sent to the counterparty.

© Onix Solutions

Source code


using System;
using OnixS.Fix;
using System.Globalization;
using System.IO;
using OnixS.Fix.Fix44;
using System.Net;
using NLog.Extensions.Logging;

namespace BuySide
{
    /// <summary>
    /// Establishes the FIX session as Initiator.
    /// </summary>
    internal static class Initiator
    {
        private const ProtocolVersion fixVersion = ProtocolVersion.Fix44;

        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 Message CreateOrderMessage(IMessageInfoDictionary dictionary)
        {
            var order = new Message(MsgType.NewOrderSingle, dictionary);

            order.Set(Tag.HandlInst, HandlInst.AutoExecPub)
                 .Set(Tag.ClOrdID, "Unique identifier for Order")
                 .Set(Tag.Symbol, "TSLA")
                 .Set(Tag.Side, Side.Buy)
                 .Set(Tag.OrderQty, 1000)
                 .Set(Tag.OrdType, OrdType.Market)
                 .Set(Tag.TransactTime, DateTime.UtcNow.ToString("yyyyMMdd-HH:mm:ss", CultureInfo.InvariantCulture));

#if DEBUG
            try
            {
                order.Validate();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception on order validation: " + ex);
            }
#endif

            return order;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static int Main()
        {
            Console.WriteLine("Buy Side Sample");

            try
            {
                var settings = new EngineSettings()
                {
                    LicenseStore = GetLicenseStoreFolder(),
                    LoggerProvider = new NLogLoggerProvider()
                };

                Engine.Init(settings);

                var dictionary = fixVersion.ToDictionary();

                using (Session session = new("BuySide", "SellSide", dictionary))
                {

                    session.InboundApplicationMessage += (object sender, InboundMessageEventArgs e) =>
                    {
                        Console.WriteLine("Inbound application-level message:\n" + e.Message);
                    };

                    session.StateChanged += (object sender, SessionStateChangeEventArgs e) =>
                    {
                        Console.WriteLine("Session state: " + e.NewState);
                    };

                    session.Error += (object sender, SessionErrorEventArgs e) =>
                    {
                        Console.WriteLine("Error: " + e.ToString());
                    };

                    session.Warning += (object sender, SessionWarningEventArgs e) =>
                    {
                        Console.WriteLine("Warning: " + e.ToString());
                    };

                    session.LogonAsInitiator(IPAddress.Loopback, 10450);

                    Message order = CreateOrderMessage(dictionary);

                    session.Send(order);

                    Console.WriteLine($"The order ({order}) was sent");

                    session.Logout("The session is disconnected by BuySide");
                }

                Engine.Shutdown();
            }
            catch (Exception ex)
            {
                Console.WriteLine("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;
        }
    }
}

In this article
Back to top Copyright © Onix Solutions.
Generated by DocFX