Search in sources :

Example 16 with PortfolioBuilder

use of name.abuchen.portfolio.PortfolioBuilder in project portfolio by buchen.

the class CostCalculationTest method testThatRoundingDifferencesAreRemovedIfZeroSharesHeld.

@Test
public void testThatRoundingDifferencesAreRemovedIfZeroSharesHeld() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 10 * Values.Share.factor(), // 
    1).sell(security, "2010-02-01", 3 * Values.Share.factor(), // 
    1).sell(security, "2010-03-01", 3 * Values.Share.factor(), // 
    1).sell(security, "2010-03-01", 4 * Values.Share.factor(), // 
    1).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, 0L)));
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, 0L)));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 17 with PortfolioBuilder

use of name.abuchen.portfolio.PortfolioBuilder in project portfolio by buchen.

the class CostCalculationTest method testFifoBuySellTransactions.

@Test
public void testFifoBuySellTransactions() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 109 * Values.Share.factor(), // 
    Values.Amount.factorize(3149.20)).sell(security, "2010-02-01", 15 * Values.Share.factor(), // 
    Values.Amount.factorize(531.50)).buy(security, "2010-03-01", 52 * Values.Share.factor(), // 
    Values.Amount.factorize(1684.92)).buy(security, "2010-03-01", 32 * Values.Share.factor(), // 
    Values.Amount.factorize(959.30)).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    // expected:
    // 3149,20 - round(3149,20 * 15/109) + 1684,92 + 959,30 = 5360,04385
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(5360.04))));
    // expected moving average is identical because it is only one buy
    // transaction
    // 3149,20 * 94/109 + 1684.92 + 959.30 = 5360,04385
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(5360.04))));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 18 with PortfolioBuilder

use of name.abuchen.portfolio.PortfolioBuilder in project portfolio by buchen.

the class CostCalculationTest method testWhenSharesHeldGoToZero.

@Test
public void testWhenSharesHeldGoToZero() {
    Client client = new Client();
    Security security = // 
    new SecurityBuilder().addTo(client);
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 100 * Values.Share.factor(), // 
    314920).sell(security, "2010-02-01", 100 * Values.Share.factor(), // 
    53150).buy(security, "2010-03-01", 50 * Values.Share.factor(), // 
    168492).sell(security, "2010-04-01", 50 * Values.Share.factor(), // 
    53150).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, 0L)));
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, 0L)));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Client(name.abuchen.portfolio.model.Client) Security(name.abuchen.portfolio.model.Security) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Example 19 with PortfolioBuilder

use of name.abuchen.portfolio.PortfolioBuilder in project portfolio by buchen.

the class TransactionComparatorTest method testTwoInboundTransactionsStay.

@Test
public void testTwoInboundTransactionsStay() {
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 1, // 
    100).buy(security, "2010-01-01", 2, // 
    100).addTo(client);
    List<PortfolioTransaction> list = portfolio.getTransactions();
    Collections.sort(list, new TransactionComparator());
    assertThat(list.get(0).getShares(), is(1L));
    assertThat(list.get(1).getShares(), is(2L));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Test(org.junit.Test)

Example 20 with PortfolioBuilder

use of name.abuchen.portfolio.PortfolioBuilder in project portfolio by buchen.

the class TransactionComparatorTest method testBuyIsPreferredOverSell2.

@Test
public void testBuyIsPreferredOverSell2() {
    Portfolio portfolio = // 
    new PortfolioBuilder().buy(security, "2010-01-01", 100, // 
    100).sell(security, "2010-01-01", 100, // 
    100).addTo(client);
    List<PortfolioTransaction> list = portfolio.getTransactions();
    Collections.sort(list, new TransactionComparator());
    assertThat(list.get(0).getType(), is(Type.BUY));
    assertThat(list.get(1).getType(), is(Type.SELL));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioBuilder(name.abuchen.portfolio.PortfolioBuilder) Test(org.junit.Test)

Aggregations

PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)25 Test (org.junit.Test)22 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)21 Client (name.abuchen.portfolio.model.Client)20 Security (name.abuchen.portfolio.model.Security)19 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)18 Portfolio (name.abuchen.portfolio.model.Portfolio)17 LocalDate (java.time.LocalDate)9 AccountBuilder (name.abuchen.portfolio.AccountBuilder)9 Account (name.abuchen.portfolio.model.Account)8 TaxonomyBuilder (name.abuchen.portfolio.TaxonomyBuilder)7 Taxonomy (name.abuchen.portfolio.model.Taxonomy)7 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)5 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)5 ArrayList (java.util.ArrayList)3 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)2 Unit (name.abuchen.portfolio.model.Transaction.Unit)2 Before (org.junit.Before)2 LocalDateTime (java.time.LocalDateTime)1 Month (java.time.Month)1