Search in sources :

Example 46 with CurrencyConverter

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

the class IssueCurrencyGainsRoundingError method testPurchaseValueOfSecurityPositionWithTransfers.

@Test
public void testPurchaseValueOfSecurityPositionWithTransfers() throws IOException {
    Client client = ClientFactory.load(IssueCurrencyGainsRoundingError.class.getResourceAsStream(// $NON-NLS-1$
    "IssueCurrencyGainsRoundingError.xml"));
    ReportingPeriod period = new // $NON-NLS-1$
    ReportingPeriod.FromXtoY(// $NON-NLS-1$
    LocalDate.parse("2015-01-09"), // $NON-NLS-1$
    LocalDate.parse("2016-01-09"));
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(client, converter, period);
    MutableMoney currencyGains = MutableMoney.of(converter.getTermCurrency());
    currencyGains.subtract(snapshot.getValue(CategoryType.INITIAL_VALUE));
    currencyGains.subtract(snapshot.getValue(CategoryType.CAPITAL_GAINS));
    currencyGains.subtract(snapshot.getValue(CategoryType.EARNINGS));
    currencyGains.add(snapshot.getValue(CategoryType.FEES));
    currencyGains.add(snapshot.getValue(CategoryType.TAXES));
    currencyGains.add(snapshot.getValue(CategoryType.TRANSFERS));
    currencyGains.add(snapshot.getValue(CategoryType.FINAL_VALUE));
    assertThat(snapshot.getCategoryByType(CategoryType.CURRENCY_GAINS).getValuation(), is(currencyGains.toMoney()));
}
Also used : ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) MutableMoney(name.abuchen.portfolio.money.MutableMoney) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Client(name.abuchen.portfolio.model.Client) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) ClientPerformanceSnapshot(name.abuchen.portfolio.snapshot.ClientPerformanceSnapshot) Test(org.junit.Test)

Example 47 with CurrencyConverter

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

the class SecuritiesChart method addMovingAveragePurchasePrice.

private void addMovingAveragePurchasePrice() {
    // no purchase price
    if (security.getCurrencyCode() == null)
        return;
    // create a list of dates that are relevant for floating avg purchase price
    // changes (i.e. all purchase and sell events)
    Client filteredClient = new ClientSecurityFilter(security).filter(client);
    CurrencyConverter securityCurrency = converter.with(security.getCurrencyCode());
    LocalDate today = LocalDate.now();
    List<LocalDate> candidates = // 
    client.getPortfolios().stream().flatMap(// 
    p -> p.getTransactions().stream()).filter(t -> t.getSecurity().equals(security)).filter(t -> !(t.getType() == PortfolioTransaction.Type.TRANSFER_IN || t.getType() == PortfolioTransaction.Type.TRANSFER_OUT)).filter(t -> t.getDateTime().toLocalDate().isBefore(today)).map(t -> (chartPeriod == null || t.getDateTime().toLocalDate().isAfter(chartPeriod)) ? t.getDateTime().toLocalDate() : chartPeriod).distinct().sorted().collect(Collectors.toList());
    // calculate floating avg purchase price for each event - separate lineSeries
    // per holding period
    List<Double> values = new ArrayList<>();
    List<LocalDate> dates = new ArrayList<>();
    int seriesCounter = 0;
    for (LocalDate eventDate : candidates) {
        Optional<Double> purchasePrice = getMovingAveragePurchasePrice(filteredClient, securityCurrency, eventDate);
        if (purchasePrice.isPresent()) {
            dates.add(eventDate);
            values.add(purchasePrice.get());
        } else {
            if (!dates.isEmpty()) {
                // add previous value if the data series ends here (no more
                // future events)
                dates.add(eventDate);
                values.add(values.get(values.size() - 1));
                createMovingAveragePurchaseLineSeries(values, dates, seriesCounter++);
                values.clear();
                dates.clear();
            } else if (dates.isEmpty()) {
            // if no holding period exists, then do not add the event at
            // all
            }
        }
    }
    // add today if needed
    getMovingAveragePurchasePrice(filteredClient, securityCurrency, today).ifPresent(price -> {
        dates.add(today);
        values.add(price);
    });
    if (!dates.isEmpty())
        createMovingAveragePurchaseLineSeries(values, dates, seriesCounter);
}
Also used : Arrays(java.util.Arrays) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) LineStyle(org.swtchart.LineStyle) Point(org.eclipse.swt.graphics.Point) Composite(org.eclipse.swt.widgets.Composite) ILineSeries(org.swtchart.ILineSeries) Interval(name.abuchen.portfolio.util.Interval) EnumSet(java.util.EnumSet) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Separator(org.eclipse.jface.action.Separator) Button(org.eclipse.swt.widgets.Button) MenuManager(org.eclipse.jface.action.MenuManager) Security(name.abuchen.portfolio.model.Security) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Colors(name.abuchen.portfolio.ui.util.Colors) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) RowLayoutFactory(org.eclipse.jface.layout.RowLayoutFactory) SWT(org.eclipse.swt.SWT) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) SeriesType(org.swtchart.ISeries.SeriesType) Optional(java.util.Optional) Label(org.eclipse.swt.widgets.Label) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) SelectionListener(org.eclipse.swt.events.SelectionListener) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PaintListener(org.eclipse.swt.events.PaintListener) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) IAxis(org.swtchart.IAxis) Images(name.abuchen.portfolio.ui.Images) AssetPosition(name.abuchen.portfolio.snapshot.AssetPosition) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ISeries(org.swtchart.ISeries) TimelineChartToolTip(name.abuchen.portfolio.ui.util.chart.TimelineChartToolTip) Range(org.swtchart.Range) ArrayList(java.util.ArrayList) MessageFormat(com.ibm.icu.text.MessageFormat) Messages(name.abuchen.portfolio.ui.Messages) TemporalAmount(java.time.temporal.TemporalAmount) Period(java.time.Period) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) DecimalFormat(java.text.DecimalFormat) SimpleAction(name.abuchen.portfolio.ui.util.SimpleAction) ILegend(org.swtchart.ILegend) Action(org.eclipse.jface.action.Action) TimelineChart(name.abuchen.portfolio.ui.util.chart.TimelineChart) PlotSymbolType(org.swtchart.ILineSeries.PlotSymbolType) PortfolioPlugin(name.abuchen.portfolio.ui.PortfolioPlugin) Color(org.eclipse.swt.graphics.Color) Unit(name.abuchen.portfolio.model.Transaction.Unit) IMenuManager(org.eclipse.jface.action.IMenuManager) DateTimeFormatter(java.time.format.DateTimeFormatter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Collections(java.util.Collections) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) ArrayList(java.util.ArrayList) Client(name.abuchen.portfolio.model.Client) LocalDate(java.time.LocalDate) Point(org.eclipse.swt.graphics.Point) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 48 with CurrencyConverter

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

the class SecuritiesChart method addFIFOPurchasePrice.

private void addFIFOPurchasePrice() {
    // no purchase price
    if (security.getCurrencyCode() == null)
        return;
    // create a list of dates that are relevant for FIFO purchase price
    // changes (i.e. all purchase and sell events)
    Client filteredClient = new ClientSecurityFilter(security).filter(client);
    CurrencyConverter securityCurrency = converter.with(security.getCurrencyCode());
    LocalDate today = LocalDate.now();
    List<LocalDate> candidates = // 
    client.getPortfolios().stream().flatMap(// 
    p -> p.getTransactions().stream()).filter(t -> t.getSecurity().equals(security)).filter(t -> !(t.getType() == PortfolioTransaction.Type.TRANSFER_IN || t.getType() == PortfolioTransaction.Type.TRANSFER_OUT)).filter(t -> t.getDateTime().toLocalDate().isBefore(today)).map(t -> (chartPeriod == null || t.getDateTime().toLocalDate().isAfter(chartPeriod)) ? t.getDateTime().toLocalDate() : chartPeriod).distinct().sorted().collect(Collectors.toList());
    // calculate FIFO purchase price for each event - separate lineSeries
    // per holding period
    List<Double> values = new ArrayList<>();
    List<LocalDate> dates = new ArrayList<>();
    int seriesCounter = 0;
    for (LocalDate eventDate : candidates) {
        Optional<Double> purchasePrice = getPurchasePrice(filteredClient, securityCurrency, eventDate);
        if (purchasePrice.isPresent()) {
            dates.add(eventDate);
            values.add(purchasePrice.get());
        } else {
            if (!dates.isEmpty()) {
                // add previous value if the data series ends here (no more
                // future events)
                dates.add(eventDate);
                values.add(values.get(values.size() - 1));
                createFIFOPurchaseLineSeries(values, dates, seriesCounter++);
                values.clear();
                dates.clear();
            } else if (dates.isEmpty()) {
            // if no holding period exists, then do not add the event at
            // all
            }
        }
    }
    // add today if needed
    getPurchasePrice(filteredClient, securityCurrency, today).ifPresent(price -> {
        dates.add(today);
        values.add(price);
    });
    if (!dates.isEmpty())
        createFIFOPurchaseLineSeries(values, dates, seriesCounter);
}
Also used : Arrays(java.util.Arrays) Client(name.abuchen.portfolio.model.Client) Transaction(name.abuchen.portfolio.model.Transaction) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) LineStyle(org.swtchart.LineStyle) Point(org.eclipse.swt.graphics.Point) Composite(org.eclipse.swt.widgets.Composite) ILineSeries(org.swtchart.ILineSeries) Interval(name.abuchen.portfolio.util.Interval) EnumSet(java.util.EnumSet) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Separator(org.eclipse.jface.action.Separator) Button(org.eclipse.swt.widgets.Button) MenuManager(org.eclipse.jface.action.MenuManager) Security(name.abuchen.portfolio.model.Security) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Colors(name.abuchen.portfolio.ui.util.Colors) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) RowLayoutFactory(org.eclipse.jface.layout.RowLayoutFactory) SWT(org.eclipse.swt.SWT) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) SeriesType(org.swtchart.ISeries.SeriesType) Optional(java.util.Optional) Label(org.eclipse.swt.widgets.Label) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) SelectionListener(org.eclipse.swt.events.SelectionListener) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) PaintListener(org.eclipse.swt.events.PaintListener) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) IAxis(org.swtchart.IAxis) Images(name.abuchen.portfolio.ui.Images) AssetPosition(name.abuchen.portfolio.snapshot.AssetPosition) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ISeries(org.swtchart.ISeries) TimelineChartToolTip(name.abuchen.portfolio.ui.util.chart.TimelineChartToolTip) Range(org.swtchart.Range) ArrayList(java.util.ArrayList) MessageFormat(com.ibm.icu.text.MessageFormat) Messages(name.abuchen.portfolio.ui.Messages) TemporalAmount(java.time.temporal.TemporalAmount) Period(java.time.Period) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) DecimalFormat(java.text.DecimalFormat) SimpleAction(name.abuchen.portfolio.ui.util.SimpleAction) ILegend(org.swtchart.ILegend) Action(org.eclipse.jface.action.Action) TimelineChart(name.abuchen.portfolio.ui.util.chart.TimelineChart) PlotSymbolType(org.swtchart.ILineSeries.PlotSymbolType) PortfolioPlugin(name.abuchen.portfolio.ui.PortfolioPlugin) Color(org.eclipse.swt.graphics.Color) Unit(name.abuchen.portfolio.model.Transaction.Unit) IMenuManager(org.eclipse.jface.action.IMenuManager) DateTimeFormatter(java.time.format.DateTimeFormatter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Collections(java.util.Collections) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) ArrayList(java.util.ArrayList) Client(name.abuchen.portfolio.model.Client) LocalDate(java.time.LocalDate) Point(org.eclipse.swt.graphics.Point) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 49 with CurrencyConverter

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

the class PerformanceView method reportingPeriodUpdated.

@Override
public void reportingPeriodUpdated() {
    ReportingPeriod period = getReportingPeriod();
    CurrencyConverter converter = new CurrencyConverterImpl(factory, getClient().getBaseCurrency());
    Client filteredClient = clientFilter.getSelectedFilter().filter(getClient());
    ClientPerformanceSnapshot snapshot = new ClientPerformanceSnapshot(filteredClient, converter, period);
    try {
        calculation.getTree().setRedraw(false);
        calculation.setInput(snapshot);
        calculation.expandAll();
        calculation.getTree().getParent().layout();
    } finally {
        calculation.getTree().setRedraw(true);
    }
    snapshotStart.setInput(snapshot.getStartClientSnapshot(), clientFilter.getSelectedFilter());
    snapshotEnd.setInput(snapshot.getEndClientSnapshot(), clientFilter.getSelectedFilter());
    earnings.setInput(snapshot.getEarnings());
    earningsByAccount.setInput(new GroupEarningsByAccount(snapshot).getItems());
    taxes.setInput(snapshot.getTaxes());
    fees.setInput(snapshot.getFees());
}
Also used : ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) GroupEarningsByAccount(name.abuchen.portfolio.snapshot.GroupEarningsByAccount) Client(name.abuchen.portfolio.model.Client) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) ClientPerformanceSnapshot(name.abuchen.portfolio.snapshot.ClientPerformanceSnapshot)

Example 50 with CurrencyConverter

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

the class PortfolioListView method createTopTable.

// //////////////////////////////////////////////////////////////
// top table: accounts
// //////////////////////////////////////////////////////////////
protected void createTopTable(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    TableColumnLayout layout = new TableColumnLayout();
    container.setLayout(layout);
    portfolios = new TableViewer(container, SWT.FULL_SELECTION);
    ColumnEditingSupport.prepare(portfolios);
    portfolioColumns = new // $NON-NLS-1$
    ShowHideColumnHelper(// $NON-NLS-1$
    PortfolioListView.class.getSimpleName() + "@top2", getPreferenceStore(), portfolios, layout);
    // $NON-NLS-1$
    Column column = new NameColumn("0", Messages.ColumnPortfolio, SWT.None, 100);
    column.setLabelProvider(new NameColumnLabelProvider() {

        @Override
        public Color getForeground(Object e) {
            boolean isRetired = ((Portfolio) e).isRetired();
            return isRetired ? Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY) : null;
        }
    });
    column.getEditingSupport().addListener(this);
    portfolioColumns.addColumn(column);
    column = new Column(Messages.ColumnReferenceAccount, SWT.None, 160);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object e) {
            Portfolio p = (Portfolio) e;
            return p.getReferenceAccount() != null ? p.getReferenceAccount().getName() : null;
        }
    });
    // $NON-NLS-1$
    ColumnViewerSorter.create(Portfolio.class, "referenceAccount").attachTo(column);
    // $NON-NLS-1$
    new ListEditingSupport(Portfolio.class, "referenceAccount", getClient().getAccounts()).addListener(this).attachTo(column);
    portfolioColumns.addColumn(column);
    column = new NoteColumn();
    column.getEditingSupport().addListener(this);
    portfolioColumns.addColumn(column);
    portfolioColumns.createColumns();
    portfolios.getTable().setHeaderVisible(true);
    portfolios.getTable().setLinesVisible(true);
    portfolios.setContentProvider(ArrayContentProvider.getInstance());
    setInput();
    portfolios.addSelectionChangedListener(event -> {
        Portfolio portfolio = (Portfolio) ((IStructuredSelection) event.getSelection()).getFirstElement();
        if (portfolio != null) {
            transactions.setInput(portfolio, portfolio.getTransactions());
            transactions.refresh();
            CurrencyConverter converter = new CurrencyConverterImpl(factory, portfolio.getReferenceAccount().getCurrencyCode());
            statementOfAssets.setInput(PortfolioSnapshot.create(portfolio, converter, LocalDate.now()));
        } else {
            transactions.setInput(null, null);
            transactions.refresh();
            statementOfAssets.setInput((PortfolioSnapshot) null);
        }
    });
    hookContextMenu(portfolios.getTable(), this::fillPortfolioContextMenu);
}
Also used : Composite(org.eclipse.swt.widgets.Composite) NameColumnLabelProvider(name.abuchen.portfolio.ui.views.columns.NameColumn.NameColumnLabelProvider) NameColumn(name.abuchen.portfolio.ui.views.columns.NameColumn) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) Color(org.eclipse.swt.graphics.Color) Portfolio(name.abuchen.portfolio.model.Portfolio) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) NameColumnLabelProvider(name.abuchen.portfolio.ui.views.columns.NameColumn.NameColumnLabelProvider) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) TableColumnLayout(org.eclipse.jface.layout.TableColumnLayout) NameColumn(name.abuchen.portfolio.ui.views.columns.NameColumn) NoteColumn(name.abuchen.portfolio.ui.views.columns.NoteColumn) Column(name.abuchen.portfolio.ui.util.viewers.Column) ListEditingSupport(name.abuchen.portfolio.ui.util.viewers.ListEditingSupport) TableViewer(org.eclipse.jface.viewers.TableViewer)

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