Search in sources :

Example 1 with SecurityHistoryEvent

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

the class YahooEventParserTest method testParser.

@Test
public void testParser() {
    final SecurityNode ibm = new SecurityNode(e.getDefaultCurrency());
    ibm.setSymbol("IBM");
    ibm.setScale((byte) 2);
    final SecurityHistoryNode historyNode = new SecurityHistoryNode(LocalDate.of(1962, Month.JANUARY, 1), BigDecimal.TEN, 1000, BigDecimal.TEN, BigDecimal.TEN);
    e.addSecurity(ibm);
    e.addSecurityHistory(ibm, historyNode);
    final Set<SecurityHistoryEvent> events = YahooEventParser.retrieveNew(ibm, LocalDate.of(2015, Month.AUGUST, 22));
    assertNotNull(events);
    assertEquals(219, events.size());
}
Also used : SecurityNode(jgnash.engine.SecurityNode) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) AbstractEngineTest(jgnash.engine.AbstractEngineTest) Test(org.junit.Test)

Example 2 with SecurityHistoryEvent

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

the class SecurityHistoryController method initialize.

@FXML
void initialize() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    numberFormat.set(CommodityFormat.getShortNumberFormat(engine.getDefaultCurrency()));
    selectedSecurityHistoryNode.bind(priceTableView.getSelectionModel().selectedItemProperty());
    selectedSecurityNode.bind(securityComboBox.getSelectionModel().selectedItemProperty());
    deletePriceButton.disableProperty().bind(Bindings.isNull(selectedSecurityHistoryNode));
    selectedSecurityHistoryEvent.bind(eventTableView.getSelectionModel().selectedItemProperty());
    deleteEventButton.disableProperty().bind(Bindings.isNull(selectedSecurityHistoryEvent));
    // Disabled the update button if a security is not selected, or it does not have a quote source
    updatePriceButton.disableProperty().bind(Bindings.or(Bindings.isNull(selectedSecurityNode), Bindings.equal(QuoteSource.NONE, quoteSource)));
    // Disabled the update button if a security is not selected, or it does not have a quote source
    updateEventButton.disableProperty().bind(Bindings.or(Bindings.isNull(selectedSecurityNode), Bindings.equal(QuoteSource.NONE, quoteSource)));
    // Can't add if a security is not selected
    addPriceButton.disableProperty().bind(Bindings.isNull(selectedSecurityNode));
    // Can't add if a security is not selected and a value is not set
    addEventButton.disableProperty().bind(Bindings.isNull(selectedSecurityNode).or(Bindings.isEmpty(eventValueTextField.textProperty())));
    priceTableView.setTableMenuButtonVisible(false);
    priceTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    final TableColumn<SecurityHistoryNode, LocalDate> priceDateColumn = new TableColumn<>(resources.getString("Column.Date"));
    priceDateColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getLocalDate()));
    priceDateColumn.setCellFactory(cell -> new ShortDateTableCell<>());
    priceTableView.getColumns().add(priceDateColumn);
    final TableColumn<SecurityHistoryNode, BigDecimal> priceCloseColumn = new TableColumn<>(resources.getString("Column.Close"));
    priceCloseColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getPrice()));
    priceCloseColumn.setCellFactory(cell -> new BigDecimalTableCell<>(numberFormat));
    priceTableView.getColumns().add(priceCloseColumn);
    final TableColumn<SecurityHistoryNode, BigDecimal> priceLowColumn = new TableColumn<>(resources.getString("Column.Low"));
    priceLowColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getLow()));
    priceLowColumn.setCellFactory(cell -> new BigDecimalTableCell<>(numberFormat));
    priceTableView.getColumns().add(priceLowColumn);
    final TableColumn<SecurityHistoryNode, BigDecimal> priceHighColumn = new TableColumn<>(resources.getString("Column.High"));
    priceHighColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getHigh()));
    priceHighColumn.setCellFactory(cell -> new BigDecimalTableCell<>(numberFormat));
    priceTableView.getColumns().add(priceHighColumn);
    final TableColumn<SecurityHistoryNode, Long> priceVolumeColumn = new TableColumn<>(resources.getString("Column.Volume"));
    priceVolumeColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getVolume()));
    priceVolumeColumn.setCellFactory(cell -> new LongFormatTableCell());
    priceTableView.getColumns().add(priceVolumeColumn);
    priceTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    sortedHistoryList.comparatorProperty().bind(priceTableView.comparatorProperty());
    priceTableView.setItems(sortedHistoryList);
    final TableColumn<SecurityHistoryEvent, LocalDate> eventDateColumn = new TableColumn<>(resources.getString("Column.Date"));
    eventDateColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getDate()));
    eventDateColumn.setCellFactory(cell -> new ShortDateTableCell<>());
    eventTableView.getColumns().add(eventDateColumn);
    final TableColumn<SecurityHistoryEvent, String> eventActionColumn = new TableColumn<>(resources.getString("Column.Event"));
    eventActionColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getType().toString()));
    eventTableView.getColumns().add(eventActionColumn);
    final NumberFormat decimalFormat = NumberFormat.getInstance();
    if (decimalFormat instanceof DecimalFormat) {
        decimalFormat.setMinimumFractionDigits(MathConstants.SECURITY_PRICE_ACCURACY);
        decimalFormat.setMaximumFractionDigits(MathConstants.SECURITY_PRICE_ACCURACY);
    }
    final TableColumn<SecurityHistoryEvent, BigDecimal> eventValueColumn = new TableColumn<>(resources.getString("Column.Value"));
    eventValueColumn.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getValue()));
    eventValueColumn.setCellFactory(cell -> new BigDecimalTableCell<>(decimalFormat));
    eventTableView.getColumns().add(eventValueColumn);
    eventTableView.setTableMenuButtonVisible(false);
    eventTableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    eventTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    sortedHistoryEventList.comparatorProperty().bind(eventTableView.comparatorProperty());
    eventTableView.setItems(sortedHistoryEventList);
    eventValueTextField.scaleProperty().set(MathConstants.SECURITY_PRICE_ACCURACY);
    chart = new SecurityNodeAreaChart();
    chart.securityNodeProperty().bind(selectedSecurityNode);
    chartPane.getChildren().addAll(chart);
    securityComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            numberFormat.set(CommodityFormat.getShortNumberFormat(newValue.getReportedCurrencyNode()));
            closeTextField.scaleProperty().set(newValue.getScale());
            lowTextField.scaleProperty().set(newValue.getScale());
            highTextField.scaleProperty().set(newValue.getScale());
            quoteSource.set(newValue.getQuoteSource());
            Platform.runLater(this::loadTables);
        }
    });
    selectedSecurityHistoryNode.addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            loadPriceForm();
        } else {
            clearPriceForm();
        }
    });
    selectedSecurityHistoryEvent.addListener((observable, oldValue, newValue) -> {
        if (newValue != null) {
            loadEventForm();
        } else {
            clearEventForm();
        }
    });
    // Install a listener to unregister from the message bus when the window closes
    parent.addListener((observable, oldValue, scene) -> {
        if (scene != null) {
            scene.windowProperty().get().addEventHandler(WindowEvent.WINDOW_HIDING, event -> {
                Logger.getLogger(SecurityHistoryController.class.getName()).info("Unregistered from the message bus");
                MessageBus.getInstance().unregisterListener(SecurityHistoryController.this, MessageChannel.COMMODITY);
            });
        }
    });
    Platform.runLater(() -> MessageBus.getInstance().registerListener(SecurityHistoryController.this, MessageChannel.COMMODITY));
}
Also used : DecimalFormat(java.text.DecimalFormat) TableColumn(javafx.scene.control.TableColumn) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) SecurityNodeAreaChart(jgnash.uifx.control.SecurityNodeAreaChart) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) Engine(jgnash.engine.Engine) NumberFormat(java.text.NumberFormat) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Example 3 with SecurityHistoryEvent

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

the class SecurityHistoryController method handleAddEventAction.

@FXML
private void handleAddEventAction() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    final SecurityHistoryEvent event = new SecurityHistoryEvent(securityEventTypeComboBox.getValue(), eventDatePicker.getValue(), eventValueTextField.getDecimal());
    engine.addSecurityHistoryEvent(selectedSecurityNode.get(), event);
    clearEventForm();
}
Also used : Engine(jgnash.engine.Engine) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Example 4 with SecurityHistoryEvent

use of jgnash.engine.SecurityHistoryEvent 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 5 with SecurityHistoryEvent

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

the class SecurityHistoryController method handleDeleteEventAction.

@FXML
private void handleDeleteEventAction() {
    final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
    Objects.requireNonNull(engine);
    final List<SecurityHistoryEvent> events = new ArrayList<>(eventTableView.getSelectionModel().getSelectedItems());
    // work backwards through the deletion list
    Collections.reverse(events);
    for (final SecurityHistoryEvent securityHistoryEvent : events) {
        engine.removeSecurityHistoryEvent(selectedSecurityNode.get(), securityHistoryEvent);
    }
}
Also used : ArrayList(java.util.ArrayList) Engine(jgnash.engine.Engine) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Aggregations

SecurityHistoryEvent (jgnash.engine.SecurityHistoryEvent)5 FXML (javafx.fxml.FXML)3 Engine (jgnash.engine.Engine)3 SecurityHistoryNode (jgnash.engine.SecurityHistoryNode)3 InjectFXML (jgnash.uifx.util.InjectFXML)3 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 URLConnection (java.net.URLConnection)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 DateTimeException (java.time.DateTimeException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TableColumn (javafx.scene.control.TableColumn)1 AbstractEngineTest (jgnash.engine.AbstractEngineTest)1 SecurityNode (jgnash.engine.SecurityNode)1