Search in sources :

Example 26 with SecurityPrice

use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.

the class CSVSecurityPriceExtractor method extract.

@Override
public List<Item> extract(int skipLines, List<String[]> rawValues, Map<String, Column> field2column, List<Exception> errors) {
    Security dummy = new Security();
    for (String[] line : rawValues) {
        try {
            SecurityPrice p = extract(line, field2column);
            if (p != null)
                dummy.addPrice(p);
        } catch (ParseException e) {
            errors.add(e);
        }
    }
    List<Item> result = new ArrayList<>();
    if (!dummy.getPrices().isEmpty())
        result.add(new SecurityItem(dummy));
    return result;
}
Also used : ArrayList(java.util.ArrayList) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) ParseException(java.text.ParseException) Security(name.abuchen.portfolio.model.Security)

Example 27 with SecurityPrice

use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.

the class ReBalancingViewer method addColumns.

@Override
protected void addColumns(ShowHideColumnHelper support) {
    addDimensionColumn(support);
    addDesiredAllocationColumn(support);
    // $NON-NLS-1$
    Column column = new Column("targetvalue", Messages.ColumnTargetValue, SWT.RIGHT, 100);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            return node.isClassification() ? Values.Money.format(node.getTarget(), getModel().getCurrencyCode()) : null;
        }
    });
    support.addColumn(column);
    addActualColumns(support);
    // $NON-NLS-1$
    column = new Column("delta%", Messages.ColumnDeltaPercent, SWT.RIGHT, 60);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            if (node.getTarget() == null)
                return null;
            return Values.Percent.format(((double) node.getActual().getAmount() / (double) node.getTarget().getAmount()) - 1);
        }

        @Override
        public Color getForeground(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            if (node.getTarget() == null)
                return null;
            return Display.getCurrent().getSystemColor(node.getActual().isGreaterOrEqualThan(node.getTarget()) ? SWT.COLOR_DARK_GREEN : SWT.COLOR_DARK_RED);
        }
    });
    support.addColumn(column);
    // $NON-NLS-1$
    column = new Column("delta", Messages.ColumnDeltaValue, SWT.RIGHT, 100);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            if (node.getTarget() == null)
                return null;
            return Values.Money.format(node.getActual().subtract(node.getTarget()), getModel().getCurrencyCode());
        }

        @Override
        public Color getForeground(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            if (node.getTarget() == null)
                return null;
            return Display.getCurrent().getSystemColor(node.getActual().isGreaterOrEqualThan(node.getTarget()) ? SWT.COLOR_DARK_GREEN : SWT.COLOR_DARK_RED);
        }
    });
    support.addColumn(column);
    // $NON-NLS-1$
    column = new Column("quote", Messages.ColumnQuote, SWT.RIGHT, 60);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            Security security = node.getBackingSecurity();
            if (security == null || security.getCurrencyCode() == null)
                return null;
            SecurityPrice price = security.getSecurityPrice(LocalDate.now());
            return Values.Quote.format(security.getCurrencyCode(), price.getValue(), getModel().getCurrencyCode());
        }
    });
    support.addColumn(column);
    // $NON-NLS-1$
    column = new Column("shares", Messages.ColumnSharesOwned, SWT.RIGHT, 60);
    column.setLabelProvider(new SharesLabelProvider() {

        @Override
        public Long getValue(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            Security security = node.getBackingSecurity();
            if (security == null || security.getCurrencyCode() == null)
                return null;
            AssetPosition position = getModel().getClientSnapshot().getPositionsByVehicle().get(security);
            if (position == null)
                return null;
            return Math.round(position.getPosition().getShares() * node.getWeight() / (double) Classification.ONE_HUNDRED_PERCENT);
        }
    });
    column.setVisible(false);
    support.addColumn(column);
    // $NON-NLS-1$
    column = new Column("deltashares", Messages.ColumnDeltaShares, SWT.RIGHT, 100);
    column.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            TaxonomyNode node = (TaxonomyNode) element;
            // no delta shares for unassigned securities
            if (node.getParent() != null && node.getParent().isUnassignedCategory())
                return null;
            Security security = node.getBackingSecurity();
            if (security == null || security.getCurrencyCode() == null)
                return null;
            String priceCurrency = security.getCurrencyCode();
            long price = security.getSecurityPrice(LocalDate.now()).getValue();
            long weightedPrice = Math.round(node.getWeight() * price / (double) Classification.ONE_HUNDRED_PERCENT);
            if (weightedPrice == 0L)
                return Values.Share.format(0L);
            String deltaCurrency = node.getActual().getCurrencyCode();
            long delta = node.getParent().getTarget().getAmount() - node.getParent().getActual().getAmount();
            // delta in order to know how many shares need to bought or sold
            if (!deltaCurrency.equals(priceCurrency)) {
                delta = getModel().getCurrencyConverter().with(priceCurrency).convert(LocalDate.now(), Money.of(deltaCurrency, delta)).getAmount();
            }
            long shares = Math.round(delta * Values.Share.divider() * Values.Quote.dividerToMoney() / weightedPrice);
            return Values.Share.format(shares);
        }
    });
    support.addColumn(column);
    addAdditionalColumns(support);
}
Also used : ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) Column(name.abuchen.portfolio.ui.util.viewers.Column) Color(org.eclipse.swt.graphics.Color) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) AssetPosition(name.abuchen.portfolio.snapshot.AssetPosition) SharesLabelProvider(name.abuchen.portfolio.ui.util.viewers.SharesLabelProvider)

Example 28 with SecurityPrice

use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.

the class CSVSecurityPriceExtractorTest method testSecurityCreation.

@Test
public void testSecurityCreation() throws ParseException {
    Client client = new Client();
    CSVExtractor extractor = new CSVSecurityPriceExtractor(client);
    List<Exception> errors = new ArrayList<Exception>();
    List<Item> results = extractor.extract(0, // 
    Arrays.<String[]>asList(new String[] { "2015-01-01", "14,20" }, new String[] { "2015-01-02", "15,20" }), buildField2Column(extractor), errors);
    assertThat(errors, empty());
    assertThat(results.size(), is(1));
    Security security = results.stream().filter(i -> i instanceof SecurityItem).findAny().get().getSecurity();
    List<SecurityPrice> prices = security.getPrices();
    assertThat(prices.size(), is(2));
    assertThat(prices.get(0), is(new SecurityPrice(LocalDate.parse("2015-01-01"), Values.Quote.factorize(14.20))));
    assertThat(prices.get(1), is(new SecurityPrice(LocalDate.parse("2015-01-02"), Values.Quote.factorize(15.20))));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) Values(name.abuchen.portfolio.money.Values) Client(name.abuchen.portfolio.model.Client) Item(name.abuchen.portfolio.datatransfer.Extractor.Item) IsEmptyCollection.empty(org.hamcrest.collection.IsEmptyCollection.empty) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) LocalDate(java.time.LocalDate) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) CSVExtractorTestUtil.buildField2Column(name.abuchen.portfolio.datatransfer.csv.CSVExtractorTestUtil.buildField2Column) ParseException(java.text.ParseException) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) ParseException(java.text.ParseException) Item(name.abuchen.portfolio.datatransfer.Extractor.Item) SecurityItem(name.abuchen.portfolio.datatransfer.Extractor.SecurityItem) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Client(name.abuchen.portfolio.model.Client) Test(org.junit.Test)

Example 29 with SecurityPrice

use of name.abuchen.portfolio.model.SecurityPrice 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 30 with SecurityPrice

use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.

the class SecurityPositionTest method testFIFOPurchasePrice.

@Test
public void testFIFOPurchasePrice() {
    List<PortfolioTransaction> tx = new ArrayList<PortfolioTransaction>();
    tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 100000, null, 100 * Values.Share.factor(), Type.BUY, 0, 0));
    tx.add(new PortfolioTransaction(LocalDateTime.now(), CurrencyUnit.EUR, 50000, null, 50 * Values.Share.factor(), Type.SELL, 0, 0));
    SecurityPosition position = new SecurityPosition(new Security(), new TestCurrencyConverter(), new SecurityPrice(), tx);
    assertThat(position.getShares(), is(50L * Values.Share.factor()));
    assertThat(position.getFIFOPurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_00)));
    assertThat(position.getMovingAveragePurchasePrice(), is(Money.of(CurrencyUnit.EUR, 10_00)));
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) ArrayList(java.util.ArrayList) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) Test(org.junit.Test)

Aggregations

SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)52 Security (name.abuchen.portfolio.model.Security)34 LocalDate (java.time.LocalDate)24 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)22 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)20 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)15 LatestSecurityPrice (name.abuchen.portfolio.model.LatestSecurityPrice)11 Client (name.abuchen.portfolio.model.Client)10 List (java.util.List)7 Values (name.abuchen.portfolio.money.Values)6 OpenDialogAction (name.abuchen.portfolio.ui.dialogs.transactions.OpenDialogAction)6 MessageFormat (java.text.MessageFormat)5 BiFunction (java.util.function.BiFunction)5 Function (java.util.function.Function)5 Collectors (java.util.stream.Collectors)5 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)5 Taxonomy (name.abuchen.portfolio.model.Taxonomy)5 Watchlist (name.abuchen.portfolio.model.Watchlist)5 Factory (name.abuchen.portfolio.online.Factory)5