Search in sources :

Example 56 with BuySellEntry

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));
}
Also used : Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Example 57 with BuySellEntry

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)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) Item(name.abuchen.portfolio.datatransfer.Extractor.Item) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) ArrayList(java.util.ArrayList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) TransactionItem(name.abuchen.portfolio.datatransfer.Extractor.TransactionItem) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) BuySellEntryItem(name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem) SBrokerPDFExtractor(name.abuchen.portfolio.datatransfer.pdf.SBrokerPDFExtractor) PDFInputFile(name.abuchen.portfolio.datatransfer.pdf.PDFInputFile) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) IOException(java.io.IOException) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) List(java.util.List) Unit(name.abuchen.portfolio.model.Transaction.Unit) Optional(java.util.Optional) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) IOException(java.io.IOException) Item(name.abuchen.portfolio.datatransfer.Extractor.Item) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) TransactionItem(name.abuchen.portfolio.datatransfer.Extractor.TransactionItem) BuySellEntryItem(name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem) BuySellEntryItem(name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem) SBrokerPDFExtractor(name.abuchen.portfolio.datatransfer.pdf.SBrokerPDFExtractor) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 58 with BuySellEntry

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);
    }
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Set(java.util.Set) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Classification(name.abuchen.portfolio.model.Classification) Visitor(name.abuchen.portfolio.model.Taxonomy.Visitor) HashSet(java.util.HashSet) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Assignment(name.abuchen.portfolio.model.Classification.Assignment) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 59 with BuySellEntry

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));
}
Also used : Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) Assignment(name.abuchen.portfolio.model.Classification.Assignment) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Classification(name.abuchen.portfolio.model.Classification) AccountBuilder(name.abuchen.portfolio.AccountBuilder) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 60 with BuySellEntry

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());
}
Also used : BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) TransactionOwner(name.abuchen.portfolio.model.TransactionOwner)

Aggregations

BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)169 Client (name.abuchen.portfolio.model.Client)150 IOException (java.io.IOException)141 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)125 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)125 Unit (name.abuchen.portfolio.model.Transaction.Unit)122 Money (name.abuchen.portfolio.money.Money)119 Security (name.abuchen.portfolio.model.Security)111 Test (org.junit.Test)108 ArrayList (java.util.ArrayList)107 Item (name.abuchen.portfolio.datatransfer.Extractor.Item)105 SecurityItem (name.abuchen.portfolio.datatransfer.Extractor.SecurityItem)105 BuySellEntryItem (name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem)104 TransactionItem (name.abuchen.portfolio.datatransfer.Extractor.TransactionItem)104 Values (name.abuchen.portfolio.money.Values)83 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)79 LocalDateTime (java.time.LocalDateTime)78 List (java.util.List)78 Optional (java.util.Optional)78 PDFInputFile (name.abuchen.portfolio.datatransfer.pdf.PDFInputFile)75