use of name.abuchen.portfolio.model.Transaction 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.model.Transaction 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;
}
use of name.abuchen.portfolio.model.Transaction in project portfolio by buchen.
the class IRRCalculationTest method testDividendPaymentsWithTaxes.
@Test
public void testDividendPaymentsWithTaxes() {
List<Transaction> tx = new ArrayList<>();
Security security = new Security();
tx.add(new //
PortfolioTransaction(//
LocalDateTime.of(2015, Month.DECEMBER, 31, 0, 0), //
CurrencyUnit.EUR, //
Values.Amount.factorize(1000), //
security, //
Values.Share.factorize(10), //
PortfolioTransaction.Type.BUY, Values.Amount.factorize(10), 0));
DividendTransaction t = new DividendTransaction();
t.setDateTime(LocalDateTime.parse("2016-06-01T00:00"));
t.setSecurity(security);
t.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(100)));
t.setShares(Values.Share.factorize(10));
t.addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(50))));
tx.add(t);
tx.add(new //
PortfolioTransaction(//
LocalDateTime.of(2016, Month.DECEMBER, 31, 0, 0), //
CurrencyUnit.EUR, //
Values.Amount.factorize(1200), //
security, //
Values.Share.factorize(10), //
PortfolioTransaction.Type.SELL, Values.Amount.factorize(10), Values.Amount.factorize(30)));
IRRCalculation calculation = Calculation.perform(IRRCalculation.class, new TestCurrencyConverter(), tx);
// Excel verification
// 31.12.15 -1000
// 01.06.16 150
// 31.12.16 1230
// =XINTZINSFUSS(B1:B3;A1:A3) = 0,412128788
assertThat(calculation.getIRR(), IsCloseTo.closeTo(0.412128788d, 0.00000001d));
}
use of name.abuchen.portfolio.model.Transaction in project portfolio by buchen.
the class AccountTransferModel method applyChanges.
@Override
public void applyChanges() {
if (sourceAccount == null)
throw new UnsupportedOperationException(Messages.MsgAccountFromMissing);
if (targetAccount == null)
throw new UnsupportedOperationException(Messages.MsgAccountToMissing);
AccountTransferEntry t;
if (source != null && sourceAccount.equals(source.getOwner(source.getSourceTransaction())) && targetAccount.equals(source.getOwner(source.getTargetTransaction()))) {
// transaction stays in same accounts
t = source;
} else {
if (source != null) {
@SuppressWarnings("unchecked") TransactionOwner<Transaction> owner = (TransactionOwner<Transaction>) source.getOwner(source.getSourceTransaction());
owner.deleteTransaction(source.getSourceTransaction(), client);
source = null;
}
t = new AccountTransferEntry(sourceAccount, targetAccount);
t.getSourceTransaction().setCurrencyCode(sourceAccount.getCurrencyCode());
t.getTargetTransaction().setCurrencyCode(targetAccount.getCurrencyCode());
t.insert();
}
t.setDate(date.atStartOfDay());
t.setNote(note);
// if source and target account have the same currencies, no forex data
// needs to be stored
AccountTransaction sourceTransaction = t.getSourceTransaction();
sourceTransaction.clearUnits();
if (sourceAccount.getCurrencyCode().equals(targetAccount.getCurrencyCode())) {
sourceTransaction.setAmount(amount);
t.getTargetTransaction().setAmount(amount);
} else {
// TODO improve naming of fields: the source amount is called
// 'fxAmount' while the target amount is just called 'amount' but
// then the source account holds the 'forex' which is switched
sourceTransaction.setAmount(fxAmount);
t.getTargetTransaction().setAmount(amount);
Transaction.Unit forex = new //
Transaction.Unit(//
Transaction.Unit.Type.GROSS_VALUE, //
Money.of(sourceAccount.getCurrencyCode(), fxAmount), //
Money.of(targetAccount.getCurrencyCode(), amount), getInverseExchangeRate());
sourceTransaction.addUnit(forex);
}
}
use of name.abuchen.portfolio.model.Transaction in project portfolio by buchen.
the class BuySellModel method applyChanges.
@Override
public void applyChanges() {
if (security == null)
throw new UnsupportedOperationException(Messages.MsgMissingSecurity);
if (account == null)
throw new UnsupportedOperationException(Messages.MsgMissingReferenceAccount);
BuySellEntry entry;
if (source != null && source.getOwner(source.getPortfolioTransaction()).equals(portfolio) && source.getOwner(source.getAccountTransaction()).equals(account)) {
entry = source;
} else {
if (source != null) {
@SuppressWarnings("unchecked") TransactionOwner<Transaction> owner = (TransactionOwner<Transaction>) source.getOwner(source.getPortfolioTransaction());
owner.deleteTransaction(source.getPortfolioTransaction(), client);
source = null;
}
entry = new BuySellEntry(portfolio, account);
entry.setCurrencyCode(account.getCurrencyCode());
entry.insert();
}
entry.setDate(LocalDateTime.of(date, time));
entry.setCurrencyCode(account.getCurrencyCode());
entry.setSecurity(security);
entry.setShares(shares);
entry.setAmount(total);
entry.setType(type);
entry.setNote(note);
writeToTransaction(entry.getPortfolioTransaction());
}
Aggregations