-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarket_demo.go
executable file
·225 lines (187 loc) · 4.74 KB
/
market_demo.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
package main
import (
"os"
"os/signal"
"syscall"
"fmt"
"encoding/json"
"time"
"net/http"
"io/ioutil"
)
type CBDepthJson struct{
Status string // ok, error
Timestamp int64 //
Symbol string //
OrderBook OderBookInfo
}
type OderBookInfo struct{
Asks []Book
Bids []Book
}
type Book struct{
Price float64
Quantity float64
}
type CBTradesJson struct{
Status string // ok, error
Timestamp int64 //
Symbol string //
Trades []CBTrade
}
type CBTrade struct{
TradeId string
Price string
Quantity string
Take string // buy, sell 主动买入 / 主动卖出
Time int64
}
type CBTickerJson struct{
Status string // ok, error
Timestamp int64 //
Ticker []CBTicker
}
type CBTicker struct{
Symbol string// true
Last string// true
Bid string// true 买一价
Ask string// true 卖一价
High24hr string `json:"24hrHigh"`// true
Low24hr string `json:"24hrLow"`// true
Vol24hr string `json:"24hrVol"`// true
Amt24hr string `json:"24hrAmt"`// true
}
type SymbolInfo struct{
Symbol string
Depth string
Size string
}
var (
HttpUrl="http://api.coinbene.com/v1/market/"
)
func main() {
testSymbol:=SymbolInfo{Symbol:"btcusdt",Depth:"20",Size:"10",}
// 获取挂单
go GoGetOrderBook(testSymbol)
// 获取成交记录
go GoGetTrades(testSymbol)
// 获取最新价
go GoGetTicker(testSymbol)
chSig := make(chan os.Signal)
signal.Notify(chSig, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("Signal: ", <-chSig)
}
func GoGetOrderBook(symbol SymbolInfo) {
for{
time.Sleep(time.Millisecond * 1000)
url := HttpUrl+"orderbook?symbol=" + symbol.Symbol+"&depth=" + symbol.Depth
// fmt.Println(url)
client := &http.Client{}
reqest, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("GoGetOrderBook http.NewRequest err:",err)
continue
}
//处理返回结果
response, err1 := client.Do(reqest)
if err1 != nil {
fmt.Println("GoGetOrderBook client.Do err:",err1)
continue
}
defer response.Body.Close()
body, err2 := ioutil.ReadAll(response.Body)
if err2 != nil {
fmt.Println("GoGetOrderBook ioutil.ReadAll err", err2)
continue
}
// fmt.Println(string(body))
var getresult CBDepthJson
jsonerr := json.Unmarshal(body, &getresult)
if jsonerr != nil {
fmt.Println("GoGetOrderBook result jsonerr:", jsonerr)
continue
} else {
if getresult.Status == "ok" {
fmt.Println("GoGetOrderBook ",getresult)
}else{
fmt.Println("GoGetOrderBook status err:", getresult.Status)
}
}
}
}
func GoGetTrades(symbol SymbolInfo) {
for{
time.Sleep(time.Millisecond * 1000)
url := HttpUrl+"trades?symbol=" + symbol.Symbol+"&size="+symbol.Size
// fmt.Println(url)
client := &http.Client{}
reqest, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("GoGetTrades http.NewRequest err:",err)
continue
}
//处理返回结果
response, err1 := client.Do(reqest)
if err1 != nil {
fmt.Println("GoGetTrades client.Do err:",err1)
continue
}
defer response.Body.Close()
body, err2 := ioutil.ReadAll(response.Body)
if err2 != nil {
fmt.Println("GoGetTrades ioutil.ReadAll err", err2)
continue
}
// fmt.Println(string(body))
var getresult CBTradesJson
jsonerr := json.Unmarshal(body, &getresult)
if jsonerr != nil {
fmt.Println("GoGetTrades result jsonerr:", jsonerr)
continue
} else {
if getresult.Status == "ok" {
fmt.Println("GoGetTrades ",getresult)
}else{
fmt.Println("GoGetTrades status err:", getresult.Status)
}
}
}
}
func GoGetTicker(symbol SymbolInfo) {
for{
time.Sleep(time.Millisecond * 1000)
url := HttpUrl+"ticker?symbol=" + symbol.Symbol
// fmt.Println(url)
client := &http.Client{}
reqest, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("GoGetTicker http.NewRequest err:",err)
continue
}
//处理返回结果
response, err1 := client.Do(reqest)
if err1 != nil {
fmt.Println("GoGetTicker client.Do err:",err1)
continue
}
defer response.Body.Close()
body, err2 := ioutil.ReadAll(response.Body)
if err2 != nil {
fmt.Println("GoGetTicker ioutil.ReadAll err", err2)
continue
}
var getresult CBTickerJson
jsonerr := json.Unmarshal(body, &getresult)
if jsonerr != nil {
fmt.Println("GoGetTicker result jsonerr:", jsonerr)
fmt.Println(string(body))
continue
} else {
if getresult.Status == "ok" {
fmt.Println("GoGetTicker ",getresult)
}else{
fmt.Println("GoGetTicker status err:", getresult.Status)
}
}
}
}