Search in sources :

Example 1 with SecurityPosition

use of name.abuchen.portfolio.snapshot.SecurityPosition in project portfolio by buchen.

the class AccountTransactionDialog method addAction.

private void addAction(IMenuManager manager, PortfolioSnapshot portfolio, final String label) {
    final SecurityPosition position = portfolio.getPositionsBySecurity().get(model().getSecurity());
    if (position != null) {
        Action action = new Action(MessageFormat.format(Messages.DividendsDialogLabelPortfolioSharesHeld, Values.Share.format(position.getShares()), label, Values.Date.format(portfolio.getTime()))) {

            @Override
            public void run() {
                model().setShares(position.getShares());
            }
        };
        manager.add(action);
    }
}
Also used : SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) Action(org.eclipse.jface.action.Action)

Example 2 with SecurityPosition

use of name.abuchen.portfolio.snapshot.SecurityPosition in project portfolio by buchen.

the class AbstractSecurityTransactionModel method updateSharesAndQuote.

protected void updateSharesAndQuote() {
    // do not auto-suggest shares and quote when editing an existing transaction
    if (hasSource())
        return;
    if (type == PortfolioTransaction.Type.SELL || type == PortfolioTransaction.Type.DELIVERY_OUTBOUND) {
        boolean hasPosition = false;
        if (portfolio != null) {
            // since the security position has always the currency of the
            // investment vehicle, actually no conversion is needed. Hence
            // we can use an arbitrary converter.
            CurrencyConverter converter = new CurrencyConverterImpl(getExchangeRateProviderFactory(), CurrencyUnit.EUR);
            PortfolioSnapshot snapshot = PortfolioSnapshot.create(portfolio, converter, date);
            SecurityPosition position = snapshot.getPositionsBySecurity().get(security);
            if (position != null) {
                setShares(position.getShares());
                setTotal(position.calculateValue().getAmount());
                hasPosition = true;
            }
        }
        if (!hasPosition) {
            setShares(0);
            setQuote(new BigDecimal(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
        }
    } else {
        setQuote(new BigDecimal(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
    }
}
Also used : SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) PortfolioSnapshot(name.abuchen.portfolio.snapshot.PortfolioSnapshot) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) BigDecimal(java.math.BigDecimal) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 3 with SecurityPosition

use of name.abuchen.portfolio.snapshot.SecurityPosition in project portfolio by buchen.

the class AccountTransactionModel method updateShares.

private void updateShares() {
    // transaction
    if (sourceTransaction != null)
        return;
    if (!supportsShares() || security == null)
        return;
    CurrencyConverter converter = new CurrencyConverterImpl(getExchangeRateProviderFactory(), client.getBaseCurrency());
    ClientSnapshot snapshot = ClientSnapshot.create(client, converter, date);
    SecurityPosition p = snapshot.getJointPortfolio().getPositionsBySecurity().get(security);
    setShares(p != null ? p.getShares() : 0);
}
Also used : ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 4 with SecurityPosition

use of name.abuchen.portfolio.snapshot.SecurityPosition in project portfolio by buchen.

the class SecurityTransferModel method updateSharesAndQuote.

private void updateSharesAndQuote() {
    // transaction
    if (source != null)
        return;
    SecurityPosition position = null;
    if (security != null) {
        CurrencyConverter converter = new CurrencyConverterImpl(getExchangeRateProviderFactory(), client.getBaseCurrency());
        PortfolioSnapshot snapshot = sourcePortfolio != null ? PortfolioSnapshot.create(sourcePortfolio, converter, date) : ClientSnapshot.create(client, converter, date).getJointPortfolio();
        position = snapshot.getPositionsBySecurity().get(security);
    }
    if (position != null) {
        setShares(position.getShares());
        // setAmount will also set quote
        setAmount(position.calculateValue().getAmount());
    } else if (security != null) {
        setShares(0);
        setQuote(new BigDecimal(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
    } else {
        setShares(0);
        setQuote(BigDecimal.ZERO);
    }
}
Also used : SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) PortfolioSnapshot(name.abuchen.portfolio.snapshot.PortfolioSnapshot) CurrencyConverterImpl(name.abuchen.portfolio.money.CurrencyConverterImpl) BigDecimal(java.math.BigDecimal) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter)

Example 5 with SecurityPosition

use of name.abuchen.portfolio.snapshot.SecurityPosition in project portfolio by buchen.

the class Issue371PurchaseValueWithTransfers method testPurchaseValueOfSecurityPositionWithTransfers.

@Test
public void testPurchaseValueOfSecurityPositionWithTransfers() throws IOException {
    Client client = ClientFactory.load(Issue371PurchaseValueWithTransfers.class.getResourceAsStream(// $NON-NLS-1$
    "Issue371PurchaseValueWithTransfers.xml"));
    Security adidas = client.getSecurities().get(0);
    // $NON-NLS-1$
    assertThat(adidas.getName(), is("Adidas AG"));
    ReportingPeriod period = new // $NON-NLS-1$
    ReportingPeriod.FromXtoY(// $NON-NLS-1$
    LocalDate.parse("2010-11-20"), // $NON-NLS-1$
    LocalDate.parse("2015-11-20"));
    // make sure that the transfer entry exists
    assertThat(client.getPortfolios().size(), is(2));
    assertThat(client.getPortfolios().stream().flatMap(p -> p.getTransactions().stream()).filter(t -> t.getSecurity() == adidas).filter(t -> t.getCrossEntry() instanceof PortfolioTransferEntry).filter(t -> t.getType() == PortfolioTransaction.Type.TRANSFER_IN).findAny().isPresent(), is(true));
    CurrencyConverter converter = new TestCurrencyConverter();
    ClientSnapshot snapshot = ClientSnapshot.create(client, converter, period.getEndDate());
    SecurityPosition securityPosition = snapshot.getPositionsByVehicle().get(adidas).getPosition();
    SecurityPerformanceSnapshot securitySnapshot = SecurityPerformanceSnapshot.create(client, converter, period);
    SecurityPerformanceRecord securityRecord = securitySnapshot.getRecords().get(0);
    assertThat(securityRecord.getSecurity(), is(adidas));
    assertThat(securityPosition.getFIFOPurchaseValue(), is(securityRecord.getFifoCost()));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Client(name.abuchen.portfolio.model.Client) IOException(java.io.IOException) Test(org.junit.Test) Security(name.abuchen.portfolio.model.Security) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Assert.assertThat(org.junit.Assert.assertThat) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) LocalDate(java.time.LocalDate) ClientFactory(name.abuchen.portfolio.model.ClientFactory) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) ClientSnapshot(name.abuchen.portfolio.snapshot.ClientSnapshot) SecurityPerformanceRecord(name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord) Security(name.abuchen.portfolio.model.Security) SecurityPerformanceSnapshot(name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) CurrencyConverter(name.abuchen.portfolio.money.CurrencyConverter) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) SecurityPosition(name.abuchen.portfolio.snapshot.SecurityPosition) TestCurrencyConverter(name.abuchen.portfolio.TestCurrencyConverter) Client(name.abuchen.portfolio.model.Client) PortfolioTransferEntry(name.abuchen.portfolio.model.PortfolioTransferEntry) Test(org.junit.Test)

Aggregations

SecurityPosition (name.abuchen.portfolio.snapshot.SecurityPosition)6 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)4 CurrencyConverterImpl (name.abuchen.portfolio.money.CurrencyConverterImpl)3 PortfolioSnapshot (name.abuchen.portfolio.snapshot.PortfolioSnapshot)3 BigDecimal (java.math.BigDecimal)2 ClientSnapshot (name.abuchen.portfolio.snapshot.ClientSnapshot)2 IOException (java.io.IOException)1 LocalDate (java.time.LocalDate)1 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)1 Client (name.abuchen.portfolio.model.Client)1 ClientFactory (name.abuchen.portfolio.model.ClientFactory)1 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)1 PortfolioTransferEntry (name.abuchen.portfolio.model.PortfolioTransferEntry)1 Security (name.abuchen.portfolio.model.Security)1 ReportingPeriod (name.abuchen.portfolio.snapshot.ReportingPeriod)1 SecurityPerformanceRecord (name.abuchen.portfolio.snapshot.security.SecurityPerformanceRecord)1 SecurityPerformanceSnapshot (name.abuchen.portfolio.snapshot.security.SecurityPerformanceSnapshot)1 Action (org.eclipse.jface.action.Action)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 Assert.assertThat (org.junit.Assert.assertThat)1