• Version 1.16.0
Show / Hide Table of Contents

Typed Messages Sample Project

This sample demonstrates how to send/receive typed messages with the FIX Engine.

© Onix Solutions

Source code


using System;
using System.IO;
using OnixS.Fix;
using Fix42.Application.OrdersAndExecutionsTrade.SingleGeneralOrderHandling;
using System.Net;
using System.Runtime.InteropServices;
using NLog.Extensions.Logging;

namespace TypedMessages
{
    static class TypedMessagesSample
    {
        static void Main()
        {
            const string SenderCompID = "Acceptor";
            const string TargetCompID = "Initiator";
            const ProtocolVersion version = ProtocolVersion.Fix42;
            const int port = 4500;

            var settings = new EngineSettings()
            {
                LicenseStore = GetLicenseStoreFolder(),
                LoggerProvider = new NLogLoggerProvider()
            };
            settings.ListenPorts.Add(port);

            try
            {
                Engine.Init(settings);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while executing sample: {0}", ex);

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && ex.Message.Contains("ErrorCode=10013"))
                {
                    Console.WriteLine("The socket is already in use, or access to the port is restricted on this OS. Please try to change the listen port in EngineSettings.ListenPorts to another one.");
                    Console.WriteLine("You can view a list of which ports are excluded from your user by running this command: 'netsh interface ipv4 show excludedportrange protocol=tcp'");
                }

                return;
            }

            try
            {
                using var acceptor = new Session(SenderCompID, TargetCompID, version);
                var acceptorTypedMessageListener = new Fix42.TypedMessageListener(acceptor);

                acceptorTypedMessageListener.OrderSingleReceived += new Action<OrderSingle>((order) =>
                {
                    Console.WriteLine("Acceptor has received the Order Single message:  ClOrdID=" + order.ClOrdID + "; Msg(" + ((Message)order) + ")\n");
                    Console.WriteLine("NoAllocs group size: " + order.NoAllocs.Count);

                    foreach (Fix42.OrderSingleNoAllocsInstance instance in order.NoAllocs)
                    {
                        Console.WriteLine("AllocAccount=" + instance.AllocAccount + "; AllocShares=" + instance.AllocShares);
                    }

                    var report = new ExecutionReport(order.ClOrdID, "Report1", Fix42.ExecTransType.New, Fix42.ExecType.Fill, Fix42.OrdStatus.Filled, order.Symbol, order.Side, 0, 10000, 100);
                    acceptor.Send(report);
                    Console.WriteLine("\n");
                });

                acceptor.LogonAsAcceptor();

                using (Session initiator = new Session(TargetCompID, SenderCompID, version))
                {
                    var initiatorTypedMessageListener = new Fix42.TypedMessageListener(initiator);

                    initiatorTypedMessageListener.ExecutionReportReceived += new Action<ExecutionReport>((report) =>
                    {
                        Console.WriteLine("Initiator has received the Execution Report message: OrderID=" + report.OrderID + "; Msg(" + ((Message)report) + ")\n");
                    });

                    initiator.LogonAsInitiator(IPAddress.Loopback, Engine.Instance.Settings.ListenPorts[0]);

                    var order = new OrderSingle();

                    order.ClOrdID = "Order1";
                    order.HandlInst = Fix42.HandlInst.AutomatedExecutionNoIntervention;
                    order.Symbol = "Ticker symbol";
                    order.Side = Fix42.Side.Buy;
                    order.TransactTime = new HighResolutionTimestamp(DateTime.UtcNow, 123456789);
                    order.OrdType = Fix42.OrdType.Market;
                    order.OrderQty = 10000;

                    Fix42.OrderSingleNoAllocsInstance orderSingleNoAllocsInstance1 = order.NoAllocs.CreateNew();
                    orderSingleNoAllocsInstance1.AllocAccount = "AllocAccount1";
                    orderSingleNoAllocsInstance1.AllocShares = 10;
                    Fix42.OrderSingleNoAllocsInstance orderSingleNoAllocsInstance2 = order.NoAllocs.CreateNew();
                    orderSingleNoAllocsInstance2.AllocAccount = "AllocAccount2";
                    orderSingleNoAllocsInstance2.AllocShares = 20;

                    ((Message)order).Validate();

                    Console.WriteLine("Press any key to send an order.\n");
                    Console.ReadKey();

                    initiator.Send(order);

                    Console.WriteLine("The order (" + ((Message)order) + ") was sent by the Initiator\n");

                    initiator.Logout();
                }

                acceptor.Logout();
            }
            finally
            {
                Engine.Shutdown();
            }
        }

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

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