Search in sources :

Example 51 with SecurityPrice

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

the class CSVExporter method exportMergedSecurityPrices.

public void exportMergedSecurityPrices(File file, List<Security> securities) throws IOException {
    // prepare: (a) find earliest date (b) ignore securities w/o quotes
    LocalDate earliestDate = null;
    List<Security> export = new ArrayList<Security>(securities.size());
    for (Security s : securities) {
        List<SecurityPrice> prices = s.getPrices();
        if (!prices.isEmpty()) {
            export.add(s);
            LocalDate quoteDate = prices.get(0).getDate();
            if (earliestDate == null)
                earliestDate = quoteDate;
            else
                earliestDate = earliestDate.isAfter(quoteDate) ? quoteDate : earliestDate;
        }
    }
    try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
        CSVPrinter printer = new CSVPrinter(writer);
        printer.setStrategy(STRATEGY);
        // write header
        printer.print(Messages.CSVColumn_Date);
        for (Security security : export) printer.print(security.getExternalIdentifier());
        printer.println();
        // stop if no securities exist
        if (earliestDate == null)
            return;
        // write quotes
        LocalDate pointer = earliestDate;
        LocalDate today = LocalDate.now();
        while (pointer.compareTo(today) <= 0) {
            // check if any quotes exist for that day at all
            int[] indices = new int[export.size()];
            int ii = 0;
            for (Security security : export) {
                SecurityPrice p = new SecurityPrice(pointer, 0);
                indices[ii] = Collections.binarySearch(security.getPrices(), p);
                ii++;
            }
            boolean hasValues = false;
            for (ii = 0; ii < indices.length && !hasValues; ii++) hasValues = indices[ii] >= 0;
            if (hasValues) {
                printer.print(pointer.toString());
                for (ii = 0; ii < indices.length; ii++) {
                    if (indices[ii] < 0)
                        // $NON-NLS-1$
                        printer.print("");
                    else
                        printer.print(Values.Quote.format(export.get(ii).getPrices().get(indices[ii]).getValue()));
                }
                printer.println();
            }
            pointer = pointer.plusDays(1);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) CSVPrinter(org.apache.commons.csv.CSVPrinter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 52 with SecurityPrice

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

the class StockSplitModel method applyChanges.

@Override
public void applyChanges() {
    // $NON-NLS-1$
    SecurityEvent event = new SecurityEvent(exDate, SecurityEvent.Type.STOCK_SPLIT, newShares + ":" + oldShares);
    security.addEvent(event);
    if (isChangeTransactions()) {
        List<TransactionPair<?>> transactions = security.getTransactions(getClient());
        for (TransactionPair<?> pair : transactions) {
            Transaction t = pair.getTransaction();
            if (t.getDateTime().toLocalDate().isBefore(exDate))
                t.setShares(t.getShares() * newShares / oldShares);
        }
    }
    if (isChangeHistoricalQuotes()) {
        List<SecurityPrice> quotes = security.getPrices();
        for (SecurityPrice p : quotes) {
            if (p.getDate().isBefore(exDate))
                p.setValue(p.getValue() * oldShares / newShares);
        }
    }
}
Also used : TransactionPair(name.abuchen.portfolio.model.TransactionPair) SecurityEvent(name.abuchen.portfolio.model.SecurityEvent) Transaction(name.abuchen.portfolio.model.Transaction) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice)

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