forked from amir-the-h/okex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
private.go
181 lines (169 loc) · 5.03 KB
/
private.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
package ws
import (
"encoding/json"
"github.com/amir-the-h/okex"
"github.com/amir-the-h/okex/events"
"github.com/amir-the-h/okex/events/private"
requests "github.com/amir-the-h/okex/requests/ws/private"
)
// Private
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel
type Private struct {
*ClientWs
aCh chan *private.Account
pCh chan *private.Position
bnpCh chan *private.BalanceAndPosition
oCh chan *private.Order
}
// NewPrivate returns a pointer to a fresh Private
func NewPrivate(c *ClientWs) *Private {
return &Private{ClientWs: c}
}
// Account
// Retrieve account information. Data will be pushed when triggered by events such as placing/canceling order, and will also be pushed in regular interval according to subscription granularity.
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-account-channel
func (c *Private) Account(req requests.Account, ch ...chan *private.Account) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.aCh = ch[0]
}
return c.Subscribe(true, []okex.ChannelName{"account"}, m)
}
// UAccount
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-account-channel
func (c *Private) UAccount(req requests.Account, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.aCh = nil
}
return c.Unsubscribe(true, []okex.ChannelName{"account"}, m)
}
// Position
// Retrieve position information. Initial snapshot will be pushed according to subscription granularity. Data will be pushed when triggered by events such as placing/canceling order, and will also be pushed in regular interval according to subscription granularity.
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-positions-channel
func (c *Private) Position(req requests.Position, ch ...chan *private.Position) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.pCh = ch[0]
}
return c.Subscribe(true, []okex.ChannelName{"positions"}, m)
}
// UPosition
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-positions-channel
func (c *Private) UPosition(req requests.Position, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.pCh = nil
}
return c.Unsubscribe(true, []okex.ChannelName{"positions"}, m)
}
// BalanceAndPosition
// Retrieve account balance and position information. Data will be pushed when triggered by events such as filled order, funding transfer.
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-balance-and-position-channel
func (c *Private) BalanceAndPosition(ch ...chan *private.BalanceAndPosition) error {
m := make(map[string]string)
if len(ch) > 0 {
c.bnpCh = ch[0]
}
return c.Subscribe(true, []okex.ChannelName{"balance_and_position"}, m)
}
// UBalanceAndPosition unsubscribes a position channel
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-balance-and-position-channel
func (c *Private) UBalanceAndPosition(rCh ...bool) error {
m := make(map[string]string)
if len(rCh) > 0 && rCh[0] {
c.bnpCh = nil
}
return c.Unsubscribe(true, []okex.ChannelName{"balance_and_position"}, m)
}
// Order
// Retrieve position information. Initial snapshot will be pushed according to subscription granularity. Data will be pushed when triggered by events such as placing/canceling order, and will also be pushed in regular interval according to subscription granularity.
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-order-channel
func (c *Private) Order(req requests.Order, ch ...chan *private.Order) error {
m := okex.S2M(req)
if len(ch) > 0 {
c.oCh = ch[0]
}
return c.Subscribe(true, []okex.ChannelName{"orders"}, m)
}
// UOrder
//
// https://www.okex.com/docs-v5/en/#websocket-api-private-channel-order-channel
func (c *Private) UOrder(req requests.Order, rCh ...bool) error {
m := okex.S2M(req)
if len(rCh) > 0 && rCh[0] {
c.oCh = nil
}
return c.Unsubscribe(true, []okex.ChannelName{"orders"}, m)
}
func (c *Private) 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 "account":
e := private.Account{}
err := json.Unmarshal(data, &e)
if err != nil {
return false
}
go func() {
if c.aCh != nil {
c.aCh <- &e
}
c.StructuredEventChan <- e
}()
return true
case "positions":
e := private.Position{}
err := json.Unmarshal(data, &e)
if err != nil {
return false
}
go func() {
if c.pCh != nil {
c.pCh <- &e
}
c.StructuredEventChan <- e
}()
return true
case "balance_and_position":
e := private.BalanceAndPosition{}
err := json.Unmarshal(data, &e)
if err != nil {
return false
}
go func() {
if c.bnpCh != nil {
c.bnpCh <- &e
}
c.StructuredEventChan <- e
}()
return true
case "orders":
e := private.Order{}
err := json.Unmarshal(data, &e)
if err != nil {
return false
}
go func() {
if c.oCh != nil {
c.oCh <- &e
}
c.StructuredEventChan <- e
}()
return true
}
}
return false
}