use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.
the class ClientPerformanceSnapshot method determineTransferAmount.
/**
* Determine the monetary amount when transferring cash between accounts.
* Because the actual exchange rate of the transferal might differ from the
* historical rate given by the exchange rate provider (e.g. ECB), we would
* get rounding differences if we do not take the original amount. If the
* transferal does not involve the term currency at all, we calculate the
* average value out of both converted amounts.
*/
private Money determineTransferAmount(AccountTransaction t) {
if (converter.getTermCurrency().equals(t.getCurrencyCode()))
return t.getMonetaryAmount();
Transaction other = t.getCrossEntry().getCrossTransaction(t);
if (converter.getTermCurrency().equals(other.getCurrencyCode()))
return other.getMonetaryAmount();
MutableMoney m = MutableMoney.of(converter.getTermCurrency());
m.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
m.add(other.getMonetaryAmount().with(converter.at(t.getDateTime())));
return m.divide(2).toMoney();
}
use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.
the class ClientPerformanceSnapshot method addCapitalGains.
private void addCapitalGains() {
Map<Security, MutableMoney> valuation = new HashMap<>();
for (Security s : client.getSecurities()) valuation.put(s, MutableMoney.of(converter.getTermCurrency()));
snapshotStart.getJointPortfolio().getPositions().stream().forEach(p -> valuation.get(p.getInvestmentVehicle()).subtract(p.calculateValue().with(converter.at(snapshotStart.getTime()))));
for (PortfolioTransaction t : snapshotStart.getJointPortfolio().getSource().getTransactions()) {
if (!period.containsTransaction().test(t))
continue;
switch(t.getType()) {
case BUY:
case DELIVERY_INBOUND:
case TRANSFER_IN:
valuation.get(t.getSecurity()).subtract(t.getGrossValue().with(converter.at(t.getDateTime())));
break;
case SELL:
case DELIVERY_OUTBOUND:
case TRANSFER_OUT:
valuation.get(t.getSecurity()).add(t.getGrossValue().with(converter.at(t.getDateTime())));
break;
default:
throw new UnsupportedOperationException();
}
}
snapshotEnd.getJointPortfolio().getPositions().stream().forEach(p -> valuation.get(p.getInvestmentVehicle()).add(p.calculateValue().with(converter.at(snapshotEnd.getTime()))));
Category capitalGains = categories.get(CategoryType.CAPITAL_GAINS);
// add securities w/ capital gains to the positions
capitalGains.positions = //
valuation.entrySet().stream().filter(entry -> !entry.getValue().isZero()).map(entry -> new Position(entry.getKey(), entry.getValue().toMoney())).sorted(//
(p1, p2) -> p1.getLabel().compareToIgnoreCase(p2.getLabel())).collect(Collectors.toList());
// total capital gains -> sum it up
capitalGains.valuation = //
capitalGains.positions.stream().map(//
p -> p.getValuation()).collect(MoneyCollectors.sum(converter.getTermCurrency()));
}
use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.
the class ClientPerformanceSnapshot method addEarnings.
private void addEarnings() {
MutableMoney mEarnings = MutableMoney.of(converter.getTermCurrency());
MutableMoney mOtherEarnings = MutableMoney.of(converter.getTermCurrency());
MutableMoney mFees = MutableMoney.of(converter.getTermCurrency());
MutableMoney mTaxes = MutableMoney.of(converter.getTermCurrency());
MutableMoney mDeposits = MutableMoney.of(converter.getTermCurrency());
MutableMoney mRemovals = MutableMoney.of(converter.getTermCurrency());
Map<Security, MutableMoney> earningsBySecurity = new HashMap<>();
for (Account account : client.getAccounts()) {
for (AccountTransaction t : account.getTransactions()) {
if (!period.containsTransaction().test(t))
continue;
switch(t.getType()) {
case DIVIDENDS:
case INTEREST:
addEarningTransaction(account, t, mEarnings, mOtherEarnings, mTaxes, earningsBySecurity);
break;
case INTEREST_CHARGE:
Money charged = t.getMonetaryAmount().with(converter.at(t.getDateTime()));
mEarnings.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
earnings.add(new TransactionPair<AccountTransaction>(account, t));
mOtherEarnings.subtract(charged);
break;
case DEPOSIT:
mDeposits.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
break;
case REMOVAL:
mRemovals.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
break;
case FEES:
mFees.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
fees.add(new TransactionPair<AccountTransaction>(account, t));
break;
case FEES_REFUND:
mFees.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
fees.add(new TransactionPair<AccountTransaction>(account, t));
break;
case TAXES:
mTaxes.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
taxes.add(new TransactionPair<AccountTransaction>(account, t));
break;
case TAX_REFUND:
mTaxes.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
taxes.add(new TransactionPair<AccountTransaction>(account, t));
break;
case BUY:
case SELL:
case TRANSFER_IN:
case TRANSFER_OUT:
// no operation
break;
default:
throw new UnsupportedOperationException();
}
}
}
for (Portfolio portfolio : client.getPortfolios()) {
for (PortfolioTransaction t : portfolio.getTransactions()) {
if (!period.containsTransaction().test(t))
continue;
Money unit = t.getUnitSum(Unit.Type.FEE, converter);
if (!unit.isZero()) {
mFees.add(unit);
fees.add(new TransactionPair<PortfolioTransaction>(portfolio, t));
}
unit = t.getUnitSum(Unit.Type.TAX, converter);
if (!unit.isZero()) {
mTaxes.add(unit);
taxes.add(new TransactionPair<PortfolioTransaction>(portfolio, t));
}
switch(t.getType()) {
case DELIVERY_INBOUND:
mDeposits.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
break;
case DELIVERY_OUTBOUND:
mRemovals.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
break;
case BUY:
case SELL:
case TRANSFER_IN:
case TRANSFER_OUT:
break;
default:
throw new UnsupportedOperationException();
}
}
}
Category earningsCategory = categories.get(CategoryType.EARNINGS);
earningsCategory.valuation = mEarnings.toMoney();
earningsCategory.positions = earningsBySecurity.entrySet().stream().filter(entry -> !entry.getValue().isZero()).map(entry -> new Position(entry.getKey(), entry.getValue().toMoney())).sorted(//
(p1, p2) -> p1.getLabel().compareToIgnoreCase(p2.getLabel())).collect(Collectors.toList());
if (!mOtherEarnings.isZero())
earningsCategory.positions.add(new Position(Messages.LabelInterest, mOtherEarnings.toMoney()));
categories.get(CategoryType.FEES).valuation = mFees.toMoney();
categories.get(CategoryType.TAXES).valuation = mTaxes.toMoney();
categories.get(CategoryType.TRANSFERS).valuation = mDeposits.toMoney().subtract(mRemovals.toMoney());
categories.get(CategoryType.TRANSFERS).positions.add(new Position(Messages.LabelDeposits, mDeposits.toMoney()));
categories.get(CategoryType.TRANSFERS).positions.add(new Position(Messages.LabelRemovals, mRemovals.toMoney()));
}
use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.
the class ClientSnapshot method getMonetaryAssets.
public Money getMonetaryAssets() {
if (this.assets == null) {
MutableMoney sum = MutableMoney.of(getCurrencyCode());
for (AccountSnapshot account : accounts) sum.add(account.getFunds());
// use joint portfolio to reduce rounding errors if a security is
// split across multiple portfolio
sum.add(getJointPortfolio().getValue());
this.assets = sum.toMoney();
}
return this.assets;
}
use of name.abuchen.portfolio.money.MutableMoney in project portfolio by buchen.
the class ClientPerformanceSnapshotTest method testCurrencyGainsWithTransferalInOtherCurrencies.
@Test
public void testCurrencyGainsWithTransferalInOtherCurrencies() {
Client client = new Client();
Account usd = //
new AccountBuilder("USD").deposit_("2015-01-01", //
1000_00).addTo(client);
Account cad = //
new AccountBuilder("CAD").deposit_("2015-01-01", //
1000_00).addTo(client);
// insert account transfer
AccountTransferEntry entry = new AccountTransferEntry(usd, cad);
entry.setDate(LocalDateTime.parse("2015-01-10T00:00"));
AccountTransaction source = entry.getSourceTransaction();
AccountTransaction target = entry.getTargetTransaction();
source.setMonetaryAmount(Money.of("USD", 500_00));
target.setMonetaryAmount(Money.of("CAD", 1000_00));
source.addUnit(new Unit(Unit.Type.GROSS_VALUE, source.getMonetaryAmount(), target.getMonetaryAmount(), BigDecimal.valueOf(.5)));
entry.insert();
// check currency gain calculation of client performance snapshot
CurrencyConverter converter = new TestCurrencyConverter();
ClientPerformanceSnapshot snapshot = new //
ClientPerformanceSnapshot(//
client, //
converter, LocalDate.parse("2015-01-01"), LocalDate.parse("2015-01-15"));
MutableMoney currencyGains = MutableMoney.of(converter.getTermCurrency());
currencyGains.subtract(snapshot.getValue(CategoryType.INITIAL_VALUE));
currencyGains.subtract(snapshot.getValue(CategoryType.CAPITAL_GAINS));
currencyGains.subtract(snapshot.getValue(CategoryType.EARNINGS));
currencyGains.add(snapshot.getValue(CategoryType.FEES));
currencyGains.add(snapshot.getValue(CategoryType.TAXES));
currencyGains.add(snapshot.getValue(CategoryType.TRANSFERS));
currencyGains.add(snapshot.getValue(CategoryType.FINAL_VALUE));
assertThat(snapshot.getCategoryByType(CategoryType.CURRENCY_GAINS).getValuation(), is(currencyGains.toMoney()));
}
Aggregations