예제 #1
0
 private String toString(HistPriceDatum d) {
   List<String> l = new ArrayList();
   l.add(d.getSymbol().toString());
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
   l.add(df.format(d.getTime()));
   l.add(d.getOpen().toString());
   l.add(d.getHigh().toString());
   l.add(d.getLow().toString());
   l.add(d.getClose().toString());
   l.add(Long.toString(d.getVolume()));
   if (d.getAmount() == null) {
     l.add(null);
   } else {
     l.add(d.getAmount().toString());
   }
   String line = "";
   for (String s : l) {
     if (s == null) {
       line += ",";
     } else {
       line += s + ",";
     }
   }
   return line.substring(0, line.length() - 1);
 }
예제 #2
0
 private List<HistPriceDatum> loadHistPriceData(File f) throws IOException {
   List<HistPriceDatum> list = new FixedSizeList(MAX_HISTORICAL_RECORD);
   BufferedReader br = new BufferedReader(new FileReader(f));
   for (String l = br.readLine(); l != null; l = br.readLine()) {
     try {
       HistPriceDatum d = toHistPriceDatum(l);
       if (d.getVolume() == 0) {
         continue; // ignored
       }
       list.add(d);
     } catch (ParseException ex) {
       logger.log(Level.WARNING, "Cannot parse " + l, ex);
     }
   }
   br.close();
   return list;
 }
예제 #3
0
 public void truncate(Date date) {
   File dir = new File(DATA_DIR);
   File[] allFiles = dir.listFiles();
   for (File f : allFiles) {
     List<HistPriceDatum> list = new ArrayList();
     try {
       BufferedReader br = new BufferedReader(new FileReader(f));
       for (String l = br.readLine(); l != null; l = br.readLine()) {
         try {
           HistPriceDatum d = toHistPriceDatum(l);
           if (d.getVolume() == 0) {
             continue; // ignored
           }
           if (d.getTime().equals(date)) {
             break;
           }
           list.add(d);
         } catch (ParseException ex) {
           logger.log(Level.WARNING, "Cannot parse " + l, ex);
         }
       }
       br.close();
       try {
         BufferedWriter bw = new BufferedWriter(new FileWriter(f));
         for (int i = 0; i < list.size(); i++) {
           bw.write(toString(list.get(i)));
           bw.newLine();
         }
         bw.flush();
         bw.close();
       } catch (IOException ex) {
         logger.log(Level.WARNING, "Failed to save hist price data into file - " + f, ex);
       }
     } catch (IOException ex) {
       logger.log(Level.WARNING, "", ex);
     }
   }
 }
예제 #4
0
  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);
      }
    }
  }
예제 #5
0
 private HistPriceDatum toHistPriceDatum(String l) throws ParseException {
   HistPriceDatum d = new HistPriceDatum();
   String[] fs = l.split(",");
   int i = 0;
   d.setSymbol(new Symbol(fs[i++]));
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
   d.setTime(df.parse(fs[i++]));
   d.setOpen(new BigDecimal(fs[i++]));
   d.setHigh(new BigDecimal(fs[i++]));
   d.setLow(new BigDecimal(fs[i++]));
   d.setClose(new BigDecimal(fs[i++]));
   d.setVolume(Long.parseLong(fs[i++]));
   if (i < fs.length - 1) {
     String a = fs[i++];
     if (a != null && !a.isEmpty()) {
       d.setAmount(new BigDecimal(a));
     }
   }
   return d;
 }