-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (133 loc) · 3.71 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
package main
import (
"crypto-scrope/app"
"fmt"
"log"
"log/slog"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/hajimehoshi/ebiten/v2"
"github.com/valyala/fastjson"
"golang.org/x/exp/slices"
)
var symbols = []string{
"btcusdt",
}
const wsBaseUrl = "wss://stream.binance.com:9443"
func main() {
go func() {
dialer := websocket.DefaultDialer
conn, _, err := dialer.Dial(getWsSubscriptionStreamUrl(), nil)
if err != nil {
panic(err)
}
for {
_, bytes, err := conn.ReadMessage()
if err != nil {
fmt.Printf("error: %v\n", err)
}
var p fastjson.Parser
v, _ := p.ParseBytes(bytes)
stream := string(v.GetStringBytes("stream"))
if strings.Contains(stream, "depth") {
data := v.Get("data")
bids := data.GetArray("bids")
asks := data.GetArray("asks")
var bidSlice []app.OrderBookData
var askSlice []app.OrderBookData
for _, v := range bids {
price, err := strconv.ParseFloat(string(v.GetStringBytes("0")), 10)
if err != nil {
fmt.Println("error converting price to float")
break
}
quantity, err := strconv.ParseFloat(string(v.GetStringBytes("1")), 10)
if err != nil {
fmt.Println("error converting quantity to float")
break
}
obData := app.OrderBookData{
Price: price,
Quantity: quantity,
Sum: price * quantity,
}
bidSlice = append(bidSlice, obData)
}
for _, v := range asks {
price, err := strconv.ParseFloat(string(v.GetStringBytes("0")), 10)
if err != nil {
fmt.Println("error converting price to float")
break
}
quantity, err := strconv.ParseFloat(string(v.GetStringBytes("1")), 10)
if err != nil {
fmt.Println("error converting quantity to float")
break
}
obData := app.OrderBookData{
Price: price,
Quantity: quantity,
Sum: price * quantity,
}
askSlice = append(askSlice, obData)
}
app.Ob.Bids = bidSlice
app.Ob.Asks = askSlice
}
if strings.Contains(stream, "aggTrade") {
data := v.Get("data")
eventTime := data.GetInt64("T")
price, err := strconv.ParseFloat(string(data.GetStringBytes("p")), 64)
if err != nil {
slog.Error("error converting price to float")
}
time := time.Unix(eventTime/1000, 0)
quantity, err := strconv.ParseFloat(string(data.GetStringBytes("q")), 64)
if err != nil {
slog.Error("error converting quantity to float")
}
if len(app.MarketTrades) < 15 {
app.MarketTrades = append(app.MarketTrades, app.MarketTrade{
Price: price,
Quantity: quantity,
Time: time,
})
} else {
slices.Delete(app.MarketTrades, 0, 1)
slices.Insert(app.MarketTrades, 14, app.MarketTrade{
Price: price,
Quantity: quantity,
Time: time,
})
}
fmt.Printf("eventTime: %s, price: %f, quantity: %f\n", time.String(), price, quantity)
fmt.Printf("market trades: %v\n", len(app.MarketTrades))
}
}
}()
err := run()
if err != nil {
log.Fatalf("could not run the application: %v", err)
}
}
func run() error {
setupWindow()
application := app.New()
return ebiten.RunGame(application)
}
func setupWindow() {
w, h := ebiten.Monitor().Size()
ebiten.SetWindowSize(w, h)
ebiten.SetWindowTitle("crypto scope v0.0.1")
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
}
func getWsSubscriptionStreamUrl() string {
var streamNames []string
for _, symbol := range symbols {
streamNames = append(streamNames, fmt.Sprintf("%s@depth20@100ms", symbol))
streamNames = append(streamNames, fmt.Sprintf("%s@aggTrade", symbol))
}
return fmt.Sprintf("%s/stream?streams=%s", wsBaseUrl, strings.Join(streamNames, "/"))
}