/** * The magic happens here. Calculate all dependent variables. * * @param totalPortfolioValue The total value of the portfolio, in base currency. */ private void calculateStatsFor(AssetClass item, Money totalPortfolioValue) { Money zero = MoneyFactory.fromString("0"); if (totalPortfolioValue.toDouble() == 0) { item.setValue(zero); item.setCurrentAllocation(zero); item.setCurrentValue(zero); item.setDifference(zero); return; } // Set Value Money allocation = item.getAllocation(); // value = allocation * totalPortfolioValue / 100; Money value = totalPortfolioValue .multiply(allocation.toDouble()) .divide(100, Constants.DEFAULT_PRECISION); item.setValue(value); // current value Money currentValue = sumStockValues(item.getStocks()); item.setCurrentValue(currentValue); // Current allocation. Money currentAllocation = currentValue .multiply(100) .divide(totalPortfolioValue.toDouble(), Constants.DEFAULT_PRECISION); item.setCurrentAllocation(currentAllocation); // difference Money difference = currentValue.subtract(value); item.setDifference(difference); }