forked from amir-the-h/okex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.go
557 lines (525 loc) · 15.9 KB
/
public.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package ws
import (
"encoding/json"
"fmt"
"github.com/amir-the-h/okex"
"github.com/amir-the-h/okex/events"
"github.com/amir-the-h/okex/events/public"
requests "github.com/amir-the-h/okex/requests/ws/public"
"strings"
)
// Public
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels
type Public struct {
*ClientWs
iCh chan *public.Instruments
tCh chan *public.Tickers
oiCh chan *public.OpenInterest
cCh chan *public.Candlesticks
trCh chan *public.Trades
edepCh chan *public.EstimatedDeliveryExercisePrice
mpCh chan *public.MarkPrice
mpcCh chan *public.MarkPriceCandlesticks
plCh chan *public.PriceLimit
obCh chan *public.OrderBook
osCh chan *public.OPTIONSummary
frCh chan *public.FundingRate
icCh chan *public.IndexCandlesticks
itCh chan *public.IndexTickers
}
// NewPublic returns a pointer to a fresh Public
func NewPublic(c *ClientWs) *Public {
return &Public{ClientWs: c}
}
// Instruments
// The full instrument list will be pushed for the first time after subscription. Subsequently, the instruments will be pushed if there's any change to the instrument’s state (such as delivery of FUTURES, exercise of OPTION, listing of new contracts / trading pairs, trading suspension, etc.).
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-instruments-channel
func (c *Public) Instruments(req requests.Instruments, ch ...chan *public.Instruments) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.iCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"instruments"}, m)
}
// UInstruments
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-instruments-channel
func (c *Public) UInstruments(req requests.Instruments, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.iCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"instruments"}, m)
}
// Tickers
// Retrieve the last traded price, bid price, ask price and 24-hour trading volume of instruments. Data will be pushed every 100 ms.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-tickers-channel
func (c *Public) Tickers(req requests.Tickers, ch ...chan *public.Tickers) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.tCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"tickers"}, m)
}
// UTickers
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-tickers-channel
func (c *Public) UTickers(req requests.Tickers, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.tCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"tickers"}, m)
}
// OpenInterest
// Retrieve the open interest. Data will be pushed every 3 seconds.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-open-interest-channel
func (c *Public) OpenInterest(req requests.OpenInterest, ch ...chan *public.OpenInterest) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.oiCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"open-interest"}, m)
}
// UOpenInterest
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-open-interest-channel
func (c *Public) UOpenInterest(req requests.OpenInterest, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.oiCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"open-interest"}, m)
}
// Candlesticks
// Retrieve the open interest. Data will be pushed every 3 seconds.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-candlesticks-channel
func (c *Public) Candlesticks(req requests.Candlesticks, ch ...chan *public.Candlesticks) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.cCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{}, m)
}
// UCandlesticks
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-candlesticks-channel
func (c *Public) UCandlesticks(req requests.Candlesticks, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.cCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{}, m)
}
// Trades
// Retrieve the recent trades data. Data will be pushed whenever there is a trade.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-trades-channel
func (c *Public) Trades(req requests.Trades, ch ...chan *public.Trades) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.trCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"trades"}, m)
}
// UTrades
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-trades-channel
func (c *Public) UTrades(req requests.Trades, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.trCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"trades"}, m)
}
// EstimatedDeliveryExercisePrice
// Retrieve the estimated delivery/exercise price of FUTURES contracts and OPTION.
//
// Only the estimated delivery/exercise price will be pushed an hour before delivery/exercise, and will be pushed if there is any price change.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-estimated-delivery-exercise-price-channel
func (c *Public) EstimatedDeliveryExercisePrice(req requests.EstimatedDeliveryExercisePrice, ch ...chan *public.EstimatedDeliveryExercisePrice) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.edepCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"estimated-price"}, m)
}
// UEstimatedDeliveryExercisePrice
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-estimated-delivery-exercise-price-channel
func (c *Public) UEstimatedDeliveryExercisePrice(req requests.EstimatedDeliveryExercisePrice, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.edepCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"estimated-price"}, m)
}
// MarkPrice
// Retrieve the mark price. Data will be pushed every 200 ms when the mark price changes, and will be pushed every 10 seconds when the mark price does not change.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-mark-price-channel
func (c *Public) MarkPrice(req requests.MarkPrice, ch ...chan *public.MarkPrice) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.mpCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"mark-price"}, m)
}
// UMarkPrice
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-mark-price-channel
func (c *Public) UMarkPrice(req requests.MarkPrice, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.mpCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"mark-price"}, m)
}
// MarkPriceCandlesticks
// Retrieve the candlesticks data of the mark price. Data will be pushed every 500 ms.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-mark-price-candlesticks-channel
func (c *Public) MarkPriceCandlesticks(req requests.MarkPriceCandlesticks, ch ...chan *public.MarkPriceCandlesticks) error {
m := okex.S2M(req)
m["channel"] = "mark-price-" + m["channel"]
if len(ch) > 0 {
c.mpcCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{}, m)
}
// UMarkPriceCandlesticks
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-mark-price-candlesticks-channel
func (c *Public) UMarkPriceCandlesticks(req requests.MarkPriceCandlesticks, rCh ...bool) error {
m := okex.S2M(req)
m["channel"] = "mark-price-" + m["channel"]
if len(rCh) > 0 && rCh[0] {
c.mpcCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{}, m)
}
// PriceLimit
// Retrieve the maximum buy price and minimum sell price of the instrument. Data will be pushed every 5 seconds when there are changes in limits, and will not be pushed when there is no changes on limit.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-price-limit-channel
func (c *Public) PriceLimit(req requests.PriceLimit, ch ...chan *public.PriceLimit) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.plCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"price-limit"}, m)
}
// UPriceLimit
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-price-limit-channel
func (c *Public) UPriceLimit(req requests.PriceLimit, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.plCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"price-limit"}, m)
}
// OrderBook
// Retrieve order book data for multiple instruments.
//
// Use books for 400 depth levels, book5 for 5 depth levels, books50-l2-tbt tick-by-tick 50 depth levels, and books-l2-tbt for tick-by-tick 400 depth levels.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-order-book-channel
func (c *Public) OrderBook(reqs []requests.OrderBook, ch ...chan *public.OrderBook) error {
if len(ch) > 0 {
c.obCh = ch[0]
}
var subscriptions []map[string]string
for _, req := range reqs {
m := okex.S2M(req)
subscriptions = append(subscriptions, m)
}
return c.Subscribe(false, []okex.ChannelName{}, subscriptions...)
}
// UOrderBook
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-order-book-channel
func (c *Public) UOrderBook(req requests.OrderBook, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.obCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{okex.ChannelName(req.Channel)}, m)
}
// OPTIONSummary
// Retrieve detailed pricing information of all OPTION contracts. Data will be pushed at once.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-option-summary-channel
func (c *Public) OPTIONSummary(req requests.OPTIONSummary, ch ...chan *public.OPTIONSummary) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.osCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"opt-summary"}, m)
}
// UOPTIONSummary
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-option-summary-channel
func (c *Public) UOPTIONSummary(req requests.OPTIONSummary, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.osCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"opt-summary"}, m)
}
// FundingRate
// Retrieve funding rate. Data will be pushed every minute.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-funding-rate-channel
func (c *Public) FundingRate(req requests.FundingRate, ch ...chan *public.FundingRate) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.frCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"funding-rate"}, m)
}
// UFundingRate
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-funding-rate-channel
func (c *Public) UFundingRate(req requests.FundingRate, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.frCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"funding-rate"}, m)
}
// IndexCandlesticks
// Retrieve the candlesticks data of the index. Data will be pushed every 500 ms.
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-index-candlesticks-channel
func (c *Public) IndexCandlesticks(req requests.IndexCandlesticks, ch ...chan *public.IndexCandlesticks) error {
m := okex.S2M(req)
m["channel"] = req.Channel
if len(ch) > 0 {
c.icCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{}, m)
}
// UIndexCandlesticks
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-index-candlesticks-channel
func (c *Public) UIndexCandlesticks(req requests.IndexCandlesticks, rCh ...bool) error {
m := okex.S2M(req)
m["channel"] = req.Channel
if len(rCh) > 0 && rCh[0] {
c.icCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{}, m)
}
// IndexTickers
// Retrieve index tickers data
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-index-tickers-channel
func (c *Public) IndexTickers(req requests.IndexTickers, ch ...chan *public.IndexTickers) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.itCh = ch[0]
}
return c.Subscribe(false, []okex.ChannelName{"index-tickers"}, m)
}
// UIndexTickers
//
// https://www.okex.com/docs-v5/en/#websocket-api-public-channels-index-tickers-channel
func (c *Public) UIndexTickers(req requests.IndexTickers, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.itCh = nil
}
return c.Unsubscribe(false, []okex.ChannelName{"index-tickers"}, m)
}
func (c *Public) Process(data []byte, e *events.Basic) bool {
if e.Event == "" && e.Arg != nil && e.Data != nil && len(e.Data) > 0 {
ch, ok := e.Arg.Get("channel")
if !ok {
return false
}
switch ch {
case "instruments":
e := public.Instruments{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.iCh != nil {
c.iCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "tickers":
e := public.Tickers{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.tCh != nil {
c.tCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "open-interest":
e := public.OpenInterest{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.oiCh != nil {
c.oiCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "trades":
e := public.Trades{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.trCh != nil {
c.trCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "estimated-price":
e := public.EstimatedDeliveryExercisePrice{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.edepCh != nil {
c.edepCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "mark-price":
e := public.MarkPrice{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.mpCh != nil {
c.mpCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "price-limit":
e := public.PriceLimit{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.plCh != nil {
c.plCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "opt-summary":
e := public.OPTIONSummary{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.osCh != nil {
c.osCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "funding-rate":
e := public.FundingRate{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.frCh != nil {
c.frCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
case "index-tickers":
e := public.IndexTickers{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.itCh != nil {
c.itCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
default:
chName := fmt.Sprint(ch)
if strings.Contains(chName, "mark-price-candle") {
e := public.MarkPriceCandlesticks{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.mpcCh != nil {
c.mpcCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
}
if strings.Contains(chName, "index-candle") {
e := public.IndexCandlesticks{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.icCh != nil {
c.icCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
}
if strings.Contains(chName, "candle") {
e := public.Candlesticks{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.cCh != nil {
c.cCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
}
if strings.Contains(chName, "books") {
e := public.OrderBook{}
if err := json.Unmarshal(data, &e); err != nil {
return false
}
if c.obCh != nil {
c.obCh <- &e
}
if c.StructuredEventChan != nil {
c.StructuredEventChan <- e
}
return true
}
}
}
return false
}