public void HandleStaleBuyIocOperations()
 {
     // This means previous Ioc is still pending and needs to be removed.
     if (BuyOperations.Count > 0 && BuyOperations.ElementAt(0).OrderType == OrderType.Ioc)
     {
         CancelBuyOperation(BuyOperations.ElementAt(0));
     }
 }
    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);
        }
    }