use of name.abuchen.portfolio.money.Money in project portfolio by buchen.
the class CSVPortfolioTransactionExtractor method createGrossValueIfNecessary.
private void createGrossValueIfNecessary(String[] rawValues, Map<String, Column> field2column, PortfolioTransaction transaction) throws ParseException {
if (transaction.getSecurity().getCurrencyCode().equals(transaction.getCurrencyCode()))
return;
BigDecimal exchangeRate = getBigDecimal(Messages.CSVColumn_ExchangeRate, rawValues, field2column);
if (exchangeRate != null && exchangeRate.compareTo(BigDecimal.ZERO) != 0) {
Money grossValue = transaction.getGrossValue();
Money forex = Money.of(transaction.getSecurity().getCurrencyCode(), Math.round(exchangeRate.multiply(BigDecimal.valueOf(grossValue.getAmount())).doubleValue()));
exchangeRate = BigDecimal.ONE.divide(exchangeRate, 10, BigDecimal.ROUND_HALF_DOWN);
transaction.addUnit(new Unit(Unit.Type.GROSS_VALUE, grossValue, forex, exchangeRate));
}
}
use of name.abuchen.portfolio.money.Money in project portfolio by buchen.
the class BankSLMPDFExtractor method addDividendTransaction.
@SuppressWarnings("nls")
private void addDividendTransaction() {
DocumentType type = new DocumentType("Dividende");
this.addDocumentTyp(type);
Block block = new Block("Dividende");
type.addBlock(block);
Transaction<AccountTransaction> extractor = new Transaction<AccountTransaction>().subject(() -> {
AccountTransaction transaction = new AccountTransaction();
transaction.setType(AccountTransaction.Type.DIVIDENDS);
return transaction;
}).section("date", "shares", "name", "wkn", "currency").match(//
"Am (?<date>\\d+.\\d+.\\d{4}+) wurde folgende Dividende gutgeschrieben:").match(//
"^.*$").match(//
"^(?<name>.*)$").match(//
"^Valor: (?<wkn>[^ ]*)$").match(//
"Brutto \\((?<shares>[\\d.']+) \\* ... ([\\d.']+)\\) (?<currency>\\w{3}+) ([\\d.']+)").assign((t, v) -> {
t.setDateTime(asDate(v.get("date")));
t.setShares(asShares(v.get("shares")));
t.setSecurity(getOrCreateSecurity(v));
}).section("amount", //
"currency").match(//
"Netto (?<currency>\\w{3}+) (?<amount>[\\d.']+)").assign((t, v) -> {
t.setAmount(asAmount(v.get("amount")));
t.setCurrencyCode(v.get("currency"));
}).section("fees", "currency").optional().match(//
".* Verrechnungssteuer (?<currency>\\w{3}+) -(?<fees>[\\d.']+)").assign((t, v) -> t.addUnit(new Unit(Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fees")))))).section("fees", "currency").optional().match(//
".* Quellensteuer (?<currency>\\w{3}+) -(?<fees>[\\d.']+)").assign((t, v) -> t.addUnit(new Unit(Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fees")))))).section("fees", "currency").optional().match(//
".* Nicht r.ckforderbare Steuern (?<currency>\\w{3}+) -(?<fees>[\\d.']+)").assign((t, v) -> t.addUnit(new Unit(Unit.Type.TAX, Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("fees")))))).section("grossValue", "forexSum", "forexCurrency", "totalValue", "currency", //
"exchangeRate").optional().match(//
"Brutto \\(([\\d.']+) \\* ... ([\\d.']+)\\) (\\w{3}+) (?<grossValue>[\\d.']+)").match(//
"Netto (?<forexCurrency>\\w{3}+) (?<forexSum>[\\d.']+)").match(//
"Change ... / ... (?<exchangeRate>[\\d.']+) (?<currency>\\w{3}+) (?<totalValue>[\\d.'-]+)").assign((t, v) -> {
// NOSONAR
// if we end up in the branch, then we have forex
// dividends and must convert taxes in local
// currency
Money totalValue = Money.of(asCurrencyCode(v.get("currency")), asAmount(v.get("totalValue")));
t.setMonetaryAmount(totalValue);
// keep tax units in case we need to convert them
List<Unit> tax = t.getUnits().collect(Collectors.toList());
t.clearUnits();
Money forexGrossValue = Money.of(asCurrencyCode(v.get("forexCurrency")), asAmount(v.get("grossValue")));
BigDecimal exchangeRate = asExchangeRate(v.get("exchangeRate"));
Money grossValue = Money.of(totalValue.getCurrencyCode(), Math.round(exchangeRate.doubleValue() * forexGrossValue.getAmount()));
Unit unit = new Unit(Unit.Type.GROSS_VALUE, grossValue, forexGrossValue, exchangeRate);
t.addUnit(unit);
// convert tax units
tax.stream().forEach(u -> {
if (u.getAmount().getCurrencyCode().equals(t.getCurrencyCode())) {
t.addUnit(u);
} else {
Money txm = Money.of(t.getCurrencyCode(), Math.round(exchangeRate.doubleValue() * u.getAmount().getAmount()));
Unit fu = new Unit(Unit.Type.TAX, txm, u.getAmount(), exchangeRate);
t.addUnit(fu);
}
});
}).wrap(t -> new TransactionItem(t));
block.set(extractor);
}
use of name.abuchen.portfolio.money.Money in project portfolio by buchen.
the class ClientPerformanceSnapshot method addCurrencyGains.
private void addCurrencyGains() {
Map<String, MutableMoney> currency2money = new HashMap<>();
for (AccountSnapshot snapshot : snapshotStart.getAccounts()) {
if (converter.getTermCurrency().equals(snapshot.getAccount().getCurrencyCode()))
continue;
MutableMoney value = currency2money.computeIfAbsent(snapshot.getAccount().getCurrencyCode(), c -> MutableMoney.of(converter.getTermCurrency()));
// subtract initial values
value.subtract(snapshot.getFunds());
// add and subtract transactions
for (AccountTransaction t : snapshot.getAccount().getTransactions()) {
if (!period.containsTransaction().test(t))
continue;
switch(t.getType()) {
case DIVIDENDS:
case INTEREST:
case DEPOSIT:
case TAX_REFUND:
case SELL:
case FEES_REFUND:
value.subtract(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
break;
case REMOVAL:
case FEES:
case INTEREST_CHARGE:
case TAXES:
case BUY:
value.add(t.getMonetaryAmount().with(converter.at(t.getDateTime())));
break;
case TRANSFER_IN:
value.subtract(determineTransferAmount(t));
break;
case TRANSFER_OUT:
value.add(determineTransferAmount(t));
break;
default:
throw new UnsupportedOperationException();
}
}
}
// add final values (if in foreign currency)
for (AccountSnapshot snapshot : snapshotEnd.getAccounts()) {
if (converter.getTermCurrency().equals(snapshot.getAccount().getCurrencyCode()))
continue;
currency2money.computeIfAbsent(snapshot.getAccount().getCurrencyCode(), //
c -> MutableMoney.of(converter.getTermCurrency())).add(snapshot.getFunds());
}
Category currencyGains = categories.get(CategoryType.CURRENCY_GAINS);
currency2money.forEach((currency, money) -> {
currencyGains.valuation = currencyGains.valuation.add(money.toMoney());
currencyGains.positions.add(new Position(currency, money.toMoney()));
});
Collections.sort(currencyGains.positions, (p1, p2) -> p1.getLabel().compareTo(p2.getLabel()));
}
use of name.abuchen.portfolio.money.Money in project portfolio by buchen.
the class ClientPerformanceSnapshot method addEarningTransaction.
private void addEarningTransaction(Account account, AccountTransaction transaction, MutableMoney mEarnings, MutableMoney mOtherEarnings, MutableMoney mTaxes, Map<Security, MutableMoney> earningsBySecurity) {
this.earnings.add(new TransactionPair<AccountTransaction>(account, transaction));
Money tax = transaction.getUnitSum(Unit.Type.TAX, converter).with(converter.at(transaction.getDateTime()));
Money earned = transaction.getGrossValue().with(converter.at(transaction.getDateTime()));
mEarnings.add(earned);
if (!tax.isZero()) {
mTaxes.add(tax);
taxes.add(new TransactionPair<AccountTransaction>(account, transaction));
}
if (transaction.getSecurity() != null)
earningsBySecurity.computeIfAbsent(transaction.getSecurity(), k -> MutableMoney.of(converter.getTermCurrency())).add(earned);
else
mOtherEarnings.add(earned);
}
use of name.abuchen.portfolio.money.Money in project portfolio by buchen.
the class PerformanceIndex method calculateAbsoluteInvestedCapital.
/**
* Calculates the absolute invested capital, i.e. starting with the first
* transaction recorded for the client.
*/
public long[] calculateAbsoluteInvestedCapital() {
ToLongBiFunction<Money, LocalDateTime> convertIfNecessary = (amount, date) -> {
if (amount.getCurrencyCode().equals(getCurrencyConverter().getTermCurrency()))
return amount.getAmount();
else
return getCurrencyConverter().convert(date, amount).getAmount();
};
long startValue = 0;
Interval interval = getActualInterval();
LocalDateTime intervalStart = interval.getStart().atStartOfDay();
for (Account account : getClient().getAccounts()) startValue += //
account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.DEPOSIT || t.getType() == AccountTransaction.Type.REMOVAL).filter(//
t -> t.getDateTime().isBefore(intervalStart)).mapToLong(t -> {
if (t.getType() == AccountTransaction.Type.DEPOSIT)
return convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else if (t.getType() == AccountTransaction.Type.REMOVAL)
return -convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else
return 0;
}).sum();
for (Portfolio portfolio : getClient().getPortfolios()) startValue += //
portfolio.getTransactions().stream().filter(t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND || t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND).filter(//
t -> t.getDateTime().isBefore(intervalStart)).mapToLong(t -> {
if (t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND)
return convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else if (t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND)
return -convertIfNecessary.applyAsLong(t.getMonetaryAmount(), t.getDateTime());
else
return 0;
}).sum();
return calculateInvestedCapital(startValue);
}
Aggregations