-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchart.go
150 lines (128 loc) · 3.72 KB
/
chart.go
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
package candlePrintGo
import (
"fmt"
"math"
"github.com/muesli/termenv"
)
const (
_symbolStick = "│"
_symbolCandle = "┃"
_symbolHalfTop = "╽"
_symbolHalfBottom = "╿"
_symbolHalfCandleTop = "╻"
_symbolHalfCandleBottom = "╹"
_symbolHalfStickTop = "╷"
_symbolHalfStickBottom = "╵"
_symbolEmpty = " "
_halfTop = 0.75
_halfBottom = 0.25
)
type Chart interface {
Render() string
}
var _ Chart = &CandleChart{}
type CandleChart struct {
profile *ColorProfile
height float64
data []Candle
chartBottom float64
chartTop float64
}
type Option func(chart *CandleChart)
func WithColorProfile(profile *ColorProfile) Option {
return func(c *CandleChart) {
c.profile = profile
}
}
func NewCandleChart(data []Candle, height float64, opts ...Option) *CandleChart {
min := math.MaxFloat64
max := math.SmallestNonzeroFloat64
for _, v := range data {
min = math.Min(min, v.Bottom())
max = math.Max(max, v.Top())
}
chart := &CandleChart{
profile: DefaultColorScheme,
height: height,
data: data,
chartBottom: min,
chartTop: max,
}
for _, o := range opts {
o(chart)
}
return chart
}
func (c *CandleChart) toHeightUnits(x float64) float64 {
return (x - c.chartBottom) / (c.chartTop - c.chartBottom) * c.height
}
func (c *CandleChart) renderCandle(tick Candle, height float64) string {
top := c.toHeightUnits(tick.High())
topCandle := c.toHeightUnits(tick.Top())
bottom := c.toHeightUnits(tick.Low())
bottomCandle := c.toHeightUnits(tick.Bottom())
if math.Ceil(top) > height && height >= math.Floor(topCandle) {
if topCandle-height > _halfTop {
return c.colorCandle(_symbolCandle, tick.IsBullish())
} else if topCandle-height > _halfBottom {
if top-height > _halfTop {
return c.colorCandle(_symbolHalfTop, tick.IsBullish())
}
return c.colorCandle(_symbolHalfCandleTop, tick.IsBullish())
} else {
if top-height > _halfTop {
return c.colorCandle(_symbolStick, tick.IsBullish())
} else if top-height > _halfBottom {
return c.colorCandle(_symbolHalfStickTop, tick.IsBullish())
}
return _symbolEmpty
}
} else if math.Floor(topCandle) >= height && height >= math.Ceil(bottomCandle) {
return c.colorCandle(_symbolCandle, tick.IsBullish())
} else if math.Ceil(bottomCandle) >= height && height >= math.Floor(bottom) {
if bottomCandle-height < _halfBottom {
return c.colorCandle(_symbolCandle, tick.IsBullish())
} else if bottomCandle-height < _halfTop {
if bottom-height < _halfBottom {
return c.colorCandle(_symbolHalfBottom, tick.IsBullish())
}
return c.colorCandle(_symbolHalfCandleBottom, tick.IsBullish())
} else {
if bottom-height < _halfBottom {
return c.colorCandle(_symbolStick, tick.IsBullish())
} else if bottom-height < _halfTop {
return c.colorCandle(_symbolHalfStickBottom, tick.IsBullish())
}
return _symbolEmpty
}
}
return _symbolEmpty
}
func (c *CandleChart) colorCandle(symbol string, isBulish bool) string {
s := termenv.String(symbol).Foreground(c.profile.p.Color(c.profile.bearColor))
if isBulish {
s = termenv.String(symbol).Foreground(c.profile.p.Color(c.profile.bullColor))
}
return fmt.Sprintf(`%v`, s)
}
func (c *CandleChart) Render() string {
r := "\n"
for i := c.height; i >= 0; i-- {
r += c.printPrice(i)
for _, v := range c.data {
r += c.renderCandle(v, float64(i))
}
r += "\n"
}
return r
}
func (c *CandleChart) printPrice(line float64) string {
if int64(line)%4 == 0 {
return formatPrice(c.chartBottom+(line*(c.chartTop-c.chartBottom)/c.height)) + "|"
}
return " |"
}
func formatPrice(value float64) string {
formatted := fmt.Sprintf("%.2f", value)
return fmt.Sprintf("%8s ", formatted)
}