-
Notifications
You must be signed in to change notification settings - Fork 11
/
order.go
291 lines (248 loc) · 7.82 KB
/
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
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
package swap
import (
"context"
"encoding/json"
"strconv"
"strings"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/NadiaSama/ccexgo/exchange/huobi"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
type (
OrderReq struct {
data map[string]interface{}
}
OrderResp struct {
OrderID int64 `json:"order_id"`
OrderIDStr string `json:"order_id_str"`
}
SwapCancelReq struct {
symbol string
orderIDS []string
clientOrderIDS []string
}
SwapCancelError struct {
OrderID string `json:"order_id"`
ErrCode int `json:"err_code"`
ErrMsg string `json:"err_msg"`
}
SwapCancelResp struct {
Errors []SwapCancelError `json:"errors"`
Successes string `json:"successes"` //id1,id2,id3 ...
}
SwapOrderDetailReq struct {
data map[string]interface{}
}
SwapOrderDetailResp struct {
Symbol string `json:"symbol"`
ContractCode string `json:"contract_code"`
LeverRate int `json:"lever_rate"`
Direction string `json:"direction"`
Offset string `json:"offset"`
Volume float64 `json:"volume"`
Price float64 `json:"price"`
CreatedAt int64 `json:"created_at"`
CanceledAt int64 `json:"canceled_at"`
OrderSource string `json:"order_source"`
OrderPriceType string `json:"order_price_type"`
MarginFrozen float64 `json:"margin_frozen"`
Profit float64 `json:"profit"`
Trades []OrderNotifyTrade `json:"trades"`
TotalPage int `json:"total_page"`
CurrentPage int `json:"current_page"`
TotalSize int `json:"total_size"`
LiquidationType string `json:"liquidation_type"`
FeeAsset string `json:"fee_asset"`
Fee float64 `json:"fee"`
OrderID int64 `json:"order_id"`
OrderIDStr string `json:"order_id_str"`
ClientOrderID interface{} `json:"client_order_id"`
OrderType string `json:"order_type"`
Status int `json:"status"`
TradeAvgPrice float64 `json:"trade_avg_price"`
TradeTurnOver float64 `json:"trade_turn_over"`
TradeVolume float64 `json:"trade_volume"`
IsTpsl interface{} `json:"is_tpsl"`
RealProfit float64 `json:"real_profit"`
}
)
const (
SwapOrderEndPoint = "/swap-api/v1/swap_order"
SwapCancelEndPoint = "/swap-api/v1/swap_cancel"
SwapOrderDetailEndPoint = "/swap-api/v1/swap_order_detail"
OrderDirectionBuy = "buy"
OrderDirectionSell = "sell"
OrderOffsetOpen = "open"
OrderOffsetClose = "close"
OrderPriceLimit = "limit"
OrderPriceMarket = "opponent"
OrderPriceOptimal5 = "optimal_5"
)
func NewOrderReq(contractCode string, volume int, direction string, offset string, lever int, orderPriceType string) *OrderReq {
ret := OrderReq{
data: make(map[string]interface{}),
}
ret.data["contract_code"] = contractCode
ret.data["volume"] = volume
ret.data["direction"] = direction
ret.data["offset"] = offset
ret.data["lever_rate"] = lever
ret.data["order_price_type"] = orderPriceType
return &ret
}
func (or *OrderReq) Price(price float64) *OrderReq {
or.data["price"] = price
return or
}
func (or *OrderReq) Serialize() ([]byte, error) {
return json.Marshal(or.data)
}
func NewSwapCancelReq(symbol string) *SwapCancelReq {
return &SwapCancelReq{
symbol: symbol,
}
}
func (scr *SwapCancelReq) Orders(ids ...string) *SwapCancelReq {
for _, id := range ids {
scr.orderIDS = append(scr.orderIDS, id)
}
return scr
}
func (scr *SwapCancelReq) ClientOrderIDs(ids ...string) *SwapCancelReq {
for _, id := range ids {
scr.clientOrderIDS = append(scr.clientOrderIDS, id)
}
return scr
}
func (scr *SwapCancelReq) Serialize() ([]byte, error) {
data := map[string]string{
"contract_code": scr.symbol,
}
if len(scr.orderIDS) != 0 {
data["order_id"] = strings.Join(scr.orderIDS, ",")
}
if len(scr.clientOrderIDS) != 0 {
data["client_order_id"] = strings.Join(scr.clientOrderIDS, ",")
}
return json.Marshal(data)
}
func NewSwapOrderDetailReq(cc string, id int64) *SwapOrderDetailReq {
return &SwapOrderDetailReq{
data: map[string]interface{}{
"contract_code": cc,
"order_id": id,
},
}
}
func (sdr *SwapOrderDetailReq) CreatedAt(ts int64) *SwapOrderDetailReq {
sdr.data["created_at"] = ts
return sdr
}
func (sdr *SwapOrderDetailReq) OrderType(ot int) *SwapOrderDetailReq {
sdr.data["order_type"] = ot
return sdr
}
func (sdr *SwapOrderDetailReq) PageIndex(pi int) *SwapOrderDetailReq {
sdr.data["page_index"] = pi
return sdr
}
func (sdr *SwapOrderDetailReq) PageSize(ps int) *SwapOrderDetailReq {
sdr.data["page_size"] = ps
return sdr
}
func (sdr *SwapOrderDetailReq) Serialize() ([]byte, error) {
return json.Marshal(sdr.data)
}
func (rc *RestClient) SwapOrder(ctx context.Context, req *OrderReq) (*OrderResp, error) {
var resp OrderResp
if err := rc.PrivatePostReq(ctx, SwapOrderEndPoint, req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (rc *RestClient) SwapCancel(ctx context.Context, req *SwapCancelReq) (*SwapCancelResp, error) {
var resp SwapCancelResp
if err := rc.PrivatePostReq(ctx, SwapCancelEndPoint, req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
func (rc *RestClient) SwapOrderDetail(ctx context.Context, req *SwapOrderDetailReq) (*SwapOrderDetailResp, error) {
var ret SwapOrderDetailResp
if err := rc.PrivatePostReq(ctx, SwapOrderDetailEndPoint, req, &ret); err != nil {
return nil, err
}
return &ret, nil
}
func (rc *RestClient) FetchOrder(ctx context.Context, order *exchange.Order) (*exchange.Order, error) {
id, err := strconv.ParseInt(order.ID.String(), 10, 64)
if err != nil {
return nil, errors.WithMessagef(err, "invalid order id '%s'", order.ID.String())
}
req := NewSwapOrderDetailReq(order.Symbol.String(), id)
resp, err := rc.SwapOrderDetail(ctx, req)
if err != nil {
return nil, err
}
return resp.Transform()
}
func (r *SwapOrderDetailResp) Transform() (*exchange.Order, error) {
symbol, err := ParseSymbol(r.ContractCode)
if err != nil {
return nil, err
}
ret := &exchange.Order{
ID: exchange.NewIntID(r.OrderID),
Symbol: symbol,
Amount: decimal.NewFromFloat(r.Volume),
Filled: decimal.NewFromFloat(r.TradeVolume),
Price: decimal.NewFromFloat(r.Price),
AvgPrice: decimal.NewFromFloat(r.TradeAvgPrice),
Fee: decimal.NewFromFloat(r.Fee),
FeeCurrency: r.FeeAsset,
Created: huobi.ParseTS(r.CreatedAt),
}
if r.Direction == OrderDirectionBuy {
if r.Offset == OrderOffsetOpen {
ret.Side = exchange.OrderSideBuy
} else if r.Offset == OrderOffsetClose {
ret.Side = exchange.OrderSideCloseShort
} else {
return nil, errors.Errorf("unkown order offset '%s'", r.Offset)
}
} else if r.Direction == OrderDirectionSell {
if r.Offset == OrderOffsetOpen {
ret.Side = exchange.OrderSideSell
} else if r.Offset == OrderOffsetClose {
ret.Side = exchange.OrderSideCloseLong
} else {
return nil, errors.Errorf("unkown order offset '%s'", r.Offset)
}
} else {
return nil, errors.Errorf("unkown order direction '%s'", r.Direction)
}
if r.CanceledAt != 0 {
ret.Updated = huobi.ParseTS(r.CanceledAt)
} else {
var fetchTS int64
for _, t := range r.Trades {
if t.CreatedAt > fetchTS {
ret.Updated = huobi.ParseTS(t.CreatedAt)
fetchTS = r.CreatedAt
}
}
}
st, ok := statusMap[r.Status]
if !ok {
return nil, errors.Errorf("unkown order status %d", r.Status)
}
typ, ok := typeMap[r.OrderPriceType]
if !ok {
return nil, errors.Errorf("unkown order type %s", r.OrderPriceType)
}
ret.Status = st
ret.Type = typ
ret.Raw = r
return ret, nil
}