Search in sources :

Example 36 with TestCurrencyConverter

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

the class SecurityPositionTest method testPurchasePriceWithMultipleBuyTransactions.

@Test
public void testPurchasePriceWithMultipleBuyTransactions() {
    List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
    tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 25000, null, 25 * Values.Share.factor(), Type.BUY, 0, 0));
    tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 150000, null, 75 * Values.Share.factor(), Type.BUY, 0, 0));
    tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 100000, null, 50 * Values.Share.factor(), Type.SELL, 0, 0));
    SecurityPosition position = new SecurityPosition(new Security(), new TestCurrencyConverter(), new SecurityPrice(), tx);
    assertThat(position.getShares(), is(50L * Values.Share.factor()));
    assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.EUR, 20_00)));
    // expected: (250 + 1500) * (50/100) / 50 (shares held)
    assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 17_50)));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) ArrayList(java.util.ArrayList) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) Test(org.junit.Test)

Example 37 with TestCurrencyConverter

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

the class SecurityPositionTest method testThatTransferInCountsIfTransferOutIsMissingPlusBuyTransaction.

@Test
public void testThatTransferInCountsIfTransferOutIsMissingPlusBuyTransaction() {
    SecurityPrice price = new SecurityPrice(LocalDate.of(2012, Month.DECEMBER, 2), Values.Quote.factorize(20));
    List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
    tx.add(new PortfolioTransaction(LocalDateTime.of(2012, Month.JANUARY, 1, 0, 0), CurrencyUnit.EUR, 50000, null, 50 * Values.Share.factor(), Type.BUY, 0, 0));
    tx.add(new PortfolioTransaction(LocalDateTime.of(2012, Month.FEBRUARY, 1, 0, 0), CurrencyUnit.EUR, 55000, null, 50 * Values.Share.factor(), Type.TRANSFER_IN, 0, 0));
    SecurityPosition position = new SecurityPosition(new Security(), new TestCurrencyConverter(), price, tx);
    assertThat(position.getShares(), is(100L * Values.Share.factor()));
    assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_50)));
    assertThat(position.getFIFOPurchaseValue(), is(Money.of(CurrencyUnit.EUR, 1050_00)));
    assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_50)));
    assertThat(position.getMovingAveragePurchaseValue(), is(Money.of(CurrencyUnit.EUR, 1050_00)));
    assertThat(position.calculateValue(), is(Money.of(CurrencyUnit.EUR, 2000_00)));
    assertThat(position.getProfitLoss(), is(Money.of(CurrencyUnit.EUR, 950_00)));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) ArrayList(java.util.ArrayList) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) Test(org.junit.Test)

Example 38 with TestCurrencyConverter

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

the class CostCalculationTest method testFifoBuySellTransactions2.

@Test
public void testFifoBuySellTransactions2() {
    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)).buy(security, "2010-02-01", 52 * Values.Share.factor(), // 
    Values.Amount.factorize(1684.92)).sell(security, "2010-03-01", 15 * Values.Share.factor(), // 
    Values.Amount.factorize(531.50)).addTo(client);
    CostCalculation cost = new CostCalculation();
    cost.setTermCurrency(CurrencyUnit.EUR);
    cost.visitAll(new TestCurrencyConverter(), portfolio.getTransactions());
    // expected:
    // 3149,20 + 1684,92 - round(3149,20 * 15/109) = 4400,743853211009174
    assertThat(cost.getFifoCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(4400.74))));
    // expected moving average is identical because it is only one buy
    // transaction
    // (3149,20 + 1684.92) * 146/161 = 4383,736149068322981
    assertThat(cost.getMovingAverageCost(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(4383.74))));
}
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 39 with TestCurrencyConverter

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

the class IRRCalculationTest method testDividendPaymentsWithTaxes.

@Test
public void testDividendPaymentsWithTaxes() {
    List<Transaction> tx = new ArrayList<>();
    Security security = new Security();
    tx.add(new // 
    PortfolioTransaction(// 
    LocalDateTime.of(2015, Month.DECEMBER, 31, 0, 0), // 
    CurrencyUnit.EUR, // 
    Values.Amount.factorize(1000), // 
    security, // 
    Values.Share.factorize(10), // 
    PortfolioTransaction.Type.BUY, Values.Amount.factorize(10), 0));
    DividendTransaction t = new DividendTransaction();
    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));
    t.addUnit(new Unit(Unit.Type.TAX, Money.of(CurrencyUnit.EUR, Values.Amount.factorize(50))));
    tx.add(t);
    tx.add(new // 
    PortfolioTransaction(// 
    LocalDateTime.of(2016, Month.DECEMBER, 31, 0, 0), // 
    CurrencyUnit.EUR, // 
    Values.Amount.factorize(1200), // 
    security, // 
    Values.Share.factorize(10), // 
    PortfolioTransaction.Type.SELL, Values.Amount.factorize(10), Values.Amount.factorize(30)));
    IRRCalculation calculation = Calculation.perform(IRRCalculation.class, new TestCurrencyConverter(), tx);
    // Excel verification
    // 31.12.15 -1000
    // 01.06.16 150
    // 31.12.16 1230
    // =XINTZINSFUSS(B1:B3;A1:A3) = 0,412128788
    assertThat(calculation.getIRR(), IsCloseTo.closeTo(0.412128788d, 0.00000001d));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Transaction(name.abuchen.portfolio.model.Transaction) ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) Test(org.junit.Test)

Example 40 with TestCurrencyConverter

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

the class SecurityIndexTest method testThatSecurityIndexIsCalculated.

@Test
public void testThatSecurityIndexIsCalculated() {
    LocalDate startDate = LocalDate.of(2012, 12, 31);
    LocalDate endDate = LocalDate.of(2013, 4, 1);
    long startPrice = 100 * Values.Amount.factor();
    // create model
    Client client = new Client();
    // 
    new AccountBuilder().deposit_(startDate.atStartOfDay(), // 
    startPrice).addTo(client);
    Security security = // 
    new SecurityBuilder().generatePrices(startPrice, startDate, // 
    endDate).addTo(client);
    // calculate performance indices
    List<Exception> warnings = new ArrayList<Exception>();
    ReportingPeriod reportInterval = new ReportingPeriod.FromXtoY(startDate, endDate);
    CurrencyConverter converter = new TestCurrencyConverter();
    PerformanceIndex clientIndex = PerformanceIndex.forClient(client, converter, reportInterval, warnings);
    PerformanceIndex securityIndex = PerformanceIndex.forSecurity(clientIndex, security);
    // asserts
    assertTrue(warnings.isEmpty());
    LocalDate[] dates = securityIndex.getDates();
    assertThat(dates[0], is(startDate));
    assertThat(dates[dates.length - 1], is(endDate));
    long lastPrice = security.getSecurityPrice(endDate).getValue();
    double performance = (double) (lastPrice - startPrice) / (double) startPrice;
    double[] accumulated = securityIndex.getAccumulatedPercentage();
    assertThat(accumulated[0], is(0d));
    assertThat(accumulated[accumulated.length - 1], IsCloseTo.closeTo(performance, 0.000001d));
}
Also used : ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) AccountBuilder(name.abuchen.portfolio.AccountBuilder) Client(name.abuchen.portfolio.model.Client) SecurityBuilder(name.abuchen.portfolio.SecurityBuilder) Test(org.junit.Test)

Aggregations

TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)74 Test (org.junit.Test)72 Client (name.abuchen.portfolio.model.Client)55 Security (name.abuchen.portfolio.model.Security)46 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)38 ArrayList (java.util.ArrayList)31 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)26 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)23 Portfolio (name.abuchen.portfolio.model.Portfolio)21 AccountBuilder (name.abuchen.portfolio.AccountBuilder)19 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)19 LocalDate (java.time.LocalDate)18 Account (name.abuchen.portfolio.model.Account)18 SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)15 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)13 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)10 Taxonomy (name.abuchen.portfolio.model.Taxonomy)8 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)8 PerformanceIndex (name.abuchen.portfolio.snapshot.PerformanceIndex)8 IOException (java.io.IOException)7