Search in sources :

Example 1 with ClientSecurityFilter

use of name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter in project portfolio by buchen.

the class SecurityTaxAndFeeAccountTransactionsTestCase method testAdidas.

@Test
public void testAdidas() {
    SecurityPerformanceSnapshot snapshot = SecurityPerformanceSnapshot.create(client, converter, interval);
    SecurityPerformanceRecord record = snapshot.getRecords().stream().filter(r -> r.getSecurity().equals(adidas)).findAny().orElseThrow(IllegalArgumentException::new);
    assertThat(record.getTransactions().size(), is(6));
    // fees and taxes from the buy transaction + separate transactions
    assertThat(record.getFees(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(10d + 10d - 5d))));
    assertThat(record.getTaxes(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(10d + 25d - 5d))));
    // delta is end value - starting value including all fees and taxes
    assertThat(record.getDelta(), is(Money.of(CurrencyUnit.EUR, Values.Amount.factorize(1456.5 - 1533 - 10 + 5 - 25 + 5))));
    // IRR as calculated in Excel
    // @formatter:off
    // date          w/ tax       w/o tax
    // 02.01.17     -1533         -1523
    // 09.01.17       -10           -10
    // 10.01.17         5             5
    // 11.01.17       -25             0
    // 12.01.17         5             0
    // 31.01.17      1456,5        1456,5
    // XIRR    -0,573350433  -0,453151944
    // @formatter:on
    assertThat(record.getIrr(), closeTo(-0.453151944d, 1.0e-8));
    // check filtering - ClientSecurityFilter
    Client filteredClient = new ClientSecurityFilter(adidas).filter(client);
    checkFilteredClientAdidas(filteredClient, 1.0);
    // check filtering - ClientClassificationFilter
    Taxonomy case1 = client.getTaxonomies().stream().filter(t -> "case_full_classification_adidas".equals(t.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    Classification classification = case1.getAllClassifications().stream().filter(c -> "category_security".equals(c.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    filteredClient = new ClientClassificationFilter(classification).filter(client);
    checkFilteredClientAdidas(filteredClient, 1.0);
    // TTWROR must be identical to one calculated via ClientSecurityFilter
    // (implicitly used by the SecurityPeformanceSnapshot)
    PerformanceIndex index = PerformanceIndex.forClassification(client, converter, classification, interval, new ArrayList<>());
    assertThat(index.getFinalAccumulatedPercentage(), is(record.getTrueTimeWeightedRateOfReturn()));
    // a partial assignment of the security should be identical to the full
    // assignment - only the weight differs
    Taxonomy case2 = client.getTaxonomies().stream().filter(t -> "case_partial_classification_adidas".equals(t.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    classification = case2.getAllClassifications().stream().filter(c -> "category_security".equals(c.getName())).findAny().orElseThrow(IllegalArgumentException::new);
    filteredClient = new ClientClassificationFilter(classification).filter(client);
    checkFilteredClientAdidas(filteredClient, 0.5);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Money(name.abuchen.portfolio.money.Money) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) BeforeClass(org.junit.BeforeClass) CurrencyUnit(name.abuchen.portfolio.money.CurrencyUnit) LocalDateTime(java.time.LocalDateTime) Classification(name.abuchen.portfolio.model.Classification) ArrayList(java.util.ArrayList) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Assert.assertThat(org.junit.Assert.assertThat) ClientClassificationFilter(name.abuchen.portfolio.snapshot.filter.ClientClassificationFilter) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) CoreMatchers.allOf(org.hamcrest.CoreMatchers.allOf) ClientFactory(name.abuchen.portfolio.model.ClientFactory) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) Taxonomy(name.abuchen.portfolio.model.Taxonomy) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) IOException(java.io.IOException) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) PortfolioClientFilter(name.abuchen.portfolio.snapshot.filter.PortfolioClientFilter) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Collectors(java.util.stream.Collectors) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) List(java.util.List) LocalDate(java.time.LocalDate) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) IsCloseTo.closeTo(org.hamcrest.number.IsCloseTo.closeTo) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) ClientSecurityFilter(name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter) Taxonomy(name.abuchen.portfolio.model.Taxonomy) ClientClassificationFilter(name.abuchen.portfolio.snapshot.filter.ClientClassificationFilter) Classification(name.abuchen.portfolio.model.Classification) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) Client(name.abuchen.portfolio.model.Client) PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) Test(org.junit.Test)

Example 2 with ClientSecurityFilter

use of name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter 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 3 with ClientSecurityFilter

use of name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter 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)

Aggregations

LocalDate (java.time.LocalDate)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)3 Client (name.abuchen.portfolio.model.Client)3 Security (name.abuchen.portfolio.model.Security)3 Money (name.abuchen.portfolio.money.Money)3 Values (name.abuchen.portfolio.money.Values)3 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)3 ClientSecurityFilter (name.abuchen.portfolio.snapshot.filter.ClientSecurityFilter)3 MessageFormat (com.ibm.icu.text.MessageFormat)2 DecimalFormat (java.text.DecimalFormat)2 Instant (java.time.Instant)2 Period (java.time.Period)2 ZoneId (java.time.ZoneId)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 TemporalAmount (java.time.temporal.TemporalAmount)2 Arrays (java.util.Arrays)2