// Extract all quotes from the quote bundle which cause the given // expression to equate to true. If there is no expression (string is null or // empty) then extract all the quotes. private List extractQuotesUsingRule(String filterExpression, EODQuoteBundle quoteBundle) { // If there is no rule, then just return all quotes if (filterExpression == null || filterExpression.length() == 0) return extractAllQuotes(quoteBundle); // First parse the expression Expression expression = null; try { expression = Parser.parse(filterExpressionString); } catch (ExpressionException e) { // We should have already checked the string for errors before here assert false; } // Add symbols to list when expression proves true ArrayList quotes = new ArrayList(); Iterator iterator = quoteBundle.iterator(); TradingDate lastDate = quoteBundle.getLastDate(); try { // Traverse all symbols on all dates while (iterator.hasNext()) { Quote quote = (Quote) iterator.next(); Symbol symbol = quote.getSymbol(); TradingDate date = quote.getDate(); int dateOffset = 0; try { dateOffset = quoteBundle.dateToOffset(date); } catch (WeekendDateException e) { assert false; } if (!singleDate || (lastDate.equals(quote.getDate()))) { if (expression.evaluate(new Variables(), quoteBundle, symbol, dateOffset) >= Expression.TRUE_LEVEL) quotes.add(quote); } } return quotes; } catch (EvaluationException e) { // Tell user expression didnt evaluate properly JOptionPane.showInternalMessageDialog( DesktopManager.getDesktop(), e.getReason() + ": " + expression.toString(), Locale.getString("ERROR_EVALUATION_EQUATION"), JOptionPane.ERROR_MESSAGE); // delete erroneous expression expression = null; // If the expression didn't evaluate then just return all the quotes return extractAllQuotes(quoteBundle); } }
// This function extracts all quotes from the quote bundle and returns // them as a list of Quotes. private List extractAllQuotes(EODQuoteBundle quoteBundle) { List quotes = new ArrayList(); Iterator iterator = quoteBundle.iterator(); TradingDate lastDate = quoteBundle.getLastDate(); // Traverse all symbols on all dates while (iterator.hasNext()) { Quote quote = (Quote) iterator.next(); if (!singleDate || (lastDate.equals(quote.getDate()))) quotes.add(quote); } return quotes; }
/** * Creates two objects to improve performance of getValue(). The first object contains an array of * values so that the getValue() function can return values based on an index, rather than using * date calculations to extract the date from the Graphable. The second object contains a mapping * which maps the current date to an offset in the newly created array. */ private void initialise() { dateOffsets = new HashMap(); values = new ArrayList(); TradingDate endDate = (TradingDate) graphable.getEndX(); int offset = 0; for (TradingDate date = (TradingDate) graphable.getStartX(); !date.after(endDate); date = date.next(1)) { values.add(graphable.getY(date)); dateOffsets.put(date, new Integer(offset++)); } }
public double evaluate(Variables variables, QuoteBundle quoteBundle, Symbol symbol, int day) { TradingDate date = quoteBundle.offsetToDate(day); return date.getDay(); }