📄 backdatadialog.java
字号:
package com.jsystemtrader.backdata;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import com.ib.client.*;
import com.jsystemtrader.client.*;
import com.jsystemtrader.platform.*;
import com.jsystemtrader.util.*;
/**
* Dialog to specify options for back data download.
*/
public class BackDataDialog extends JDialog {
/** Minimum frame size. Width / Height = Golden Mean */
private static final Dimension MIN_SIZE = new Dimension(450, 278);
private static final String NOT_APPLICABLE = "Not Applicable";
private JButton cancelButton, downloadButton, selectFileButton;
private JTextField tickerText, fileNameText;
private JComboBox securityTypeCombo, expirationMonthCombo, expirationYearCombo, exchangeCombo, currencyCombo,
barSizeCombo, rthOnlyCombo;
private JLabel expirationMonthLabel, expirationYearLabel, exchangeLabel;
private JProgressBar progressBar;
private BackDataDownloader downloader;
private final HTMLLog eventLog;
public BackDataDialog(JFrame parent) throws JSystemTraderException, FileNotFoundException, IOException {
super(parent);
eventLog = Account.getLogger();
jbInit();
pack();
assignListeners();
setLocationRelativeTo(null);
setVisible(true);
}
public void setProgress(int value, String text) {
progressBar.setValue(value);
text = text + " " + value + "%";
progressBar.setString(text);
}
public void setProgress(String text) {
progressBar.setString(text);
}
public void signalCompleted() {
progressBar.setVisible(false);
downloadButton.setEnabled(true);
downloadButton.requestFocus();
getRootPane().setDefaultButton(downloadButton);
}
private void assignListeners() {
downloadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
validateOptions();
String ticker = tickerText.getText();
String expiration = null;
if (expirationYearCombo.isEnabled() && expirationMonthCombo.isEnabled()) {
int expirationMonth = expirationMonthCombo.getSelectedIndex();
String expirationMonthStr = String.valueOf(expirationMonth);
if (expirationMonth < 10) {
expirationMonthStr = "0" + expirationMonthStr;
}
expiration = expirationYearCombo.getSelectedItem() + expirationMonthStr;
}
String fileName = fileNameText.getText();
String securityType = (String) securityTypeCombo.getSelectedItem();
String exchange = (String) exchangeCombo.getSelectedItem();
String currency = (String) currencyCombo.getSelectedItem();
String barSize = (String) barSizeCombo.getSelectedItem();
String rthText = (String) rthOnlyCombo.getSelectedItem();
boolean rthOnly = rthText.equalsIgnoreCase("yes") ? true : false;
Contract contract = ContractFactory.makeContract(ticker, securityType, exchange, expiration,
currency);
downloader = new BackDataDownloader(BackDataDialog.this, contract, barSize, rthOnly, fileName);
downloadButton.setEnabled(false);
progressBar.setValue(0);
progressBar.setVisible(true);
getRootPane().setDefaultButton(cancelButton);
downloader.start();
} catch (Exception ex) {
eventLog.write(ex);
MessageDialog.showError(BackDataDialog.this, ex.getMessage());
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancel();
}
});
selectFileButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser(JSystemTrader.getAppPath());
fileChooser.setDialogTitle("Save Back Data As");
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
fileNameText.setText(file.getAbsolutePath());
}
}
});
BackDataDialog.this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
cancel();
}
});
securityTypeCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String securityType = (String) securityTypeCombo.getSelectedItem();
boolean isFuture = securityType.equalsIgnoreCase("FUT");
expirationMonthCombo.setEnabled(isFuture);
expirationYearCombo.setEnabled(isFuture);
expirationMonthLabel.setEnabled(isFuture);
expirationYearLabel.setEnabled(isFuture);
if (isFuture) {
String yearAndMonth = MostLiquidContract.getMostLiquid();
int mostLiquidMonth = Integer.valueOf(yearAndMonth.substring(4, 6));
expirationMonthCombo.setSelectedIndex(mostLiquidMonth);
expirationYearCombo.setSelectedItem(yearAndMonth.substring(0, 4));
}
if (!isFuture) {
expirationMonthCombo.setSelectedIndex(0);
expirationYearCombo.setSelectedIndex(0);
}
if (securityType.equalsIgnoreCase("CASH")) {
exchangeCombo.setSelectedItem("IDEALPRO");
exchangeCombo.setEnabled(false);
exchangeLabel.setEnabled(false);
} else {
exchangeCombo.setSelectedItem("SMART");
exchangeCombo.setEnabled(true);
exchangeLabel.setEnabled(true);
}
}
});
}
private void jbInit() throws JSystemTraderException, FileNotFoundException, IOException {
setModal(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Back Data");
getContentPane().setLayout(new BorderLayout());
JPanel optionsPanel = new JPanel(new SpringLayout());
Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder border = BorderFactory.createTitledBorder(etchedBorder);
border.setTitle("Back Data Options");
optionsPanel.setBorder(border);
Dimension dimension = new Dimension(120, 23);
JLabel tickerLabel = new JLabel("Ticker:", JLabel.TRAILING);
tickerText = new JTextField("MSFT");
tickerText.setPreferredSize(dimension);
tickerText.setMaximumSize(dimension);
tickerLabel.setLabelFor(tickerText);
JLabel securityTypeLabel = new JLabel("Security Type:", JLabel.TRAILING);
securityTypeCombo = new JComboBox(new String[] {"STK", "FUT", "CASH"});
securityTypeCombo.setPreferredSize(dimension);
securityTypeCombo.setMaximumSize(dimension);
securityTypeLabel.setLabelFor(securityTypeCombo);
exchangeLabel = new JLabel("Exchange:", JLabel.TRAILING);
exchangeCombo = new JComboBox(new String[] {"SMART", "GLOBEX", "ECBOT", "NYMEX", "LIFFE", "IDEALPRO"});
exchangeCombo.setPreferredSize(dimension);
exchangeCombo.setMaximumSize(dimension);
exchangeLabel.setLabelFor(exchangeCombo);
expirationYearLabel = new JLabel("Expiry Year:", JLabel.TRAILING);
expirationYearLabel.setEnabled(false);
expirationYearCombo = new JComboBox(new String[] {NOT_APPLICABLE, "2004", "2005", "2006", "2007", "2008"});
expirationYearCombo.setEnabled(false);
expirationYearCombo.setPreferredSize(dimension);
expirationYearCombo.setMaximumSize(dimension);
expirationYearLabel.setLabelFor(expirationYearCombo);
expirationMonthLabel = new JLabel("Expiry Month:", JLabel.TRAILING);
expirationMonthLabel.setEnabled(false);
expirationMonthCombo = new JComboBox(new String[] {NOT_APPLICABLE, "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December"});
expirationMonthCombo.setEnabled(false);
expirationMonthCombo.setPreferredSize(dimension);
expirationMonthCombo.setMaximumSize(dimension);
expirationMonthLabel.setLabelFor(expirationMonthCombo);
JLabel currencyLabel = new JLabel("Currency:", JLabel.TRAILING);
currencyCombo = new JComboBox(new String[] {"USD", "EUR", "GBP", "CHF", "JPY", "AUD"});
currencyCombo.setPreferredSize(dimension);
currencyCombo.setMaximumSize(dimension);
currencyLabel.setLabelFor(currencyCombo);
JLabel barSizeLabel = new JLabel("Bar Size:", JLabel.TRAILING);
barSizeCombo = new JComboBox(new String[] {"1 min", "2 mins", "5 mins", "15 mins", "30 mins"});
barSizeCombo.setPreferredSize(dimension);
barSizeCombo.setMaximumSize(dimension);
barSizeLabel.setLabelFor(barSizeCombo);
JLabel rthOnlyLabel = new JLabel("RTH Only:", JLabel.TRAILING);
rthOnlyCombo = new JComboBox(new String[] {"Yes", "No"});
rthOnlyCombo.setPreferredSize(dimension);
rthOnlyCombo.setMaximumSize(dimension);
rthOnlyLabel.setLabelFor(rthOnlyCombo);
optionsPanel.add(tickerLabel);
optionsPanel.add(tickerText);
optionsPanel.add(securityTypeLabel);
optionsPanel.add(securityTypeCombo);
optionsPanel.add(exchangeLabel);
optionsPanel.add(exchangeCombo);
optionsPanel.add(currencyLabel);
optionsPanel.add(currencyCombo);
optionsPanel.add(expirationYearLabel);
optionsPanel.add(expirationYearCombo);
optionsPanel.add(expirationMonthLabel);
optionsPanel.add(expirationMonthCombo);
optionsPanel.add(barSizeLabel);
optionsPanel.add(barSizeCombo);
optionsPanel.add(rthOnlyLabel);
optionsPanel.add(rthOnlyCombo);
// rows, cols, initX, initY, xPad, yPad
SpringUtilities.makeCompactGrid(optionsPanel, 4, 4, 12, 12, 11, 5);
JLabel fileNameLabel = new JLabel("Save as:", JLabel.TRAILING);
fileNameText = new JTextField();
fileNameText.setPreferredSize(new Dimension(260, 23));
fileNameLabel.setLabelFor(fileNameText);
selectFileButton = new JButton("Browse...");
//selectFileButton.setPreferredSize(new Dimension(23, 23));
FlowLayout flowLayout = new FlowLayout();
flowLayout.setHgap(12);
flowLayout.setVgap(12);
flowLayout.setAlignment(FlowLayout.LEFT);
JPanel filePanel = new JPanel(flowLayout);
filePanel.add(fileNameLabel);
filePanel.add(fileNameText);
filePanel.add(selectFileButton);
JPanel controlPanel = new JPanel();
cancelButton = new JButton("Cancel");
cancelButton.setMnemonic('C');
downloadButton = new JButton("Download");
downloadButton.setMnemonic('D');
progressBar = new JProgressBar();
progressBar.setValue(0);
progressBar.setPreferredSize(new Dimension(250, 18));
progressBar.setVisible(false);
progressBar.setStringPainted(true);
controlPanel.add(downloadButton);
controlPanel.add(cancelButton);
controlPanel.add(progressBar);
getContentPane().add(optionsPanel, BorderLayout.NORTH);
getContentPane().add(filePanel, BorderLayout.CENTER);
getContentPane().add(controlPanel, BorderLayout.SOUTH);
getRootPane().setDefaultButton(downloadButton);
getContentPane().setPreferredSize(MIN_SIZE);
getContentPane().setMinimumSize(getContentPane().getPreferredSize());
}
private void validateOptions() throws JSystemTraderException {
String ticker = tickerText.getText();
if (ticker.length() == 0) {
tickerText.requestFocus();
throw new JSystemTraderException("Ticker must be specified.");
}
String expirationYear = (String) expirationYearCombo.getSelectedItem();
if (expirationYearCombo.isEnabled() && expirationYear.equals(NOT_APPLICABLE)) {
expirationYearCombo.requestFocus();
throw new JSystemTraderException("Expiration year must be specified for security type \"FUT\".");
}
String expirationMonth = (String) expirationMonthCombo.getSelectedItem();
if (expirationMonthCombo.isEnabled() && expirationMonth.equals(NOT_APPLICABLE)) {
expirationMonthCombo.requestFocus();
throw new JSystemTraderException("Expiration month must be specified for security type \"FUT\".");
}
String fileName = fileNameText.getText();
if (fileName.length() == 0) {
fileNameText.requestFocus();
throw new JSystemTraderException("File name must be specified.");
}
}
private void cancel() {
if (downloader != null) {
downloader.cancel();
}
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -