use of name.abuchen.portfolio.model.PortfolioTransaction 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.model.PortfolioTransaction in project portfolio by buchen.
the class SecurityPosition method split.
public static SecurityPosition split(SecurityPosition position, int weight) {
List<PortfolioTransaction> splitTransactions = new ArrayList<>(position.transactions.size());
for (PortfolioTransaction t : position.transactions) {
PortfolioTransaction t2 = new PortfolioTransaction();
t2.setDateTime(t.getDateTime());
t2.setSecurity(t.getSecurity());
t2.setType(t.getType());
t2.setCurrencyCode(t.getCurrencyCode());
t2.setAmount(Math.round(t.getAmount() * weight / (double) Classification.ONE_HUNDRED_PERCENT));
t2.setShares(Math.round(t.getShares() * weight / (double) Classification.ONE_HUNDRED_PERCENT));
t.getUnits().forEach(u -> {
long splitAmount = Math.round(u.getAmount().getAmount() * weight / (double) Classification.ONE_HUNDRED_PERCENT);
if (u.getForex() == null) {
t2.addUnit(new //
Unit(//
u.getType(), Money.of(u.getAmount().getCurrencyCode(), splitAmount)));
} else {
long splitForex = Math.round(u.getForex().getAmount() * weight / (double) Classification.ONE_HUNDRED_PERCENT);
t2.addUnit(new //
Unit(//
u.getType(), Money.of(u.getAmount().getCurrencyCode(), splitAmount), //
Money.of(u.getForex().getCurrencyCode(), splitForex), u.getExchangeRate()));
}
});
splitTransactions.add(t2);
}
return new SecurityPosition(position.investment, position.converter, position.price, Math.round(position.shares * weight / (double) Classification.ONE_HUNDRED_PERCENT), splitTransactions);
}
use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.
the class ClientClassificationFilter method addDeliveryT.
private void addDeliveryT(CalculationState state, Portfolio portfolio, PortfolioTransaction t, PortfolioTransaction.Type targetType, int weight) {
PortfolioTransaction copy = new PortfolioTransaction();
copy.setDateTime(t.getDateTime());
copy.setCurrencyCode(t.getCurrencyCode());
copy.setSecurity(t.getSecurity());
copy.setShares(value(t.getShares(), weight));
copy.setType(targetType);
// calculation is without taxes -> remove any taxes & adapt total
// accordingly
long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), weight);
long amount = value(t.getAmount(), weight);
copy.setAmount(copy.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND ? amount - taxes : amount + taxes);
// copy all units (except for taxes) over to the new transaction
t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.addUnit(value(u, weight)));
state.asReadOnly(portfolio).internalAddTransaction(copy);
}
use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.
the class ClientClassificationFilter method addBuySellT.
private void addBuySellT(CalculationState state, Portfolio portfolio, PortfolioTransaction t) {
int securityWeight = state.getWeight(t.getSecurity());
long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), securityWeight);
long securityAmount = value(t.getAmount(), securityWeight);
securityAmount = t.getType() == PortfolioTransaction.Type.BUY ? securityAmount - taxes : securityAmount + taxes;
Account account = (Account) t.getCrossEntry().getCrossOwner(t);
int accountWeight = state.getWeight(account);
long accountAmount = value(t.getAmount(), accountWeight);
long commonAmount = Math.min(securityAmount, accountAmount);
int commonWeight = (int) Math.round(((double) commonAmount / (double) securityAmount) * securityWeight);
// create a buy/sell transactions with the amount shared by the account
// assignment and the security assignment
BuySellEntry copy = new BuySellEntry(state.asReadOnly(portfolio), state.account2readonly.get(account));
copy.setDate(t.getDateTime());
copy.setCurrencyCode(t.getCurrencyCode());
copy.setSecurity(t.getSecurity());
copy.setType(t.getType());
copy.setNote(t.getNote());
copy.setShares(value(t.getShares(), commonWeight));
copy.setAmount(commonAmount);
// copy all units (except for taxes) over to the new transaction
t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.getPortfolioTransaction().addUnit(value(u, commonWeight)));
state.asReadOnly(portfolio).internalAddTransaction(copy.getPortfolioTransaction());
state.asReadOnly(account).internalAddTransaction(copy.getAccountTransaction());
if (accountAmount - commonAmount > 0) {
AccountTransaction ta = new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), accountAmount - commonAmount, null, t.getType() == PortfolioTransaction.Type.BUY ? AccountTransaction.Type.REMOVAL : AccountTransaction.Type.DEPOSIT);
state.asReadOnly(account).internalAddTransaction(ta);
}
if (securityAmount - commonAmount > 0) {
PortfolioTransaction tp = new PortfolioTransaction();
tp.setDateTime(t.getDateTime());
tp.setCurrencyCode(t.getCurrencyCode());
tp.setSecurity(t.getSecurity());
tp.setShares(value(t.getShares(), securityWeight - commonWeight));
tp.setType(t.getType() == PortfolioTransaction.Type.BUY ? PortfolioTransaction.Type.DELIVERY_INBOUND : PortfolioTransaction.Type.DELIVERY_OUTBOUND);
tp.setAmount(securityAmount - commonAmount);
t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> tp.addUnit(value(u, securityWeight - commonWeight)));
state.asReadOnly(portfolio).internalAddTransaction(tp);
}
}
use of name.abuchen.portfolio.model.PortfolioTransaction in project portfolio by buchen.
the class QuoteFromTransactionExtractor method extractQuotes.
/**
* Extracts the quotes for the given {@link Security}.
*
* @param security
* {@link Security}
* @return true if quotes were found, else false
*/
public boolean extractQuotes(Security security) {
boolean bChanges = false;
SecurityPrice pLatest = null;
// walk through all all transactions for securiy
for (TransactionPair<?> p : security.getTransactions(client)) {
Transaction t = p.getTransaction();
// check the type of the transaction
if (t instanceof PortfolioTransaction) {
PortfolioTransaction pt = (PortfolioTransaction) t;
// get date and quote and build a price from it
Quote q = pt.getGrossPricePerShare();
LocalDate d = pt.getDateTime().toLocalDate();
SecurityPrice price = new SecurityPrice(d, q.getAmount());
bChanges |= security.addPrice(price);
// remember the lates price
if ((pLatest == null) || d.isAfter(pLatest.getDate())) {
pLatest = price;
}
}
}
// set the latest price (if at leas one price was found)
if (pLatest != null) {
LatestSecurityPrice lsp = new LatestSecurityPrice(pLatest.getDate(), pLatest.getValue());
bChanges |= security.setLatest(lsp);
}
return bChanges;
}
Aggregations