Search in sources :

Example 21 with CurrencyConverter

use of name.abuchen.portfolio.money.CurrencyConverter in project portfolio by buchen.

the class StatementOfAssetsViewer method addCurrencyColumns.

private // NOSONAR
void addCurrencyColumns() {
    // $NON-NLS-1$
    Column column = new Column("baseCurrency", Messages.ColumnCurrency, SWT.LEFT, 80);
    column.setGroupLabel(Messages.ColumnForeignCurrencies);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object e) {
            Element element = (Element) e;
            if (!element.isPosition())
                return null;
            return element.getPosition().getInvestmentVehicle().getCurrencyCode();
        }
    });
    column.setComparator(new ElementComparator(new AttributeComparator(e -> ((Element) e).isPosition() ? ((Element) e).getPosition().getInvestmentVehicle().getCurrencyCode() : null)));
    column.setVisible(false);
    support.addColumn(column);
    // $NON-NLS-1$
    column = new Column("exchangeRate", Messages.ColumnExchangeRate, SWT.RIGHT, 80);
    column.setGroupLabel(Messages.ColumnForeignCurrencies);
    column.setLabelProvider(new // NOSONAR
    ColumnLabelProvider() {

        @Override
        public String getText(Object e) {
            Element element = (Element) e;
            if (!element.isPosition())
                return null;
            String baseCurrency = element.getPosition().getInvestmentVehicle().getCurrencyCode();
            CurrencyConverter converter = getCurrencyConverter();
            ExchangeRate rate = converter.getRate(getDate(), baseCurrency);
            if (useIndirectQuotation)
                rate = rate.inverse();
            return Values.ExchangeRate.format(rate.getValue());
        }

        @Override
        public String getToolTipText(Object e) {
            String text = getText(e);
            if (text == null)
                return null;
            String term = getCurrencyConverter().getTermCurrency();
            String base = ((Element) e).getPosition().getInvestmentVehicle().getCurrencyCode();
            return text + ' ' + (useIndirectQuotation ? base + '/' + term : term + '/' + base);
        }
    });
    column.setVisible(false);
    support.addColumn(column);
    column = new // $NON-NLS-1$
    Column(// $NON-NLS-1$
    "marketValueBaseCurrency", Messages.ColumnMarketValue + Messages.BaseCurrencyCue, SWT.RIGHT, 80);
    column.setDescription(Messages.ColumnMarketValueBaseCurrency);
    column.setGroupLabel(Messages.ColumnForeignCurrencies);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object e) {
            Element element = (Element) e;
            if (!element.isPosition())
                return null;
            return Values.Money.format(element.getPosition().getPosition().calculateValue(), client.getBaseCurrency());
        }
    });
    column.setComparator(new ElementComparator(new AttributeComparator(e -> ((Element) e).isPosition() ? ((Element) e).getPosition().getPosition().calculateValue() : null)));
    column.setVisible(false);
    support.addColumn(column);
    column = new // $NON-NLS-1$
    Column(// $NON-NLS-1$
    "purchaseValueBaseCurrency", Messages.ColumnPurchaseValue + Messages.BaseCurrencyCue, SWT.RIGHT, 80);
    column.setDescription(Messages.ColumnPurchaseValueBaseCurrency);
    column.setGroupLabel(Messages.ColumnForeignCurrencies);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object e) {
            Element element = (Element) e;
            if (!element.isPosition())
                return null;
            return Values.Money.formatNonZero(element.getPosition().getPosition().getFIFOPurchaseValue(), client.getBaseCurrency());
        }
    });
    column.setComparator(new ElementComparator(new AttributeComparator(e -> ((Element) e).isPosition() ? ((Element) e).getPosition().getPosition().getFIFOPurchaseValue() : null)));
    column.setVisible(false);
    support.addColumn(column);
    column = new // $NON-NLS-1$
    Column(// $NON-NLS-1$
    "profitLossBaseCurrency", Messages.ColumnProfitLoss + Messages.BaseCurrencyCue, SWT.RIGHT, 80);
    column.setDescription(Messages.ColumnProfitLossBaseCurrency);
    column.setGroupLabel(Messages.ColumnForeignCurrencies);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object e) {
            Element element = (Element) e;
            if (!element.isPosition())
                return null;
            return Values.Money.formatNonZero(element.getPosition().getPosition().getProfitLoss(), client.getBaseCurrency());
        }
    });
    column.setComparator(new ElementComparator(new AttributeComparator(e -> ((Element) e).isPosition() ? ((Element) e).getPosition().getPosition().getProfitLoss() : null)));
    column.setVisible(false);
    support.addColumn(column);
}
Also used : ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) NameColumnLabelProvider(name.abuchen.portfolio.ui.views.columns.NameColumn.NameColumnLabelProvider) ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) NameColumn(name.abuchen.portfolio.ui.views.columns.NameColumn) TaxonomyColumn(name.abuchen.portfolio.ui.views.columns.TaxonomyColumn) AttributeColumn(name.abuchen.portfolio.ui.views.columns.AttributeColumn) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) IsinColumn(name.abuchen.portfolio.ui.views.columns.IsinColumn) Column(name.abuchen.portfolio.ui.util.viewers.Column) AttributeComparator(name.abuchen.portfolio.ui.util.AttributeComparator) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 22 with CurrencyConverter

use of name.abuchen.portfolio.money.CurrencyConverter 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)

Example 23 with CurrencyConverter

use of name.abuchen.portfolio.money.CurrencyConverter 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));
}
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)

Example 24 with CurrencyConverter

use of name.abuchen.portfolio.money.CurrencyConverter 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()));
}
Also used : Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) ArrayList(java.util.ArrayList) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Security(name.abuchen.portfolio.model.Security) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) Unit(name.abuchen.portfolio.model.Transaction.Unit) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 25 with CurrencyConverter

use of name.abuchen.portfolio.money.CurrencyConverter in project portfolio by buchen.

the class ClassificationIndexTest method testThat100PercentAssignmentIsIdenticalToClientPerformance.

@Test
public void testThat100PercentAssignmentIsIdenticalToClientPerformance() {
    Client client = createClient(Classification.ONE_HUNDRED_PERCENT);
    Classification classification = client.getTaxonomies().get(0).getClassificationById("one");
    List<Exception> warnings = new ArrayList<Exception>();
    CurrencyConverter converter = new TestCurrencyConverter();
    PerformanceIndex iClient = PerformanceIndex.forClient(client, converter, period, warnings);
    PerformanceIndex iClassification = PerformanceIndex.forClassification(client, converter, classification, period, warnings);
    assertThat(warnings.isEmpty(), is(true));
    assertThat(iClient.getDates(), is(iClassification.getDates()));
    assertThat(iClient.getAccumulatedPercentage(), is(iClassification.getAccumulatedPercentage()));
    assertThat(iClient.getDeltaPercentage(), is(iClassification.getDeltaPercentage()));
    assertThat(iClient.getTotals(), is(iClassification.getTotals()));
    assertThat(iClient.getTransferals(), is(iClassification.getTransferals()));
}
Also used : TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Classification(name.abuchen.portfolio.model.Classification) ArrayList(java.util.ArrayList) Client(name.abuchen.portfolio.model.Client) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) Test(org.junit.Test)

Aggregations

CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)53 Client (name.abuchen.portfolio.model.Client)41 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)38 Test (org.junit.Test)36 Security (name.abuchen.portfolio.model.Security)19 ArrayList (java.util.ArrayList)18 AccountBuilder (name.abuchen.portfolio.AccountBuilder)15 Account (name.abuchen.portfolio.model.Account)15 LocalDate (java.time.LocalDate)13 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)12 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)11 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)9 CurrencyConverterImpl (name.abuchen.portfolio.money.CurrencyConverterImpl)9 Portfolio (name.abuchen.portfolio.model.Portfolio)7 SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)6 Transaction (name.abuchen.portfolio.model.Transaction)6 Unit (name.abuchen.portfolio.model.Transaction.Unit)6 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)6 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)6 Classification (name.abuchen.portfolio.model.Classification)5