Search in sources :

Example 6 with ExchangeRate

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

the class ChainedExchangeRateTimeSeriesTest method testChainedLookupOfExchangeRate.

@Test
public void testChainedLookupOfExchangeRate() {
    ExchangeRateTimeSeriesImpl first = new ExchangeRateTimeSeriesImpl(null, "EUR", "USD");
    first.addRate(new ExchangeRate(LocalDate.parse("2014-12-01"), BigDecimal.valueOf(1)));
    first.addRate(new ExchangeRate(LocalDate.parse("2014-12-02"), BigDecimal.valueOf(2)));
    first.addRate(new ExchangeRate(LocalDate.parse("2014-12-03"), BigDecimal.valueOf(3)));
    ExchangeRateTimeSeriesImpl second = new ExchangeRateTimeSeriesImpl(null, "USD", "CHF");
    second.addRate(new ExchangeRate(LocalDate.parse("2014-12-01"), BigDecimal.valueOf(1)));
    second.addRate(new ExchangeRate(LocalDate.parse("2014-12-02"), BigDecimal.valueOf(2)));
    second.addRate(new ExchangeRate(LocalDate.parse("2014-12-03"), BigDecimal.valueOf(3)));
    ChainedExchangeRateTimeSeries chained = new ChainedExchangeRateTimeSeries(first, second);
    assertThat(chained.lookupRate(LocalDate.parse("2014-11-30")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(chained.lookupRate(LocalDate.parse("2014-12-01")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(chained.lookupRate(LocalDate.parse("2014-12-02")).get().getValue(), is(BigDecimal.valueOf(4)));
    assertThat(chained.lookupRate(LocalDate.parse("2014-12-03")).get().getValue(), is(BigDecimal.valueOf(9)));
    assertThat(chained.lookupRate(LocalDate.parse("2014-12-04")).get().getValue(), is(BigDecimal.valueOf(9)));
    assertThat(chained.getBaseCurrency(), is("EUR"));
    assertThat(chained.getTermCurrency(), is("CHF"));
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) Test(org.junit.Test)

Example 7 with ExchangeRate

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

the class ExchangeRateTimeSeriesImplTest method testLookupOfExchangeRateWithOneRateOnly.

@Test
public void testLookupOfExchangeRateWithOneRateOnly() {
    ExchangeRateTimeSeriesImpl series = new ExchangeRateTimeSeriesImpl();
    series.addRate(new ExchangeRate(LocalDate.parse("2014-12-01"), BigDecimal.valueOf(1)));
    assertThat(series.lookupRate(LocalDate.parse("2014-11-30")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(series.lookupRate(LocalDate.parse("2014-12-01")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(series.lookupRate(LocalDate.parse("2014-12-02")).get().getValue(), is(BigDecimal.valueOf(1)));
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) Test(org.junit.Test)

Example 8 with ExchangeRate

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

the class ExchangeRateTimeSeriesImplTest method testLookupOfExchangeRateWithGaps.

@Test
public void testLookupOfExchangeRateWithGaps() {
    ExchangeRateTimeSeriesImpl series = new ExchangeRateTimeSeriesImpl();
    series.addRate(new ExchangeRate(LocalDate.parse("2014-12-01"), BigDecimal.valueOf(1)));
    series.addRate(new ExchangeRate(LocalDate.parse("2014-12-03"), BigDecimal.valueOf(3)));
    assertThat(series.lookupRate(LocalDate.parse("2014-11-30")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(series.lookupRate(LocalDate.parse("2014-12-01")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(series.lookupRate(LocalDate.parse("2014-12-02")).get().getValue(), is(BigDecimal.valueOf(1)));
    assertThat(series.lookupRate(LocalDate.parse("2014-12-03")).get().getValue(), is(BigDecimal.valueOf(3)));
    assertThat(series.lookupRate(LocalDate.parse("2014-12-04")).get().getValue(), is(BigDecimal.valueOf(3)));
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) Test(org.junit.Test)

Example 9 with ExchangeRate

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

the class ExchangeRatesListView method refreshChart.

private void refreshChart(ExchangeRateTimeSeries series) {
    try {
        chart.suspendUpdate(true);
        for (ISeries s : chart.getSeriesSet().getSeries()) chart.getSeriesSet().deleteSeries(s.getId());
        if (series == null || series.getRates().isEmpty()) {
            chart.getTitle().setText(Messages.LabelCurrencies);
            return;
        }
        List<ExchangeRate> rates = series.getRates();
        LocalDate[] dates = new LocalDate[rates.size()];
        double[] values = new double[rates.size()];
        int ii = 0;
        for (ExchangeRate rate : rates) {
            dates[ii] = rate.getTime();
            values[ii] = rate.getValue().doubleValue();
            ii++;
        }
        String title = // $NON-NLS-1$
        MessageFormat.format(// $NON-NLS-1$
        "{0}/{1} ({2})", series.getBaseCurrency(), series.getTermCurrency(), series.getProvider().getName());
        chart.getTitle().setText(title);
        chart.addDateSeries(dates, values, Colors.TOTALS, title);
        chart.adjustRange();
    } finally {
        chart.suspendUpdate(false);
        chart.redraw();
    }
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) ISeries(org.swtchart.ISeries) LocalDate(java.time.LocalDate)

Example 10 with ExchangeRate

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

Aggregations

ExchangeRate (name.abuchen.portfolio.money.ExchangeRate)23 Test (org.junit.Test)8 ExchangeRateTimeSeries (name.abuchen.portfolio.money.ExchangeRateTimeSeries)6 BigDecimal (java.math.BigDecimal)5 LocalDate (java.time.LocalDate)5 HashMap (java.util.HashMap)3 List (java.util.List)3 Column (name.abuchen.portfolio.ui.util.viewers.Column)3 ColumnLabelProvider (org.eclipse.jface.viewers.ColumnLabelProvider)3 Composite (org.eclipse.swt.widgets.Composite)3 File (java.io.File)2 MessageFormat (java.text.MessageFormat)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Inject (javax.inject.Inject)2 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)2 Values (name.abuchen.portfolio.money.Values)2 Messages (name.abuchen.portfolio.ui.Messages)2 Colors (name.abuchen.portfolio.ui.util.Colors)2 ShowHideColumnHelper (name.abuchen.portfolio.ui.util.viewers.ShowHideColumnHelper)2