use of name.abuchen.portfolio.model.BuySellEntry in project portfolio by buchen.
the class CheckCurrenciesBuySellEntryTest method testBuySellEntry.
@Test
public void testBuySellEntry() {
Account account = new Account();
account.setCurrencyCode("EUR");
Security security = new Security();
security.setCurrencyCode("USD");
Portfolio portfolio = new Portfolio();
BuySellEntry entry = new BuySellEntry();
entry.setType(PortfolioTransaction.Type.BUY);
entry.setSecurity(security);
entry.setMonetaryAmount(Money.of("EUR", 100_00));
entry.getPortfolioTransaction().addUnit(new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 80_00), Money.of("USD", 100_00), BigDecimal.valueOf(0.8)));
assertThat(action.process(entry, account, portfolio).getCode(), is(Status.Code.OK));
}
use of name.abuchen.portfolio.model.BuySellEntry in project portfolio by buchen.
the class SBrokerPDFExtractorTest method testWertpapierVerkauf1.
@Test
public void testWertpapierVerkauf1() throws IOException {
SBrokerPDFExtractor extractor = new SBrokerPDFExtractor(new Client());
List<Exception> errors = new ArrayList<Exception>();
List<Item> results = extractor.extract(PDFInputFile.loadTestCase(getClass(), "sBroker_Verkauf1.txt"), errors);
assertThat(errors, empty());
assertThat(results.size(), is(2));
// check security
Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
assertThat(security.getIsin(), is("DE000A0H0785"));
assertThat(security.getName(), is("iS.EO G.B.C.1.5-10.5y.U.ETF DE"));
// check buy sell transaction
Optional<Item> item = results.stream().filter(i -> i instanceof BuySellEntryItem).findFirst();
assertThat(item.isPresent(), is(true));
assertThat(item.get().getSubject(), instanceOf(BuySellEntry.class));
BuySellEntry entry = (BuySellEntry) item.get().getSubject();
assertThat(entry.getPortfolioTransaction().getType(), is(PortfolioTransaction.Type.SELL));
assertThat(entry.getAccountTransaction().getType(), is(AccountTransaction.Type.SELL));
assertThat(entry.getPortfolioTransaction().getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 5648_24)));
assertThat(entry.getPortfolioTransaction().getDateTime(), is(LocalDateTime.parse("2015-06-04T00:00")));
assertThat(entry.getPortfolioTransaction().getShares(), is(Values.Share.factorize(47)));
assertThat(entry.getPortfolioTransaction().getUnitSum(Unit.Type.FEE), is(Money.of(CurrencyUnit.EUR, 821L)));
}
use of name.abuchen.portfolio.model.BuySellEntry 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.BuySellEntry in project portfolio by buchen.
the class ClassificationIndexTest method testThatTaxesAreNotIncludedInTTWRORCalculation.
@Test
public void testThatTaxesAreNotIncludedInTTWRORCalculation() {
Client client = new Client();
Security security = //
new SecurityBuilder().addPrice("2015-12-31", //
Values.Quote.factorize(100)).addPrice("2016-12-31", //
Values.Quote.factorize(110)).addTo(client);
Account account = //
new AccountBuilder().deposit_("2014-01-01", Values.Amount.factorize(1000)).addTo(client);
AccountTransaction t = new AccountTransaction();
t.setType(AccountTransaction.Type.DIVIDENDS);
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));
account.addTransaction(t);
Portfolio portfolio = //
new PortfolioBuilder(account).addTo(client);
BuySellEntry buy = new BuySellEntry(portfolio, account);
buy.setType(PortfolioTransaction.Type.BUY);
buy.setDate(LocalDateTime.parse("2015-12-31T00:00"));
buy.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(1000)));
buy.setShares(Values.Share.factorize(10));
buy.setSecurity(security);
buy.insert();
BuySellEntry sell = new BuySellEntry(portfolio, account);
sell.setType(PortfolioTransaction.Type.SELL);
sell.setDate(LocalDateTime.parse("2016-12-31T00:00"));
sell.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(1070)));
sell.setShares(Values.Share.factorize(10));
sell.setSecurity(security);
sell.getPortfolioTransaction().addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(30))));
sell.insert();
Classification classification = new Classification(null, null);
classification.addAssignment(new Assignment(security));
List<Exception> warnings = new ArrayList<Exception>();
PerformanceIndex index = PerformanceIndex.forClassification(client, new TestCurrencyConverter(), classification, new ReportingPeriod.FromXtoY(LocalDate.parse("2015-01-01"), LocalDate.parse("2017-01-01")), warnings);
assertThat(warnings.isEmpty(), is(true));
// dividend payment 10% * quote change 10%
assertThat(index.getFinalAccumulatedPercentage(), IsCloseTo.closeTo((1.1 * 1.1) - 1, 0.000000001d));
// add taxes to dividend payment
t.addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(50))));
index = PerformanceIndex.forClassification(client, new TestCurrencyConverter(), classification, new ReportingPeriod.FromXtoY(LocalDate.parse("2015-01-01"), LocalDate.parse("2017-01-01")), warnings);
// dividend payment 15% * quote change 10%
assertThat(index.getFinalAccumulatedPercentage(), IsCloseTo.closeTo((1.15 * 1.1) - 1, 0.000000001d));
}
use of name.abuchen.portfolio.model.BuySellEntry 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