📄 strategyperformancechart.java
字号:
package com.jsystemtrader.chart;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import com.jsystemtrader.indicator.*;
import com.jsystemtrader.performance.*;
import com.jsystemtrader.platform.*;
import com.jsystemtrader.util.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.time.*;
import org.jfree.data.xy.*;
import org.jfree.ui.*;
/**
* Multi-indicator chart where indicators can be grouped together and displayed
* on subplots, one group of indicators per plot.
*/
public class StrategyPerformanceChart {
private static final int PRICE_PLOT_WEIGHT = 5;
private static final int ANNOTATION_RADIUS = 6;
private static final int CANDLE_WIDTH = 4;
private static final Font ANNOTATION_FONT = new Font("SansSerif", Font.BOLD, 12);
private JFreeChart chart;
private CombinedDomainXYPlot combinedPlot;
private ChartPanel chartPanel;
private NumberAxis valueAxis;
private DateAxis dateAxis;
private final Strategy strategy;
private final Map<Integer, TimeSeriesCollection> tsCollections;
private FastXYPlot pricePlot, profitAndLossPlot;
private CandlestickRenderer candleRenderer;
private MultiColoredBarRenderer mcbRenderer;
private JComboBox chartTypeCombo, timeLineCombo, profitAndLossCombo, tradesCombo;
private ArrayList<CircledTextAnnotation> annotations = new ArrayList<CircledTextAnnotation> ();
public StrategyPerformanceChart(Strategy strategy) throws JSystemTraderException {
this.strategy = strategy;
tsCollections = new HashMap<Integer, TimeSeriesCollection> ();
chart = createChart();
}
private void setRenderer() {
int chartType = chartTypeCombo.getSelectedIndex();
switch (chartType) {
case 0:
pricePlot.setRenderer(mcbRenderer);
break;
case 1:
pricePlot.setRenderer(candleRenderer);
break;
}
}
private void setTimeline() {
int timeLineType = timeLineCombo.getSelectedIndex();
int barSizeInMinutes = strategy.getBarSizeInSecs() / 60;
QuoteHistory qh = strategy.getQuoteHistory();
MarketTimeLine mtl = new MarketTimeLine(barSizeInMinutes, qh.getFirstPriceBar().getDate(),
qh.getLastPriceBar().getDate());
SegmentedTimeline segmentedTimeline = null;
switch (timeLineType) {
case 0:
segmentedTimeline = mtl.getAllHoursTimeline();
break;
case 1:
segmentedTimeline = mtl.getNormalHoursTimeline();
break;
}
dateAxis.setTimeline(segmentedTimeline);
}
public JFrame getChartFrame(JFrame parent) {
final JFrame chartFrame = new JFrame("Strategy Performance Chart - " + strategy);
chartFrame.setIconImage(parent.getIconImage());
chartPanel = new ChartPanel(chart, true);
chartPanel.setRangeZoomable(false);
chartPanel.setPreferredSize(new Dimension(640, 480));
XYPlot plot = (XYPlot) combinedPlot.getSubplots().get(0);
Container contentPane = chartFrame.getContentPane();
JPanel chartOptionsPanel = new JPanel(new SpringLayout());
Border etchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
TitledBorder border = BorderFactory.createTitledBorder(etchedBorder);
border.setTitle("Chart Options");
chartOptionsPanel.setBorder(border);
Dimension dimension = new Dimension(110, 20);
JLabel chartTypeLabel = new JLabel("Chart Type:", JLabel.TRAILING);
chartTypeCombo = new JComboBox(new String[] {"OHLC Bar", "Candlestick"});
chartTypeCombo.setPreferredSize(dimension);
chartTypeCombo.setMaximumSize(dimension);
chartTypeLabel.setLabelFor(chartTypeCombo);
JLabel timeLineLabel = new JLabel("Timeline:", JLabel.TRAILING);
// "Regular hours" option is disabled for now until zoom problems are fixed.
timeLineCombo = new JComboBox(new String[] {"All Hours"});
timeLineCombo.setPreferredSize(dimension);
timeLineCombo.setMaximumSize(dimension);
timeLineLabel.setLabelFor(timeLineCombo);
JLabel profitAndLossLabel = new JLabel("P&L:", JLabel.TRAILING);
profitAndLossCombo = new JComboBox(new String[] {"Show", "Hide"});
profitAndLossCombo.setPreferredSize(dimension);
profitAndLossCombo.setMaximumSize(dimension);
profitAndLossLabel.setLabelFor(profitAndLossCombo);
JLabel tradesLabel = new JLabel("Trades:", JLabel.TRAILING);
tradesCombo = new JComboBox(new String[] {"Show", "Hide"});
tradesCombo.setPreferredSize(dimension);
tradesCombo.setMaximumSize(dimension);
tradesLabel.setLabelFor(tradesCombo);
setRenderer();
chartTypeCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setRenderer();
}
});
timeLineCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setTimeline();
}
});
profitAndLossCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean show = (profitAndLossCombo.getSelectedIndex() == 0);
if (show) {
combinedPlot.add(profitAndLossPlot, 1);
} else {
combinedPlot.remove(profitAndLossPlot);
}
}
});
tradesCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean show = (tradesCombo.getSelectedIndex() == 0);
for (CircledTextAnnotation annotation : annotations) {
if (show) {
pricePlot.addAnnotation(annotation);
} else {
pricePlot.removeAnnotation(annotation);
}
}
}
});
chartOptionsPanel.add(chartTypeLabel);
chartOptionsPanel.add(chartTypeCombo);
chartOptionsPanel.add(timeLineLabel);
chartOptionsPanel.add(timeLineCombo);
chartOptionsPanel.add(profitAndLossLabel);
chartOptionsPanel.add(profitAndLossCombo);
chartOptionsPanel.add(tradesLabel);
chartOptionsPanel.add(tradesCombo);
SpringUtilities.makeCompactGrid(chartOptionsPanel, 2, 4, 10, 5, 15, 5); //rows, cols, initX, initY, xPad, yPad
contentPane.add(chartOptionsPanel, BorderLayout.NORTH);
JPanel scrollBarPanel = new JPanel(new BorderLayout());
DateScrollBar dateScrollBar = new DateScrollBar(plot);
scrollBarPanel.add(dateScrollBar, BorderLayout.SOUTH);
contentPane.add(chartPanel, BorderLayout.CENTER);
contentPane.add(scrollBarPanel, BorderLayout.PAGE_END);
chartFrame.setContentPane(contentPane);
chartFrame.pack();
RefineryUtilities.centerFrameOnScreen(chartFrame);
chartFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
chartFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
});
return chartFrame;
}
private TimeSeries createIndicatorSeries(IndicatorHistory indHistory) {
TimeSeries ts = new TimeSeries(indHistory.getName(), Minute.class);
ts.setRangeDescription(indHistory.getName());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -