Пример #1
0
    @Override
    public Boolean call() throws Exception {
      boolean result = false;

      final Engine e = EngineFactory.getEngine(EngineFactory.DEFAULT);

      if (e != null && securityNode.getQuoteSource() != QuoteSource.NONE) {
        final SecurityParser parser = securityNode.getQuoteSource().getParser();

        if (parser != null
            && !Thread.currentThread().isInterrupted()) { // check for thread interruption
          if (parser.parse(securityNode)) {

            final SecurityHistoryNode node =
                new SecurityHistoryNode(
                    parser.getDate(),
                    parser.getPrice(),
                    parser.getVolume(),
                    parser.getHigh(),
                    parser.getLow());

            if (!Thread.currentThread().isInterrupted()) { // check for thread interruption
              result = e.addSecurityHistory(securityNode, node);

              if (result) {
                logger.info(
                    ResourceUtils.getString("Message.UpdatedPrice", securityNode.getSymbol()));
              }
            }
          }
        }
      }

      return result;
    }
Пример #2
0
  private void updateCommodityText() {
    if (!commodityList.isEmpty()) {
      StringBuilder buf = new StringBuilder();
      Iterator<SecurityNode> it = commodityList.iterator();

      SecurityNode node = it.next();
      buf.append(node.getSymbol());
      while (it.hasNext()) {
        buf.append(", ");
        node = it.next();
        buf.append(node.getSymbol());
      }
      securityButton.setText(buf.toString());
      securityButton.setToolTipText(buf.toString());
    } else {
      securityButton.setText(rb.getString("Word.None"));
    }
  }
Пример #3
0
  private CurrencyNode decodeCurrency(final String currency) {

    // Check if the currency is already in the cache.
    if (currencyCache.containsKey(currency)) {
      return currencyCache.get(currency);
    }

    String split[] = CURRENCY_DELIMITER_PATTERN.split(currency);
    String symbol = split[0];

    CurrencyNode node = EngineFactory.getEngine(EngineFactory.DEFAULT).getCurrency(symbol);

    if (node == null) {
      logger.log(Level.INFO, "Converting a commodity into a currency: {0}", symbol);

      Commodity cNode = commodityMap.get(symbol);

      if (cNode != null) {
        node = new CurrencyNode();

        node.setDescription(cNode.description);
        node.setPrefix(cNode.prefix);
        node.setSuffix(cNode.suffix);
        node.setScale(cNode.scale);
        node.setSymbol(cNode.symbol);

        EngineFactory.getEngine(EngineFactory.DEFAULT).addCurrency(node);
      } else {
        // Convert security to currency.  For users who figured out how to push the limits of the
        // jGnash 1.x commodity interface
        SecurityNode sNode = EngineFactory.getEngine(EngineFactory.DEFAULT).getSecurity(symbol);
        if (sNode != null) {
          node = new CurrencyNode();

          node.setDescription(sNode.getDescription());
          node.setPrefix(sNode.getPrefix());
          node.setSuffix(sNode.getSuffix());
          node.setScale(sNode.getScale());
          node.setSymbol(sNode.getSymbol());

          EngineFactory.getEngine(EngineFactory.DEFAULT).addCurrency(node);
        } else {
          logger.log(Level.SEVERE, "Bad file, currency " + symbol + " not mapped", new Exception());
        }
      }
    }

    // Put the currency in the cache.
    currencyCache.put(currency, node);

    return node;
  }
Пример #4
0
  private void writeSecID(final SecurityNode node) {

    // write security information
    indentedWriter.println(wrapOpen(SECID), indentLevel++);

    if (node.getISIN() != null && !node.getISIN().isEmpty()) {
      indentedWriter.println(wrap(UNIQUEID, node.getISIN()), indentLevel);
    } else {
      indentedWriter.println(wrap(UNIQUEID, node.getSymbol()), indentLevel);
    }

    indentedWriter.println(wrap(UNIQUEIDTYPE, "CUSIP"), indentLevel);
    indentedWriter.println(wrapClose(SECID), --indentLevel);
  }
Пример #5
0
    @Override
    public Boolean call() throws Exception {

      boolean result = true;

      final Engine e = EngineFactory.getEngine(EngineFactory.DEFAULT);

      final LocalDate oldest = securityNode.getHistoryNodes().get(0).getLocalDate();

      if (e != null && securityNode.getQuoteSource() != QuoteSource.NONE) {

        final Set<SecurityHistoryEvent> oldHistoryEvents =
            new HashSet<>(securityNode.getHistoryEvents());

        for (final SecurityHistoryEvent securityHistoryEvent :
            YahooEventParser.retrieveNew(securityNode)) {
          if (!Thread.currentThread().isInterrupted()) { // check for thread interruption

            if (securityHistoryEvent.getDate().isAfter(oldest)
                || securityHistoryEvent.getDate().isEqual(oldest)) {
              if (!oldHistoryEvents.contains(securityHistoryEvent)) {
                result = e.addSecurityHistoryEvent(securityNode, securityHistoryEvent);

                if (result) {
                  logger.info(
                      ResourceUtils.getString(
                          "Message.UpdatedSecurityEvent", securityNode.getSymbol()));
                }
              }
            }
          }
        }
      }

      return result;
    }
Пример #6
0
  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();

    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) {

        // Read, parse, and load the new history nodes into a list to be persisted later.  A
        // relational
        // 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

            line = in.readLine(); // prime the first read

            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;
  }