-
Notifications
You must be signed in to change notification settings - Fork 95
/
AbstractBasicFinancialApplication.java
375 lines (322 loc) · 14.2 KB
/
AbstractBasicFinancialApplication.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package io.fair_acc.sample.financial;
import static io.fair_acc.chartfx.ui.ProfilerInfoBox.DebugLevel.VERSION;
import java.io.IOException;
import java.text.ParseException;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.fair_acc.chartfx.Chart;
import io.fair_acc.chartfx.XYChart;
import io.fair_acc.chartfx.axes.AxisLabelOverlapPolicy;
import io.fair_acc.chartfx.axes.AxisMode;
import io.fair_acc.chartfx.axes.spi.DefaultNumericAxis;
import io.fair_acc.chartfx.axes.spi.format.DefaultTimeFormatter;
import io.fair_acc.chartfx.plugins.ChartPlugin;
import io.fair_acc.chartfx.plugins.DataPointTooltip;
import io.fair_acc.chartfx.plugins.EditAxis;
import io.fair_acc.chartfx.plugins.Zoomer;
import io.fair_acc.chartfx.renderer.spi.financial.AbstractFinancialRenderer;
import io.fair_acc.chartfx.renderer.spi.financial.FinancialTheme;
import io.fair_acc.chartfx.ui.ProfilerInfoBox;
import io.fair_acc.chartfx.ui.geometry.Side;
import io.fair_acc.dataset.spi.DefaultDataSet;
import io.fair_acc.dataset.spi.financial.OhlcvDataSet;
import io.fair_acc.dataset.spi.financial.api.ohlcv.IOhlcv;
import io.fair_acc.dataset.spi.financial.api.ohlcv.IOhlcvItem;
import io.fair_acc.dataset.utils.ProcessingProfiler;
import io.fair_acc.sample.chart.ChartSample;
import io.fair_acc.sample.financial.dos.Interval;
import io.fair_acc.sample.financial.service.CalendarUtils;
import io.fair_acc.sample.financial.service.SimpleOhlcvDailyParser;
import io.fair_acc.sample.financial.service.SimpleOhlcvReplayDataSet;
import io.fair_acc.sample.financial.service.SimpleOhlcvReplayDataSet.DataInput;
import io.fair_acc.sample.financial.service.consolidate.OhlcvConsolidationAddon;
import io.fair_acc.sample.financial.service.period.IntradayPeriod;
import fxsampler.SampleBase;
/**
* Base class for demonstration of financial charts.
* This abstract class assemblies and configures important chart components and elements for financial charts.
* Any part can be overridden and modified for final Sample test.
*
* @author afischer
*/
public abstract class AbstractBasicFinancialApplication extends ChartSample {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractBasicFinancialApplication.class);
protected int prefChartWidth = 640; // 1024
protected int prefChartHeight = 480; // 768
protected int prefSceneWidth = 1920;
protected int prefSceneHeight = 1080;
private final double UPDATE_PERIOD = 10.0; // replay multiple
protected int DEBUG_UPDATE_RATE = 500;
protected String title; // application title
protected FinancialTheme theme = FinancialTheme.Sand;
protected String resource = "@ES-[TF1D]";
protected String timeRange = "2020/08/24 0:00-2020/11/12 0:00";
protected String tt;
protected String replayFrom;
protected IntradayPeriod period;
protected OhlcvDataSet ohlcvDataSet;
protected Map<String, OhlcvConsolidationAddon[]> consolidationAddons;
private final Spinner<Double> updatePeriod = new Spinner<>(1.0, 500.0, UPDATE_PERIOD, 1.0);
private final CheckBox localRange = new CheckBox("auto-y");
private boolean timerActivated = false;
//@Override
// public void start(final Stage primaryStage) {
// ProcessingProfiler.setVerboseOutputState(true);
// ProcessingProfiler.setLoggerOutputState(true);
// ProcessingProfiler.setDebugState(false);
// long startTime = ProcessingProfiler.getTimeStamp();
// ProcessingProfiler.getTimeDiff(startTime, "adding data to chart");
// startTime = ProcessingProfiler.getTimeStamp();
// configureApp();
// Scene scene = prepareScene();
// ProcessingProfiler.getTimeDiff(startTime, "adding chart into StackPane");
// startTime = ProcessingProfiler.getTimeStamp();
// primaryStage.setTitle(this.getClass().getSimpleName());
// primaryStage.setScene(scene);
// primaryStage.setOnCloseRequest(this::closeDemo);
// primaryStage.show();
// ProcessingProfiler.getTimeDiff(startTime, "for showing");
// // ensure correct state after restart demo
// stopTimer();
//}
protected void configureApp() {
// configure shared variables for application sample tests
}
// protected void closeDemo(final WindowEvent evt) {
// if (evt.getEventType().equals(WindowEvent.WINDOW_CLOSE_REQUEST) && LOGGER.isInfoEnabled()) {
// LOGGER.atInfo().log("requested demo to shut down");
// }
// stopTimer();
// Platform.exit();
// }
protected ToolBar getTestToolBar(Chart chart, AbstractFinancialRenderer<?> renderer, boolean replaySupport) {
ToolBar testVariableToolBar = new ToolBar();
localRange.setSelected(renderer.computeLocalRange());
localRange.setTooltip(new Tooltip("select for auto-adjusting min/max the y-axis (prices)"));
localRange.selectedProperty().bindBidirectional(renderer.computeLocalRangeProperty());
localRange.selectedProperty().addListener((ch, old, selection) -> {
for (ChartPlugin plugin : chart.getPlugins()) {
if (plugin instanceof Zoomer) {
((Zoomer) plugin).setAxisMode(selection ? AxisMode.X : AxisMode.XY);
}
}
chart.invalidate();
});
Button periodicTimer = null;
if (replaySupport) {
// repetitively generate new data
periodicTimer = new Button("replay");
periodicTimer.setTooltip(new Tooltip("replay instrument data in realtime"));
periodicTimer.setOnAction(evt -> {
pauseResumeTimer();
});
updatePeriod.valueProperty().addListener((ch, o, n) -> updateTimer());
updatePeriod.setEditable(true);
updatePeriod.setPrefWidth(80);
}
final ProfilerInfoBox profilerInfoBox = new ProfilerInfoBox(DEBUG_UPDATE_RATE);
profilerInfoBox.setDebugLevel(VERSION);
final Pane spacer = new Pane();
HBox.setHgrow(spacer, Priority.ALWAYS);
if (replaySupport) {
testVariableToolBar.getItems().addAll(localRange, periodicTimer, updatePeriod, new Label("[multiply]"), spacer, profilerInfoBox);
} else {
testVariableToolBar.getItems().addAll(localRange, spacer, profilerInfoBox);
}
return testVariableToolBar;
}
/**
* Prepare charts to the root.
*
* @return prepared scene for sample app
*/
public Node getChartPanel(Stage stage) {
// show all default financial color schemes
final var root = new FlowPane();
root.setAlignment(Pos.CENTER);
Arrays.stream(FinancialTheme.values())
.map(this::getDefaultFinancialTestChart)
.forEach(root.getChildren()::add);
return root;
}
/**
* Default financial chart configuration
*
* @param theme defines theme which has to be used for sample app
*/
protected Chart getDefaultFinancialTestChart(final FinancialTheme theme) {
// load datasets
DefaultDataSet indiSet = null;
if (resource.startsWith("REALTIME")) {
try {
Interval<Calendar> timeRangeInt = CalendarUtils.createByDateTimeInterval(timeRange);
Interval<Calendar> ttInt = CalendarUtils.createByTimeInterval(tt);
Calendar replayFromCal = CalendarUtils.createByDateTime(replayFrom);
ohlcvDataSet = new SimpleOhlcvReplayDataSet(
DataInput.valueOf(resource.substring("REALTIME-".length())),
period,
timeRangeInt,
ttInt,
replayFromCal,
consolidationAddons);
} catch (ParseException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
} else {
ohlcvDataSet = new OhlcvDataSet(resource);
indiSet = new DefaultDataSet("MA(24)");
try {
loadTestData(resource, ohlcvDataSet, indiSet);
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
// prepare axis
final DefaultNumericAxis xAxis1 = new DefaultNumericAxis("time", "iso");
xAxis1.setOverlapPolicy(AxisLabelOverlapPolicy.SKIP_ALT);
xAxis1.setAutoRangeRounding(false);
xAxis1.setTimeAxis(true);
// set localised time offset
if (xAxis1.isTimeAxis() && xAxis1.getAxisLabelFormatter() instanceof DefaultTimeFormatter) {
final DefaultTimeFormatter axisFormatter = (DefaultTimeFormatter) xAxis1.getAxisLabelFormatter();
axisFormatter.setTimeZoneOffset(ZoneOffset.ofHoursMinutes(2, 0));
}
// category axis support tests
// final CategoryAxis xAxis = new CategoryAxis("time [iso]");
// xAxis.setTickLabelRotation(90);
// xAxis.setOverlapPolicy(AxisLabelOverlapPolicy.SKIP_ALT);
final DefaultNumericAxis yAxis1 = new DefaultNumericAxis("price", "points");
// prepare chart structure
final XYChart chart = new XYChart(xAxis1, yAxis1);
chart.setTitle(theme.name());
chart.setLegendVisible(true);
chart.setPrefSize(prefChartWidth, prefChartHeight);
// set them false to make the plot faster
chart.setAnimated(false);
// prepare plugins
chart.getPlugins().add(new Zoomer(AxisMode.X));
chart.getPlugins().add(new EditAxis());
chart.getPlugins().add(new DataPointTooltip());
// basic chart financial structure style
chart.getGridRenderer().setDrawOnTop(false);
yAxis1.setAutoRangeRounding(true);
yAxis1.setSide(Side.RIGHT);
// prepare financial renderers
prepareRenderers(chart, ohlcvDataSet, indiSet);
// apply color scheme
theme.applyPseudoClasses(chart);
// zoom to specific time range
if (timeRange != null) {
showPredefinedTimeRange(timeRange, ohlcvDataSet, xAxis1, yAxis1);
}
return chart;
}
/**
* Show required part of the OHLC resource
*
* @param dateIntervalPattern from to pattern for time range
* @param ohlcvDataSet domain object with filled ohlcv data
* @param xaxis X-axis for settings
* @param yaxis Y-axis for settings
*/
protected void showPredefinedTimeRange(String dateIntervalPattern, OhlcvDataSet ohlcvDataSet,
DefaultNumericAxis xaxis, DefaultNumericAxis yaxis) {
try {
Interval<Calendar> fromTo = CalendarUtils.createByDateTimeInterval(dateIntervalPattern);
double fromTime = fromTo.from.getTime().getTime() / 1000.0;
double toTime = fromTo.to.getTime().getTime() / 1000.0;
int fromIdx = ohlcvDataSet.getXIndex(fromTime);
int toIdx = ohlcvDataSet.getXIndex(toTime);
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = fromIdx; i <= toIdx; i++) {
IOhlcvItem ohlcvItem = ohlcvDataSet.getItem(i);
if (max < ohlcvItem.getHigh()) {
max = ohlcvItem.getHigh();
}
if (min > ohlcvItem.getLow()) {
min = ohlcvItem.getLow();
}
}
xaxis.set(fromTime, toTime);
yaxis.set(min, max);
xaxis.setAutoRanging(false);
yaxis.setAutoRanging(false);
} catch (ParseException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
/**
* Load OHLC structures and indi calc
*
* @param data required data
* @param dataSet dataset which will be filled by this data
* @param indiSet example of indicator calculation
* @throws IOException if loading fails
*/
protected void loadTestData(String data, final OhlcvDataSet dataSet, DefaultDataSet indiSet) throws IOException {
final long startTime = ProcessingProfiler.getTimeStamp();
IOhlcv ohlcv = new SimpleOhlcvDailyParser().getContinuousOHLCV(data);
dataSet.setData(ohlcv);
DescriptiveStatistics stats = new DescriptiveStatistics(24);
for (IOhlcvItem ohlcvItem : ohlcv) {
double timestamp = ohlcvItem.getTimeStamp().getTime() / 1000.0;
stats.addValue(ohlcvItem.getClose());
indiSet.add(timestamp, stats.getMean());
}
ProcessingProfiler.getTimeDiff(startTime, "adding data into DataSet");
}
/**
* Create and apply renderers
*
* @param chart for applying renderers
*/
protected abstract void prepareRenderers(XYChart chart, OhlcvDataSet ohlcvDataSet, DefaultDataSet indiSet);
//--------- replay support ---------
private void pauseResumeTimer() {
if (!timerActivated) {
startTimer();
} else if (ohlcvDataSet instanceof SimpleOhlcvReplayDataSet) {
((SimpleOhlcvReplayDataSet) ohlcvDataSet).pauseResume();
}
}
private void updateTimer() {
if (timerActivated) {
startTimer();
}
}
private void startTimer() {
if (ohlcvDataSet instanceof SimpleOhlcvReplayDataSet) {
SimpleOhlcvReplayDataSet realtimeDataSet = (SimpleOhlcvReplayDataSet) ohlcvDataSet;
realtimeDataSet.setUpdatePeriod(updatePeriod.getValue());
timerActivated = true;
}
}
private void stopTimer() {
if (timerActivated && ohlcvDataSet instanceof SimpleOhlcvReplayDataSet) {
timerActivated = false;
SimpleOhlcvReplayDataSet realtimeDataSet = (SimpleOhlcvReplayDataSet) ohlcvDataSet;
realtimeDataSet.stop();
}
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}