-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcandlestick_chart.go
More file actions
578 lines (522 loc) · 19.9 KB
/
candlestick_chart.go
File metadata and controls
578 lines (522 loc) · 19.9 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package charts
import (
"errors"
"math"
)
type candlestickChart struct {
p *Painter
opt *CandlestickChartOption
}
// newCandlestickChart returns a candlestick chart renderer.
func newCandlestickChart(p *Painter, opt CandlestickChartOption) *candlestickChart {
return &candlestickChart{
p: p,
opt: &opt,
}
}
// CandlestickChartOption defines options for rendering candlestick charts. Render the chart using Painter.CandlestickChart.
type CandlestickChartOption struct {
// Theme specifies the colors used for the candlestick chart.
Theme ColorPalette
// Padding specifies the padding around the chart.
Padding Box
// SeriesList provides the OHLC data population for the chart. Typically constructed using NewSeriesListCandlestick.
SeriesList CandlestickSeriesList
// XAxis contains options for the x-axis.
XAxis XAxisOption
// YAxis contains options for the y-axis. At most two y-axes are supported.
YAxis []YAxisOption
// Title contains options for rendering the chart title.
Title TitleOption
// Legend contains options for the data legend.
Legend LegendOption
// CandleWidth sets body width ratio (0.0–1.0, default 0.8).
CandleWidth float64
// ShowWicks controls whether high-low wicks are displayed by default. When nil, wicks are shown.
// Individual series can override this setting.
ShowWicks *bool
// WickWidth sets wick stroke width in pixels (default 1.0).
WickWidth float64
// CandleMargin sets inter-series spacing ratio (0.0–1.0, auto by default).
// Only applies with multiple candlestick series.
CandleMargin *float64
// ValueFormatter formats numeric values.
ValueFormatter ValueFormatter
}
// NewCandlestickOptionWithData creates a CandlestickChartOption from OHLC data slices.
func NewCandlestickOptionWithData(data ...[]OHLCData) CandlestickChartOption {
seriesList := make(CandlestickSeriesList, len(data))
for i, ohlcData := range data {
seriesList[i] = CandlestickSeries{Data: ohlcData}
}
return NewCandlestickOptionWithSeries(seriesList...)
}
// NewCandlestickOptionWithSeries returns an initialized CandlestickChartOption with the provided Series.
func NewCandlestickOptionWithSeries(series ...CandlestickSeries) CandlestickChartOption {
seriesList := make(CandlestickSeriesList, len(series))
copy(seriesList, series)
return CandlestickChartOption{
SeriesList: seriesList,
Padding: defaultPadding,
Theme: GetDefaultTheme(),
YAxis: make([]YAxisOption, getSeriesYAxisCount(seriesList)),
ValueFormatter: defaultValueFormatter,
CandleWidth: 0.8, // Default 80% of available space
WickWidth: 1.0,
}
}
// createPatternAwareLabelFormatter creates a label formatter that can handle pattern detection
// while respecting user-provided label formatters based on Replace/Complement mode
func createPatternAwareLabelFormatter(originalSeries *CandlestickSeries, seriesIndex int, theme ColorPalette,
patternMap map[int][]PatternDetectionResult) SeriesLabelFormatter {
return func(index int, name string, val float64) (string, *LabelStyle) {
// Check for patterns at this index using pre-computed map
var patterns []PatternDetectionResult
if patternMap != nil {
patterns = patternMap[index]
}
if !originalSeries.PatternConfig.PreferPatternLabels && originalSeries.Label.LabelFormatter != nil &&
flagIs(true, originalSeries.Label.Show) { // prefer user label if provided
userText, userStyle := originalSeries.Label.LabelFormatter(index, name, val)
if userText != "" {
return userText, userStyle // User label takes precedence
}
}
var patternText string
var patternStyle *LabelStyle
if originalSeries.PatternConfig.PatternFormatter != nil {
patternText, patternStyle = originalSeries.PatternConfig.PatternFormatter(patterns, originalSeries.Name, val)
} else {
patternText, patternStyle = formatPatternsDefault(patterns, seriesIndex, theme)
}
if patternText != "" {
// either no user input, or configured to replace and we have a matching pattern
return patternText, patternStyle
}
if flagIs(true, originalSeries.Label.Show) { // user explicitly requested a label, show something
if originalSeries.Label.LabelFormatter != nil {
return originalSeries.Label.LabelFormatter(index, name, val)
} else if originalSeries.Label.ValueFormatter != nil {
return originalSeries.Label.ValueFormatter(val), nil
}
return defaultValueFormatter(val), nil
}
return "", nil
}
}
// TODO - v0.6 - attempt to de-duplicate with calculateBarMarginsAndSize
// calculateCandleMarginsAndSize calculates margins and candle sizes similar to bar charts.
func calculateCandleMarginsAndSize(seriesCount, space int, configuredCandleSize int, configuredCandleMargin *float64) (int, int, int) {
// default margins, adjusted below with config and series count
margin := 10 // margin between each series group
candleMargin := 5 // margin between each candle
if space < 20 {
margin = 2
candleMargin = 2
} else if space < 50 {
margin = 5
candleMargin = 3
}
// check margin configuration if candle size allows margin
if configuredCandleSize+candleMargin < space/seriesCount {
// CandleWidth is in range that we should also consider an optional margin configuration
if configuredCandleMargin != nil {
candleMargin = int(math.Round(*configuredCandleMargin))
if candleMargin+configuredCandleSize > space/seriesCount {
candleMargin = (space / seriesCount) - configuredCandleSize
}
}
} // else, candle width is out of range. Ignore margin config
candleSize := (space - 2*margin - candleMargin*(seriesCount-1)) / seriesCount
// check candle size configuration, limited by the series count and space available
if configuredCandleSize > 0 && configuredCandleSize < candleSize {
candleSize = configuredCandleSize
// recalculate margin
margin = (space - seriesCount*candleSize - candleMargin*(seriesCount-1)) / 2
}
return margin, candleMargin, candleSize
}
func (k *candlestickChart) renderChart(result *defaultRenderResult) (Box, error) {
p := k.p
opt := k.opt
seriesList := opt.SeriesList
if seriesList.len() == 0 {
result.renderNoData(opt.Theme)
return p.box, nil
}
seriesPainter := result.seriesPainter
// Find maximum data count across all series
maxDataCount := getSeriesMaxDataCount(seriesList)
if maxDataCount == 0 {
result.renderNoData(opt.Theme)
return p.box, nil
}
width := seriesPainter.Width()
if width <= 0 {
return BoxZero, errors.New("invalid painter width")
}
seriesCount := seriesList.len()
// Calculate candle width using CandleWidth ratio (default 80%)
candleWidthRatio := opt.CandleWidth
if candleWidthRatio <= 0 {
candleWidthRatio = 0.8 // Default 80% of available space
} else if candleWidthRatio > 1 {
candleWidthRatio = 1
}
candleWidth := int(float64(width) * candleWidthRatio / float64(maxDataCount))
if candleWidth < 1 {
candleWidth = 1
}
// Calculate candleWidthPerSeries for body rendering
candleWidthPerSeries := candleWidth / seriesCount
if candleWidthPerSeries < 1 {
candleWidthPerSeries = 1
}
// Use autoDivide for positioning
divideValues := result.categoryAxisRange.autoDivide()
// Center positions for each series index
seriesCenterValues := make([][]int, seriesList.len())
// render list must start with the markPointPainter, as it can influence label painters (if enabled)
markPointPainter := newMarkPointPainter(seriesPainter)
markLinePainter := newMarkLinePainter(seriesPainter)
trendLinePainter := newTrendLinePainter(seriesPainter)
rendererList := []renderer{markPointPainter, markLinePainter, trendLinePainter}
seriesNames := seriesList.names()
// Store points and label painters for each series
seriesClosePoints := make([][]Point, seriesList.len())
seriesOpenPoints := make([][]Point, seriesList.len())
seriesHighPoints := make([][]Point, seriesList.len())
seriesLowPoints := make([][]Point, seriesList.len())
allLabelPainters := make([]*seriesLabelPainter, seriesList.len())
// Render each series
for seriesIndex := 0; seriesIndex < seriesList.len(); seriesIndex++ {
series := seriesList.getSeries(seriesIndex).(*CandlestickSeries)
// Bounds check for Y axis index to prevent panic
if series.YAxisIndex >= len(result.valueAxisRanges) {
return BoxZero, errors.New("candlestick series YAxisIndex out of bounds")
}
yRange := result.valueAxisRanges[series.YAxisIndex]
// pre-compute patterns for this series
var patternMap map[int][]PatternDetectionResult
if series.PatternConfig != nil {
patternMap = scanForCandlestickPatterns(series.Data, *series.PatternConfig)
}
// Create labelPainter only when labels are enabled or patterns were detected
var labelPainter *seriesLabelPainter
if flagIs(true, series.Label.Show) || len(patternMap) > 0 {
if len(patternMap) > 0 {
labelCopy := series.Label
labelCopy.LabelFormatter = createPatternAwareLabelFormatter(series, seriesIndex, opt.Theme, patternMap)
labelCopy.Show = Ptr(true) // Enable labels when patterns are detected, even if user labels are disabled
labelPainter = newSeriesLabelPainter(seriesPainter, seriesNames, labelCopy, opt.Theme, opt.Padding.Right)
} else {
labelPainter = newSeriesLabelPainter(seriesPainter, seriesNames, series.Label, opt.Theme, opt.Padding.Right)
}
rendererList = append(rendererList, labelPainter)
}
allLabelPainters[seriesIndex] = labelPainter
seriesThemeIndex := seriesIndex
if series.absThemeIndex != nil {
seriesThemeIndex = *series.absThemeIndex
}
upColor, downColor := opt.Theme.GetSeriesUpDownColors(seriesThemeIndex)
// Initialize point arrays for all OHLC values for this series
seriesClosePoints[seriesIndex] = make([]Point, len(series.Data))
seriesOpenPoints[seriesIndex] = make([]Point, len(series.Data))
seriesHighPoints[seriesIndex] = make([]Point, len(series.Data))
seriesLowPoints[seriesIndex] = make([]Point, len(series.Data))
seriesCenterValues[seriesIndex] = make([]int, len(series.Data))
// Render each candlestick in this series
for j, ohlc := range series.Data {
if j >= maxDataCount || j >= len(divideValues) {
continue
}
// Position calculation: center candlesticks in each time period
// Calculate the center of each time period section
var sectionWidth int
if j < len(divideValues)-1 {
sectionWidth = divideValues[j+1] - divideValues[j]
} else {
// Last section uses same width as previous
if j > 0 {
sectionWidth = divideValues[j] - divideValues[j-1]
} else {
sectionWidth = seriesPainter.Width() / maxDataCount
}
}
// Calculate margins and positioning exactly like bar charts
var groupMargin, candleMargin, candleWidth int
if seriesList.len() == 1 {
// Single series: use simple centering
groupMargin = 0
candleMargin = 0
candleWidth = candleWidthPerSeries
} else {
// Multiple series: use bar chart margin calculation logic
// Convert CandleMargin percentage to pixels
var candleMarginFloat *float64
if opt.CandleMargin != nil {
// Convert percentage to absolute pixels
marginPixels := float64(sectionWidth) * (*opt.CandleMargin)
candleMarginFloat = &marginPixels
}
// Use bar chart logic: calculateBarMarginsAndSize equivalent
groupMargin, candleMargin, candleWidth =
calculateCandleMarginsAndSize(seriesList.len(),
sectionWidth, candleWidthPerSeries, candleMarginFloat)
}
var centerX int
if seriesList.len() == 1 {
// Single series: center in the time period section
centerX = divideValues[j] + sectionWidth/2
} else {
// Multiple series: use exact bar chart positioning formula
// x = divideValues[j] + margin + index*(barWidth+barMargin)
x := divideValues[j] + groupMargin + seriesIndex*(candleWidth+candleMargin)
centerX = x + candleWidth/2
}
seriesCenterValues[seriesIndex][j] = centerX
if !validateOHLCData(ohlc) { // if invalid mark as null and skip
// Mark all OHLC points as invalid
invalidPoint := Point{X: centerX, Y: math.MaxInt32}
seriesClosePoints[seriesIndex][j] = invalidPoint
seriesOpenPoints[seriesIndex][j] = invalidPoint
seriesHighPoints[seriesIndex][j] = invalidPoint
seriesLowPoints[seriesIndex][j] = invalidPoint
continue
}
leftX := centerX - candleWidth/2
rightX := centerX + candleWidth/2
highY := yRange.getRestHeight(ohlc.High)
lowY := yRange.getRestHeight(ohlc.Low)
openY := yRange.getRestHeight(ohlc.Open)
closeY := yRange.getRestHeight(ohlc.Close)
bodyTop := int(math.Min(float64(openY), float64(closeY)))
bodyBottom := int(math.Max(float64(openY), float64(closeY)))
// Determine colors and style
isBullish := ohlc.Close >= ohlc.Open
candleStyle := series.CandleStyle
if candleStyle == "" {
candleStyle = CandleStyleFilled
}
var bodyColor, wickColor Color
if isBullish {
bodyColor = upColor
} else {
bodyColor = downColor
}
wickColor = opt.Theme.GetCandleWickColor()
if wickColor.IsZero() {
wickColor = bodyColor
}
// Draw high-low wick (if enabled)
showWicks := !flagIs(false, opt.ShowWicks)
if series.ShowWicks != nil {
showWicks = *series.ShowWicks
}
wickWidth := opt.WickWidth
if wickWidth <= 0 {
wickWidth = 1.0
}
if showWicks {
if highY < bodyTop {
seriesPainter.LineStroke([]Point{
{X: centerX, Y: highY},
{X: centerX, Y: bodyTop},
}, wickColor, wickWidth)
}
if lowY > bodyBottom {
seriesPainter.LineStroke([]Point{
{X: centerX, Y: bodyBottom},
{X: centerX, Y: lowY},
}, wickColor, wickWidth)
}
// Calculate cap width (based on series candle width)
capWidth := candleWidthPerSeries / 4
if capWidth < 1 {
capWidth = 1
}
// Draw horizontal cap at high point
seriesPainter.LineStroke([]Point{
{X: centerX - capWidth, Y: highY},
{X: centerX + capWidth, Y: highY},
}, wickColor, wickWidth)
// Draw horizontal cap at low point
seriesPainter.LineStroke([]Point{
{X: centerX - capWidth, Y: lowY},
{X: centerX + capWidth, Y: lowY},
}, wickColor, wickWidth)
}
// Draw open-close body based on style
if bodyTop == bodyBottom { // Doji (open == close)
// Draw thin line instead of rectangle
seriesPainter.LineStroke([]Point{
{X: leftX, Y: bodyTop},
{X: rightX, Y: bodyTop},
}, bodyColor, 1.0)
} else {
switch candleStyle {
case CandleStyleFilled:
seriesPainter.FilledRect(leftX, bodyTop, rightX, bodyBottom,
bodyColor, bodyColor, 0.0)
case CandleStyleTraditional:
if isBullish { // Hollow body for bullish
seriesPainter.FilledRect(leftX, bodyTop, rightX, bodyBottom,
ColorTransparent, bodyColor, wickWidth)
} else { // Filled body for bearish
seriesPainter.FilledRect(leftX, bodyTop, rightX, bodyBottom,
bodyColor, bodyColor, 0.0)
}
case CandleStyleOutline:
seriesPainter.FilledRect(leftX, bodyTop, rightX, bodyBottom,
ColorTransparent, bodyColor, wickWidth)
}
}
// Store points for all OHLC values for mark points
seriesClosePoints[seriesIndex][j] = Point{X: centerX, Y: closeY}
seriesOpenPoints[seriesIndex][j] = Point{X: centerX, Y: openY}
seriesHighPoints[seriesIndex][j] = Point{X: centerX, Y: highY}
seriesLowPoints[seriesIndex][j] = Point{X: centerX, Y: lowY}
// Add label if enabled (pattern logic is now handled in the label formatter)
if labelPainter != nil {
labelPainter.Add(labelValue{
index: j, // Data point index (candlestick position), not series index
value: ohlc.Close, // Use close price for label
x: centerX,
y: closeY,
fontStyle: series.Label.FontStyle,
offset: series.Label.Offset,
})
}
}
}
// Handle mark lines, mark points, and trend lines for each series and OHLC component
for seriesIndex := 0; seriesIndex < seriesList.len(); seriesIndex++ {
series := seriesList.getSeries(seriesIndex).(*CandlestickSeries)
// Bounds check for Y axis index to prevent panic
if series.YAxisIndex >= len(result.valueAxisRanges) {
continue // Skip this series if YAxisIndex is out of bounds
}
yRange := result.valueAxisRanges[series.YAxisIndex]
seriesThemeIndex := seriesIndex
if series.absThemeIndex != nil {
seriesThemeIndex = *series.absThemeIndex
}
seriesColor := opt.Theme.GetSeriesColor(seriesThemeIndex)
ohlcComponents := []struct {
markLine SeriesMarkLine
markPoint SeriesMarkPoint
trendLines []SeriesTrendLine
extractFunc func(*CandlestickSeries) []float64
points []Point
}{
{
markLine: series.OpenMarkLine,
markPoint: series.OpenMarkPoint,
trendLines: series.OpenTrendLine,
extractFunc: (*CandlestickSeries).ExtractOpenPrices,
points: seriesOpenPoints[seriesIndex],
},
{
markLine: series.HighMarkLine,
markPoint: series.HighMarkPoint,
trendLines: series.HighTrendLine,
extractFunc: (*CandlestickSeries).ExtractHighPrices,
points: seriesHighPoints[seriesIndex],
},
{
markLine: series.LowMarkLine,
markPoint: series.LowMarkPoint,
trendLines: series.LowTrendLine,
extractFunc: (*CandlestickSeries).ExtractLowPrices,
points: seriesLowPoints[seriesIndex],
},
{
markLine: series.CloseMarkLine,
markPoint: series.CloseMarkPoint,
trendLines: series.CloseTrendLine,
extractFunc: (*CandlestickSeries).ExtractClosePrices,
points: seriesClosePoints[seriesIndex],
},
}
for _, component := range ohlcComponents {
var values []float64
// Handle mark lines
if seriesMarks := component.markLine.Lines.filterGlobal(false); len(seriesMarks) > 0 {
if values == nil {
values = component.extractFunc(series)
}
markLineValueFormatter := getPreferredValueFormatter(component.markLine.ValueFormatter,
series.Label.ValueFormatter, opt.ValueFormatter)
markLinePainter.add(markLineRenderOption{
fillColor: seriesColor,
fontColor: opt.Theme.GetMarkTextColor(),
strokeColor: seriesColor,
font: getPreferredFont(series.Label.FontStyle.Font),
marklines: seriesMarks,
seriesValues: values,
axisRange: yRange,
valueFormatter: markLineValueFormatter,
})
}
// Handle mark points
if seriesMarks := component.markPoint.Points.filterGlobal(false); len(seriesMarks) > 0 {
if values == nil {
values = component.extractFunc(series)
}
markPointValueFormatter := getPreferredValueFormatter(component.markPoint.ValueFormatter,
series.Label.ValueFormatter, opt.ValueFormatter)
markPointPainter.add(markPointRenderOption{
fillColor: seriesColor,
font: getPreferredFont(series.Label.FontStyle.Font),
symbolSize: component.markPoint.SymbolSize,
points: component.points,
markpoints: seriesMarks,
seriesValues: values,
valueFormatter: markPointValueFormatter,
seriesLabelPainter: allLabelPainters[seriesIndex],
})
}
// Handle trend lines
if len(component.trendLines) > 0 {
if values == nil {
values = component.extractFunc(series)
}
trendLinePainter.add(trendLineRenderOption{
defaultStrokeColor: opt.Theme.GetSeriesTrendColor(seriesThemeIndex),
xValues: seriesCenterValues[seriesIndex],
seriesValues: values,
axisRange: yRange,
trends: component.trendLines,
dashed: false, // Default for candlestick charts
})
}
}
}
if err := doRender(rendererList...); err != nil {
return BoxZero, err
}
return p.box, nil
}
func (k *candlestickChart) Render() (Box, error) {
p := k.p
opt := k.opt
if opt.Theme == nil {
opt.Theme = getPreferredTheme(p.theme)
}
opt.Legend.Symbol = symbolCandlestick
renderResult, err := defaultRender(p, defaultRenderOption{
theme: opt.Theme,
padding: opt.Padding,
seriesList: &opt.SeriesList,
categoryAxis: &opt.XAxis,
valueAxis: opt.YAxis,
title: opt.Title,
legend: &opt.Legend,
valueFormatter: opt.ValueFormatter,
})
if err != nil {
return BoxZero, err
}
return k.renderChart(renderResult)
}