use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.
the class ApiTest method testSecurities.
@Test
public void testSecurities() {
SecurityNode securityNode = new SecurityNode(e.getDefaultCurrency());
securityNode.setSymbol("GGG");
securityNode.setScale((byte) 2);
e.addSecurity(securityNode);
assertEquals(securityNode, e.getSecurity("GGG"));
assertEquals(0, securityNode.getHistoryNodes().size());
assertFalse(securityNode.getClosestHistoryNode(LocalDate.now()).isPresent());
assertFalse(securityNode.getHistoryNode(LocalDate.now()).isPresent());
assertEquals(BigDecimal.ZERO, securityNode.getMarketPrice(LocalDate.now(), e.getDefaultCurrency()));
// Add the security node to the account
final List<SecurityNode> securitiesList = new ArrayList<>();
securitiesList.addAll(investAccount.getSecurities());
final int securityCount = securitiesList.size();
securitiesList.add(securityNode);
e.updateAccountSecurities(investAccount, securitiesList);
assertEquals(securityCount + 1, investAccount.getSecurities().size());
// Returned market price should be zero
assertEquals(BigDecimal.ZERO, Engine.getMarketPrice(new ArrayList<>(), securityNode, e.getDefaultCurrency(), LocalDate.now()));
// Create and add some security history
final SecurityHistoryNode historyNode = new SecurityHistoryNode(LocalDate.now(), BigDecimal.TEN, 10000, BigDecimal.TEN, BigDecimal.TEN);
e.addSecurityHistory(securityNode, historyNode);
assertEquals(1, securityNode.getHistoryNodes().size());
// Returned market price should be 10
assertEquals(BigDecimal.TEN, Engine.getMarketPrice(new ArrayList<>(), securityNode, e.getDefaultCurrency(), LocalDate.now()));
// Ensure the security updates correctly with a replacement value
final SecurityHistoryNode historyNodeReplacement = new SecurityHistoryNode(LocalDate.now(), BigDecimal.ONE, 10000, BigDecimal.ONE, BigDecimal.ONE);
e.addSecurityHistory(securityNode, historyNodeReplacement);
assertEquals(1, securityNode.getHistoryNodes().size());
// Returned market price should be 1
assertEquals(BigDecimal.ONE, Engine.getMarketPrice(new ArrayList<>(), securityNode, e.getDefaultCurrency(), LocalDate.now()));
}
use of jgnash.engine.SecurityHistoryNode 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());
}
use of jgnash.engine.SecurityHistoryNode 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));
}
use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.
the class SecurityHighLowChart method createHighLowDataSet.
private static AbstractXYDataset createHighLowDataSet(final SecurityNode node) {
Objects.requireNonNull(node);
List<SecurityHistoryNode> hNodes = node.getHistoryNodes();
int count = hNodes.size();
Date[] date = new Date[count];
double[] high = new double[count];
double[] low = new double[count];
double[] open = new double[count];
double[] close = new double[count];
double[] volume = new double[count];
for (int i = 0; i < count; i++) {
final SecurityHistoryNode hNode = hNodes.get(i);
date[i] = DateUtils.asDate(hNode.getLocalDate());
high[i] = hNode.getAdjustedHigh().doubleValue();
low[i] = hNode.getAdjustedLow().doubleValue();
open[i] = hNode.getAdjustedPrice().doubleValue();
close[i] = hNode.getAdjustedPrice().doubleValue();
volume[i] = hNode.getVolume();
}
return new DefaultHighLowDataset(node.getDescription(), date, high, low, open, close, volume);
}
use of jgnash.engine.SecurityHistoryNode in project jgnash by ccavanaugh.
the class GenericImport method importSecurities.
public static void importSecurities(final List<ImportSecurity> importSecurities, final CurrencyNode currencyNode) {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
for (final ImportSecurity importSecurity : importSecurities) {
if (!ImportUtils.matchSecurity(importSecurity).isPresent()) {
// Import only if a match is not found
final SecurityNode securityNode = ImportUtils.createSecurityNode(importSecurity, currencyNode);
// link the security node
importSecurity.setSecurityNode(securityNode);
engine.addSecurity(securityNode);
// if the ImportSecurity has pricing information, import it as well
importSecurity.getLocalDate().ifPresent(localDate -> importSecurity.getUnitPrice().ifPresent(price -> {
SecurityHistoryNode securityHistoryNode = new SecurityHistoryNode(localDate, price, 0, price, price);
engine.addSecurityHistory(securityNode, securityHistoryNode);
}));
} else {
// check to see if the cuspid needs to be updated
// link the security node
ImportUtils.matchSecurity(importSecurity).ifPresent(importSecurity::setSecurityNode);
ImportUtils.matchSecurity(importSecurity).ifPresent(securityNode -> importSecurity.getId().ifPresent(securityId -> {
if (securityNode.getISIN() == null || securityNode.getISIN().isEmpty()) {
try {
final SecurityNode clone = (SecurityNode) securityNode.clone();
clone.setISIN(securityId);
engine.updateCommodity(securityNode, clone);
Logger.getLogger(GenericImport.class.getName()).info("Assigning CUSPID");
} catch (final CloneNotSupportedException e) {
Logger.getLogger(GenericImport.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
}));
}
}
}
Aggregations