forked from fair-acc/chart-fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinancialAdvancedCandlestickSample.java
109 lines (92 loc) · 4.26 KB
/
FinancialAdvancedCandlestickSample.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
package io.fair_acc.sample.financial;
import java.util.Calendar;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import io.fair_acc.chartfx.XYChart;
import io.fair_acc.chartfx.renderer.ErrorStyle;
import io.fair_acc.chartfx.renderer.spi.ErrorDataSetRenderer;
import io.fair_acc.chartfx.renderer.spi.financial.AbstractFinancialRenderer;
import io.fair_acc.chartfx.renderer.spi.financial.CandleStickRenderer;
import io.fair_acc.chartfx.renderer.spi.financial.FinancialTheme;
import io.fair_acc.dataset.spi.DefaultDataSet;
import io.fair_acc.dataset.spi.financial.OhlcvDataSet;
import io.fair_acc.dataset.spi.financial.api.attrs.AttributeKey;
import io.fair_acc.dataset.spi.financial.api.ohlcv.IOhlcvItem;
/**
* Advanced configuration of Candlestick renderer. Support for PaintBars and extension points.
*
* @author afischer
*/
public class FinancialAdvancedCandlestickSample extends AbstractBasicFinancialApplication {
public static final AttributeKey<Boolean> MARK_BAR = AttributeKey.create(Boolean.class, "MARK_BAR");
/**
* Prepare charts to the root.
*/
@Override
public Node getChartPanel(Stage stage) {
timeRange = "2020/06/24 0:00-2020/11/12 0:00";
final var chart = getDefaultFinancialTestChart(FinancialTheme.Sand);
final AbstractFinancialRenderer<?> renderer = (AbstractFinancialRenderer<?>) chart.getRenderers().get(0);
// prepare top financial toolbar
var testVariableToolBar = getTestToolBar(chart, renderer, false);
var root = new VBox();
VBox.setVgrow(chart, Priority.SOMETIMES);
root.getChildren().addAll(testVariableToolBar, chart);
return root;
}
protected void prepareRenderers(XYChart chart, OhlcvDataSet ohlcvDataSet, DefaultDataSet indiSet) {
// create and apply renderers
var candleStickRenderer = new CandleStickRenderer(true);
candleStickRenderer.getDatasets().addAll(ohlcvDataSet);
var avgRenderer = new ErrorDataSetRenderer();
avgRenderer.setDrawMarker(false);
avgRenderer.setErrorStyle(ErrorStyle.NONE);
avgRenderer.getDatasets().addAll(indiSet);
chart.getRenderers().clear();
chart.getRenderers().add(candleStickRenderer);
chart.getRenderers().add(avgRenderer);
//------------------------------------------
// Example of extension possibilities
// PaintBar Service Usage
candleStickRenderer.setPaintBarMarker(d -> d.ohlcvItem != null && (d.ohlcvItem.getOpen() - d.ohlcvItem.getClose() > 100.0) ? Color.MAGENTA : null);
// PaintAfter Extension Point Usage
// select every friday with yellow square point in the middle of candle
var cal = Calendar.getInstance(); // set this up however you need it.
for (IOhlcvItem ohlcvItem : ohlcvDataSet) {
cal.setTime(ohlcvItem.getTimeStamp());
int day = cal.get(Calendar.DAY_OF_WEEK);
if (day == Calendar.FRIDAY) {
ohlcvItem.getAddonOrCreate().setAttribute(MARK_BAR, true);
}
}
// example of extension point PaintAfter - Paint yellow square if the bar is selected by addon model attribute
candleStickRenderer.addPaintAfterEp(d -> {
if (d.ohlcvItem == null || d.ohlcvItem.getAddon() == null) {
return;
}
// addon extension with MARK BAR settings
if (Boolean.TRUE.equals(d.ohlcvItem.getAddon().getAttribute(MARK_BAR, false))) {
double yy;
if (d.ohlcvItem.getClose() > d.ohlcvItem.getOpen()) {
yy = d.yClose - (d.yClose - d.yOpen) / 2;
d.gc.setFill(Color.CRIMSON);
} else {
yy = d.yOpen - (d.yOpen - d.yClose) / 2;
d.gc.setFill(Color.YELLOW);
}
final double rectCorr = d.barWidthHalf / 2.0;
d.gc.fillRect(d.xCenter - rectCorr, yy - rectCorr, rectCorr * 2.0, rectCorr * 2.0);
}
});
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}