Exemplo n.º 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;
    }
Exemplo n.º 2
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);
  }
Exemplo n.º 3
0
    @Override
    public void run() {
      try {
        SecurityNode clone = (SecurityNode) sNode.clone();

        Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

        CurrencyNode currencyNode = engine.getCurrency(cNode);

        if (currencyNode != null) {
          clone.setReportedCurrencyNode(currencyNode);
          engine.updateCommodity(sNode, clone);
        }
      } catch (CloneNotSupportedException e) {
        logger.log(Level.SEVERE, e.toString(), e);
      }
    }
Exemplo n.º 4
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"));
    }
  }
Exemplo n.º 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;
    }
Exemplo n.º 6
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;
  }
Exemplo n.º 7
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;
  }
Exemplo n.º 8
0
  private SecurityNode decodeSecurity(final String symbol) {
    Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

    SecurityNode sNode = engine.getSecurity(symbol);

    if (sNode == null) {

      Commodity cNode = commodityMap.get(symbol);

      if (cNode != null) {

        logger.log(Level.INFO, "Converting a commodity into a security: {0}", symbol);

        sNode = new SecurityNode(engine.getDefaultCurrency());

        sNode.setDescription(cNode.description);
        sNode.setPrefix(cNode.prefix);
        sNode.setSuffix(cNode.suffix);
        sNode.setScale(cNode.scale);
        sNode.setSymbol(cNode.symbol);
        sNode.setQuoteSource(QuoteSource.YAHOO);

        engine.addSecurity(sNode);
      } else { // may be a currency... try to create a security

        CurrencyNode currency = decodeCurrency(symbol);

        if (currency != null) {

          logger.info("Converting a currency into a security");

          sNode = new SecurityNode();

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

          sNode.setReportedCurrencyNode(currency);
          sNode.setQuoteSource(QuoteSource.YAHOO);

          engine.addSecurity(sNode);
        }
      }
    }

    if (sNode == null) {
      logger.log(Level.SEVERE, "Bad file, security {0} not mapped", symbol);
    }

    return sNode;
  }
Exemplo n.º 9
0
  private void parseSecurityNode(final XMLStreamReader reader) {
    Map<String, String> elementMap = new HashMap<>();
    List<SecurityHistoryNode> history = null;

    try {
      /* still at start of the element.  Need to know when end is reached */
      QName parsingElement = reader.getName();

      while (reader.hasNext()) {
        int event = reader.next();

        switch (event) {
          case XMLStreamConstants.START_ELEMENT:
            if (reader.getLocalName().equals("history")) {
              logger.finest("parse history");
              history = parseHistoryNodes(reader);
            } else {
              String element = reader.getLocalName();
              elementMap.put(element, reader.getElementText());
            }
            break;
          case XMLStreamConstants.END_ELEMENT:
            if (reader.getName().equals(parsingElement)) {
              logger.finest("Found the end of a SecurityNode");

              Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);

              if (engine.getSecurity(elementMap.get("symbol")) == null) {

                SecurityNode node = new SecurityNode(engine.getDefaultCurrency());

                node.setSymbol(elementMap.get("symbol"));
                node.setDescription(elementMap.get("description"));
                node.setPrefix(elementMap.get("prefix"));
                node.setSuffix(elementMap.get("suffix"));
                node.setScale(Byte.parseByte(elementMap.get("scale")));
                node.setQuoteSource(QuoteSource.YAHOO);

                if (elementMap.get("reportedCurrency") != null) {
                  SecurityThread thread =
                      new SecurityThread(node, elementMap.get("reportedCurrency"));
                  workQueue.add(thread);
                }

                engine.addSecurity(node);
              }

              if (history != null) {
                SecurityNode node = engine.getSecurity(elementMap.get("symbol"));
                if (node != null) {
                  for (SecurityHistoryNode hNode : history) {
                    if (!engine.addSecurityHistory(node, hNode)) {
                      logger.warning("Failed to add security history");
                    }
                  }
                }
              }
              return;
            }
            break;

          default:
            break;
        }
      }
    } catch (XMLStreamException e) {
      logger.log(Level.SEVERE, "Exception at element: {0}", reader.getName().toString());
      logger.log(Level.SEVERE, e.toString(), e);
    }
  }