Exemplo n.º 1
0
        public virtual void Replace(Order order, int deltaSize, double newPrice)
        {
            ++Transaction;

            bool   matched      = false;
            bool   found        = false;
            bool   priceChanged = newPrice != 0 && newPrice != order.Price;
            double price        = (newPrice == PriceUnchanged) ? order.Price : newPrice;

            if (order.IsBuy)
            {
                int index = FindBid(order);

                if (index != -1)
                {
                    found = true;

                    if (ValidateReplace(Bids.Values[index], deltaSize, price))
                    {
                        OrderTracker tracker = Bids.Values[index];

                        OnReplaced(order, tracker.Open, deltaSize, price, Transaction);
                        OnBookUpdated(Transaction);

                        int newOpen = tracker.Open + deltaSize;
                        tracker.ChangeQuantity(newOpen);

                        if (newOpen == 0)
                        {
                            OnCanceled(order, 0, Transaction);
                            Bids.RemoveAt(index);
                        }
                        else
                        {
                            matched = AddOrder(tracker, price);
                            Bids.RemoveAt(index);
                        }
                    }
                }
            }
            else
            {
                int index = FindAsk(order);

                if (index != -1)
                {
                    found = true;

                    if (ValidateReplace(Asks.Values[index], deltaSize, price))
                    {
                        OrderTracker tracker = Asks.Values[index];

                        OnReplaced(order, tracker.Open, deltaSize, price, Transaction);
                        OnBookUpdated(Transaction);

                        int newOpen = tracker.Open + deltaSize;
                        tracker.ChangeQuantity(newOpen);

                        if (newOpen == 0)
                        {
                            OnCanceled(order, 0, Transaction);
                            Asks.RemoveAt(index);
                        }
                        else
                        {
                            matched = AddOrder(tracker, price);
                            Asks.RemoveAt(index);
                        }
                    }
                }
            }

            if (!found)
            {
                OnReplaceRejected(order, "Order not found.", Transaction);
            }
        }