-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
v5_ws_private_order.go
129 lines (120 loc) · 4.31 KB
/
v5_ws_private_order.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
package bybit
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
)
// SubscribeOrder :
func (s *V5WebsocketPrivateService) SubscribeOrder(
f func(V5WebsocketPrivateOrderResponse) error,
) (func() error, error) {
key := V5WebsocketPrivateParamKey{
Topic: V5WebsocketPrivateTopicOrder,
}
if err := s.addParamOrderFunc(key, f); err != nil {
return nil, err
}
param := struct {
Op string `json:"op"`
Args []interface{} `json:"args"`
}{
Op: "subscribe",
Args: []interface{}{V5WebsocketPrivateTopicOrder},
}
buf, err := json.Marshal(param)
if err != nil {
return nil, err
}
if err := s.writeMessage(websocket.TextMessage, buf); err != nil {
return nil, err
}
return func() error {
param := struct {
Op string `json:"op"`
Args []interface{} `json:"args"`
}{
Op: "unsubscribe",
Args: []interface{}{V5WebsocketPrivateTopicOrder},
}
buf, err := json.Marshal(param)
if err != nil {
return err
}
if err := s.writeMessage(websocket.TextMessage, []byte(buf)); err != nil {
return err
}
s.removeParamOrderFunc(key)
return nil
}, nil
}
// V5WebsocketPrivateOrderResponse :
type V5WebsocketPrivateOrderResponse struct {
ID string `json:"id"`
Topic V5WebsocketPrivateTopic `json:"topic"`
CreationTime int64 `json:"creationTime"`
Data []V5WebsocketPrivateOrderData `json:"data"`
}
// V5WebsocketPrivateOrderData :
type V5WebsocketPrivateOrderData struct {
AvgPrice string `json:"avgPrice"`
BlockTradeID string `json:"blockTradeId"`
CancelType string `json:"cancelType"`
Category string `json:"category"`
CloseOnTrigger bool `json:"closeOnTrigger"`
CreatedTime string `json:"createdTime"`
CumExecFee string `json:"cumExecFee"`
CumExecQty string `json:"cumExecQty"`
CumExecValue string `json:"cumExecValue"`
LeavesQty string `json:"leavesQty"`
LeavesValue string `json:"leavesValue"`
OrderID string `json:"orderId"`
OrderIv string `json:"orderIv"`
IsLeverage string `json:"isLeverage"`
LastPriceOnCreated string `json:"lastPriceOnCreated"`
OrderStatus OrderStatus `json:"orderStatus"`
OrderLinkID string `json:"orderLinkId"`
OrderType OrderType `json:"orderType"`
PositionIdx int `json:"positionIdx"`
Price string `json:"price"`
Qty string `json:"qty"`
ReduceOnly bool `json:"reduceOnly"`
RejectReason string `json:"rejectReason"`
Side Side `json:"side"`
SlTriggerBy TriggerBy `json:"slTriggerBy"`
StopLoss string `json:"stopLoss"`
StopOrderType string `json:"stopOrderType"`
Symbol SymbolV5 `json:"symbol"`
TakeProfit string `json:"takeProfit"`
TimeInForce TimeInForce `json:"timeInForce"`
TpTriggerBy TriggerBy `json:"tpTriggerBy"`
TriggerBy TriggerBy `json:"triggerBy"`
TriggerDirection TriggerDirection `json:"triggerDirection"`
TriggerPrice string `json:"triggerPrice"`
UpdatedTime string `json:"updatedTime"`
}
// Key :
func (r *V5WebsocketPrivateOrderResponse) Key() V5WebsocketPrivateParamKey {
return V5WebsocketPrivateParamKey{
Topic: r.Topic,
}
}
// addParamOrderFunc :
func (s *V5WebsocketPrivateService) addParamOrderFunc(param V5WebsocketPrivateParamKey, f func(V5WebsocketPrivateOrderResponse) error) error {
if _, exist := s.paramOrderMap[param]; exist {
return errors.New("already registered for this param")
}
s.paramOrderMap[param] = f
return nil
}
// removeParamOrderFunc :
func (s *V5WebsocketPrivateService) removeParamOrderFunc(key V5WebsocketPrivateParamKey) {
delete(s.paramOrderMap, key)
}
// retrieveOrderFunc :
func (s *V5WebsocketPrivateService) retrieveOrderFunc(key V5WebsocketPrivateParamKey) (func(V5WebsocketPrivateOrderResponse) error, error) {
f, exist := s.paramOrderMap[key]
if !exist {
return nil, errors.New("func not found")
}
return f, nil
}