-
Notifications
You must be signed in to change notification settings - Fork 1
/
kline.go
156 lines (140 loc) · 2.91 KB
/
kline.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
151
152
153
154
155
156
package kline
import (
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
candleHalfShadowUp = "╷"
candleShadow = "│"
candleCross = "┼"
candleHalfShadowDown = "╵"
candleHalfBodyUp = "▄"
candleBody = "█"
candleHalfBodyDown = "▀"
)
type Kline struct {
Open float64
High float64
Low float64
Close float64
CloseTime time.Time
}
func New(data []Kline, width, height int) Model {
return Model{
Klines: data,
Width: width,
Height: height,
}
}
type Model struct {
Width int
Height int
Klines []Kline
maxPrice float64
minPrice float64
pricePerBlock float64
visibleIndex int
XOffset int
}
func (m *Model) SetSize(width, height int) {
m.Width = width
m.Height = height
m.calculate()
}
func (m *Model) calculate() {
var maxPrice float64
var minPrice float64
m.visibleIndex = len(m.Klines) - m.Width
m.visibleIndex -= m.XOffset
if m.visibleIndex < 0 {
m.visibleIndex = 0
}
for i, c := range m.Klines[m.visibleIndex : len(m.Klines)-m.XOffset] {
if i == 0 {
minPrice = c.Low
}
if c.High > maxPrice {
maxPrice = c.High
}
if c.Low < minPrice {
minPrice = c.Low
}
}
m.maxPrice = maxPrice
m.minPrice = minPrice
priceDelta := maxPrice - minPrice
m.pricePerBlock = priceDelta / float64(m.Height)
}
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.MouseMsg:
switch msg.Type {
case tea.MouseWheelDown:
m.XOffset++
if m.XOffset > len(m.Klines)-m.Width {
m.XOffset = len(m.Klines) - m.Width
}
m.calculate()
return m, nil
case tea.MouseWheelUp:
m.XOffset--
if m.XOffset < 0 {
m.XOffset = 0
}
m.calculate()
return m, nil
}
}
return m, nil
}
func (m Model) View() string {
var s []string
for _, c := range m.Klines[m.visibleIndex : len(m.Klines)-m.XOffset] {
if c.Open <= c.Close {
s = append(s, m.renderCandle(c, true))
} else {
c.Close, c.Open = c.Open, c.Close
s = append(s, m.renderCandle(c, false))
}
}
body := lipgloss.JoinHorizontal(lipgloss.Top, s...)
return body
}
func (m Model) renderCandle(c Kline, green bool) string {
var (
candle string
paddingTop int
)
for i := m.Height; i > 0; i-- {
j := (float64(i) * m.pricePerBlock) + m.minPrice
if j > c.High {
paddingTop++
continue
}
if (j > c.Close && ((float64(i-1))/m.pricePerBlock) > c.Open) || (j > c.Close && c.Open != c.Close) {
candle += candleShadow
continue
}
if j > c.Open {
if c.Open == c.Close {
candle += candleCross
} else {
candle += candleBody
}
continue
}
if j > c.Low {
candle += candleShadow
continue
}
}
style := lipgloss.NewStyle().
Width(1).
PaddingTop(paddingTop).
Foreground(lipgloss.Color("#008000"))
if !green {
style = style.Foreground(lipgloss.Color("#FF0000"))
}
return style.Render(candle)
}