-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
361 lines (303 loc) · 8.88 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"math"
"strconv"
"strings"
"time"
ws "github.com/gorilla/websocket"
termbox "github.com/nsf/termbox-go"
gdax "github.com/preichenberger/go-gdax"
)
var crypto = flag.String(
"crypto", "BTC", "Crypto to show. Supports BTC, ETH, LTC, and BCH.")
var fiat = flag.String(
"fiat", "USD", "Fiat currency to show. Supports USD, EUR, GBP (USD only for BCH).")
var candleSize = flag.Duration(
"candle_size", 15*time.Minute, "Candlestick window size.")
var volumeWidth = flag.Int("volume_width", 11, "Width of the volume pane.")
var tradeWidth = flag.Int("trade_width", 14, "Width of the trade pane.")
func print_tb(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func printf_tb(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
print_tb(x, y, fg, bg, s)
}
func DrawBox(x, y, w, h int, fg, bg termbox.Attribute) {
for i := 0; i < h; i++ {
termbox.SetCell(x, y+i, '│', fg, bg)
termbox.SetCell(x+w, y+i, '│', fg, bg)
}
for i := 0; i < w; i++ {
termbox.SetCell(x+i, y, '─', fg, bg)
termbox.SetCell(x+i, y+h, '─', fg, bg)
}
termbox.SetCell(x, y, '┌', fg, bg)
termbox.SetCell(x, y+h, '└', fg, bg)
termbox.SetCell(x+w, y, '┐', fg, bg)
termbox.SetCell(x+w, y+h, '┘', fg, bg)
}
func shortDuration(d time.Duration) string {
s := d.String()
if strings.HasSuffix(s, "m0s") {
s = s[:len(s)-2]
}
if strings.HasSuffix(s, "h0m") {
s = s[:len(s)-2]
}
return s
}
var connectionErrCount int = 0
func Connect(out chan gdax.Message) {
var wsDialer ws.Dialer
wsConn, _, err := wsDialer.Dial("wss://ws-feed.gdax.com", nil)
if err != nil {
println(err.Error())
}
subscribe := gdax.Message{
Type: "subscribe",
Channels: []gdax.MessageChannel{
{
Name: "ticker",
ProductIds: []string{
fmt.Sprintf("%s-%s", *crypto, *fiat),
},
},
},
}
if err := wsConn.WriteJSON(subscribe); err != nil {
println(err.Error())
}
message := gdax.Message{}
go func() {
for true {
if err := wsConn.ReadJSON(&message); err != nil {
connectionErrCount++
time.Sleep(time.Duration(connectionErrCount*connectionErrCount) * time.Second)
Connect(out)
break
}
// It seems that the first two messages that come back are always missing
// their side and have broken timestamps. Skip them.
if len(message.Side) == 0 {
continue
}
connectionErrCount = 0
out <- message
}
}()
}
func GetMessages() chan gdax.Message {
out := make(chan gdax.Message)
Connect(out)
return out
}
func GetEvents() chan termbox.Event {
out := make(chan termbox.Event)
go func() {
for {
out <- termbox.PollEvent()
}
}()
return out
}
type Bucket struct {
Open float64
Close float64
Max float64
Min float64
Trades int64
Start time.Time
Duration time.Duration
}
type Frame struct {
x, y, w, h int
}
func (f *Frame) Clear() {
for y := 0; y < f.h; y++ {
for x := 0; x < f.x; x++ {
f.SetCell(x, y, ' ', termbox.ColorDefault, termbox.ColorDefault)
}
}
}
func (f *Frame) SetCell(x, y int, r rune, fg, bg termbox.Attribute) {
termbox.SetCell(f.x+x, f.y+y, r, fg, bg)
}
func (f *Frame) Box(fg, bg termbox.Attribute) {
DrawBox(f.x, f.y, f.w, f.h, fg, bg)
}
func (f *Frame) Printf(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
f.Print(x, y, fg, bg, s)
}
func (f *Frame) Print(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
f.SetCell(x, y, c, fg, bg)
x++
}
}
func (f *Frame) PrintHeader(msg string, fg, bg termbox.Attribute) {
f.Print(int(math.Ceil(float64(f.w)/2.0-float64(len(msg))/2.0)), 0, fg, bg, msg)
}
func DrawTrades(tradeFrame Frame, trades []gdax.Message) {
tradeFrame.Box(termbox.ColorWhite, termbox.ColorDefault)
for i, j := 1, len(trades)-1; j >= 0 && i < tradeFrame.h; i, j = i+1, j-1 {
message := trades[j]
fg := termbox.ColorGreen
bg := termbox.ColorDefault
if message.Side == "sell" {
fg = termbox.ColorRed
}
fg |= termbox.AttrBold
price, _ := strconv.ParseFloat(message.Price, 64)
tradeFrame.Printf(1, i, fg, bg, "%-4s %.2f", message.Side, price)
}
tradeFrame.PrintHeader("Trades", termbox.ColorWhite, termbox.ColorDefault)
}
func DrawCandles(candleFrame Frame, buckets []*Bucket) {
candleFrame.Box(termbox.ColorWhite, termbox.ColorDefault)
candleFrame.PrintHeader("Price", termbox.ColorWhite, termbox.ColorDefault)
lowerBound, upperBound := math.MaxFloat32, 0.0
for _, box := range buckets {
lowerBound = math.Min(lowerBound, box.Min)
upperBound = math.Max(upperBound, box.Max)
}
if upperBound-lowerBound < 100 {
lowerBound -= 50
upperBound += 50
}
priceSpread := upperBound - lowerBound
for line, j := 1, len(buckets)-1; j >= 0 && line < candleFrame.h; line, j = line+1, j-1 {
bucket := buckets[j]
fg := termbox.ColorGreen
bg := termbox.ColorDefault
// Draw the legend.
candleFrame.Printf(1, 0, fg, bg, "%.2f", lowerBound)
s := fmt.Sprintf("%.2f", upperBound)
candleFrame.Print(candleFrame.w-len(s), 0, fg, bg, s)
if bucket.Close < bucket.Open {
fg = termbox.ColorRed
}
// Draw the bounds.
start := 1 + (math.Min(bucket.Min, bucket.Max)-lowerBound)/priceSpread*float64(candleFrame.w-2)
stop := 1 + (math.Max(bucket.Min, bucket.Max)-lowerBound)/priceSpread*float64(candleFrame.w-2)
for i := int(start); i <= int(stop); i++ {
candleFrame.SetCell(i, line, '─', fg, bg)
}
// Then we draw the open/close.
start = 1 + (math.Min(bucket.Open, bucket.Close)-lowerBound)/priceSpread*float64(candleFrame.w-2)
stop = 1 + (math.Max(bucket.Open, bucket.Close)-lowerBound)/priceSpread*float64(candleFrame.w-2)
for i := int(start); i <= int(stop); i++ {
candleFrame.SetCell(i, line, '█', fg, bg)
}
}
}
func DrawVolume(volumeFrame Frame, buckets []*Bucket) {
volumeFrame.Box(termbox.ColorWhite, termbox.ColorDefault)
volumeFrame.PrintHeader("Volume", termbox.ColorWhite, termbox.ColorDefault)
volumeUpperBound := 0.0
for _, box := range buckets {
volumeUpperBound = math.Max(volumeUpperBound, float64(box.Trades))
}
volumeSpread := volumeUpperBound
volumeMaxStr := fmt.Sprintf("%d", int(volumeUpperBound))
volumeFrame.Print(volumeFrame.w-len(volumeMaxStr), volumeFrame.h, termbox.ColorGreen, termbox.ColorDefault, volumeMaxStr)
for line, j := 1, len(buckets)-1; j >= 0 && line < volumeFrame.h; line, j = line+1, j-1 {
bucket := buckets[j]
fg := termbox.ColorBlue
bg := termbox.ColorDefault
start := 1
stop := float64(bucket.Trades) / volumeSpread * float64(volumeFrame.w-1)
for i := int(start); i <= int(stop); i++ {
volumeFrame.SetCell(i, line, '─', fg, bg)
}
}
}
func ProcessMessage(message gdax.Message, trades *[]gdax.Message, buckets *[]*Bucket) {
*trades = append(*trades, message)
t := message.Time.Time().Truncate(*candleSize)
price, err := strconv.ParseFloat(message.Price, 64)
if err != nil {
return
}
// If there are no buckets, start one.
if len(*buckets) == 0 {
*buckets = append(*buckets, &Bucket{
Open: price,
Close: price,
Start: t,
Min: math.MaxFloat32,
Max: 0.0,
Duration: *candleSize,
})
}
bucket := (*buckets)[len(*buckets)-1]
if (*buckets)[len(*buckets)-1].Start.Equal(t) {
bucket.Close = price
} else {
// Time to start a new bucket.
*buckets = append(*buckets, &Bucket{
Open: price,
Close: price,
Start: t,
Min: math.MaxFloat32,
Max: 0.0,
Duration: *candleSize,
})
bucket = (*buckets)[len(*buckets)-1]
}
bucket.Trades++
bucket.Max = math.Max(bucket.Max, price)
bucket.Min = math.Min(bucket.Min, price)
}
func Draw(trades []gdax.Message, buckets []*Bucket) {
width, height := termbox.Size()
candleWidth := width - (*volumeWidth + *tradeWidth + 3)
volumeFrame := Frame{0, 1, *volumeWidth, height - 2}
candleFrame := Frame{*volumeWidth + 1, 1, candleWidth, height - 2}
tradeFrame := Frame{*volumeWidth + candleWidth + 2, 1, *tradeWidth, height - 2}
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
printf_tb(0, 0, termbox.ColorWhite, termbox.ColorDefault,
"Crypto: %s Fiat: %s Exchange: GDAX Candle Size: %s",
strings.ToUpper(*crypto), strings.ToUpper(*fiat), shortDuration(*candleSize))
DrawTrades(tradeFrame, trades)
DrawCandles(candleFrame, buckets)
DrawVolume(volumeFrame, buckets)
termbox.Flush()
}
func main() {
flag.Parse()
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
messages := GetMessages()
events := GetEvents()
trades := []gdax.Message{}
buckets := []*Bucket{}
Draw(trades, buckets)
loop:
for {
select {
case message := <-messages:
ProcessMessage(message, &trades, &buckets)
Draw(trades, buckets)
case ev := <-events:
switch ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
break loop
}
case termbox.EventResize:
Draw(trades, buckets)
}
}
}
}