Exemple #1
0
  // If the user double clicks on a stock with the LMB, graph the stock.
  // If the user right clicks over the table, open up a popup menu.
  private void handleMouseClicked(MouseEvent event) {

    Point point = event.getPoint();

    // Right click on the table - raise menu
    if (event.getButton() == MouseEvent.BUTTON3) {
      JPopupMenu menu = new JPopupMenu();

      popupGraphSymbols = MenuHelper.addMenuItem(this, menu, Locale.getString("GRAPH"));
      popupGraphSymbols.setEnabled(getSelectedRowCount() > 0);

      popupTableSymbols = MenuHelper.addMenuItem(this, menu, Locale.getString("TABLE"));
      popupTableSymbols.setEnabled(getSelectedRowCount() > 0);

      menu.show(this, point.x, point.y);
    }

    // Left double click on the table - graph stock
    else if (event.getButton() == MouseEvent.BUTTON1 && event.getClickCount() == 2) {
      int[] selectedRows = getSelectedRows();
      List symbols = new ArrayList();
      for (int i = 0; i < selectedRows.length; i++) {
        Symbol symbol = (Symbol) model.getValueAt(selectedRows[i], EODQuoteModel.SYMBOL_COLUMN);

        symbols.add(symbol);
      }

      // Graph the highlighted symbols
      CommandManager.getInstance().graphStockBySymbol(symbols);
    }
  }
  private boolean isAllValuesAcceptable() {
    boolean retValue = true;

    try {
      setNumericalValues();
    } catch (ParseException e) {
      showErrorMessage(
          Locale.getString("ERROR_PARSING_NUMBER", e.getMessage()),
          Locale.getString("INVALID_GP_ERROR"));
      retValue = false;
    }

    if (!isAllValuesPositive()) {
      showErrorMessage(
          Locale.getString("NO_POSITIVE_VALUES_ERROR"), Locale.getString("INVALID_GP_ERROR"));
      retValue = false;
    }

    if (!isTotalOK()) {
      // Messages inside the isTotalOK method
      retValue = false;
    }

    return retValue;
  }
Exemple #3
0
  // Create a menu
  private void addMenu() {
    menuBar = new JMenuBar();

    // Table Menu
    {
      JMenu tableMenu = MenuHelper.addMenu(menuBar, Locale.getString("TABLE"));

      // Show columns menu
      tableMenu.add(createShowColumnMenu(model));

      tableMenu.addSeparator();

      applyExpressions =
          MenuHelper.addMenuItem(this, tableMenu, Locale.getString("APPLY_EQUATIONS"));

      applyFilter = MenuHelper.addMenuItem(this, tableMenu, Locale.getString("APPLY_FILTER"));

      sortByMostActive =
          MenuHelper.addMenuItem(this, tableMenu, Locale.getString("SORT_BY_MOST_ACTIVE"));

      tableMenu.addSeparator();

      tableClose = MenuHelper.addMenuItem(this, tableMenu, Locale.getString("CLOSE"));
    }

    // Symbols Menu
    {
      JMenu symbolsMenu = MenuHelper.addMenu(menuBar, Locale.getString("SYMBOLS"));

      findSymbol = MenuHelper.addMenuItem(this, symbolsMenu, Locale.getString("FIND"));

      symbolsMenu.addSeparator();

      graphSymbols = MenuHelper.addMenuItem(this, symbolsMenu, Locale.getString("GRAPH"));
      tableSymbols = MenuHelper.addMenuItem(this, symbolsMenu, Locale.getString("TABLE"));

      alertSymbols = MenuHelper.addMenuItem(this, symbolsMenu, Locale.getString("ALERT_TITLE"));
    }

    // Listen for changes in selection so we can update the menus
    getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {

              public void valueChanged(ListSelectionEvent e) {
                checkMenuDisabledStatus();
              }
            });

    checkMenuDisabledStatus();
  }
  private boolean isTotalOK() {
    boolean retValue = true;

    long total = 0;
    int totalLength = perc.length;
    for (int i = 0; (i < totalLength); i++) {
      total += perc[i];
    }
    // Check total == 0
    if (total == 0) {
      showErrorMessage(
          Locale.getString("NO_TOTAL_GREATER_THAN_ZERO_PAGE_ERROR"),
          Locale.getString("INVALID_GP_ERROR"));
      retValue = false;
    }
    return retValue;
  }
  public boolean parse() {
    boolean returnValue = true;

    mutations = 0;

    if (!isAllValuesAcceptable()) {
      returnValue = false;
    } else {
      if (!isFitAll()) {
        ConfirmDialog dialog =
            new ConfirmDialog(
                desktop, Locale.getString("GP_FIT_PAGE"), Locale.getString("GP_FIT_TITLE"));
        boolean returnConfirm = dialog.showDialog();
        if (returnConfirm) {
          fitAll();
        } else {
          returnValue = false;
        }
      }
    }

    try {

      if (!numberMutationTextRow.getText().equals("")) {
        mutations = Integer.parseInt(numberMutationTextRow.getText());
      }
    } catch (NumberFormatException e) {
      showErrorMessage(
          Locale.getString("ERROR_PARSING_NUMBER", e.getMessage()),
          Locale.getString("INVALID_GP_ERROR"));
      returnValue = false;
    }

    // If we have maximum percent for random population,
    // we don't have to check for the table,
    // because the values on the table are of initial population
    // and we do not need them.
    if (perc[PERCENT_RANDOM] != PERCENT_INT) {
      // Parse all the values in the GPPageInitialPopulationModule
      // so that we know if the table with initial population rules is OK or not.
      if (!GPPageInitialPopulationModule.parse()) {
        returnValue = false;
      }
    }
    return returnValue;
  }
Exemple #6
0
  // 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);
    }
  }
Exemple #7
0
  /**
   * Return the window title.
   *
   * @return the window title
   */
  public String getTitle() {
    // Title depends on the quote bundle we are listing
    String title = Locale.getString("TABLE_OF", quoteBundle.getQuoteRange().getDescription());

    // If there is only one date it makes sense to tell the user it
    if (singleDate)
      title = title.concat(" (" + quoteBundle.getLastDate().toString("dd/mm/yyyy") + ")");

    return title;
  }
  /**
   * Parse the given text string and returns the stock quote or null if it did not contain a valid
   * quote.
   *
   * @param quoteLine a single line of text containing a quote
   * @return the stock quote
   * @exception QuoteFormatException if the quote could not be parsed
   */
  public EODQuote toEODQuote(String quoteLine) throws QuoteFormatException {
    EODQuote quote = null;

    if (quoteLine != null) {
      String[] quoteParts = quoteLine.split(",");
      int i = 0;

      if (quoteParts.length == 7) {
        Symbol symbol = null;

        try {
          symbol = Symbol.find(quoteParts[i++]);
        } catch (SymbolFormatException e) {
          throw new QuoteFormatException(e.getMessage());
        }

        TradingDate date = null;

        try {
          date = new TradingDate(quoteParts[i++], TradingDate.BRITISH);
        } catch (TradingDateFormatException e) {
          throw new QuoteFormatException(e.getMessage());
        }

        try {
          double day_open = Double.parseDouble(quoteParts[i++]);
          double day_high = Double.parseDouble(quoteParts[i++]);
          double day_low = Double.parseDouble(quoteParts[i++]);
          double day_close = Double.parseDouble(quoteParts[i++]);

          // Convert volume from 1/100th volume to real volume
          long day_volume = Long.parseLong(quoteParts[i++]) * 100;
          quote = new EODQuote(symbol, date, day_volume, day_low, day_high, day_open, day_close);
        } catch (NumberFormatException e) {
          throw new QuoteFormatException(
              Locale.getString("ERROR_PARSING_NUMBER", quoteParts[i - 1]));
        }
      } else throw new QuoteFormatException(Locale.getString("WRONG_FIELD_COUNT"));
    }
    return quote;
  }
  private void setGraphic(Dimension preferredSize) {

    GridBagLayout gridbag = new GridBagLayout();

    TitledBorder titledBorder =
        new TitledBorder(Locale.getString("GP_PAGE_INITIAL_POPULATION_LONG"));
    this.setBorder(titledBorder);
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    this.setPreferredSize(preferredSize);

    JPanel panelUp = new JPanel();
    JPanel panelDown = new JPanel();
    panelUp.setLayout(new BoxLayout(panelUp, BoxLayout.Y_AXIS));
    panelDown.setLayout(new BoxLayout(panelDown, BoxLayout.Y_AXIS));
    TitledBorder titledBorderSectionUp = new TitledBorder(Locale.getString("RULES_PAGE_TITLE"));
    TitledBorder titledBorderSectionDown = new TitledBorder(Locale.getString("GP_PAGE_PERCENTAGE"));
    panelUp.setBorder(titledBorderSectionUp);
    panelDown.setBorder(titledBorderSectionDown);

    // GPPageInitialPopulationModule is already declared as global variable
    GPPageInitialPopulationModule.setLayout(
        new BoxLayout(GPPageInitialPopulationModule, BoxLayout.Y_AXIS));

    JScrollPane upDownScrollPane = new JScrollPane(GPPageInitialPopulationModule);
    upDownScrollPane.setLayout(new ScrollPaneLayout());

    JButton addButton = new JButton(Locale.getString("ADD"));
    addButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            addRules();
          }
        });

    JButton editButton = new JButton(Locale.getString("EDIT"));
    editButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            editRules();
          }
        });

    JButton deleteButton = new JButton(Locale.getString("DELETE"));
    deleteButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            deleteRules();
          }
        });

    JPanel rulesButtons = new JPanel();
    rulesButtons.add(addButton);
    rulesButtons.add(editButton);
    rulesButtons.add(deleteButton);

    JButton fitAllButton = new JButton(Locale.getString("FIT"));
    fitAllButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            // Fit the percent values for each buy/sell rule
            GPPageInitialPopulationModule.fitAll();
            // Fit the percent values for random or not
            fitAll();
          }
        });

    JButton defaultButton = new JButton(Locale.getString("DEFAULT"));
    defaultButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            // Set the values to default, so the GP will work with random generation only
            setDefaultValues();
          }
        });

    JPanel innerPanel = new JPanel();
    GridBagConstraints c = new GridBagConstraints();
    innerPanel.setLayout(gridbag);

    // Add Generation number to fix the range of mutation
    // the beginning formulas will be modified n times with mutation mechanism
    // according to the number in the combo box (user defined).
    c.weightx = 1.0;
    c.ipadx = 5;
    c.anchor = GridBagConstraints.WEST;

    generateRandomPopTextRow =
        GridBagHelper.addTextRow(
            innerPanel,
            Locale.getString("GP_PAGE_GENERATE_RANDOM_PERC_TEXT_ROW"),
            "",
            gridbag,
            c,
            12);
    generateInitPopTextRow =
        GridBagHelper.addTextRow(
            innerPanel, Locale.getString("GP_PAGE_GENERATE_PERC_TEXT_ROW"), "", gridbag, c, 12);
    numberMutationTextRow =
        GridBagHelper.addTextRow(
            innerPanel,
            Locale.getString("GP_PAGE_GENERATE_NUMBER_MUTATION_TEXT_ROW"),
            "",
            gridbag,
            c,
            6);

    panelUp.add(upDownScrollPane);
    rulesButtons.setAlignmentX(CENTER_ALIGNMENT);
    panelUp.add(rulesButtons);

    panelDown.add(innerPanel);
    fitAllButton.setAlignmentX(CENTER_ALIGNMENT);
    defaultButton.setAlignmentX(CENTER_ALIGNMENT);
    panelDown.add(fitAllButton);
    panelDown.add(defaultButton);

    this.add(panelUp);
    this.add(panelDown);

    // Put the default values so that random initial population is the default behaviour
    this.setDefaultValues();
  }
 public String getTitle() {
   return Locale.getString("GP_PAGE_INITIAL_POPULATION_SHORT");
 }
 /**
  * Return the name of the filter.
  *
  * @return the name of the filter.
  */
 public String getName() {
   return new String("MetaStock (" + Locale.getString("VOLUME") + "/100)");
 }