// Find lots for the specified instrument
List<Lot> lots =
brokerageAccountRepository.findActiveLots(this, symbol);
// Withdraw specified quantity from available lots
DecimalQuantity quantityLeftToWithdraw = quantity;
for (Lot lot : lots) {
DecimalQuantity lotQuantity = lot.getQuantity();
if (lotQuantity.isMinus()) {
continue;
}
DecimalQuantity withdrawQuantity =
(lotQuantity.gteq(quantityLeftToWithdraw)) ?
quantityLeftToWithdraw : lotQuantity;
lot.sell(withdrawQuantity);
lot.addAllocation(factory.createAllocation(withdrawQuantity.negate()));
quantityLeftToWithdraw = quantityLeftToWithdraw.minus(withdrawQuantity);
if (quantityLeftToWithdraw.isZero()) {
break;
}
}