use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot 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.snapshot.security.SecurityPerformanceSnapshot in project portfolio by buchen.
the class Issue371PurchaseValueWithTransfers method testPurchaseValueOfSecurityPositionWithTransfers.
@Test
public void testPurchaseValueOfSecurityPositionWithTransfers() throws IOException {
Client client = ClientFactory.load(Issue371PurchaseValueWithTransfers.class.getResourceAsStream(// $NON-NLS-1$
"Issue371PurchaseValueWithTransfers.xml"));
Security adidas = client.getSecurities().get(0);
// $NON-NLS-1$
assertThat(adidas.getName(), is("Adidas AG"));
ReportingPeriod period = new // $NON-NLS-1$
ReportingPeriod.FromXtoY(// $NON-NLS-1$
LocalDate.parse("2010-11-20"), // $NON-NLS-1$
LocalDate.parse("2015-11-20"));
// make sure that the transfer entry exists
assertThat(client.getPortfolios().size(), is(2));
assertThat(client.getPortfolios().stream().flatMap(p -> p.getTransactions().stream()).filter(t -> t.getSecurity() == adidas).filter(t -> t.getCrossEntry() instanceof PortfolioTransferEntry).filter(t -> t.getType() == PortfolioTransaction.Type.TRANSFER_IN).findAny().isPresent(), is(true));
CurrencyConverter converter = new TestCurrencyConverter();
ClientSnapshot snapshot = ClientSnapshot.create(client, converter, period.getEndDate());
SecurityPosition securityPosition = snapshot.getPositionsByVehicle().get(adidas).getPosition();
SecurityPerformanceSnapshot securitySnapshot = SecurityPerformanceSnapshot.create(client, converter, period);
SecurityPerformanceRecord securityRecord = securitySnapshot.getRecords().get(0);
assertThat(securityRecord.getSecurity(), is(adidas));
assertThat(securityPosition.getFIFOPurchaseValue(), is(securityRecord.getFifoCost()));
}
use of name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot in project portfolio by buchen.
the class Issue672CapitalGainsIfSecurityIsTransferred method testPurchaseValueOfSecurityPositionWithTransfers.
@Test
public void testPurchaseValueOfSecurityPositionWithTransfers() throws IOException {
Client client = ClientFactory.load(Issue371PurchaseValueWithTransfers.class.getResourceAsStream(// $NON-NLS-1$
"Issue672CapitalGainsIfSecurityIsTransferred.xml"));
CurrencyConverter converter = new TestCurrencyConverter();
Portfolio secondPortfolio = client.getPortfolios().get(1);
ReportingPeriod period = new // $NON-NLS-1$
ReportingPeriod.FromXtoY(// $NON-NLS-1$
LocalDate.parse("2016-01-01"), // $NON-NLS-1$
LocalDate.parse("2017-01-01"));
SecurityPerformanceSnapshot snapshot = SecurityPerformanceSnapshot.create(client, converter, secondPortfolio, period);
assertThat(snapshot.getRecords().size(), is(1));
SecurityPerformanceRecord record = snapshot.getRecords().get(0);
assertThat(record.getMarketValue(), is(Money.of(CurrencyUnit.EUR, Values.Money.factorize(971.41))));
assertThat(record.getFifoCost(), is(Money.of(CurrencyUnit.EUR, Values.Money.factorize(883.1))));
assertThat(record.getCapitalGainsOnHoldings(), is(Money.of(CurrencyUnit.EUR, Values.Money.factorize(88.31))));
assertThat(record.getCapitalGainsOnHoldingsPercent(), is(IsCloseTo.closeTo(0.1d, 0.0000000001)));
}
Aggregations