Search in sources :

Example 11 with Portfolio

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

the class ClientClassificationFilter method addDeliveryT.

private void addDeliveryT(CalculationState state, Portfolio portfolio, PortfolioTransaction t, PortfolioTransaction.Type targetType, int weight) {
    PortfolioTransaction copy = new PortfolioTransaction();
    copy.setDateTime(t.getDateTime());
    copy.setCurrencyCode(t.getCurrencyCode());
    copy.setSecurity(t.getSecurity());
    copy.setShares(value(t.getShares(), weight));
    copy.setType(targetType);
    // calculation is without taxes -> remove any taxes & adapt total
    // accordingly
    long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), weight);
    long amount = value(t.getAmount(), weight);
    copy.setAmount(copy.getType() == PortfolioTransaction.Type.DELIVERY_INBOUND ? amount - taxes : amount + taxes);
    // copy all units (except for taxes) over to the new transaction
    t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.addUnit(value(u, weight)));
    state.asReadOnly(portfolio).internalAddTransaction(copy);
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Set(java.util.Set) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Classification(name.abuchen.portfolio.model.Classification) Visitor(name.abuchen.portfolio.model.Taxonomy.Visitor) HashSet(java.util.HashSet) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Assignment(name.abuchen.portfolio.model.Classification.Assignment) Portfolio(name.abuchen.portfolio.model.Portfolio) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction)

Example 12 with Portfolio

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

the class ClientClassificationFilter method addBuySellT.

private void addBuySellT(CalculationState state, Portfolio portfolio, PortfolioTransaction t) {
    int securityWeight = state.getWeight(t.getSecurity());
    long taxes = value(t.getUnitSum(Unit.Type.TAX).getAmount(), securityWeight);
    long securityAmount = value(t.getAmount(), securityWeight);
    securityAmount = t.getType() == PortfolioTransaction.Type.BUY ? securityAmount - taxes : securityAmount + taxes;
    Account account = (Account) t.getCrossEntry().getCrossOwner(t);
    int accountWeight = state.getWeight(account);
    long accountAmount = value(t.getAmount(), accountWeight);
    long commonAmount = Math.min(securityAmount, accountAmount);
    int commonWeight = (int) Math.round(((double) commonAmount / (double) securityAmount) * securityWeight);
    // create a buy/sell transactions with the amount shared by the account
    // assignment and the security assignment
    BuySellEntry copy = new BuySellEntry(state.asReadOnly(portfolio), state.account2readonly.get(account));
    copy.setDate(t.getDateTime());
    copy.setCurrencyCode(t.getCurrencyCode());
    copy.setSecurity(t.getSecurity());
    copy.setType(t.getType());
    copy.setNote(t.getNote());
    copy.setShares(value(t.getShares(), commonWeight));
    copy.setAmount(commonAmount);
    // copy all units (except for taxes) over to the new transaction
    t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> copy.getPortfolioTransaction().addUnit(value(u, commonWeight)));
    state.asReadOnly(portfolio).internalAddTransaction(copy.getPortfolioTransaction());
    state.asReadOnly(account).internalAddTransaction(copy.getAccountTransaction());
    if (accountAmount - commonAmount > 0) {
        AccountTransaction ta = new AccountTransaction(t.getDateTime(), t.getCurrencyCode(), accountAmount - commonAmount, null, t.getType() == PortfolioTransaction.Type.BUY ? AccountTransaction.Type.REMOVAL : AccountTransaction.Type.DEPOSIT);
        state.asReadOnly(account).internalAddTransaction(ta);
    }
    if (securityAmount - commonAmount > 0) {
        PortfolioTransaction tp = new PortfolioTransaction();
        tp.setDateTime(t.getDateTime());
        tp.setCurrencyCode(t.getCurrencyCode());
        tp.setSecurity(t.getSecurity());
        tp.setShares(value(t.getShares(), securityWeight - commonWeight));
        tp.setType(t.getType() == PortfolioTransaction.Type.BUY ? PortfolioTransaction.Type.DELIVERY_INBOUND : PortfolioTransaction.Type.DELIVERY_OUTBOUND);
        tp.setAmount(securityAmount - commonAmount);
        t.getUnits().filter(u -> u.getType() != Unit.Type.TAX).forEach(u -> tp.addUnit(value(u, securityWeight - commonWeight)));
        state.asReadOnly(portfolio).internalAddTransaction(tp);
    }
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Money(name.abuchen.portfolio.money.Money) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) InvestmentVehicle(name.abuchen.portfolio.model.InvestmentVehicle) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) Set(java.util.Set) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Classification(name.abuchen.portfolio.model.Classification) Visitor(name.abuchen.portfolio.model.Taxonomy.Visitor) HashSet(java.util.HashSet) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) AccountTransferEntry(name.abuchen.portfolio.model.AccountTransferEntry) Assignment(name.abuchen.portfolio.model.Classification.Assignment) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) BuySellEntry(name.abuchen.portfolio.model.BuySellEntry) PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction)

Example 13 with Portfolio

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

the class ClientSecurityFilter method filter.

@Override
public Client filter(Client client) {
    ReadOnlyClient pseudoClient = new ReadOnlyClient(client);
    Map<Account, ReadOnlyAccount> account2readonly = new HashMap<>();
    Map<Portfolio, ReadOnlyPortfolio> portfolio2readonly = new HashMap<>();
    Function<Account, ReadOnlyAccount> transformAccount = a -> account2readonly.computeIfAbsent(a, aa -> {
        ReadOnlyAccount readonly = new ReadOnlyAccount(aa);
        pseudoClient.internalAddAccount(readonly);
        return readonly;
    });
    Function<Portfolio, ReadOnlyPortfolio> transformPortfolio = p -> portfolio2readonly.computeIfAbsent(p, pp -> {
        ReadOnlyPortfolio pseudoPortfolio = new ReadOnlyPortfolio(pp);
        pseudoPortfolio.setReferenceAccount(transformAccount.apply(pp.getReferenceAccount()));
        pseudoClient.internalAddPortfolio(pseudoPortfolio);
        return pseudoPortfolio;
    });
    for (Security security : securities) {
        pseudoClient.internalAddSecurity(security);
        addSecurity(client, transformPortfolio, transformAccount, security);
    }
    return pseudoClient;
}
Also used : PortfolioTransaction(name.abuchen.portfolio.model.PortfolioTransaction) Arrays(java.util.Arrays) Client(name.abuchen.portfolio.model.Client) Account(name.abuchen.portfolio.model.Account) AccountTransaction(name.abuchen.portfolio.model.AccountTransaction) TransactionPair(name.abuchen.portfolio.model.TransactionPair) HashMap(java.util.HashMap) Security(name.abuchen.portfolio.model.Security) Function(java.util.function.Function) List(java.util.List) Unit(name.abuchen.portfolio.model.Transaction.Unit) Map(java.util.Map) Portfolio(name.abuchen.portfolio.model.Portfolio) Account(name.abuchen.portfolio.model.Account) HashMap(java.util.HashMap) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security)

Example 14 with Portfolio

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

the class PortfolioClientFilter method filter.

@Override
public Client filter(Client client) {
    ReadOnlyClient pseudoClient = new ReadOnlyClient(client);
    Map<Account, ReadOnlyAccount> account2pseudo = new HashMap<>();
    Set<Security> usedSecurities = new HashSet<>();
    for (Portfolio portfolio : portfolios) {
        ReadOnlyAccount pseudoAccount = account2pseudo.computeIfAbsent(portfolio.getReferenceAccount(), a -> {
            ReadOnlyAccount pa = new ReadOnlyAccount(a);
            pseudoClient.internalAddAccount(pa);
            return pa;
        });
        ReadOnlyPortfolio pseudoPortfolio = new ReadOnlyPortfolio(portfolio);
        pseudoPortfolio.setReferenceAccount(pseudoAccount);
        pseudoClient.internalAddPortfolio(pseudoPortfolio);
        adaptPortfolioTransactions(portfolio, pseudoPortfolio, usedSecurities);
        if (!accounts.contains(portfolio.getReferenceAccount()))
            collectSecurityRelevantTx(portfolio, pseudoAccount, usedSecurities);
    }
    for (Account account : accounts) {
        ReadOnlyAccount pseudoAccount = account2pseudo.computeIfAbsent(account, a -> {
            ReadOnlyAccount pa = new ReadOnlyAccount(a);
            pseudoClient.internalAddAccount(pa);
            return pa;
        });
        adaptAccountTransactions(account, pseudoAccount, usedSecurities);
    }
    for (Security security : usedSecurities) pseudoClient.internalAddSecurity(security);
    return pseudoClient;
}
Also used : Account(name.abuchen.portfolio.model.Account) HashMap(java.util.HashMap) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security) HashSet(java.util.HashSet)

Example 15 with Portfolio

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

the class SecurityPerformanceSnapshot method create.

public static SecurityPerformanceSnapshot create(Client client, CurrencyConverter converter, ReportingPeriod period) {
    Map<Security, SecurityPerformanceRecord> transactions = initRecords(client);
    for (Account account : client.getAccounts()) extractSecurityRelatedAccountTransactions(account, period, transactions);
    for (Portfolio portfolio : client.getPortfolios()) {
        extractSecurityRelatedPortfolioTransactions(portfolio, period, transactions);
        addPseudoValuationTansactions(portfolio, converter, period, transactions);
    }
    return doCreateSnapshot(client, converter, transactions, period);
}
Also used : Account(name.abuchen.portfolio.model.Account) Portfolio(name.abuchen.portfolio.model.Portfolio) Security(name.abuchen.portfolio.model.Security)

Aggregations

Portfolio (name.abuchen.portfolio.model.Portfolio)75 Security (name.abuchen.portfolio.model.Security)52 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)44 Test (org.junit.Test)41 Account (name.abuchen.portfolio.model.Account)39 Client (name.abuchen.portfolio.model.Client)38 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)24 PortfolioBuilder (name.abuchen.portfolio.PortfolioBuilder)21 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)20 Unit (name.abuchen.portfolio.model.Transaction.Unit)19 SecurityBuilder (name.abuchen.portfolio.SecurityBuilder)17 LocalDate (java.time.LocalDate)16 CurrencyConverter (name.abuchen.portfolio.money.CurrencyConverter)12 BuySellEntry (name.abuchen.portfolio.model.BuySellEntry)11 Money (name.abuchen.portfolio.money.Money)11 ArrayList (java.util.ArrayList)10 Values (name.abuchen.portfolio.money.Values)10 AccountTransferEntry (name.abuchen.portfolio.model.AccountTransferEntry)9 CurrencyUnit (name.abuchen.portfolio.money.CurrencyUnit)9 Collections (java.util.Collections)8