Search in sources :

Example 41 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CSVPortfolioTransactionExtractorTest method testBuyTransactionWithForex.

@Test
public void testBuyTransactionWithForex() throws ParseException {
    Client client = new Client();
    Security security = new Security();
    security.setTickerSymbol("SAP.DE");
    security.setCurrencyCode("USD");
    client.addSecurity(security);
    CSVExtractor extractor = new CSVPortfolioTransactionExtractor(client);
    List<Exception> errors = new ArrayList<Exception>();
    List<Item> results = extractor.extract(0, Arrays.<String[]>asList(new String[] { "2013-01-02", "DE0007164600", "SAP.DE", "", "SAP SE", "-100", "EUR", "", "12", "110", "USD", "0,9091", "1,9", "SELL", "Notiz" }), buildField2Column(extractor), errors);
    assertThat(errors, empty());
    assertThat(results.size(), is(1));
    new AssertImportActions().check(results, CurrencyUnit.EUR);
    BuySellEntry entry = (BuySellEntry) results.stream().filter(i -> i instanceof BuySellEntryItem).findAny().get().getSubject();
    PortfolioTransaction t = entry.getPortfolioTransaction();
    assertThat(t.getType(), is(PortfolioTransaction.Type.SELL));
    assertThat(t.getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 100_00)));
    assertThat(t.getSecurity(), is(security));
    assertThat(t.getUnitSum(Unit.Type.TAX), is(Money.of("EUR", 12_00)));
    assertThat(t.getUnit(Unit.Type.FEE).isPresent(), is(false));
    Unit grossAmount = t.getUnit(Unit.Type.GROSS_VALUE).get();
    assertThat(grossAmount.getAmount(), is(Money.of("EUR", 100_00)));
    assertThat(grossAmount.getForex(), is(Money.of("USD", 110_00)));
    assertThat(grossAmount.getExchangeRate(), is(BigDecimal.valueOf(0.9091)));
}
Also used : BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) ParseException(java.text.ParseException) AssertImportActions(name.abuchen.portfolio.datatransfer.actions.AssertImportActions) PortfolioTransferItem(name.abuchen.portfolio.datatransfer.Extractor.PortfolioTransferItem) 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) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) BuySellEntryItem(name.abuchen.portfolio.datatransfer.Extractor.BuySellEntryItem) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 42 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class DeutscheBankPDFExtractorTest method testErtragsgutschrift2.

@Test
public void testErtragsgutschrift2() throws IOException {
    DeutscheBankPDFExtractor extractor = new DeutscheBankPDFExtractor(new Client());
    List<Exception> errors = new ArrayList<Exception>();
    List<Item> results = extractor.extract(PDFInputFile.loadTestCase(getClass(), "DeutscheBankErtragsgutschrift2.txt"), errors);
    assertThat(errors, empty());
    assertThat(results.size(), is(2));
    new AssertImportActions().check(results, CurrencyUnit.EUR);
    // check security
    Security security = results.stream().filter(i -> i instanceof SecurityItem).findFirst().get().getSecurity();
    assertThat(security.getName(), is("ISHS-MSCI N. AMERIC.UCITS ETF BE.SH.(DT.ZT.)"));
    assertThat(security.getIsin(), is("DE000A0J2060"));
    assertThat(security.getWkn(), is("A0J206"));
    assertThat(security.getCurrencyCode(), is("USD"));
    // check transaction
    Optional<Item> item = results.stream().filter(i -> i instanceof TransactionItem).findFirst();
    assertThat(item.isPresent(), is(true));
    assertThat(item.get().getSubject(), instanceOf(AccountTransaction.class));
    AccountTransaction transaction = (AccountTransaction) item.get().getSubject();
    assertThat(transaction.getType(), is(AccountTransaction.Type.DIVIDENDS));
    assertThat(transaction.getSecurity(), is(security));
    assertThat(transaction.getDateTime(), is(LocalDateTime.parse("2015-03-24T00:00")));
    assertThat(transaction.getMonetaryAmount(), is(Money.of(CurrencyUnit.EUR, 16_17L)));
    assertThat(transaction.getShares(), is(Values.Share.factorize(123)));
    Optional<Unit> grossValue = transaction.getUnit(Unit.Type.GROSS_VALUE);
    assertThat(grossValue.isPresent(), is(true));
    assertThat(grossValue.get().getAmount(), is(Money.of("EUR", 16_17L)));
    assertThat(grossValue.get().getForex(), is(Money.of("USD", 17_38L)));
    assertThat(grossValue.get().getExchangeRate().doubleValue(), IsCloseTo.closeTo(0.930578, 0.000001));
}
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) IsCloseTo(org.hamcrest.number.IsCloseTo) 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) DeutscheBankPDFExtractor(name.abuchen.portfolio.datatransfer.pdf.DeutscheBankPDFExtractor) List(java.util.List) AssertImportActions(name.abuchen.portfolio.datatransfer.actions.AssertImportActions) Unit(name.abuchen.portfolio.model.Transaction.Unit) Optional(java.util.Optional) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) TransactionItem(name.abuchen.portfolio.datatransfer.Extractor.TransactionItem) ArrayList(java.util.ArrayList) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) IOException(java.io.IOException) AssertImportActions(name.abuchen.portfolio.datatransfer.actions.AssertImportActions) 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) DeutscheBankPDFExtractor(name.abuchen.portfolio.datatransfer.pdf.DeutscheBankPDFExtractor) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 43 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class SecurityPositionTest method testFIFOPurchasePriceWithForex.

@Test
public void testFIFOPurchasePriceWithForex() {
    CurrencyConverter currencyConverter = new TestCurrencyConverter().with(CurrencyUnit.USD);
    Security security = new Security("", CurrencyUnit.USD);
    PortfolioTransaction t = new PortfolioTransaction();
    t.setType(PortfolioTransaction.Type.DELIVERY_INBOUND);
    t.setDateTime(LocalDateTime.parse("2017-01-25T00:00"));
    t.setShares(Values.Share.factorize(13));
    t.setSecurity(security);
    t.setMonetaryAmount(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(9659.24)));
    t.addUnit(new Unit(Unit.Type.GROSS_VALUE, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(9644.24)), Money.of(CurrencyUnit.USD, Values.Amount.factorize(10287.13)), BigDecimal.valueOf(0.937470704040499)));
    t.addUnit(new Unit(Unit.Type.FEE, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(15)), Money.of(CurrencyUnit.USD, Values.Amount.factorize(16)), BigDecimal.valueOf(0.937470704040499)));
    List<PortfolioTransaction> tx = new ArrayList<>();
    tx.add(t);
    SecurityPosition position = new SecurityPosition(security, currencyConverter, new SecurityPrice(), tx);
    assertThat(position.getShares(), is(13L * Values.Share.factor()));
    // 10287.13 / 13 = 791.32
    assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(791.32))));
    assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(791.32))));
    // 9659.24 EUR x ( 1 / 0.937470704040499) = 10303,51
    assertThat(position.getFIFOPurchaseValue(), is(Money.of(CurrencyUnit.USD, Values.Amount.factorize(9659.24 * (1 / 0.937470704040499)))));
    Client client = new Client();
    client.addSecurity(security);
    Account a = new Account();
    client.addAccount(a);
    Portfolio p = new Portfolio();
    p.setReferenceAccount(a);
    p.addTransaction(t);
    client.addPortfolio(p);
    SecurityPerformanceSnapshot snapshot = SecurityPerformanceSnapshot.create(client, currencyConverter, new ReportingPeriod.FromXtoY(LocalDate.parse("2016-12-31"), LocalDate.parse("2017-02-01")));
    assertThat(snapshot.getRecords().size(), is(1));
    SecurityPerformanceRecord record = snapshot.getRecords().get(0);
    assertThat(record.getSecurity(), is(security));
    assertThat(record.getFifoCost(), is(position.getFIFOPurchaseValue()));
    assertThat(record.getFifoCostPerSharesHeld().toMoney(), is(position.getFIFOPurchasePrice()));
}
Also used : Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 44 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CheckCurrenciesAccountTransactionTest method testCheckTransactionUnitIfCurrenciesAreEqual.

@Test
public void testCheckTransactionUnitIfCurrenciesAreEqual() {
    Account account = new Account();
    account.setCurrencyCode("EUR");
    Security security = new Security("", "EUR");
    AccountTransaction t = new AccountTransaction();
    t.setMonetaryAmount(Money.of("EUR", 1_00));
    t.setSecurity(security);
    assertThat(action.process(t, account).getCode(), is(Status.Code.OK));
    Unit unit = new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 1_00), Money.of("USD", 2_00), BigDecimal.valueOf(0.5));
    t.addUnit(unit);
    assertThat(action.process(t, account).getCode(), is(Status.Code.ERROR));
}
Also used : Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Example 45 with Unit

use of name.abuchen.portfolio.model.Transaction.Unit in project portfolio by buchen.

the class CheckCurrenciesAccountTransactionTest method testCheckTransactionUnitIfCurrenciesAreDifferent.

@Test
public void testCheckTransactionUnitIfCurrenciesAreDifferent() {
    Account account = new Account();
    account.setCurrencyCode("EUR");
    Security security = new Security("", "USD");
    Unit unit = new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 1_00), Money.of("USD", 2_00), BigDecimal.valueOf(0.5));
    AccountTransaction t = new AccountTransaction();
    t.setMonetaryAmount(Money.of("EUR", 1_00));
    t.setSecurity(security);
    t.addUnit(unit);
    assertThat(action.process(t, account).getCode(), is(Status.Code.OK));
    t.removeUnit(unit);
    assertThat(action.process(t, account).getCode(), is(Status.Code.ERROR));
    Unit other = new Unit(Unit.Type.GROSS_VALUE, Money.of("EUR", 1_00), Money.of("JPY", 2_00), BigDecimal.valueOf(0.5));
    t.addUnit(other);
    assertThat(action.process(t, account).getCode(), is(Status.Code.ERROR));
}
Also used : Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Security(name.abuchen.portfolio.model.Security) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Aggregations

Unit (name.abuchen.portfolio.model.Transaction.Unit)75 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)58 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)51 Money (name.abuchen.portfolio.money.Money)51 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)50 Client (name.abuchen.portfolio.model.Client)50 IOException (java.io.IOException)44 Block (name.abuchen.portfolio.datatransfer.pdf.PDFParser.Block)37 DocumentType (name.abuchen.portfolio.datatransfer.pdf.PDFParser.DocumentType)37 Transaction (name.abuchen.portfolio.datatransfer.pdf.PDFParser.Transaction)37 BigDecimal (java.math.BigDecimal)32 Security (name.abuchen.portfolio.model.Security)26 Matcher (java.util.regex.Matcher)24 Pattern (java.util.regex.Pattern)24 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)24 Test (org.junit.Test)24 Map (java.util.Map)23 Values (name.abuchen.portfolio.money.Values)18 ArrayList (java.util.ArrayList)13 LocalDateTime (java.time.LocalDateTime)12