Ejemplo n.º 1
0
  @Test
  public void testThatPerformanceOfAnInvestmentIntoAnIndexIsIdenticalToIndex() {
    LocalDate startDate = LocalDate.of(2012, 1, 1);
    LocalDate endDate = LocalDate.of(2012, 4, 29); // a weekend
    long startPrice = Values.Quote.factorize(100);

    Client client = new Client();

    Security security =
        new SecurityBuilder() //
            .generatePrices(startPrice, startDate, endDate) //
            .addTo(client);

    PortfolioBuilder portfolio = new PortfolioBuilder(new Account());

    // add some buy transactions
    LocalDate date = startDate;
    while (date.isBefore(endDate)) {
      long p = security.getSecurityPrice(date).getValue();
      portfolio.inbound_delivery(security, date, Values.Share.factorize(100), p);
      date = date.plusDays(20);
    }

    portfolio.addTo(client);

    ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY(startDate, endDate);

    List<Exception> warnings = new ArrayList<Exception>();
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientIndex index = PerformanceIndex.forClient(client, converter, period, warnings);
    assertTrue(warnings.isEmpty());

    double[] accumulated = index.getAccumulatedPercentage();
    long lastPrice = security.getSecurityPrice(endDate).getValue();

    assertThat(
        (double) (lastPrice - startPrice) / (double) startPrice,
        IsCloseTo.closeTo(accumulated[accumulated.length - 1], PRECISION));

    PerformanceIndex benchmark = PerformanceIndex.forSecurity(index, security);
    assertThat(
        benchmark.getFinalAccumulatedPercentage(), is(index.getFinalAccumulatedPercentage()));
  }
Ejemplo n.º 2
0
  @Test
  public void testThatPerformanceOfInvestmentAndIndexIsIdendicalWhenInForeignCurrency() {
    LocalDate startDate = LocalDate.of(2015, 1, 1);
    LocalDate endDate = LocalDate.of(2015, 8, 1);
    long startPrice = Values.Quote.factorize(100);

    Client client = new Client();

    Security security =
        new SecurityBuilder("USD") //
            .generatePrices(startPrice, startDate, endDate) //
            .addTo(client);

    new PortfolioBuilder() //
        .inbound_delivery(security, "2014-01-01", Values.Share.factorize(100), 100) //
        .addTo(client);

    ReportingPeriod.FromXtoY period = new ReportingPeriod.FromXtoY(startDate, endDate);

    List<Exception> warnings = new ArrayList<Exception>();
    CurrencyConverter converter = new TestCurrencyConverter().with(CurrencyUnit.EUR);

    ClientIndex index = PerformanceIndex.forClient(client, converter, period, warnings);
    assertTrue(warnings.isEmpty());

    PerformanceIndex benchmark = PerformanceIndex.forSecurity(index, security);
    assertTrue(warnings.isEmpty());
    assertThat(
        benchmark.getFinalAccumulatedPercentage(), is(index.getFinalAccumulatedPercentage()));

    PerformanceIndex investment =
        PerformanceIndex.forInvestment(client, converter, security, period, warnings);
    assertTrue(warnings.isEmpty());
    assertThat(
        investment.getFinalAccumulatedPercentage(), is(index.getFinalAccumulatedPercentage()));
  }