-
Notifications
You must be signed in to change notification settings - Fork 6
/
account_active.go
243 lines (211 loc) · 7.27 KB
/
account_active.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
// Active Orders (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-activeorders)
package iperpetual
import (
"encoding/json"
"fmt"
"time"
"github.com/ginarea/gobybit/bybitv2/transport"
)
type OrderMain struct {
UserID int `json:"user_id"`
Symbol string `json:"symbol"`
Side Side `json:"side"`
OrderType OrderType `json:"order_type"`
Price transport.Float64 `json:"price"`
Qty int `json:"qty"`
TimeInForce TimeInForce `json:"time_in_force"`
OrderStatus OrderStatus `json:"order_status"`
LeavesQty float64 `json:"leaves_qty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type OrderBase struct {
OrderMain
OrderID string `json:"order_id"`
OrderLinkID string `json:"order_link_id"`
CumExecQty transport.Float64 `json:"cum_exec_qty"`
CumExecValue transport.Float64 `json:"cum_exec_value"`
CumExecFee transport.Float64 `json:"cum_exec_fee"`
RejectReason string `json:"reject_reason"`
}
type OrderProfitLoss struct {
TakeProfit string `json:"take_profit"`
StopLoss string `json:"stop_loss"`
TpTrigger TriggerPrice `json:"tp_trigger_by"`
SlTrigger TriggerPrice `json:"sl_trigger_by"`
}
// Place Active Order (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-placeactive)
type PlaceActiveOrder struct {
Side Side `param:"side"`
Symbol string `param:"symbol"`
OrderType OrderType `param:"order_type"`
Qty float64 `param:"qty"`
TimeInForce TimeInForce `param:"time_in_force"`
Price *float64 `param:"price"`
CloseOnTrigger *bool `param:"close_on_trigger"`
OrderLinkID *string `param:"order_link_id"`
TakeProfit *float64 `param:"take_profit"`
StopLoss *float64 `param:"stop_loss"`
TpTrigger *TriggerPrice `param:"tp_trigger_by"`
SlTrigger *TriggerPrice `param:"sl_trigger_by"`
ReduceOnly *bool `param:"reduce_only"`
}
type OrderCreated struct {
OrderBase
OrderProfitLoss
LastExecTime transport.Float64 `json:"last_exec_time"`
LastExecPrice transport.Float64 `json:"last_exec_price"`
}
func (o PlaceActiveOrder) Do(client *Client) (OrderCreated, error) {
return Post[OrderCreated](client, "order/create", o)
}
func (o *Client) PlaceActiveOrder(v PlaceActiveOrder) (OrderCreated, error) {
return v.Do(o)
}
// Get Active Order (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-getactive)
type OrderList struct {
Symbol string `param:"symbol"`
OrderStatus *OrderStatus `param:"order_status"`
Direction *Direction `param:"direction"`
Limit *int `param:"limit"`
Cursor *string `param:"cursor"`
}
func (o OrderList) Do(client *Client) (OrderListResult, error) {
return Get[OrderListResult](client, "order/list", o)
}
type OrderListResult struct {
Items []OrderItem `json:"data"`
Cursor string `json:"cursor"`
}
type OrderItem struct {
OrderBase
OrderProfitLoss
LeavesValue string `json:"leaves_value"`
PositionIdx PositionIdx `json:"position_idx"`
}
func (o *Client) OrderList(v OrderList) (OrderListResult, error) {
return v.Do(o)
}
// Cancel Active Order (https://bybit-exchange.github.io/docs/futuresV2/inverse_futures/#t-cancelactive)
type CancelOrder struct {
Symbol string `param:"symbol"`
OrderId *string `param:"order_id"`
OrderLinkId *string `param:"order_link_id"`
}
func (o CancelOrder) Do(client *Client) (OrderCancelled, error) {
return Post[OrderCancelled](client, "order/cancel", o)
}
type OrderCancelled struct {
OrderCreated
}
func (o *Client) CancelOrder(v CancelOrder) (OrderCancelled, error) {
return v.Do(o)
}
// Cancel All Active Orders (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-cancelallactive)
type CancelAllOrders struct {
Symbol string `param:"symbol"`
}
func (o CancelAllOrders) Do(client *Client) ([]CancelOrderItem, error) {
return Post[[]CancelOrderItem](client, "order/cancelAll", o)
}
type CancelOrderItem struct {
OrderMain
OrderID string `json:"clOrdID"`
LeavesValue string `json:"leaves_value"`
CreateType CreateType `json:"create_type"`
CancelType CancelType `json:"cancel_type"`
CrossStatus OrderStatus `json:"cross_status"`
CrossSeq int `json:"cross_seq"`
OrderLinkID string `оыщт:"order_link_id"`
}
func (o *Client) CancelAllOrders(symbol string) ([]CancelOrderItem, error) {
return CancelAllOrders{Symbol: symbol}.Do(o)
}
// Replace Active Order (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-replaceactive)
type ReplaceOrder struct {
Symbol string `param:"symbol"`
OrderID *string `param:"order_id"`
OrderLinkID *string `param:"order_link_id"`
Qty *int `param:"p_r_qty"`
Price *string `param:"p_r_price"`
TakeProfit *float64 `param:"take_profit"`
StopLoss *float64 `param:"stop_loss"`
TpTrigger *TriggerPrice `param:"tp_trigger_by"`
SlTrigger *TriggerPrice `param:"sl_trigger_by"`
}
func (o ReplaceOrder) Do(client *Client) (string, error) {
type result struct {
OrderID string `json:"order_id"`
}
r, err := Post[result](client, "order/replace", o)
return r.OrderID, err
}
func (o *Client) ReplaceOrder(v ReplaceOrder) (string, error) {
return v.Do(o)
}
// Query Active Order (real-time) (https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-queryactive)
//
// Query real-time active order information. If only order_id or order_link_id are passed,
// a single order will be returned; otherwise, returns up to 500 unfilled orders.
type QueryOrder struct {
Symbol string `param:"symbol"`
OrderID *string `param:"order_id"`
OrderLinkID *string `param:"order_link_id"`
}
func (o QueryOrder) OnlySymbol() bool {
return o.OrderID == nil && o.OrderLinkID == nil
}
// When only symbol is passed, the response uses a different structure.
func (o QueryOrder) Do(client *Client) (l []Order, err error) {
var r json.RawMessage
r, err = Get[json.RawMessage](client, "order", o)
if len(r) > 0 && r[0] == '[' {
err = json.Unmarshal(r, &l)
} else {
l = make([]Order, 1)
err = json.Unmarshal(r, &l[0])
}
return
}
type Order struct {
OrderCancelled
LeavesValue string `json:"leaves_value"`
PositionIdx PositionIdx `json:"position_idx"`
CancelType CancelType `json:"cancel_type"`
ExtFields OrderExtFields `json:"ext_fields"`
}
type OrderExtFields struct {
oreqNum int64 `json:"o_req_num"`
XreqType string `json:"xreq_type"`
}
func (o *Client) QueryOrder(v QueryOrder) ([]Order, error) {
return v.Do(o)
}
func (o *Client) QueryOrderByID(symbol string, orderID string) (i Order, err error) {
ret, err := o.QueryOrder(QueryOrder{
Symbol: symbol,
OrderID: &orderID,
})
if err == nil {
if len(ret) == 1 {
i = ret[0]
} else {
err = fmt.Errorf("query order result len != 1 (%d)", len(ret))
}
}
return
}
func (o *Client) QueryOrderByLinkID(symbol string, orderLinkID string) (i Order, err error) {
ret, err := o.QueryOrder(QueryOrder{
Symbol: symbol,
OrderLinkID: &orderLinkID,
})
if err == nil {
if len(ret) == 1 {
i = ret[0]
} else {
err = fmt.Errorf("query order result len != 1 (%d)", len(ret))
}
}
return
}