Search in sources :

Example 36 with Portfolio

use of name.abuchen.portfolio.model.Portfolio 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 37 with Portfolio

use of name.abuchen.portfolio.model.Portfolio in project portfolio by buchen.

the class PortfolioClientFilterTest method testThatDividendTransactionAreIncluded.

@Test
public void testThatDividendTransactionAreIncluded() {
    Portfolio portfolio = client.getPortfolios().get(0);
    Client result = new PortfolioClientFilter(portfolio).filter(client);
    assertThat(result.getPortfolios().size(), is(1));
    assertThat(result.getAccounts().size(), is(1));
    Account account = result.getAccounts().get(0);
    assertThat(account.getTransactions().size(), is(2));
    assertThat(account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.DIVIDENDS).findAny().isPresent(), is(true));
    assertThat(account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.REMOVAL).findAny().isPresent(), is(true));
    assertThat(AccountSnapshot.create(account, new TestCurrencyConverter(), LocalDate.parse("2016-09-03")).getFunds(), is(Money.of(CurrencyUnit.EUR, 0)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Account(name.abuchen.portfolio.model.Account) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 38 with Portfolio

use of name.abuchen.portfolio.model.Portfolio in project portfolio by buchen.

the class PortfolioClientFilterTest method testCrossPortfolioTransfersAreKept.

@Test
public void testCrossPortfolioTransfersAreKept() {
    Portfolio portfolioA = client.getPortfolios().get(0);
    Portfolio portfolioB = client.getPortfolios().get(1);
    PortfolioTransferEntry entry = new PortfolioTransferEntry(portfolioA, portfolioB);
    entry.setDate(LocalDateTime.parse("2016-04-01T00:00"));
    entry.setAmount(Values.Amount.factorize(10));
    entry.setShares(Values.Share.factorize(1));
    entry.setSecurity(client.getSecurities().get(0));
    entry.insert();
    Client result = new PortfolioClientFilter(Arrays.asList(portfolioA, portfolioB), Collections.emptyList()).filter(client);
    assertThat(result.getPortfolios().size(), is(2));
    assertThat(result.getAccounts().size(), is(2));
    Portfolio portfolio = result.getPortfolios().get(0);
    // check that the 4 transactions are transformed:
    // - buy -> inbound delivery
    // - transfer must exist
    assertThat(portfolio.getTransactions().size(), is(2));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-02-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.TRANSFER_OUT).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Test(org.junit.Test)

Example 39 with Portfolio

use of name.abuchen.portfolio.model.Portfolio in project portfolio by buchen.

the class PortfolioClientFilterTest method testCrossPortfolioTransfersAreConvertedToDeliveries.

@Test
public void testCrossPortfolioTransfersAreConvertedToDeliveries() {
    Portfolio portfolioA = client.getPortfolios().get(0);
    Portfolio portfolioB = client.getPortfolios().get(1);
    PortfolioTransferEntry entry = new PortfolioTransferEntry(portfolioA, portfolioB);
    entry.setDate(LocalDateTime.parse("2016-04-01T00:00"));
    entry.setAmount(Values.Amount.factorize(10));
    entry.setShares(Values.Share.factorize(1));
    entry.setSecurity(client.getSecurities().get(0));
    entry.insert();
    Client result = new PortfolioClientFilter(Arrays.asList(portfolioA), Collections.emptyList()).filter(client);
    assertThat(result.getPortfolios().size(), is(1));
    assertThat(result.getAccounts().size(), is(1));
    Portfolio portfolio = result.getPortfolios().get(0);
    // check that the 4 transactions are transformed:
    // - buy -> inbound delivery
    // - transfer -> outbound delivery
    assertThat(portfolio.getTransactions().size(), is(2));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-02-01T00:00"))).findAny().isPresent(), is(true));
    assertThat(// 
    portfolio.getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_OUTBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
    // check the other portfolio
    result = new PortfolioClientFilter(Arrays.asList(portfolioB), Collections.emptyList()).filter(client);
    assertThat(// 
    result.getPortfolios().get(0).getTransactions().stream().filter(// 
    t -> t.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND).filter(t -> t.getDateTime().equals(LocalDateTime.parse("2016-04-01T00:00"))).findAny().isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Test(org.junit.Test)

Example 40 with Portfolio

use of name.abuchen.portfolio.model.Portfolio in project portfolio by buchen.

the class PortfolioClientFilterTest method testThatFullReferenceAccountIsIncluded.

@Test
public void testThatFullReferenceAccountIsIncluded() {
    Portfolio portfolio = client.getPortfolios().get(0);
    Client result = new PortfolioClientFilter(portfolio, portfolio.getReferenceAccount()).filter(client);
    assertThat(result.getPortfolios().size(), is(1));
    assertThat(result.getAccounts().size(), is(1));
    Account account = result.getAccounts().get(0);
    // 3 transactions: buy, dividend, and deposit
    assertThat(account.getTransactions().size(), is(3));
    assertThat(account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.BUY).findAny().isPresent(), is(true));
    assertThat(account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.DIVIDENDS).findAny().isPresent(), is(true));
    assertThat(account.getTransactions().stream().filter(t -> t.getType() == AccountTransaction.Type.DEPOSIT).findAny().isPresent(), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) AccountSnapshot(name.abuchen.portfolio.snapshot.AccountSnapshot) LocalDateTime(java.time.LocalDateTime) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) AccountBuilder(name.abuchen.portfolio.AccountBuilder) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Before(org.junit.Before) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) LocalDate(java.time.LocalDate) Collections(java.util.Collections) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Aggregations

Portfolio (name.abuchen.portfolio.model.Portfolio)75 Security (name.abuchen.portfolio.model.Security)52 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)44 Test (org.junit.Test)41 Account (name.abuchen.portfolio.model.Account)39 Client (name.abuchen.portfolio.model.Client)38 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)24 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)21 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)20 Unit (name.abuchen.portfolio.model.Transaction.Unit)19 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)17 LocalDate (java.time.LocalDate)16 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)12 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)11 Money (name.abuchen.portfolio.money.Money)11 ArrayList (java.util.ArrayList)10 Values (name.abuchen.portfolio.money.Values)10 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)9 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)9 Collections (java.util.Collections)8