/** * Prints out the records for each symbol. * * <p>Can also verify the state of the database with csh> sqlite3 stocks.bd ".dump" * * @param symbols - list of symbols to dump data for */ static Map<String, List<QuoteRecord>> dumpSymbols(List<String> symbols) throws Exception { Connection conn = initialize(); Map<String, List<QuoteRecord>> symbolToQuotesMap = new TreeMap<String, List<QuoteRecord>>(); for (int i = 0; i < symbols.size(); i++) { String symbolname = symbols.get(i); List<QuoteRecord> symbolToQuotes = new ArrayList<QuoteRecord>(); symbolToQuotesMap.put(symbolname, symbolToQuotes); Statement stat = conn.createStatement(); try { ResultSet rs = stat.executeQuery("SELECT * FROM " + symbolname + ";"); // 'Date','Open','High','Low','Close','Volume','Adj Close' while (rs.next()) { String date = rs.getString("Date"); String open = rs.getString("Open"); String high = rs.getString("High"); String low = rs.getString("Low"); String close = rs.getString("Close"); String volume = rs.getString("Volume"); String adjClose = rs.getString("Adj Close"); QuoteRecord qr = new QuoteRecord(date, open, high, low, close, volume, adjClose); symbolToQuotes.add(qr); // System.out.println(symbolname + ":open," + open + ":date," + date); } // TODO: why/when do we need to close this? rs.close(); } catch (java.sql.SQLException e) { // it can happen that symbols in Symbols.java go away over time // so we need to ignore them } } return symbolToQuotesMap; }
/** * Makes the entries in the database for a given symbol up to date. * * <p>This method as it currently is written is wasteful, since it does not attempt to see if the * date range has any actual trading days - e.g., may try to pull in a range of a Sat and a * Sunday. * * @param symbol - the symbol we are trying to bring up to date */ static void makeUpToDate(String symbol) throws Exception { Connection conn = initialize(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("select * from " + symbol + ";"); Date today = new Date(); Date latestDateFoundSoFar = null; Date dateBeingProcessed = null; while (rs.next()) { String dateString = rs.getString("Date"); dateBeingProcessed = DateFromString(dateString); System.out.println("processing:" + dateBeingProcessed); if ((latestDateFoundSoFar == null) || (latestDateFoundSoFar.compareTo(dateBeingProcessed) < 0)) { latestDateFoundSoFar = dateBeingProcessed; System.out.println("\tupdated latestDateFoundSoFar:" + dateBeingProcessed); } } if (latestDateFoundSoFar.compareTo(today) < 0) { Calendar cal = new GregorianCalendar(); cal.setTime(latestDateFoundSoFar); Date succLatestDateFoundSoFar = nextDate(latestDateFoundSoFar); if (DaysAreEqual(succLatestDateFoundSoFar, today)) { return; } updateSymbol(symbol, succLatestDateFoundSoFar, today); } }
/** * Get the database as a map from symbols (e.g., "XOM") to a list of QuoteRecords. These records * are sorted in ascending order by date. * * <p>The procedure does not guarantee duplicate quote records have been removed, but it does draw * attention to their existence. (Such records can enter the database when performing * tests/updates.) * * @see QuoteRecord, CheckSymbolToQuotesMap, SortSymbolToQuotesMap */ public static Map<String, List<QuoteRecord>> GetDatabaseAsMap() { Map<String, List<QuoteRecord>> result = null; List<String> symbols = new ArrayList<String>(); Connection conn = null; try { conn = initialize(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("select * from listofsymbols;"); while (rs.next()) { symbols.add(rs.getString("symbolname")); } rs.close(); result = dumpSymbols(symbols); conn.close(); } catch (Exception e) { e.printStackTrace(); } SortSymbolToQuotesMap(result); // CheckSymbolToQuotesMap( result ); return result; }