private int DecideBuyQuantity(float price, PositionDelta delta, Position?currentPos, float currentAskPrice)
        {
            IEnumerable <Order> existingBuyOrders = BrokerClient.GetOpenOrdersForSymbol(delta.Symbol)
                                                    .Where(order => order.Instruction == InstructionType.BUY_TO_OPEN);

            if (existingBuyOrders.Count() >= _config.MaxNumOpenBuyOrdersForSymbol)
            {
                Log.Warning("Many buy orders encountered: {@BuyOrders}- skipping this order.", existingBuyOrders.ToList());
                return(0);
            }
            float openBuyAlloc = AggregateBuyOrders(existingBuyOrders, currentAskPrice);

            float currentPosTotalAlloc = openBuyAlloc + (currentPos != null
                ? currentPos.LongQuantity * currentPos.AveragePrice * 100
                : 0);

            int buyQuantity;

            if (delta.DeltaType == DeltaType.NEW ||
                delta.DeltaType == DeltaType.ADD && currentPosTotalAlloc == 0)
            {
                float deltaMarketValue = delta.Price * delta.Quantity * 100;
                float percentOfMaxSize = deltaMarketValue / _config.LivePortfolioPositionMaxSize;
                if (percentOfMaxSize > 1)
                {
                    Log.Warning("New position in live portfolio exceeds expected max size. Delta {@Delta}", delta);
                    percentOfMaxSize = 1;
                }
                buyQuantity = (int)Math.Floor((percentOfMaxSize * _config.MyPositionMaxSize) / (price * 100));
            }
            else if (delta.DeltaType == DeltaType.ADD && currentPosTotalAlloc > 0)
            {
                float addAlloc = currentPosTotalAlloc * delta.Percent;
                buyQuantity = (int)Math.Floor(addAlloc / (price * 100));
            }
            else
            {
                Log.Warning("Invalid delta type supplied to DecideBuyQuantity function. Delta {@Delta}", delta);
                return(0);
            }

            float addedAlloc = buyQuantity * price * 100;
            float remainingFundsForTrading = BrokerClient.GetAvailableFundsForTrading() - addedAlloc;
            float newTotalAllocThisPos     = currentPosTotalAlloc + addedAlloc;

            if (newTotalAllocThisPos > _config.MyPositionMaxSize)
            {
                Log.Information("Buying " + buyQuantity + " {Symbol} would exceed maximum allocation of " + _config.MyPositionMaxSize.ToString("0.00"), delta.Symbol);
                return(0);
            }
            else if (remainingFundsForTrading < _config.MinAvailableFundsForTrading)
            {
                Log.Information("Buying " + buyQuantity + " {Symbol} would put available funds below " + _config.MinAvailableFundsForTrading.ToString("0.00"), delta.Symbol);
                return(0);
            }
            else
            {
                return(buyQuantity);
            }
        }