use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class SecurityIndexTest method testWhenQuotesAreOnlyAvailableFromTheMiddleOfTheReportInterval.
@Test
public void testWhenQuotesAreOnlyAvailableFromTheMiddleOfTheReportInterval() {
LocalDate startDate = LocalDate.of(2012, 12, 31);
LocalDate middleDate = LocalDate.of(2013, 2, 18);
LocalDate endDate = LocalDate.of(2013, 4, 1);
// create model
Client client = new Client();
//
new AccountBuilder().deposit_(startDate.atStartOfDay(), //
100 * Values.Amount.factor()).interest(startDate.atStartOfDay().plusDays(10), //
10 * Values.Amount.factor()).addTo(client);
Security security = //
new SecurityBuilder().generatePrices(50 * Values.Amount.factor(), middleDate, //
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[] clientDates = clientIndex.getDates();
LocalDate[] securityDates = securityIndex.getDates();
assertThat(securityDates[0], is(middleDate));
assertThat(securityDates[securityDates.length - 1], is(endDate));
assertThat(clientDates[clientDates.length - 1], is(securityDates[securityDates.length - 1]));
double[] clientAccumulated = clientIndex.getAccumulatedPercentage();
double[] securityAccumulated = securityIndex.getAccumulatedPercentage();
int index = Dates.daysBetween(startDate, middleDate);
assertThat(clientDates[index], is(middleDate));
assertThat(securityAccumulated[0], IsCloseTo.closeTo(clientAccumulated[index], 0.000001d));
long middlePrice = security.getSecurityPrice(middleDate).getValue();
long lastPrice = security.getSecurityPrice(endDate).getValue();
// 10% is interest of the deposit
double performance = (double) (lastPrice - middlePrice) / (double) middlePrice + 0.1d;
assertThat(securityAccumulated[securityAccumulated.length - 1], IsCloseTo.closeTo(performance, 0.000001d));
}
use of name.abuchen.portfolio.TestCurrencyConverter 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()));
}
use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class SecurityPositionTest method testFIFOPurchasePrice.
@Test
public void testFIFOPurchasePrice() {
List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 100000, null, 100 * Values.Share.factor(), Type.BUY, 0, 0));
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 50000, 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, 10_00)));
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_00)));
}
use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class SecurityPositionTest method testPurchasePriceWithMultipleBuyTransactionsMiddlePrice.
@Test
public void testPurchasePriceWithMultipleBuyTransactionsMiddlePrice() {
List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 75000, null, 75 * Values.Share.factor(), Type.BUY, 0, 0));
tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 50000, null, 25 * 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, 15_00)));
// expected: (750 + 500) * (50/100) / 50 (shares held)
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 12_50)));
}
use of name.abuchen.portfolio.TestCurrencyConverter in project portfolio by buchen.
the class SecurityPositionTest method testThatTransferInCountsIfTransferOutIsMissing.
@Test
public void testThatTransferInCountsIfTransferOutIsMissing() {
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.TRANSFER_IN, 0, 0));
SecurityPosition position = new SecurityPosition(new Security(), new TestCurrencyConverter(), price, tx);
assertThat(position.getShares(), is(50L * Values.Share.factor()));
assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_00)));
assertThat(position.getFIFOPurchaseValue(), is(Money.of(CurrencyUnit.EUR, 500_00)));
assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_00)));
assertThat(position.getMovingAveragePurchaseValue(), is(Money.of(CurrencyUnit.EUR, 500_00)));
assertThat(position.calculateValue(), is(Money.of(CurrencyUnit.EUR, 1000_00)));
assertThat(position.getProfitLoss(), is(Money.of(CurrencyUnit.EUR, 500_00)));
}
Aggregations