Search in sources :

Example 16 with ExchangeRate

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

the class InverseExchangeRateTimeSeriesTest method testInverseLookupOfExchangeRate.

@Test
public void testInverseLookupOfExchangeRate() {
    ExchangeRateTimeSeriesImpl source = new ExchangeRateTimeSeriesImpl(null, "EUR", "USD");
    source.addRate(new ExchangeRate(LocalDate.parse("2014-12-01"), BigDecimal.valueOf(1)));
    source.addRate(new ExchangeRate(LocalDate.parse("2014-12-02"), BigDecimal.valueOf(2)));
    source.addRate(new ExchangeRate(LocalDate.parse("2014-12-03"), BigDecimal.valueOf(3)));
    InverseExchangeRateTimeSeries inverse = new InverseExchangeRateTimeSeries(source);
    assertThat(inverse.lookupRate(LocalDate.parse("2014-11-30")).get().getValue(), is(new BigDecimal(1).setScale(10)));
    assertThat(inverse.lookupRate(LocalDate.parse("2014-12-01")).get().getValue(), is(new BigDecimal(1).setScale(10)));
    assertThat(inverse.lookupRate(LocalDate.parse("2014-12-02")).get().getValue(), is(new BigDecimal(0.500).setScale(10)));
    assertThat(inverse.lookupRate(LocalDate.parse("2014-12-03")).get().getValue(), is(BigDecimal.valueOf(0.3333333333)));
    assertThat(inverse.lookupRate(LocalDate.parse("2014-12-04")).get().getValue(), is(BigDecimal.valueOf(0.3333333333)));
    assertThat(inverse.getBaseCurrency(), is(source.getTermCurrency()));
    assertThat(inverse.getTermCurrency(), is(source.getBaseCurrency()));
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 17 with ExchangeRate

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

the class AccountTransactionModel method updateExchangeRate.

private void updateExchangeRate() {
    // transaction
    if (sourceTransaction != null)
        return;
    if (getAccountCurrencyCode().equals(getSecurityCurrencyCode())) {
        setExchangeRate(BigDecimal.ONE);
    } else if (!getSecurityCurrencyCode().isEmpty()) {
        ExchangeRateTimeSeries series = // 
        getExchangeRateProviderFactory().getTimeSeries(getSecurityCurrencyCode(), getAccountCurrencyCode());
        if (series != null)
            setExchangeRate(series.lookupRate(date).orElse(new ExchangeRate(date, BigDecimal.ONE)).getValue());
        else
            setExchangeRate(BigDecimal.ONE);
    }
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) ExchangeRateTimeSeries(name.abuchen.portfolio.money.ExchangeRateTimeSeries)

Example 18 with ExchangeRate

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

the class TestCurrencyConverter method convert.

@Override
public Money convert(LocalDate date, Money amount) {
    if (termCurrency.equals(amount.getCurrencyCode()))
        return amount;
    if (amount.isZero())
        return Money.of(termCurrency, 0);
    // testing: any other currency will be converted 1:1
    if (!amount.getCurrencyCode().equals(series.getBaseCurrency()))
        return Money.of(termCurrency, amount.getAmount());
    ExchangeRate rate = getRate(date, amount.getCurrencyCode());
    BigDecimal converted = rate.getValue().multiply(BigDecimal.valueOf(amount.getAmount()));
    return Money.of(termCurrency, Math.round(converted.doubleValue()));
}
Also used : ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) BigDecimal(java.math.BigDecimal)

Example 19 with ExchangeRate

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

the class ExchangeRatesListView method createTopTable.

@Override
protected void createTopTable(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    TableColumnLayout layout = new TableColumnLayout();
    container.setLayout(layout);
    TableViewer indeces = new TableViewer(container, SWT.FULL_SELECTION);
    ShowHideColumnHelper support = new // $NON-NLS-1$
    ShowHideColumnHelper(// $NON-NLS-1$
    ExchangeRatesListView.class.getSimpleName() + "@top2", getPreferenceStore(), indeces, layout);
    Column column = new Column(Messages.ColumnBaseCurrency, SWT.None, 80);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return ((ExchangeRateTimeSeries) element).getBaseCurrency();
        }
    });
    // $NON-NLS-1$ //$NON-NLS-2$
    ColumnViewerSorter.create(ExchangeRateTimeSeries.class, "baseCurrency", "termCurrency").attachTo(column, SWT.DOWN);
    support.addColumn(column);
    column = new Column(Messages.ColumnTermCurrency, SWT.None, 80);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return ((ExchangeRateTimeSeries) element).getTermCurrency();
        }
    });
    // $NON-NLS-1$ //$NON-NLS-2$
    ColumnViewerSorter.create(ExchangeRateTimeSeries.class, "termCurrency", "baseCurrency").attachTo(column);
    support.addColumn(column);
    column = new Column(Messages.ColumnCurrencyProvider, SWT.None, 150);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return ((ExchangeRateTimeSeries) element).getProvider().getName();
        }
    });
    // $NON-NLS-1$
    ColumnViewerSorter.create(ExchangeRateTimeSeries.class, "provider").attachTo(column);
    support.addColumn(column);
    column = new Column(Messages.ColumnDateLatestExchangeRate, SWT.None, 150);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            ExchangeRateTimeSeries series = (ExchangeRateTimeSeries) element;
            List<ExchangeRate> rates = series.getRates();
            return rates.isEmpty() ? null : Values.Date.format(rates.get(rates.size() - 1).getTime());
        }
    });
    ColumnViewerSorter.create(element -> {
        ExchangeRateTimeSeries series = (ExchangeRateTimeSeries) element;
        List<ExchangeRate> rates = series.getRates();
        return rates.isEmpty() ? null : rates.get(rates.size() - 1).getTime();
    }).attachTo(column);
    support.addColumn(column);
    support.createColumns();
    indeces.getTable().setHeaderVisible(true);
    indeces.getTable().setLinesVisible(true);
    indeces.setContentProvider(ArrayContentProvider.getInstance());
    indeces.setInput(providerFactory.getAvailableTimeSeries());
    indeces.refresh();
    indeces.addSelectionChangedListener(event -> refreshChart((ExchangeRateTimeSeries) ((IStructuredSelection) event.getSelection()).getFirstElement()));
}
Also used : Values(name.abuchen.portfolio.money.Values) TableViewer(org.eclipse.jface.viewers.TableViewer) ExchangeRateProviderFactory(name.abuchen.portfolio.money.ExchangeRateProviderFactory) DecimalFormat(java.text.DecimalFormat) Column(name.abuchen.portfolio.ui.util.viewers.Column) ColumnViewerSorter(name.abuchen.portfolio.ui.util.viewers.ColumnViewerSorter) ISeries(org.swtchart.ISeries) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) TimelineChart(name.abuchen.portfolio.ui.util.chart.TimelineChart) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) MessageFormat(java.text.MessageFormat) ShowHideColumnHelper(name.abuchen.portfolio.ui.util.viewers.ShowHideColumnHelper) Inject(javax.inject.Inject) ExchangeRateTimeSeries(name.abuchen.portfolio.money.ExchangeRateTimeSeries) List(java.util.List) Colors(name.abuchen.portfolio.ui.util.Colors) Composite(org.eclipse.swt.widgets.Composite) Messages(name.abuchen.portfolio.ui.Messages) LocalDate(java.time.LocalDate) SWT(org.eclipse.swt.SWT) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) Composite(org.eclipse.swt.widgets.Composite) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) ShowHideColumnHelper(name.abuchen.portfolio.ui.util.viewers.ShowHideColumnHelper) Column(name.abuchen.portfolio.ui.util.viewers.Column) ExchangeRateTimeSeries(name.abuchen.portfolio.money.ExchangeRateTimeSeries) List(java.util.List) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 20 with ExchangeRate

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

the class ECBExchangeRateProvider method save.

@Override
public synchronized void save(IProgressMonitor monitor) throws IOException {
    if (!data.isDirty())
        return;
    // store latest exchange rate separately -> faster to load upon startup
    // of the application
    File file = getStorageFile(FILE_SUMMARY);
    Map<String, ExchangeRate> summary = new HashMap<>();
    for (ExchangeRateTimeSeriesImpl s : data.getSeries()) s.getLatest().ifPresent(rate -> summary.put(s.getTermCurrency(), rate));
    write(summary, file);
    // write the full history data
    file = getStorageFile(FILE_STORAGE);
    write(data, file);
}
Also used : XStreamLocalDateConverter(name.abuchen.portfolio.util.XStreamLocalDateConverter) XStream(com.thoughtworks.xstream.XStream) ExchangeRateProvider(name.abuchen.portfolio.money.ExchangeRateProvider) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Messages(name.abuchen.portfolio.Messages) ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) File(java.io.File) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ExchangeRateTimeSeries(name.abuchen.portfolio.money.ExchangeRateTimeSeries) BigDecimal(java.math.BigDecimal) List(java.util.List) LocalDate(java.time.LocalDate) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) FrameworkUtil(org.osgi.framework.FrameworkUtil) ExchangeRate(name.abuchen.portfolio.money.ExchangeRate) HashMap(java.util.HashMap) File(java.io.File)

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