-
Notifications
You must be signed in to change notification settings - Fork 2
/
OhlcPlotBuilder.java
355 lines (281 loc) · 10.8 KB
/
OhlcPlotBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* jfreechart-builder: a builder pattern module for working with the jfreechart library
*
* (C) Copyright 2020, by Matt E. and project contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.jfcbuilder.builders;
import java.awt.Color;
import java.awt.Paint;
import java.text.NumberFormat;
import java.util.List;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.data.xy.XYDataset;
import com.jfcbuilder.types.Orientation;
import com.jfcbuilder.types.ZeroBasedIndexRange;
/**
* Builder for producing stock market Open High Low Close (OHLC) plots using configured builder
* properties, series, and datasets.
*/
public class OhlcPlotBuilder implements IXYTimeSeriesPlotBuilder<OhlcPlotBuilder> {
private static final String DEFAULT_Y_AXIS_NAME = "Price";
protected XYTimeSeriesPlotBuilderElements elements;
private OhlcSeriesBuilder ohlcSeriesBuilder;
/**
* Hidden constructor.
*/
protected OhlcPlotBuilder() {
elements = new XYTimeSeriesPlotBuilderElements();
elements.yAxisTickFormat().setMinimumFractionDigits(2);
ohlcSeriesBuilder = null;
yAxisName(DEFAULT_Y_AXIS_NAME);
}
/**
* Factory method for obtaining new instances of this class.
*
* @return New instance of this class
*/
public static OhlcPlotBuilder get() {
return new OhlcPlotBuilder();
}
@Override
public OhlcPlotBuilder indexRange(ZeroBasedIndexRange indexRange) {
elements.indexRange(indexRange);
return this;
}
@Override
public OhlcPlotBuilder xAxis(ValueAxis xAxis) {
elements.xAxis(xAxis);
return this;
}
@Override
public OhlcPlotBuilder timeData(long[] timeData) {
elements.timeData(timeData);
return this;
}
@Override
public OhlcPlotBuilder showTimeGaps(boolean showTimeGaps) {
elements.showTimeGaps(showTimeGaps);
return this;
}
@Override
public OhlcPlotBuilder series(IXYTimeSeriesBuilder<?> series) {
elements.series(series);
return this;
}
/**
* Same as interface {@code IXYTimeSeriesPlotBuilder<?>.series(IXYTimeSeriesDatasetBuilder<?>)}
* but also checks that only one {@code OhlcSeriesBuilder} is ever added because only one is
* allowed per plot.
*
* @throws IllegalArgumentException If an OhlcSeriesBuilder was already added.
*/
@Override
public OhlcPlotBuilder series(IXYTimeSeriesDatasetBuilder<?> dataset) {
if (dataset instanceof OhlcSeriesBuilder) {
if (ohlcSeriesBuilder != null) {
throw new IllegalArgumentException(
"More than one " + OhlcSeriesBuilder.class.getSimpleName()
+ " being added but only one can be plotted.");
}
ohlcSeriesBuilder = (OhlcSeriesBuilder) dataset;
} else {
elements.dataset(dataset);
}
return this;
}
@Override
public OhlcPlotBuilder line(LineBuilder line) {
elements.line(line);
return this;
}
@Override
public OhlcPlotBuilder annotation(IXYAnnotationBuilder<?> annotation) {
elements.annotation(annotation);
return this;
}
@Override
public OhlcPlotBuilder plotWeight(int weight) {
elements.plotWeight(weight);
return this;
}
@Override
public int plotWeight() {
return elements.plotWeight();
}
@Override
public OhlcPlotBuilder yAxisName(String name) {
elements.yAxisName(name);
return this;
}
@Override
public OhlcPlotBuilder yAxisRange(double lower, double upper) throws IllegalArgumentException {
elements.yAxisRange(lower, upper);
return this;
}
@Override
public OhlcPlotBuilder yAxisTickSize(double size) {
elements.yAxisTickSize(size);
return this;
}
@Override
public OhlcPlotBuilder yTickFormat(NumberFormat format) {
elements.yTickFormat(format);
return this;
}
@Override
public OhlcPlotBuilder backgroundColor(Paint color) {
elements.backgroundColor(color);
return this;
}
@Override
public OhlcPlotBuilder axisFontColor(Paint color) {
elements.axisFontColor(color);
return this;
}
@Override
public OhlcPlotBuilder axisColor(Paint color) {
elements.axisColor(color);
return this;
}
@Override
public OhlcPlotBuilder gridLines() {
elements.gridLines();
return this;
}
@Override
public OhlcPlotBuilder noGridLines() {
elements.noGridLines();
return this;
}
private void checkBuildPreconditions() throws IllegalStateException {
elements.checkBuildPreconditions();
}
/**
* Builds the DOHLC plot. The DOHLC series is shown behind all other series that were added.
*
* @return New instance of an plot corresponding to all configured data sets and settings
* @throws IllegalStateException If an OhlcSeriesBuilder, if time axis was not set, or if time
* data was not set.
*/
@Override
public XYPlot build() throws IllegalStateException {
checkBuildPreconditions();
final ValueAxis xAxis = elements.xAxis();
xAxis.setAxisLinePaint(elements.axisColor());
xAxis.setTickLabelPaint(elements.axisFontColor());
final long[] timeData = elements.timeData();
XYDataset ohlc = buildOhlcDataset();
if (!elements.showTimeGaps()) {
if (ohlc instanceof OHLCSeriesCollection) {
ohlc = new NumberMappedOHLCSeriesCollection((OHLCSeriesCollection) ohlc);
}
}
final NumberAxis yAxis = BuilderUtils.createYAxis(elements);
final XYPlot plot = BuilderUtils.createPlot(xAxis, yAxis, ohlc, getCandleRenderer(), elements);
StringBuilder axisSubName = new StringBuilder();
final List<IXYTimeSeriesBuilder<?>> seriesBuilders = elements.unmodifiableSeries();
if (!seriesBuilders.isEmpty()) {
StandardXYItemRenderer renderer = new StandardXYItemRenderer();
TimeSeriesCollection collection = elements.showTimeGaps() ? new TimeSeriesCollection()
: new NumberMappedTimeSeriesCollection();
for (IXYTimeSeriesBuilder<?> builder : seriesBuilders) {
builder.indexRange(elements.indexRange()).timeData(timeData);
TimeSeries series = builder.build();
if (!series.isEmpty()) {
renderer.setSeriesPaint(collection.getSeriesCount(), builder.color());
renderer.setSeriesStroke(collection.getSeriesCount(), builder.style());
collection.addSeries(series);
// JFreeChart series key is the name given to the series.
if (series.getKey() != null && !series.getKey().toString().isEmpty()) {
axisSubName.append(' ').append(series.getKey().toString());
}
}
}
for (LineBuilder builder : elements.unmodifiableLines()) {
ValueMarker line = builder.build();
if (builder.orientation() == Orientation.HORIZONTAL) {
plot.addRangeMarker(line);
} else {
plot.addDomainMarker(line);
}
}
final List<IXYAnnotationBuilder<?>> annotationBuilders = elements.unmodifiableAnnotations();
for (IXYAnnotationBuilder<?> builder : annotationBuilders) {
if (!elements.showTimeGaps()) {
// We need to map the annotation time series x-axis value to the number axis.
BuilderUtils.mapAnnotationXToTimeIndex(timeData, builder,
elements.indexRange().getStartIndex());
}
// Annotations don't have ability to get their max/min y-value to adjust y-axis range :(
plot.addAnnotation(builder.build());
}
yAxis.setLabel(elements.yAxisName() + axisSubName.toString());
final int seriesIndex = plot.getSeriesCount();
// We don't create a new axis but map datasets to the time axis (i.e. domain axis used for the
// main OHLCSeriesCollection data). Now create the dataset to axis mapping prior to even
// adding the dataset so that when the dataset is added the min/max value range of the axis is
// properly auto-calculated and set.
plot.mapDatasetToDomainAxis(seriesIndex, 0);
// Now also set the renderer that will be used because it will be used to determine the axis
// min/max value range when this is auto-calculated. FIXME: Candlestick renderer offsets
// center of candle from x-axis start pixel. When rendering general xy lines, those line
// points are drawn at the x-axis start pixel. This causes indicator lines to be misaligned by
// a few pixels from the OHLC bars and may provide false signals to those looking at charts.
plot.setRenderer(seriesIndex, renderer, false);
// We're now ready to add the dataset
plot.setDataset(seriesIndex, collection);
}
return plot;
}
/**
* Helper method to get the OHLC XYDataset to be plotted.
*
* @return The OHLC XYDataset
* @throws IllegalStateException If ohlcSeriesBuilder is null, if source array sizes don't match,
* or if an index range is configured and its indexes are out of bounds
*/
protected XYDataset buildOhlcDataset() throws IllegalStateException {
if (ohlcSeriesBuilder == null) {
final String ohlcBuilderName = OhlcSeriesBuilder.class.getSimpleName();
throw new IllegalStateException("No " + ohlcBuilderName + " configured. Use series("
+ IXYTimeSeriesDatasetBuilder.class.getSimpleName()
+ "<T>) to add a just one instance of " + ohlcBuilderName);
}
return ohlcSeriesBuilder.indexRange(elements.indexRange()).timeData(elements.timeData())
.build();
}
/**
* Creates and configures a new candlestick renderer instance for use with plots
*
* @return New renderer instance
*/
protected CandlestickRenderer getCandleRenderer() {
Color upColor = (ohlcSeriesBuilder != null) ? ohlcSeriesBuilder.upColor()
: BuilderConstants.DEFAULT_UP_COLOR;
Color downColor = (ohlcSeriesBuilder != null) ? ohlcSeriesBuilder.downColor()
: BuilderConstants.DEFAULT_DOWN_COLOR;
return CandlestickRendererBuilder.get().upColor(upColor).downColor(downColor).build();
}
}