Esempio n. 1
0
 public void SessionLoggedOut(FixSessionID sessionID)
 {
     lock (_lock)
     {
         _sessions[sessionID].LoginStatus = SessionLoginStatus.LoggedOut;
     }
 }
Esempio n. 2
0
 public void AddSession(FixSessionID sessionID, IFixMessageHandler messageHandler)
 {
     lock (_lock)
     {
         _sessions.Add(sessionID, new SessionContext(messageHandler));
     }
 }
Esempio n. 3
0
 public void RemoveSession(FixSessionID sessionID)
 {
     lock (_lock)
     {
         _sessions.Remove(sessionID);
     }
 }
Esempio n. 4
0
 public void RemoveSession(FixSessionID sessionID)
 {
     lock (_lock)
     {
         _sessions.Remove(sessionID);
     }
 }
Esempio n. 5
0
 public void AddSession(FixSessionID sessionID, IFixMessageHandler messageHandler)
 {
     lock (_lock)
     {
         _sessions.Add(sessionID, new SessionContext(messageHandler));
     }
 }
Esempio n. 6
0
 public void SessionLoggedOut(FixSessionID sessionID)
 {
     lock (_lock)
     {
         _sessions[sessionID].LoginStatus = SessionLoginStatus.LoggedOut;
     }
 }
Esempio n. 7
0
        public void AddSession(SessionID fixID, IFixMessageHandler messageHandler)
        {
            var newID = new FixSessionID();

            _sessionIDMap.Add(fixID, newID);
            _sessionRepository.AddSession(newID, messageHandler);
        }
Esempio n. 8
0
 public void NewOrderAccepted(FixSessionID ownerSessionID, IOrder order)
 {
     var orders = new List<IOrder> {order};
     foreach (var sessionID in GetAllLoggedInSessions())
     {
         SendOrders(sessionID, orders);
     }
 }
Esempio n. 9
0
 public void SendMessageToHandler(FixSessionID sessionID, Action <IFixMessageHandler> f)
 {
     lock (_lock)
     {
         var handler = _sessions[sessionID];
         f(handler.MessageHandler);
     }
 }
Esempio n. 10
0
 public void SendMessageToHandler(FixSessionID sessionID, Action<IFixMessageHandler> f)
 {
     lock (_lock)
     {
         var handler = _sessions[sessionID];
         f(handler.MessageHandler);
     }
 }
Esempio n. 11
0
        public void SendOrders(FixSessionID sessionID, List <IOrder> orders)
        {
            var fixID = _sessionIDMap.GetBySecond(sessionID);

            Action <IFixMessageHandler> messageSendF =
                handler => handler.SendOrdersToSession(fixID, orders);

            _sessionRepository.SendMessageToHandler(sessionID, messageSendF);
        }
Esempio n. 12
0
        /// <summary>
        /// Adds an order to the system with the given properties
        /// </summary>
        /// <param name="sessionID">The FIX Session ID</param>
        /// <param name="orderType">
        /// The type of order, currently only Limit orders are supported
        /// </param>
        /// <param name="symbol">The symbol for the order</param>
        /// <param name="marketSide">The side of the market for the order</param>
        /// <param name="clOrdID">The FIX ClOrdID for the order, set by the client</param>
        /// <param name="account">The trading account associated with the order</param>
        /// <param name="quantity">The quantity of the order</param>
        /// <param name="price">The price of the order, may be null for market orders</param>
        /// <returns>The new order after it has been added</returns>
        /// <exception cref="FixATServerException">If the order is rejected</exception>
        public IOrder AddOrder(FixSessionID sessionID,
                               OrderType orderType,
                               string symbol,
                               MarketSide marketSide,
                               string clOrdID,
                               TradingAccount account,
                               decimal quantity,
                               decimal? price = null)
        {
            // A more complete system would look the contract up in a contract store
            var contract = new Contract(symbol);

            // TODO Replace this with a better mechanism (esp if more order types are supported)
            decimal orderPrice;
            switch (orderType)
            {
                case OrderType.Limit:
                    {
                        if (!price.HasValue)
                            throw new FixATServerException("Limit order must specify a price");
                        orderPrice = price.Value;
                        break;
                    }

                    // Uncomment this if and when market orders are supported
                    //case OrderType.Market:
                    //    {
                    //        if (price.HasValue)
                    //            throw new FixATServerException(
                    //                "Market order should not have a specified price");

                    //        orderPrice = GetMarketPrice(contract, marketSide);
                    //        break;
                    //    }

                default:
                    throw new FixATServerException(
                        string.Format("Order Type {0} not supported", orderType));
            }

            if (OrderWouldLeadToACrossedMarket(marketSide, contract, orderPrice))
                throw new FixATServerException("Order would lead to a crossed market");

            var order = _orderRepository.AddOrder(CreateOrderID(),
                                                  contract,
                                                  orderType,
                                                  marketSide,
                                                  orderPrice,
                                                  quantity,
                                                  clOrdID,
                                                  account);

            _orderOwners[order.ID] = sessionID;

            return order;
        }
Esempio n. 13
0
        /// <summary>
        /// Deletes all the orders owned by the given FIX session
        /// </summary>
        public void DeleteAllOrders(FixSessionID sessionID)
        {
            // TODO This could get racey
            var sessionsOrders =
                _orderOwners.Where(p => sessionID.Equals(p.Value)).Select(p => p.Key).ToList();

            foreach (var orderID in sessionsOrders)
            {
                DeleteOrder(orderID);
            }
        }
Esempio n. 14
0
        public void NewOrderAccepted(FixSessionID ownerSessionID, IOrder order)
        {
            var orders = new List <IOrder> {
                order
            };

            foreach (var sessionID in GetAllLoggedInSessions())
            {
                SendOrders(sessionID, orders);
            }
        }
Esempio n. 15
0
        public void OrderFilled(FixSessionID ownerSessionID, OrderMatch matchDetails)
        {
            // If we ever support owner details etc then filter those out is session != sessionID
            foreach (var sessionID in GetAllLoggedInSessions())
            {
                var fixID = _sessionIDMap.GetBySecond(sessionID);

                Action<IFixMessageHandler> messageSendF =
                    handler => handler.OnOrderFilled(fixID, matchDetails);

                _sessionRepository.SendMessageToHandler(sessionID, messageSendF);
            }
        }
Esempio n. 16
0
        public void OrderFilled(FixSessionID ownerSessionID, OrderMatch matchDetails)
        {
            // If we ever support owner details etc then filter those out is session != sessionID
            foreach (var sessionID in GetAllLoggedInSessions())
            {
                var fixID = _sessionIDMap.GetBySecond(sessionID);

                Action <IFixMessageHandler> messageSendF =
                    handler => handler.OnOrderFilled(fixID, matchDetails);

                _sessionRepository.SendMessageToHandler(sessionID, messageSendF);
            }
        }
Esempio n. 17
0
        public void CancellingAnOrderWhenYouAreTheOwningSessionSucceeds()
        {
            var mockRepository = new Mock<IOrderRepository>();
            var o = DefaultOrder(1);

            mockRepository.Setup(r => r.AddOrder(o.ID,
                                                 o.Contract,
                                                 o.OrderType,
                                                 o.MarketSide,
                                                 o.Price,
                                                 o.Quantity,
                                                 o.ClOrdID,
                                                 o.Account))
                          .Returns(o);

            mockRepository.Setup(r => r.DeleteOrder(o.ID)).Returns(o);

            Action<OrderMatch, FixSessionID> emptyMatchCallback = (m, s) => { };
            var mediator = new OrderMediator(mockRepository.Object, emptyMatchCallback);
            var fakeSessionId = new FixSessionID();

            var addedOrder = mediator.AddOrder(fakeSessionId,
                                               o.OrderType,
                                               o.Contract.Symbol,
                                               o.MarketSide,
                                               o.ClOrdID,
                                               o.Account,
                                               o.Quantity,
                                               o.Price);

            Assert.AreEqual(o.ID, addedOrder.ID);
            Assert.AreEqual(o.ClOrdID, addedOrder.ClOrdID);

            var cancelledOrder = mediator.CancelOrder(addedOrder.ID, fakeSessionId);
            Assert.IsNotNull(cancelledOrder, "Cancelled order should not be null");

            mockRepository.Verify(r => r.DeleteOrder(o.ID), Times.Once());

            Assert.AreEqual(o.ID, cancelledOrder.ID);
            Assert.AreEqual(o.ClOrdID, cancelledOrder.ClOrdID);
        }
Esempio n. 18
0
        /// <summary>
        /// Checks the given FIX session is the order owner before deleting an order
        /// </summary>
        /// <param name="orderID">The order to delete</param>
        /// <param name="sessionID">The ID of the FIX session</param>
        /// <returns>The order which has been cancelled</returns>
        /// <exception cref="FixATServerException">If the FIX session does not own the order</exception>
        public IOrder CancelOrder(long orderID, FixSessionID sessionID)
        {
            var owner = _orderOwners[orderID];

            if (!owner.Equals(sessionID))
            {
                var e = new FixATServerException(
                    string.Format("Unable to cancel order {0}, permission denied", orderID));
                e.Data[RejectReasonExceptionString] = OrderCancelRejectReason.PermissionDenied;
                throw e;
            }

            var deletedOrder = DeleteOrder(orderID);

            if (deletedOrder == null)
            {
                var e = new FixATServerException(
                    string.Format("Unable to cancel order {0}, order not found", orderID));
                e.Data[RejectReasonExceptionString] = OrderCancelRejectReason.OrderNotFound;
                throw e;
            }
            return(deletedOrder);
        }
Esempio n. 19
0
        public void CancellingAnOrderWhenYouAreNotTheOwningSessionFails()
        {
            var mockRepository = new Mock<IOrderRepository>();
            var o = DefaultOrder(1);

            mockRepository.Setup(r => r.AddOrder(o.ID,
                                                 o.Contract,
                                                 o.OrderType,
                                                 o.MarketSide,
                                                 o.Price,
                                                 o.Quantity,
                                                 o.ClOrdID,
                                                 o.Account))
                          .Returns(o);

            Action<OrderMatch, FixSessionID> emptyMatchCallback = (m, s) => { };
            var mediator = new OrderMediator(mockRepository.Object, emptyMatchCallback);

            var fakeSessionId1 = new FixSessionID();
            var fakeSessionId2 = new FixSessionID();
            Assert.AreNotEqual(fakeSessionId1, fakeSessionId2);

            var addedOrder = mediator.AddOrder(fakeSessionId1,
                                               o.OrderType,
                                               o.Contract.Symbol,
                                               o.MarketSide,
                                               o.ClOrdID,
                                               o.Account,
                                               o.Quantity,
                                               o.Price);

            Assert.Throws<FixATServerException>(
                () => mediator.CancelOrder(addedOrder.ID, fakeSessionId2));

            mockRepository.Verify(r => r.DeleteOrder(o.ID), Times.Never());
        }
Esempio n. 20
0
 public void SendMessage(Message message, FixSessionID sessionID)
 {
     SendMessage(message, _sessionIDMap.GetBySecond(sessionID));
 }
Esempio n. 21
0
        private void OnOrderMatched(OrderMatch matchDetails, FixSessionID sessionID)
        {
            var cmd = _commandFactory.CreateSendOrderFill(matchDetails, sessionID);

            _outputQueue.Enqueue(cmd);
        }
Esempio n. 22
0
        /// <summary>
        /// Adds an order to the system with the given properties
        /// </summary>
        /// <param name="sessionID">The FIX Session ID</param>
        /// <param name="orderType">
        /// The type of order, currently only Limit orders are supported
        /// </param>
        /// <param name="symbol">The symbol for the order</param>
        /// <param name="marketSide">The side of the market for the order</param>
        /// <param name="clOrdID">The FIX ClOrdID for the order, set by the client</param>
        /// <param name="account">The trading account associated with the order</param>
        /// <param name="quantity">The quantity of the order</param>
        /// <param name="price">The price of the order, may be null for market orders</param>
        /// <returns>The new order after it has been added</returns>
        /// <exception cref="FixATServerException">If the order is rejected</exception>
        public IOrder AddOrder(FixSessionID sessionID,
                               OrderType orderType,
                               string symbol,
                               MarketSide marketSide,
                               string clOrdID,
                               TradingAccount account,
                               decimal quantity,
                               decimal?price = null)
        {
            // A more complete system would look the contract up in a contract store
            var contract = new Contract(symbol);

            // TODO Replace this with a better mechanism (esp if more order types are supported)
            decimal orderPrice;

            switch (orderType)
            {
            case OrderType.Limit:
            {
                if (!price.HasValue)
                {
                    throw new FixATServerException("Limit order must specify a price");
                }
                orderPrice = price.Value;
                break;
            }

            // Uncomment this if and when market orders are supported
            //case OrderType.Market:
            //    {
            //        if (price.HasValue)
            //            throw new FixATServerException(
            //                "Market order should not have a specified price");

            //        orderPrice = GetMarketPrice(contract, marketSide);
            //        break;
            //    }

            default:
                throw new FixATServerException(
                          string.Format("Order Type {0} not supported", orderType));
            }

            if (OrderWouldLeadToACrossedMarket(marketSide, contract, orderPrice))
            {
                throw new FixATServerException("Order would lead to a crossed market");
            }

            var order = _orderRepository.AddOrder(CreateOrderID(),
                                                  contract,
                                                  orderType,
                                                  marketSide,
                                                  orderPrice,
                                                  quantity,
                                                  clOrdID,
                                                  account);

            _orderOwners[order.ID] = sessionID;

            return(order);
        }
Esempio n. 23
0
 public void AddSession(SessionID fixID, IFixMessageHandler messageHandler)
 {
     var newID = new FixSessionID();
     _sessionIDMap.Add(fixID, newID);
     _sessionRepository.AddSession(newID, messageHandler);
 }
Esempio n. 24
0
 public void SendMessage(Message message, FixSessionID sessionID)
 {
     SendMessage(message, _sessionIDMap.GetBySecond(sessionID));
 }
Esempio n. 25
0
        /// <summary>
        /// Checks the given FIX session is the order owner before deleting an order
        /// </summary>
        /// <param name="orderID">The order to delete</param>
        /// <param name="sessionID">The ID of the FIX session</param>
        /// <returns>The order which has been cancelled</returns>
        /// <exception cref="FixATServerException">If the FIX session does not own the order</exception>
        public IOrder CancelOrder(long orderID, FixSessionID sessionID)
        {
            var owner = _orderOwners[orderID];
            if (!owner.Equals(sessionID))
            {
                var e = new FixATServerException(
                    string.Format("Unable to cancel order {0}, permission denied", orderID));
                e.Data[RejectReasonExceptionString] = OrderCancelRejectReason.PermissionDenied;
                throw e;
            }

            var deletedOrder = DeleteOrder(orderID);
            if (deletedOrder == null)
            {
                var e = new FixATServerException(
                    string.Format("Unable to cancel order {0}, order not found", orderID));
                e.Data[RejectReasonExceptionString] = OrderCancelRejectReason.OrderNotFound;
                throw e;
            }
            return deletedOrder;
        }
Esempio n. 26
0
 /// <summary>
 /// Deletes all the orders owned by the given FIX session
 /// </summary>
 public void DeleteAllOrders(FixSessionID sessionID)
 {
     // TODO This could get racey
     var sessionsOrders =
         _orderOwners.Where(p => sessionID.Equals(p.Value)).Select(p => p.Key).ToList();
     foreach (var orderID in sessionsOrders)
     {
         DeleteOrder(orderID);
     }
 }
Esempio n. 27
0
        public void SendOrders(FixSessionID sessionID, List<IOrder> orders)
        {
            var fixID = _sessionIDMap.GetBySecond(sessionID);

            Action<IFixMessageHandler> messageSendF =
                   handler => handler.SendOrdersToSession(fixID, orders);

            _sessionRepository.SendMessageToHandler(sessionID, messageSendF);
        }
Esempio n. 28
0
 private void OnOrderMatched(OrderMatch matchDetails, FixSessionID sessionID)
 {
     var cmd = _commandFactory.CreateSendOrderFill(matchDetails, sessionID);
     _outputQueue.Enqueue(cmd);
 }