Transpiled version (31450L) is out of date.
1 | sclass TradingCandlePainter extends AbstractTickerPainter is IToolTipMaker { |
2 | settable L<TradingCandle> candles; |
3 | |
4 | settable int spikeWidth = 1; |
5 | settable Color shortColor = java.awt.Color.blue; |
6 | settable Color longColor = java.awt.Color.yellow; |
7 | |
8 | // Heikin-Ashi style candles? |
9 | settable bool heikinAshi; |
10 | |
11 | settable bool autoHorizontalRange = true; |
12 | |
13 | settable int maxCandlesToShow = 1000; |
14 | |
15 | *() {} |
16 | *(L<TradingCandle> candles) { candles(candles); } |
17 | |
18 | TradingRun visiblePartOfRun() { |
19 | int i = 0; |
20 | while (i < l(candles) && candles.get(i).endOrProjectedEndTime() < horizontalRange().start) |
21 | ++i; |
22 | int j = l(candles); |
23 | while (j > i && candles.get(j-1).startTime().unixDate() >= horizontalRange().end) |
24 | --j; |
25 | ret new TradingRun(subList(candles, i, j)); |
26 | } |
27 | |
28 | void autoVerticalRange() { |
29 | var run = visiblePartOfRun(); |
30 | verticalRange(doubleRange(run.min(), run.max())); |
31 | } |
32 | |
33 | public void drawOn(Graphics2D g) { |
34 | if (empty(candles)) ret; |
35 | |
36 | var run = new TradingRun(candles); |
37 | if (autoHorizontalRange() || horizontalRange() == null) { |
38 | var firstCandle = candles.get(max(0, l(candles)-maxCandlesToShow)); |
39 | horizontalRange(doubleRange(firstCandle.startTime().unixDate(), run.endOrProjectedEndTime())); |
40 | } |
41 | autoVerticalRange(); |
42 | |
43 | drawPercentLines(g); |
44 | drawAdditionalObjects(g); |
45 | |
46 | var candles = candles(); |
47 | if (heikinAshi) candles = convertToHeikinAshi(candles); |
48 | |
49 | // Draw candles |
50 | |
51 | var nCandles = l(candles); |
52 | |
53 | for i to nCandles: { |
54 | var candle = candles.get(i); |
55 | //var x1 = xRange().start()+doubleRatio(i, nCandles)*xRange().length(); |
56 | //var x2 = xRange().start()+doubleRatio(i+1, nCandles)*xRange().length(); |
57 | double x1 = xToScreen(candle.startTime().unixDate()); |
58 | double x2 = xToScreen(candle.endOrProjectedEndTime()); |
59 | x2 = blend(x1, x2, 0.75); |
60 | var xCenter = avg(x1, x2); |
61 | var y1 = yToScreen(candle.max()); |
62 | var y2 = yToScreen(candle.min()); |
63 | var yStart = yToScreen(candle.start()); |
64 | var yEnd = yToScreen(candle.end()); |
65 | |
66 | // draw "spikes" |
67 | var r = doubleRectFromPoints(xCenter, y1, xCenter+spikeWidth, y2); |
68 | fillRect(g, growRectBottom(1, toRect_round(r)), candle.color()); |
69 | |
70 | // draw candle body |
71 | r = doubleRectFromPoints(x1, min(yStart, yEnd), x2, max(yStart, yEnd)); |
72 | fillRect(g, growRectBottom(1, toRect_round(r)), candle.color()); |
73 | } |
74 | |
75 | drawPositions(g); |
76 | } |
77 | |
78 | public BufferedImage render() { |
79 | var img = super.render(); |
80 | var img2 = cloneBufferedImageWithMeta(img); |
81 | metaSet(img2, IToolTipMaker, this); |
82 | ret img2; |
83 | } |
84 | |
85 | public swappable S getToolTip(Pt p) { |
86 | if (!hasScale()) ret "No candles yet"; |
87 | double time = xFromScreen(p.x); |
88 | //double price = yFromScreen(p.y); |
89 | |
90 | TradingCandle candle = firstThat(candles, c -> c.startTime().unixDate() >= time); |
91 | |
92 | if (candle == null) |
93 | ret formatLocalDateWithSeconds(lround(time)); |
94 | |
95 | ret str(candle); |
96 | } |
97 | |
98 | selfType candles(TradingCandleMaker candleMaker) { |
99 | ret candles(candleMaker!); |
100 | } |
101 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1036145 |
Snippet name: | TradingCandlePainter |
Eternal ID of this version: | #1036145/41 |
Text MD5: | 0b2b1f94abc536f99a65c2931fdcc266 |
Author: | stefan |
Category: | javax / gazelle 22 |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-03-24 16:30:42 |
Source code size: | 3215 bytes / 101 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 244 / 483 |
Version history: | 40 change(s) |
Referenced in: | [show references] |