Exemple #1
0
        public bool SendSessionStatus(string status)
        {
            switch (status)
            {
            case "2":
                ProviderSimulator.SetOrderServerOnline();
                break;

            case "3":
                ProviderSimulator.SetOrderServerOffline();
                break;

            default:
                throw new ApplicationException("Unknown session status:" + status);
            }
            var mbtMsg = FixFactory.Create();

            mbtMsg.AddHeader("h");
            mbtMsg.SetTradingSessionId("TSSTATE");
            mbtMsg.SetTradingSessionStatus(status);
            if (debug)
            {
                log.Debug("Sending order server status: " + mbtMsg);
            }
            SendMessage(mbtMsg);
            return(true);
        }
Exemple #2
0
        private void FIXCreateOrder(MessageFIX4_2 packet)
        {
            if (debug)
            {
                log.Debug("FIXCreateOrder() for " + packet.Symbol + ". Client id: " + packet.ClientOrderId);
            }
            var symbol = Factory.Symbol.LookupSymbol(packet.Symbol);
            var order  = ConstructOrder(packet, packet.ClientOrderId);

            if (!ProviderSimulator.IsOrderServerOnline)
            {
                if (debug)
                {
                    log.Debug(symbol + ": Rejected " + packet.ClientOrderId + ". Order server offline.");
                }
                OnRejectOrder(order, symbol + ": Order Server Offline.");
                return;
            }
            var simulator = simulators[SimulatorType.RejectSymbol];

            if (FixFactory != null && simulator.CheckFrequencyAndSymbol(symbol))
            {
                if (debug)
                {
                    log.Debug("Simulating create order reject of 35=" + packet.MessageType);
                }
                OnRejectOrder(order, "Testing reject of create order");
                return;
            }
            simulator = simulators[SimulatorType.ServerOfflineReject];
            if (FixFactory != null && simulator.CheckFrequency())
            {
                if (debug)
                {
                    log.Debug("Simulating order server offline business reject of 35=" + packet.MessageType);
                }
                OnBusinessRejectOrder(packet.ClientOrderId, "Server offline for create order.");
                ProviderSimulator.SwitchBrokerState("offline", false);
                ProviderSimulator.SetOrderServerOffline();
                return;
            }
            if (packet.Symbol == "TestPending")
            {
                log.Info("Ignoring FIX order since symbol is " + packet.Symbol);
            }
            else
            {
                if (string.IsNullOrEmpty(packet.ClientOrderId))
                {
                    System.Diagnostics.Debugger.Break();
                }
                ProviderSimulator.CreateOrder(order);
                ProcessCreateOrder(order);
                ProviderSimulator.TryProcessAdustments(order);
            }
            return;
        }
Exemple #3
0
        private void FIXCancelOrder(MessageFIX4_2 packet)
        {
            var symbol = Factory.Symbol.LookupSymbol(packet.Symbol);

            if (!ProviderSimulator.IsOrderServerOnline)
            {
                throw new LimeException("Order server offline testing for Lime not yet implemeneted");
                if (debug)
                {
                    log.Debug(symbol + ": Cannot cancel order by client id: " + packet.OriginalClientOrderId + ". Order Server Offline.");
                }
                OnRejectCancel(packet.Symbol, packet.ClientOrderId, packet.OriginalClientOrderId, symbol + ": Order Server Offline");
                return;
            }
            var simulator = simulators[SimulatorType.RejectSymbol];

            if (FixFactory != null && simulator.CheckFrequencyAndSymbol(symbol))
            {
                if (debug)
                {
                    log.Debug("Simulating cancel order reject of 35=" + packet.MessageType);
                }
                OnRejectCancel(packet.Symbol, packet.ClientOrderId, packet.OriginalClientOrderId, "Testing reject of cancel order.");
                return;
            }
            simulator = simulators[SimulatorType.ServerOfflineReject];
            if (FixFactory != null && simulator.CheckFrequency())
            {
                throw new LimeException("Order server offline testing for Lime not yet implemeneted");
                if (debug)
                {
                    log.Debug("Simulating order server offline business reject of 35=" + packet.MessageType);
                }
                OnBusinessRejectOrder(packet.ClientOrderId, "Server offline for cancel order.");
                ProviderSimulator.SwitchBrokerState("offline", false);
                ProviderSimulator.SetOrderServerOffline();
                return;
            }
            if (debug)
            {
                log.Debug("FIXCancelOrder() for " + packet.Symbol + ". Original client id: " + packet.OriginalClientOrderId);
            }
            CreateOrChangeOrder origOrder = null;

            try
            {
                long origClientId;
                if (!long.TryParse(packet.OriginalClientOrderId, out origClientId))
                {
                    log.Error("original client order id " + packet.OriginalClientOrderId +
                              " cannot be converted to long: " + packet);
                    origClientId = 0;
                }
                origOrder = ProviderSimulator.GetOrderById(symbol, origClientId);
            }
            catch (ApplicationException)
            {
                if (debug)
                {
                    log.Debug(symbol + ": Cannot cancel order by client id: " + packet.OriginalClientOrderId + ". Probably already filled or canceled.");
                }
                OnRejectCancel(packet.Symbol, packet.ClientOrderId, packet.OriginalClientOrderId, "No such order");
                return;
            }
            var cancelOrder = ConstructCancelOrder(packet, packet.ClientOrderId);

            cancelOrder.OriginalOrder = origOrder;
            ProviderSimulator.CancelOrder(cancelOrder);
            ProcessCancelOrder(cancelOrder);
            ProviderSimulator.TryProcessAdustments(cancelOrder);
            return;
        }
Exemple #4
0
        private void FIXChangeOrder(MessageFIX4_2 packet)
        {
            var symbol = Factory.Symbol.LookupSymbol(packet.Symbol);
            var order  = ConstructOrder(packet, packet.ClientOrderId);

            if (!ProviderSimulator.IsOrderServerOnline)
            {
                throw new LimeException("Order server offline testing for Lime not yet implemeneted");
                log.Info(symbol + ": Rejected " + packet.ClientOrderId + ". Order server offline.");
                OnRejectOrder(order, symbol + ": Order Server Offline.");
                return;
            }
            var simulator = simulators[SimulatorType.RejectSymbol];

            if (FixFactory != null && simulator.CheckFrequencyAndSymbol(symbol))
            {
                if (debug)
                {
                    log.Debug("Simulating create order reject of 35=" + packet.MessageType);
                }
                OnRejectOrder(order, "Testing reject of change order.");
                return;
            }
            simulator = simulators[SimulatorType.ServerOfflineReject];
            if (FixFactory != null && simulator.CheckFrequency())
            {
                throw new LimeException("Order server offline testing for Lime not yet implemeneted");
                if (debug)
                {
                    log.Debug("Simulating order server offline business reject of 35=" + packet.MessageType);
                }
                OnBusinessRejectOrder(packet.ClientOrderId, "Server offline for change order.");
                ProviderSimulator.SwitchBrokerState("offline", false);
                ProviderSimulator.SetOrderServerOffline();
                return;
            }
            CreateOrChangeOrder origOrder = null;

            if (debug)
            {
                log.Debug("FIXChangeOrder() for " + packet.Symbol + ". Client id: " + packet.ClientOrderId + ". Original client id: " + packet.OriginalClientOrderId);
            }
            try
            {
                long origClientId;
                if (!long.TryParse(packet.OriginalClientOrderId, out origClientId))
                {
                    log.Error("original client order id " + packet.OriginalClientOrderId + " cannot be converted to long: " + packet);
                    origClientId = 0;
                }
                origOrder = ProviderSimulator.GetOrderById(symbol, origClientId);
            }
            catch (ApplicationException ex)
            {
                if (debug)
                {
                    log.Debug(symbol + ": Rejected " + packet.ClientOrderId + ". Cannot change order: " + packet.OriginalClientOrderId + ". Already filled or canceled.  Message: " + ex.Message);
                }
                OnRejectOrder(order, symbol + ": Cannot change order. Probably already filled or canceled.");
                return;
            }
            order.OriginalOrder = origOrder;
#if VERIFYSIDE
            if (order.Side != origOrder.Side)
            {
                var message = symbol + ": Cannot change " + origOrder.Side + " to " + order.Side;
                log.Error(message);
                OnRejectOrder(order, false, message);
                return;
            }
            if (order.Type != origOrder.Type)
            {
                var message = symbol + ": Cannot change " + origOrder.Type + " to " + order.Type;
                log.Error(message);
                OnRejectOrder(order, false, message);
                return;
            }
#endif
            ProviderSimulator.ChangeOrder(order);
            ProcessChangeOrder(order);
        }
Exemple #5
0
        public void SendMessage(FIXTMessage1_1 fixMessage)
        {
            FixFactory.AddHistory(fixMessage);
            if (isConnectionLost)
            {
                RemoveTickSync(fixMessage);
                return;
            }
            var simulator = simulators[SimulatorType.SendDisconnect];

            if (simulator.CheckSequence(fixMessage.Sequence))
            {
                if (debug)
                {
                    log.Debug("Ignoring message: " + fixMessage);
                }
                ProviderSimulator.SwitchBrokerState("disconnect", false);
                isConnectionLost = true;
                return;
            }
            if (simulateSendFailed && IsRecovered && random.Next(50) == 4)
            {
                if (debug)
                {
                    log.Debug("Skipping send of sequence # " + fixMessage.Sequence + " to simulate lost message. " + fixMessage);
                }
                if (fixMessage.Type == "1")
                {
                    isHeartbeatPending = TimeStamp.MaxValue;
                }
                if (debug)
                {
                    log.Debug("Message type is: " + fixMessage.Type);
                }
                return;
            }
            var writePacket = fixSocket.MessageFactory.Create();
            var message     = fixMessage.ToString();

            writePacket.DataOut.Write(message.ToCharArray());
            writePacket.SendUtcTime = TimeStamp.UtcNow.Internal;
            if (debug)
            {
                log.Debug("Simulating FIX Message: " + fixMessage);
            }
            try
            {
                fixPacketQueue.Enqueue(writePacket, writePacket.SendUtcTime);
            }
            catch (QueueException ex)
            {
                if (ex.EntryType == EventType.Terminate)
                {
                    log.Warn("fix packet queue returned queue exception " + ex.EntryType + ". Dropping message due to dispose.");
                    Dispose();
                }
                else
                {
                    throw;
                }
            }

            simulator = simulators[SimulatorType.SendServerOffline];
            if (IsRecovered && FixFactory != null && simulator.CheckSequence(fixMessage.Sequence))
            {
                if (debug)
                {
                    log.Debug("Skipping message: " + fixMessage);
                }
                ProviderSimulator.SwitchBrokerState("offline", false);
                ProviderSimulator.SetOrderServerOffline();
                if (requestSessionStatus)
                {
                    SendSessionStatus("3");
                }
                else
                {
                    log.Info("RequestSessionStatus is false so not sending order server offline message.");
                }
            }
        }
Exemple #6
0
        private bool ProcessMessage(MessageFIXT1_1 packetFIX)
        {
            if (isConnectionLost)
            {
                if (debug)
                {
                    log.Debug("Ignoring message: " + packetFIX);
                }
                RemoveTickSync(packetFIX);
                return(true);
            }
            var simulator = simulators[SimulatorType.ReceiveDisconnect];

            if (FixFactory != null && simulator.CheckSequence(packetFIX.Sequence))
            {
                if (debug)
                {
                    log.Debug("Ignoring message: " + packetFIX);
                }
                // Ignore this message. Pretend we never received it AND disconnect.
                // This will test the message recovery.)
                ProviderSimulator.SwitchBrokerState("disconnect", false);
                isConnectionLost = true;
                return(true);
            }
            if (simulateReceiveFailed && FixFactory != null && random.Next(50) == 1)
            {
                // Ignore this message. Pretend we never received it.
                // This will test the message recovery.
                if (debug)
                {
                    log.Debug("Ignoring fix message sequence " + packetFIX.Sequence);
                }
                return(Resend(packetFIX));
            }
            simulator = simulators[SimulatorType.ReceiveServerOffline];
            if (IsRecovered && FixFactory != null && simulator.CheckSequence(packetFIX.Sequence))
            {
                if (debug)
                {
                    log.Debug("Skipping message: " + packetFIX);
                }
                ProviderSimulator.SwitchBrokerState("disconnect", false);
                ProviderSimulator.SetOrderServerOffline();
                if (requestSessionStatus)
                {
                    SendSessionStatus("3"); //offline
                }
                else
                {
                    log.Info("RequestSessionStatus is false so not sending order server offline message.");
                }
                return(true);
            }

            simulator = simulators[SimulatorType.SystemOffline];
            if (IsRecovered && FixFactory != null && simulator.CheckSequence(packetFIX.Sequence))
            {
                SendSystemOffline();
                return(true);
            }

            if (debug)
            {
                log.Debug("Processing message with " + packetFIX.Sequence + ". So updating remote sequence...");
            }
            RemoteSequence = packetFIX.Sequence + 1;
            switch (packetFIX.MessageType)
            {
            case "G":
            case "D":
                simulator = simulators[SimulatorType.BlackHole];
                break;

            case "F":
                simulator = simulators[SimulatorType.CancelBlackHole];
                break;
            }
            if (FixFactory != null && simulator.CheckFrequency())
            {
                if (debug)
                {
                    log.Debug("Simulating order 'black hole' of 35=" + packetFIX.MessageType + " by incrementing sequence to " + RemoteSequence + " but ignoring message with sequence " + packetFIX.Sequence);
                }
                return(true);
            }
            ParseFIXMessage(_fixReadMessage);
            return(true);
        }