Esempio n. 1
0
        /// <summary>
        /// Change quantity of an order
        /// </summary>
        /// <param name="price"></param>
        /// <param name="quantity"></param>
        /// <param name="orderSide"></param>
        public void ChangeOrderQuantity(Price price, decimal quantity, OrderSide orderSide)
        {
            DepthLevel depthLevel = FindLevel(price, orderSide, orderSide == OrderSide.Buy ? _bidLevels : _askLevels);

            if (depthLevel != null && quantity != 0)
            {
                // If volume is positive, increase quantity(used when order is replaced)
                if (quantity > 0)
                {
                    depthLevel.IncreaseVolume(new Volume(quantity));
                }
                // If volume is negative, decrease the quantity(used when orders fill or get replaced)
                else
                {
                    // If after decreasing the volume, the volume becomes 0, then we will need to remove this level from depth
                    if (depthLevel.AggregatedVolume.Value - (-quantity) == 0)
                    {
                        EraseLevel(depthLevel, orderSide);
                    }
                    else
                    {
                        // We will convert the negative quantity into positive as Volume does not instantiate with a negative
                        // quantity, and also we are already decreasing the volume from the depth level so negative value is
                        // not required
                        depthLevel.DecreaseVolume(new Volume(-quantity));
                    }
                }
                depthLevel.LastChange(new ChangeId(++_lastChangeId));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Add Order to the Depth
        /// </summary>
        /// <returns></returns>
        public void AddOrder(Price price, Volume volume, OrderSide orderSide)
        {
            int        lastChangeIdCopy = _lastChangeId;
            DepthLevel depthLevel       = null;

            switch (orderSide)
            {
            case OrderSide.Buy:
                depthLevel = FindLevel(price, orderSide, _bidLevels);
                break;

            case OrderSide.Sell:
                depthLevel = FindLevel(price, orderSide, _askLevels);
                break;
            }
            if (depthLevel != null)
            {
                depthLevel.AddOrder(volume);

                // Note: As there are no excess levels, we will mark change in every depth with a change Id. We won't mark
                // a change if it is not a visible level,but in our case we don't have any excess levels so every level
                // is as visible level
                _lastChangeId = lastChangeIdCopy + 1;
                depthLevel.LastChange(new ChangeId(lastChangeIdCopy + 1));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Cancel or fill an order
        /// </summary>
        /// <param name="price"></param>
        /// <param name="volume"></param>
        /// <param name="orderSide"> </param>
        /// <returns></returns>
        public bool CloseOrder(Price price, Volume volume, OrderSide orderSide)
        {
            DepthLevel level = FindLevel(price, orderSide, orderSide == OrderSide.Sell ? _askLevels : _bidLevels);

            if (level != null)
            {
                // If no levels remain
                if (level.CloseOrder(volume))
                {
                    EraseLevel(level, orderSide);
                }
                // Else, mark the level as changed
                else
                {
                    level.LastChange(new ChangeId(++_lastChangeId));
                }
            }
            return(false);
        }