Search in sources :

Example 6 with SecurityHistoryNode

use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.

the class UpdateFactory method downloadHistory.

public static List<SecurityHistoryNode> downloadHistory(final SecurityNode securityNode, final LocalDate startDate, final LocalDate endDate) {
    final List<SecurityHistoryNode> newSecurityNodes = new ArrayList<>();
    final String s = securityNode.getSymbol().toLowerCase(Locale.ROOT);
    final String a = Integer.toString(startDate.getMonthValue() - 1);
    final String b = Integer.toString(startDate.getDayOfMonth());
    final String c = Integer.toString(startDate.getYear());
    final String d = Integer.toString(endDate.getMonthValue() - 1);
    final String e = Integer.toString(endDate.getDayOfMonth());
    final String f = Integer.toString(endDate.getYear());
    // http://ichart.finance.yahoo.com/table.csv?s=AMD&d=1&e=14&f=2007&g=d&a=2&b=21&c=1983&ignore=.csv << new URL 2.14.07
    StringBuilder r = new StringBuilder("http://ichart.finance.yahoo.com/table.csv?a=");
    r.append(a).append("&b=").append(b).append("&c=").append(c);
    r.append("&d=").append(d).append("&e=").append(e);
    r.append("&f=").append(f).append("&s=").append(s);
    r.append("&y=0&g=d&ignore=.csv");
    URLConnection connection = null;
    try {
        /* Yahoo uses the English locale for the date... force the locale */
        final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        connection = ConnectionFactory.openConnection(r.toString());
        if (connection != null) {
            // database may stall and cause the network connection to timeout if persisted inline
            try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                String line = in.readLine();
                // make sure that we have valid data format.
                if (RESPONSE_HEADER.equals(line)) {
                    //Date,Open,High,Low,Close,Volume,Adj Close
                    //2007-02-13,14.75,14.86,14.47,14.60,17824500,14.60
                    // prime the first read
                    line = in.readLine();
                    while (line != null) {
                        if (Thread.currentThread().isInterrupted()) {
                            Thread.currentThread().interrupt();
                        }
                        if (line.charAt(0) != '<') {
                            // may have comments in file
                            final String[] fields = COMMA_DELIMITER_PATTERN.split(line);
                            final LocalDate date = DateUtils.asLocalDate(df.parse(fields[0]));
                            final BigDecimal high = new BigDecimal(fields[2]);
                            final BigDecimal low = new BigDecimal(fields[3]);
                            final BigDecimal close = new BigDecimal(fields[4]);
                            final long volume = Long.parseLong(fields[5]);
                            newSecurityNodes.add(new SecurityHistoryNode(date, close, volume, high, low));
                        }
                        line = in.readLine();
                    }
                }
            }
            logger.info(ResourceUtils.getString("Message.UpdatedPrice", securityNode.getSymbol()));
        }
    } catch (NullPointerException | IOException | ParseException | NumberFormatException ex) {
        logger.log(Level.SEVERE, null, ex);
    } finally {
        if (connection != null) {
            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).disconnect();
            }
        }
    }
    return newSecurityNodes;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LocalDate(java.time.LocalDate) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) BigDecimal(java.math.BigDecimal) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with SecurityHistoryNode

use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.

the class SecuritiesHistoryDialog method removeNode.

/**
     * Delete the history nodes selected in the table model
     */
private void removeNode() {
    SecurityNode node = securityCombo.getSelectedSecurityNode();
    int[] selection = table.getSelectedRows();
    for (int i = 0; i < selection.length; i++) {
        selection[i] = table.convertRowIndexToModel(selection[i]);
    }
    List<SecurityHistoryNode> history = node.getHistoryNodes();
    /* Capture a list of references */
    LocalDate[] temp = new LocalDate[selection.length];
    for (int i = 0; i < selection.length; i++) {
        temp[i] = history.get(selection[i]).getLocalDate();
    }
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    if (engine != null) {
        for (int i = selection.length - 1; i >= 0; i--) {
            engine.removeSecurityHistory(node, temp[i]);
        }
    }
}
Also used : SecurityNode(jgnash.engine.SecurityNode) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) LocalDate(java.time.LocalDate) Engine(jgnash.engine.Engine)

Example 8 with SecurityHistoryNode

use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.

the class YahooEventParser method retrieveNew.

public static Set<SecurityHistoryEvent> retrieveNew(final SecurityNode securityNode, final LocalDate endDate) {
    final Set<SecurityHistoryEvent> events = new HashSet<>();
    LocalDate startDate = LocalDate.now().minusDays(1);
    final List<SecurityHistoryNode> historyNodeList = securityNode.getHistoryNodes();
    if (historyNodeList.size() > 0) {
        startDate = historyNodeList.get(0).getLocalDate();
    }
    /*final List<SecurityHistoryEvent> historyEvents = new ArrayList<>(securityNode.getHistoryEvents());
        if (historyEvents.size() > 0) {
            Collections.sort(historyEvents);

            startDate = historyEvents.get(historyEvents.size() - 1).getDate().plusDays(1);
        }*/
    // s = symbol
    // a = start month -1
    // b = start day
    // c = start year
    // d = end month -1
    // e = end day
    // f = end year
    // g=v&y=0&z=30000 dividends only
    // http://ichart.finance.yahoo.com/x?s=IBM&a=00&b=2&c=1962&d=04&e=25&f=2011&g=v&y=0&z=30000
    /*
        Date,Dividends
        DIVIDEND, 20110506,0.750000
        DIVIDEND, 20110208,0.650000
        DIVIDEND, 20101108,0.650000
        DIVIDEND, 19971106,0.100000
        DIVIDEND, 19970807,0.100000
        SPLIT, 19970528,2:1
        DIVIDEND, 19970206,0.087500
        DIVIDEND, 19961106,0.087500
        STARTDATE, 19620102
        ENDDATE, 20110525
        TOTALSIZE, 195
        STATUS, 0
        */
    final String s = securityNode.getSymbol();
    final String a = Integer.toString(startDate.getMonthValue());
    final String b = Integer.toString(startDate.getDayOfMonth());
    final String c = Integer.toString(startDate.getYear());
    final String d = Integer.toString(endDate.getMonthValue());
    final String e = Integer.toString(endDate.getDayOfMonth());
    final String f = Integer.toString(endDate.getYear());
    //http://ichart.finance.yahoo.com/x?s=IBM&a=00&b=2&c=1962&d=04&e=25&f=2011&g=v&y=0&z=30000
    final StringBuilder url = new StringBuilder("https://ichart.finance.yahoo.com/x?s=").append(s);
    url.append("&a=").append(a).append("&b=").append(b).append("&c=").append(c);
    url.append("&d=").append(d).append("&e=").append(e);
    url.append("&f=").append(f);
    url.append("&g=v&y=0&z=30000");
    URLConnection connection = null;
    try {
        connection = ConnectionFactory.openConnection(url.toString());
        if (connection != null) {
            try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                String line = in.readLine();
                if (RESPONSE_HEADER.equals(line)) {
                    // prime the first read
                    line = in.readLine();
                    while (line != null) {
                        if (Thread.currentThread().isInterrupted()) {
                            Thread.currentThread().interrupt();
                        }
                        final String[] fields = COMMA_DELIMITER_PATTERN.split(line);
                        if (fields.length == 3) {
                            try {
                                final LocalDate date = LocalDate.of(Integer.parseInt(fields[1].trim().substring(0, 4)), Integer.parseInt(fields[1].trim().substring(4, 6)), Integer.parseInt(fields[1].trim().substring(6, 8)));
                                switch(fields[0]) {
                                    case "DIVIDEND":
                                        final BigDecimal dividend = new BigDecimal(fields[2]);
                                        events.add(new SecurityHistoryEvent(SecurityHistoryEventType.DIVIDEND, date, dividend));
                                        break;
                                    case "SPLIT":
                                        final String[] fraction = fields[2].split(":");
                                        final BigDecimal value = new BigDecimal(fraction[0]).divide(new BigDecimal(fraction[1]), MathContext.DECIMAL32);
                                        events.add(new SecurityHistoryEvent(SecurityHistoryEventType.SPLIT, date, value));
                                        break;
                                    default:
                                        Logger.getLogger(YahooEventParser.class.getName()).log(Level.SEVERE, "Unknown event: " + fields[0]);
                                        break;
                                }
                            } catch (final DateTimeException | NumberFormatException ex) {
                                Logger.getLogger(YahooEventParser.class.getName()).log(Level.INFO, line);
                                Logger.getLogger(YahooEventParser.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                            }
                        }
                        line = in.readLine();
                    }
                }
            }
        }
    } catch (final NullPointerException | IOException ex) {
        Logger.getLogger(YahooEventParser.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    } finally {
        if (connection != null) {
            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).disconnect();
            }
        }
    }
    return events;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) LocalDate(java.time.LocalDate) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) BigDecimal(java.math.BigDecimal) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) DateTimeException(java.time.DateTimeException) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) HashSet(java.util.HashSet)

Example 9 with SecurityHistoryNode

use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.

the class HistoricalImportController method handleStartAction.

@FXML
private void handleStartAction() {
    disableUI.set(true);
    final DateTimeFormatter dateTimeFormatter = DateUtils.getShortDateFormatter();
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    updateTask = new Task<Void>() {

        @Override
        protected Void call() throws Exception {
            final LocalDate startDate = startDatePicker.getValue();
            final LocalDate endDate = endDatePicker.getValue();
            final Map<SecurityNode, List<SecurityHistoryNode>> historyMap = new HashMap<>();
            // create a defensive copy
            final List<SecurityNode> securityNodes = new ArrayList<>(checkListView.getCheckedItems());
            // need to determine the total count
            long historyCount = 0;
            // Collect and count the number of nodes
            for (final SecurityNode securityNode : securityNodes) {
                updateMessage(ResourceUtils.getString("Message.DownloadingX", securityNode.getSymbol()));
                final List<SecurityHistoryNode> historyNodes = UpdateFactory.downloadHistory(securityNode, startDate, endDate);
                // reverse the sort order
                Collections.reverse(historyNodes);
                historyCount += historyNodes.size();
                historyMap.put(securityNode, historyNodes);
            }
            // need to track the total processed count
            long processedHistory = 0;
            for (final Map.Entry<SecurityNode, List<SecurityHistoryNode>> entry : historyMap.entrySet()) {
                if (!requestCancel) {
                    for (final SecurityHistoryNode historyNode : entry.getValue()) {
                        if (!requestCancel) {
                            engine.addSecurityHistory(entry.getKey(), historyNode);
                            updateProgress(++processedHistory, historyCount);
                            updateMessage(ResourceUtils.getString("Message.UpdatedPriceDate", entry.getKey().getSymbol(), dateTimeFormatter.format(historyNode.getLocalDate())));
                        }
                    }
                }
            }
            updateMessage("");
            return null;
        }
    };
    updateTask.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, event -> taskComplete());
    updateTask.addEventHandler(WorkerStateEvent.WORKER_STATE_CANCELLED, event -> taskComplete());
    updateTask.addEventHandler(WorkerStateEvent.WORKER_STATE_FAILED, event -> taskComplete());
    progressBar.progressProperty().bind(updateTask.progressProperty());
    messageLabel.textProperty().bind(updateTask.messageProperty());
    final Thread thread = new Thread(updateTask);
    thread.setDaemon(true);
    thread.start();
}
Also used : LocalDate(java.time.LocalDate) ExecutionException(java.util.concurrent.ExecutionException) SecurityNode(jgnash.engine.SecurityNode) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) ArrayList(java.util.ArrayList) List(java.util.List) DateTimeFormatter(java.time.format.DateTimeFormatter) HashMap(java.util.HashMap) Map(java.util.Map) Engine(jgnash.engine.Engine) InjectFXML(jgnash.uifx.util.InjectFXML) FXML(javafx.fxml.FXML)

Example 10 with SecurityHistoryNode

use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.

the class SecurityHistoryController method handleAddPriceAction.

@FXML
private void handleAddPriceAction() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    final SecurityHistoryNode history = new SecurityHistoryNode(historyDatePicker.getValue(), closeTextField.getDecimal(), volumeTextField.getLong(), highTextField.getDecimal(), lowTextField.getDecimal());
    engine.addSecurityHistory(selectedSecurityNode.get(), history);
    clearPriceForm();
}
Also used : SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) Engine(jgnash.engine.Engine) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Aggregations

SecurityHistoryNode (jgnash.engine.SecurityHistoryNode)13 LocalDate (java.time.LocalDate)7 Engine (jgnash.engine.Engine)7 SecurityNode (jgnash.engine.SecurityNode)5 ArrayList (java.util.ArrayList)4 FXML (javafx.fxml.FXML)4 InjectFXML (jgnash.uifx.util.InjectFXML)4 BigDecimal (java.math.BigDecimal)3 List (java.util.List)3 SecurityHistoryEvent (jgnash.engine.SecurityHistoryEvent)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 HttpURLConnection (java.net.HttpURLConnection)2 URLConnection (java.net.URLConnection)2 Date (java.util.Date)2 AbstractEngineTest (jgnash.engine.AbstractEngineTest)2 DefaultHighLowDataset (org.jfree.data.xy.DefaultHighLowDataset)2 Test (org.junit.Test)2 Color (java.awt.Color)1