public void HandleStaleSellIocOperations()
 {
     // This means previous Ioc is still pending and needs to be removed.
     if (SellOperations.Count > 0 && SellOperations.ElementAt(0).OrderType == OrderType.Ioc)
     {
         CancelSellOperation(SellOperations.ElementAt(0));
     }
 }
    public void Cancel(string orderId)
    {
        var operation = Find(orderId);

        if (operation == null)
        {
            return;
        }
        BalancePriceToQuantityMap(operation);

        if (operation.OperationType == OperationType.Buy)
        {
            BuyOperations.Remove(operation);
        }
        else
        {
            SellOperations.Remove(operation);
        }
    }
    public void Trade(BuySellOperation operation)
    {
        var buysToBeRemoved  = new List <BuySellOperation>();
        var sellsToBeRemoved = new List <BuySellOperation>();

        if (operation.OperationType == OperationType.Buy)
        {
            for (var i = 0; i < SellOperations.Count; i++)
            {
                if (operation.Quantity <= 0)
                {
                    break;
                }
                if (operation.Price >= SellOperations.ElementAt(i).Price)
                {
                    AddToTradeResult(operation, SellOperations.ElementAt(i), buysToBeRemoved, sellsToBeRemoved);
                }
            }
        }
        else
        {
            for (var i = 0; i < BuyOperations.Count; i++)
            {
                if (operation.Quantity <= 0)
                {
                    break;
                }
                if (operation.Price <= BuyOperations.ElementAt(i).Price)
                {
                    AddToTradeResult(BuyOperations.ElementAt(i), operation, buysToBeRemoved, sellsToBeRemoved);
                }
            }
        }
        foreach (var sellToBeRemoved in sellsToBeRemoved)
        {
            CancelSellOperation(sellToBeRemoved);
        }
        foreach (var buyToBeRemoved in buysToBeRemoved)
        {
            CancelBuyOperation(buyToBeRemoved);
        }
    }
    public void Process(BuySellOperation buySellOperation)
    {
        if (buySellOperation.Price <= 0 || buySellOperation.Quantity < 0)
        {
            return;
        }

        if (buySellOperation.OperationType == OperationType.Buy)
        {
            // Put the current buy / sell operation on the queue.
            BuyOperations.Add(buySellOperation);

            // Add buyOperation to historical price to quantity map.
            if (buySellOperation.OrderType != OrderType.Ioc && BuyPriceToQuantityMap.TryGetValue(buySellOperation.Price, out var quantity))
            {
                BuyPriceToQuantityMap[buySellOperation.Price] = quantity + buySellOperation.Quantity;
            }
            else if (buySellOperation.OrderType != OrderType.Ioc)
            {
                BuyPriceToQuantityMap.Add(buySellOperation.Price, buySellOperation.Quantity);
            }
        }
        else
        {
            // Put the current buy / sell operation on the queue.
            SellOperations.Add(buySellOperation);

            // Add buyOperation to historical price to quantity map.
            if (buySellOperation.OrderType != OrderType.Ioc && SellPriceToQuantityMap.TryGetValue(buySellOperation.Price, out var quantity))
            {
                SellPriceToQuantityMap[buySellOperation.Price] = quantity + buySellOperation.Quantity;
            }
            else if (buySellOperation.OrderType != OrderType.Ioc)
            {
                SellPriceToQuantityMap.Add(buySellOperation.Price, buySellOperation.Quantity);
            }
        }
    }
 public void CancelSellOperation(BuySellOperation operation)
 {
     BalancePriceToQuantityMap(operation);
     SellOperations.Remove(operation);
 }