📄 strategyperformancechart.java
字号:
synchronized (indHistory) {
for (Indicator indicator : indHistory.getHistory()) {
try {
ts.add(new Minute(indicator.getDate()), indicator.getValue(), false);
} catch (Exception e) {
Account.getLogger().write(e);
}
}
}
ts.fireSeriesChanged();
return ts;
}
private TimeSeries createProfitAndLossSeries(ProfitAndLossHistory profitAndLossHistory) {
TimeSeries ts = new TimeSeries("P&L", Minute.class);
synchronized (profitAndLossHistory) {
for (ProfitAndLoss profitAndLoss : profitAndLossHistory.getHistory()) {
Date date = new Date(profitAndLoss.getDate());
Minute minute = new Minute(date);
ts.setRangeDescription("P&L");
try {
ts.add(minute, profitAndLoss.getValue());
} catch (Exception e) {
Account.getLogger().write(e);
}
}
}
return ts;
}
private OHLCDataset createHighLowDataset() {
QuoteHistory qh = strategy.getQuoteHistory();
int qhSize = qh.size();
Date[] dates = new Date[qhSize];
double[] highs = new double[qhSize];
double[] lows = new double[qhSize];
double[] opens = new double[qhSize];
double[] closes = new double[qhSize];
double[] volumes = new double[qhSize];
for (int bar = 0; bar < qhSize; bar++) {
Calendar cal = Calendar.getInstance();
PriceBar priceBar = qh.getPriceBar(bar);
cal.setTimeInMillis(priceBar.getDate());
dates[bar] = cal.getTime();
highs[bar] = priceBar.getHigh();
lows[bar] = priceBar.getLow();
opens[bar] = priceBar.getOpen();
closes[bar] = priceBar.getClose();
volumes[bar] = priceBar.getVolume();
}
OHLCDataset dataset = new DefaultHighLowDataset(strategy.getTicker(), dates, highs, lows, opens, closes,
volumes);
return dataset;
}
private JFreeChart createChart() {
int barSizeInSeconds = strategy.getBarSizeInSecs();
// create OHLC bar renderer
mcbRenderer = new MultiColoredBarRenderer();
//mcbRenderer.setSeriesPaint(0, Color.GREEN);
mcbRenderer.setSeriesPaint(0, Color.WHITE);
mcbRenderer.setStroke(new BasicStroke(4));
// create candlestick renderer
candleRenderer = new CandlestickRenderer(CANDLE_WIDTH, false, new HighLowItemLabelGenerator());
candleRenderer.setDrawVolume(false);
candleRenderer.setAutoWidthMethod(candleRenderer.WIDTHMETHOD_AVERAGE);
candleRenderer.setAutoWidthFactor(8.0);
candleRenderer.setAutoWidthGap(1.0);
candleRenderer.setUpPaint(Color.GREEN);
candleRenderer.setDownPaint(Color.RED);
candleRenderer.setSeriesPaint(0, Color.WHITE);
candleRenderer.setStroke(new BasicStroke(1.0f));
candleRenderer.setSeriesPaint(1, Color.GRAY);
dateAxis = new DateAxis();
int barSizeInMinutes = strategy.getBarSizeInSecs() / 60;
QuoteHistory qh = strategy.getQuoteHistory();
MarketTimeLine mtl = new MarketTimeLine(barSizeInMinutes, qh.getFirstPriceBar().getDate(),
qh.getLastPriceBar().getDate());
SegmentedTimeline segmentedTimeline = mtl.getAllHoursTimeline();
dateAxis.setTimeline(segmentedTimeline);
// create price plot
OHLCDataset highLowDataset = createHighLowDataset();
valueAxis = new NumberAxis("Price");
valueAxis.setAutoRangeIncludesZero(false);
pricePlot = new FastXYPlot(highLowDataset, dateAxis, valueAxis, null);
pricePlot.setBackgroundPaint(Color.BLACK);
pricePlot.setDomainCrosshairVisible(true);
pricePlot.setDomainCrosshairLockedOnData(false);
pricePlot.setRangeCrosshairVisible(true);
pricePlot.setRangeCrosshairLockedOnData(false);
pricePlot.setRangeCrosshairPaint(Color.WHITE);
pricePlot.setDomainCrosshairPaint(Color.WHITE);
// parent plot
combinedPlot = new CombinedDomainXYPlot(dateAxis);
combinedPlot.setGap(10.0);
combinedPlot.setOrientation(PlotOrientation.VERTICAL);
combinedPlot.add(pricePlot, PRICE_PLOT_WEIGHT);
// Put all indicators into groups, so that each group is
// displayed on its own subplot
for (IndicatorHistory indHist : strategy.getIndicators()) {
TimeSeries ts = createIndicatorSeries(indHist);
int subChart = indHist.getSubChartNumber();
TimeSeriesCollection tsCollection = tsCollections.get(subChart);
if (tsCollection == null) {
tsCollection = new TimeSeriesCollection();
tsCollections.put(subChart, tsCollection);
}
tsCollection.addSeries(ts);
}
// Plot executions
for (OrderStatus execution : strategy.getExecutions()) {
Date date = new Date(execution.getDate());
double aveFill = execution.getAvgFillPrice();
int decision = execution.getDecision();
String annotationText = "?";
Color bkColor = null;
switch (decision) {
case Strategy.DECISION_LONG:
annotationText = "L";
bkColor = Color.GREEN;
break;
case Strategy.DECISION_SHORT:
annotationText = "S";
bkColor = Color.RED;
break;
case Strategy.DECISION_FLAT:
annotationText = "F";
bkColor = Color.YELLOW;
break;
}
CircledTextAnnotation circledText = new CircledTextAnnotation(annotationText, date.getTime(), aveFill,
ANNOTATION_RADIUS);
circledText.setFont(ANNOTATION_FONT);
circledText.setBkColor(bkColor);
circledText.setPaint(Color.BLACK);
circledText.setTextAnchor(TextAnchor.CENTER);
pricePlot.addAnnotation(circledText);
annotations.add(circledText);
}
// Now that the indicators are grouped, create subplots
for (Map.Entry mapEntry : tsCollections.entrySet()) {
int subChart = (Integer) mapEntry.getKey();
TimeSeriesCollection tsCollection = (TimeSeriesCollection) mapEntry.getValue();
StandardXYItemRenderer renderer = new StandardXYItemRenderer();
renderer.setStroke(new BasicStroke(2));
if (subChart == 0) {
pricePlot.setDataset(1, tsCollection);
pricePlot.setRenderer(1, renderer);
} else {
NumberAxis valueAxis = new NumberAxis();
valueAxis.setAutoRangeIncludesZero(false);
FastXYPlot plot = new FastXYPlot(tsCollection, dateAxis, valueAxis, renderer);
plot.setBackgroundPaint(Color.BLACK);
int weight = 1;
combinedPlot.add(plot, weight);
}
}
// Plot P&L
NumberAxis valueAxis = new NumberAxis();
valueAxis.setAutoRangeIncludesZero(false);
TimeSeries ts = createProfitAndLossSeries(strategy.getPositionManager().getProfitAndLossHistory());
TimeSeriesCollection tsCollection = new TimeSeriesCollection();
tsCollection.addSeries(ts);
StandardXYItemRenderer renderer = new StandardXYItemRenderer();
renderer.setSeriesPaint(0, Color.WHITE);
renderer.setStroke(new BasicStroke(2));
profitAndLossPlot = new FastXYPlot(tsCollection, dateAxis, valueAxis, renderer);
profitAndLossPlot.setBackgroundPaint(Color.BLACK);
combinedPlot.add(profitAndLossPlot, 1);
combinedPlot.setDomainAxis(dateAxis);
// Finally, create the chart
chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, combinedPlot, true);
chart.getLegend().setPosition(RectangleEdge.TOP);
chart.getLegend().setBackgroundPaint(Color.LIGHT_GRAY);
return chart;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -