Esempio n. 1
0
        private void CloseWithError(FIXContext context, MessageFIX4_4 packetIn, string textMessage)
        {
            Message   message    = context.LocalSocket.MessageFactory.Create();
            var       fixFactory = new FIXFactory4_4(1, fixSender, packetIn.Sender);
            var       fixMsg     = (FIXMessage4_4)fixFactory.Create();
            TimeStamp timeStamp  = TimeStamp.UtcNow;

            fixMsg.SetAccount(packetIn.Account);
            fixMsg.SetText(textMessage);
            fixMsg.AddHeader("j");
            string errorMessage = fixMsg.ToString();

            message.DataOut.Write(errorMessage.ToCharArray());
            long end = Factory.Parallel.TickCount + 2000;

            if (debug)
            {
                log.Debug("Writing Error Message: " + textMessage);
            }
            while (!context.LocalSocket.TrySendMessage(message))
            {
                if (Factory.Parallel.TickCount > end)
                {
                    throw new ApplicationException("Timeout while sending an order.");
                }
                Factory.Parallel.Yield();
            }
            throw new FilterException();
        }
Esempio n. 2
0
        public void Local(FIXContext context, Message localMessage)
        {
            var packetFIX = (MessageFIX4_4)localMessage;

            switch (packetFIX.MessageType)
            {
            case "AF":
                isRecovered           = false;
                isOrderUpdateComplete = false;
                if (debug)
                {
                    log.Debug("OrderUpdate Starting.");
                }
                break;

            case "AN":
                isRecovered = false;
                isPositionUpdateComplete = false;
                if (debug)
                {
                    log.Debug("PositionUpdate Starting.");
                }
                break;

            case "G":
            case "D":
                AssertOrderMaximum(context, packetFIX);
                AssertPositionMaximum(context, packetFIX);
                break;
            }
        }
Esempio n. 3
0
 private void PositionUpdate(FIXContext context, MessageFIX4_4 packet)
 {
     if (packet.MessageType == "AO")
     {
         isPositionUpdateComplete = true;
         if (debug)
         {
             log.Debug("PositionUpdate Complete.");
         }
         TryEndRecovery();
     }
     else
     {
         double     position = packet.LongQuantity + packet.ShortQuantity;
         SymbolInfo symbolInfo;
         try {
             symbolInfo = Factory.Symbol.LookupSymbol(packet.Symbol);
         } catch (Exception ex) {
             log.Error("Error looking up " + packet.Symbol + ": " + ex.Message);
             return;
         }
         if (debug)
         {
             log.Debug("PositionUpdate: " + symbolInfo + "=" + position);
         }
         symbolPositionMap[symbolInfo.BinaryIdentifier] = position;
     }
 }
Esempio n. 4
0
 private void OnConnectRemote()
 {
     remoteSelector.AddReader(remoteSocket);
     remoteTask = Factory.Parallel.Loop("FilterRemoteRead", OnException, RemoteReadLoop);
     fixContext = new FIXContextDefault(localSocket, remoteSocket);
     log.Info("Connected at " + remoteAddress + " and port " + remotePort + " with socket: " + localSocket);
 }
Esempio n. 5
0
        private void InitFix()
        {
            _fixContext = new FIXContext();

            _fixContext.OnException += FIXContext_OnException;

            InitFixCash();
        }
Esempio n. 6
0
 private void OnConnectRemote()
 {
     remoteTask = Factory.Parallel.Loop("FilterRemoteRead", OnException, RemoteReadLoop);
     remoteTask.Start();
     fixContext = new FIXContextDefault(localSocket, remoteSocket);
     remoteSocket.ReceiveQueue.ConnectInbound(remoteTask);
     log.Info("Connected at " + remoteAddress + " and port " + remotePort + " with socket: " + localSocket);
 }
Esempio n. 7
0
        private SymbolInfo GetSymbolInfo(FIXContext context, MessageFIX4_4 packet)
        {
            SymbolInfo symbolInfo = null;

            try {
                symbolInfo = Factory.Symbol.LookupSymbol(packet.Symbol);
            } catch (ApplicationException ex) {
                log.Error("Error looking up " + packet.Symbol + ": " + ex.Message);
            }
            return(symbolInfo);
        }
Esempio n. 8
0
 private void ExecutionReport(FIXContext context, MessageFIX4_4 packetFIX)
 {
     if (packetFIX.Text == "END")
     {
         isOrderUpdateComplete = true;
         if (debug)
         {
             log.Debug("ExecutionReport Complete.");
         }
         TryEndRecovery();
     }
 }
Esempio n. 9
0
 private void AssertPositionMaximum(FIXContext context, MessageFIX4_4 packet)
 {
     if (isRecovered)
     {
         var quantity   = GetOrderQuantity(context, packet);
         var symbolInfo = GetSymbolInfo(context, packet);
         if (symbolInfo != null)
         {
             var position = GetPosition(symbolInfo);
             position += quantity;
             var maxPositionSize = symbolInfo.MaxPositionSize;
             var positionSize    = Math.Abs(position);
             if (positionSize > maxPositionSize)
             {
                 CloseWithError(context, packet, "Position size " + positionSize + " for " + symbolInfo + " was greater than MaxPositionSize of " + maxPositionSize + " in Message sequence #" + packet.Sequence);
             }
         }
     }
 }
Esempio n. 10
0
        public void Remote(FIXContext context, Message remoteMessage)
        {
            var packetFIX = (MessageFIX4_4)remoteMessage;

            switch (packetFIX.MessageType)
            {
            // For simulating MBT Demo server failure.
            //			case "A":
            //				throw new FilterException();
            case "AP":
            case "AO":
                PositionUpdate(context, packetFIX);
                break;

            case "8":
            case "9":
                ExecutionReport(context, packetFIX);
                break;
            }
        }
Esempio n. 11
0
        private int GetOrderQuantity(FIXContext context, MessageFIX4_4 packet)
        {
            var quantity = packet.OrderQuantity;

            switch (packet.Side)
            {
            case "1":
                break;

            case "2":
            case "5":
                quantity *= -1;
                break;

            default:
                CloseWithError(context, packet, "Unknown order side " + packet.Side + " in fix message. Unable to perform pre-trade verification.");
                break;
            }
            return(quantity);
        }
Esempio n. 12
0
 private void AssertOrderMaximum(FIXContext context, MessageFIX4_4 packet)
 {
     if (isRecovered)
     {
         var quantity   = GetOrderQuantity(context, packet);
         var symbolInfo = GetSymbolInfo(context, packet);
         if (symbolInfo != null)
         {
             var position = GetPosition(symbolInfo);
             if (Math.Sign(quantity) != Math.Sign(position))
             {
                 quantity += (int)position;
             }
             var maxOrderSize = symbolInfo.MaxOrderSize;
             if (Math.Abs(quantity) > maxOrderSize)
             {
                 CloseWithError(context, packet, "Order size " + quantity + " for " + symbolInfo + " was greater than MaxOrderSize of " + maxOrderSize + " in Message sequence #" + packet.Sequence);
             }
         }
     }
 }