Exemple #1
0
  public static List<TopDatum> parse(JSONArray a) throws JSONException {
    List<TopDatum> list = new ArrayList();
    DateFormat df = new SimpleDateFormat("HH:mm:ss");
    for (int i = 0; i < a.length(); i++) {
      JSONObject o = a.getJSONObject(i);
      TopDatum d = new TopDatum();
      d.setSymbol(new Symbol(o.getString("symbol")));
      d.setName(o.getString("name"));
      d.setLast(new BigDecimal(o.getString("trade")));
      d.setBuy(new BigDecimal(o.getString("buy")));
      d.setSell(new BigDecimal(o.getString("sell")));
      d.setPrevClose(new BigDecimal(o.getString("settlement")));
      d.setChange(new BigDecimal(o.getString("changepercent")));
      d.setAmount(new BigDecimal(o.getString("amount")));
      d.setOpen(new BigDecimal(o.getString("open")));
      try {
        Date t = df.parse(o.getString("ticktime"));
        Date date = TimeServer.now();
        MarketCalendar mc = Calendars.get(d.getSymbol().getMarket());
        if (!mc.isTradingDate(date)) {
          date = mc.previousTradingDate(date);
        } else {
          Date biddingStartTime = mc.getOpenBiddingStartTime(date);
          if (date.before(biddingStartTime)) {
            logger.info(
                "Using the previous date as the current time is before the bidding start time");
            date = mc.previousTradingDate(date);
          }
        }
        d.setTime(TimeUtils.merge(date, t));
      } catch (ParseException ex) {
        Logger.getLogger(TopFetcher.class.getName()).log(Level.SEVERE, null, ex);
        continue;
      }
      d.setVolume(o.getLong("volume"));
      d.setHigh(new BigDecimal(o.getString("high")));
      d.setLow(new BigDecimal(o.getString("low")));
      d.setMarketCapital(new BigDecimal(o.getString("mktcap")).multiply(new BigDecimal("10000")));
      d.setHandOff(new BigDecimal(o.getString("turnoverratio")));
      list.add(d);
    }
    Collections.sort(
        list,
        new Comparator<TopDatum>() {

          @Override
          public int compare(TopDatum o1, TopDatum o2) {
            return -o1.getChange().compareTo(o2.getChange());
          }
        });
    return list;
  }
 public void download(Set<Symbol> symbols) {
   if (symbols == null || symbols.isEmpty()) {
     symbols = getAllSymbols();
   }
   logger.log(Level.INFO, "Total {0} to be downloaded.", symbols.size());
   MarketCalendar cal = Calendars.get(symbols.iterator().next().getMarket());
   Date to = TimeServer.now();
   Date from = cal.timeAdd(to, -DAYS * cal.getDailyTradingTime());
   long used = 0;
   int i = 0;
   for (Symbol s : symbols) {
     if (checkDataFileExists(s)) {
       logger.log(Level.INFO, "{0}: Data file exists, will not download again.", s);
       continue;
     }
     logger.log(Level.INFO, "{0}: Downloading hist price data for...", s);
     long start = System.currentTimeMillis();
     try {
       download(s, from, to);
     } catch (Exception ex) {
       logger.log(Level.WARNING, s + ": failed to download hist price data", ex);
     }
     used += System.currentTimeMillis() - start;
     if (i > 0 && i % 10 == 0) {
       long avgUsed = used / 10;
       used = 0;
       long willUsed = (symbols.size() - i) * avgUsed / Interval.M1;
       logger.log(
           Level.INFO,
           "Total {0} of {1} symbols are completed, estimated the remaining will need {2} minutes to complete.",
           new Object[] {i, symbols.size(), willUsed});
     }
     try {
       Thread.sleep(SLEEP_TIME);
     } catch (Exception ex) {
     }
     i++;
   }
 }
  public void update(Set<Symbol> symbols) {
    if (symbols == null || symbols.isEmpty()) {
      symbols = getAllSymbols();
    }

    for (Symbol s : symbols) {
      try {
        Date from = null;
        Date to = TimeServer.now();
        HistPriceDatum last = getLastInFile(s);
        MarketCalendar cal = Calendars.get(s.getMarket());
        if (last == null) {
          logger.log(
              Level.WARNING,
              "{0}: not found data file, will use the default 'start date' to download the all data.",
              s);
          from = cal.timeAdd(to, -DAYS * cal.getDailyTradingTime());
        } else {
          from = last.getTime();
          if (cal.isInSameDate(from, to)) {
            logger.log(
                Level.INFO,
                "{0}: all data are up to date, no need to download",
                new Object[] {s, from, to});
            continue; // no need to download since all data are in file
          }
          from = cal.timeAdd(from, cal.getDailyTradingTime()); // a day ahead
        }
        logger.log(
            Level.INFO, "{0}: downloading price data from {1} to {2}", new Object[] {s, from, to});
        download(s, from, to);
        logger.log(
            Level.INFO, "{0}: Downloaded price data from {1} to {2}", new Object[] {s, from, to});
      } catch (Exception ex) {
        logger.log(Level.WARNING, s + ": ignored to download/update the hist price", ex);
      }
    }
  }