/
trading_client.go
435 lines (413 loc) · 13.9 KB
/
trading_client.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
package websocket
import (
"context"
"encoding/json"
"fmt"
"github.com/cryptomkt/cryptomkt-go/v3/args"
"github.com/cryptomkt/cryptomkt-go/v3/internal"
"github.com/cryptomkt/cryptomkt-go/v3/models"
)
// SpotTradingClient connects via websocket to cryptomarket to enable the user to manage orders. uses SHA256 as auth method and authenticates automatically.
type SpotTradingClient struct {
clientBase
}
// NewSpotTradingClient returns a new spot trading client if the connection with the
// cryptomarket server is successful and if the authentication is successfull.
// return error otherwise.
// Arguments:
//
// apiKey // The API key
// apiSecret // The API secret
// window // Maximum difference between the creation of the request and the moment of request processing in milliseconds. Max is 60_000. Defaul is 10_000 (use 0 as argument for default)
func NewSpotTradingClient(apiKey, apiSecret string, window int) (*SpotTradingClient, error) {
client := &SpotTradingClient{
clientBase: clientBase{
wsManager: newWSManager("/api/3/ws/trading"),
chanCache: newChanCache(),
window: window,
},
}
// connect to streaming
if err := client.wsManager.connect(); err != nil {
return nil, fmt.Errorf("error in websocket client connection: %s", err)
}
// handle incomming data
go client.handle(client.wsManager.rcv)
if err := client.authenticate(apiKey, apiSecret); err != nil {
return nil, err
}
return client, nil
}
// GetSpotTradingBalances gets the user's spot trading balance for all currencies with balance
//
// https://api.exchange.cryptomkt.com/#get-spot-trading-balances
func (client *SpotTradingClient) GetSpotTradingBalances(
ctx context.Context,
) ([]models.Balance, error) {
var resp struct {
Result []models.Balance
}
err := client.doRequest(ctx, methodSpotBalances, nil, nil, &resp)
if err != nil {
return nil, err
}
return resp.Result, nil
}
// GetSpotTradingBalanceOfCurrency gets the user spot trading balance of a currency
//
// https://api.exchange.cryptomkt.com/#get-spot-trading-balance-2
//
// Arguments:
//
// Currency(string) // The currency code to query the balance
func (client *SpotTradingClient) GetSpotTradingBalanceOfCurrency(
ctx context.Context,
arguments ...args.Argument,
) (*models.Balance, error) {
var resp struct {
Result models.Balance
}
err := client.doRequest(
ctx,
methodSpotBalance,
arguments,
[]string{internal.ArgNameCurrency},
&resp,
)
if err != nil {
return nil, err
}
return &resp.Result, nil
}
// GetActiveSpotOrders gets the user's active spot orders
//
// https://api.exchange.cryptomkt.com/#get-all-active-spot-orders
func (client *SpotTradingClient) GetActiveSpotOrders(
ctx context.Context,
) ([]models.Report, error) {
var resp struct {
Result []models.Report
}
err := client.doRequest(ctx, methodGetSpotOrders, nil, nil, &resp)
if err != nil {
return nil, err
}
return resp.Result, nil
}
// CreateSpotOrder creates a new spot order
//
// For fee, for price accuracy and quantity, and for order status information see the api docs at https://api.exchange.cryptomkt.com/#create-new-spot-order
//
// https://api.exchange.cryptomkt.com/#place-new-spot-order
//
// Arguments:
//
// Symbol(string) // Trading symbol
// Side(SideType) // Either SideBuy or SideSell
// Quantity(string) // Order quantity
// ClientOrderID(string) // Optional. If given must be unique within the trading day, including all active orders. If not given, is generated by the server
// Type(OrderType) // Optional. OrderLimit, OrderMarket, OrderStopLimit, OrderStopMarket, OrderTakeProfitLimit or OrderTakeProfitMarket. Default is OrderLimit
// TimeInForce(TimeInForceType) // Optional. TimeInForceGTC, TimeInForceIOC, TimeInForceFOK, TimeInForceDay, TimeInForceGTD. Default to TimeInForceGTC
// Price(string) // Optional. Required for OrderLimit and OrderStopLimit. limit price of the order
// StopPrice(string) // Optional. Required for OrderStopLimit and OrderStopMarket orders. stop price of the order
// ExpireTime(string) // Optional. Required for orders with timeInForceGDT
// StrictValidate(bool) // Optional. If False, the server rounds half down for tickerSize and quantityIncrement. Example of ETHBTC: tickSize = '0.000001', then price '0.046016' is valid, '0.0460165' is invalid
// PostOnly(bool) // Optional. If True, your postOnly order causes a match with a pre-existing order as a taker, then the order will be cancelled
// TakeRate(string) // Optional. Liquidity taker fee, a fraction of order volume, such as 0.001 (for 0.1% fee). Can only increase the fee. Used for fee markup.
// MakeRate(string) // Optional. Liquidity provider fee, a fraction of order volume, such as 0.001 (for 0.1% fee). Can only increase the fee. Used for fee markup.
func (client *SpotTradingClient) CreateSpotOrder(
ctx context.Context,
arguments ...args.Argument,
) (*models.Report, error) {
var resp struct {
Result models.Report
}
err := client.doRequest(
ctx,
methodCreateSpotOrder,
arguments,
[]string{
internal.ArgNameSymbol,
internal.ArgNameSide,
internal.ArgNameQuantity,
},
&resp,
)
if err != nil {
return nil, err
}
return &resp.Result, nil
}
// CreateSpotOrderList creates a list of spot orders and returns a list of reports of the created orders, or a possible error
//
// Types or contingency:
//
// - ContingencyTypeAllOrNone (ContingencyTypeAON) (AON)
// - ContingencyTypeOneCancelOther (ContingencyTypeOCO) (OCO)
// - ContingencyOneTriggerOther (ContingencyTypeOTO) (OTO)
// - ContingencyOneTriggerOneCancelOther (ContingencyTypeOTOCO) (OTOCO)
//
// Restriction in the number of orders:
//
// - An AON list must have 2 or 3 orders
// - An OCO list must have 2 or 3 orders
// - An OTO list must have 2 or 3 orders
// - An OTOCO must have 3 or 4 orders
//
// Symbol restrictions:
//
// - For an AON order list, the symbol code of orders must be unique for each order in the list.
// - For an OCO order list, there are no symbol code restrictions.
// - For an OTO order list, there are no symbol code restrictions.
// - For an OTOCO order list, the symbol code of orders must be the same for all orders in the list (placing orders in different order books is not supported).
//
// ORDER_TYPE restrictions:
// - For an AON order list, orders must be OrderLimit or OrderMarket
// - For an OCO order list, orders must be OrderLimit, OrderStopLimit, OrderStopMarket, OrderTakeProfitLimit or OrderTakeProfitMarket.
// - An OCO order list cannot include more than one limit order (the same applies to secondary orders in an OTOCO order list).
// - For an OTO order list there are no order type restrictions.
// - For an OTOCO order list, the first order must be OrderLimit, OrderMarket, OrderStopLimit, OrderStopMarket, OrderTakeProfitLimit or OrderTakeProfitMarket.
// - For an OTOCO order list, the secondary orders have the same restrictions as an OCO order
// - Default is OrderTypeLimit
//
// https://api.exchange.cryptomkt.com/#create-new-spot-order-list-2
//
// Arguments:
//
// OrderListID(string) // order list identifier. If ommited, it will be generated by the system. Must be equal to the client order id of the first order in the request
// Contingency(ContingencyType) // order list type. ContingencyAON, ContingencyOCO or ContingencyOTOCO
// Orders([]Order) // the list of orders, orders from the args package
func (client *SpotTradingClient) CreateSpotOrderList(
ctx context.Context,
arguments ...args.Argument,
) ([]models.Report, error) {
reports := make([]models.Report, 0)
err := client.doRequestOfNNotifications(
ctx,
methodCreateSpotOrderList,
arguments,
[]string{internal.ArgNameOrderListID, internal.ArgNameContingencyType, internal.ArgNameOrders},
func(data []byte) {
var response struct {
Result models.Report
}
json.Unmarshal(data, &response)
reports = append(reports, response.Result)
},
internal.ArgNameOrders,
)
if err != nil {
return nil, err
}
return reports, nil
}
// CancelSpotOrders cancels a spot order
//
// https://api.exchange.cryptomkt.com/#cancel-spot-order-2
//
// Arguments:
//
// ClientOrderID(string) // the client order id of the order to cancel
func (client *SpotTradingClient) CancelSpotOrder(
ctx context.Context,
arguments ...args.Argument,
) (*models.Report, error) {
var resp struct {
Result models.Report
}
err := client.doRequest(
ctx,
methodCancelSpotOrder,
arguments,
[]string{internal.ArgNameClientOrderID},
&resp,
)
if err != nil {
return nil, err
}
return &resp.Result, nil
}
// CancelAllSpotOrders cancel all active spot orders and returns the ones that could not be canceled
//
// https://api.exchange.cryptomkt.com/#cancel-spot-orders
func (client *SpotTradingClient) CancelAllSpotOrders(
ctx context.Context,
) ([]models.Order, error) {
var resp struct {
Result []models.Order
}
err := client.doRequest(
ctx,
methodCancelSpotOrders,
nil,
nil,
&resp,
)
if err != nil {
return nil, err
}
return resp.Result, nil
}
// ReplaceSpotOrder changes the parameters of an existing order, quantity or price
//
// https://api.exchange.cryptomkt.com/#cancel-replace-spot-order
//
// Arguments:
//
// ClientOrderID(string) // the client order id of the order to change
// NewClientOrderID(string) // the new client order id for the modified order. must be unique within the trading day
// Quantity(string) // new order quantity
// Price(string) // new order price
// StrictValidate(bool) // price and quantity will be checked for the incrementation with tick size and quantity step. See symbol's tick_size and quantity_increment
func (client *SpotTradingClient) ReplaceSpotOrder(
ctx context.Context,
arguments ...args.Argument,
) (*models.Report, error) {
var resp struct {
Result models.Report
}
err := client.doRequest(
ctx,
methodReplaceSpotOrder,
arguments,
[]string{
internal.ArgNameClientOrderID,
internal.ArgNameNewClientOrderID,
internal.ArgNamePrice,
internal.ArgNameQuantity,
},
&resp,
)
if err != nil {
return nil, err
}
return &resp.Result, nil
}
// GetTradingCommissions gets the personal trading commission rates for all symbols
//
// https://api.exchange.cryptomkt.com/#get-spot-fees
func (client *SpotTradingClient) GetTradingCommissions(
ctx context.Context,
) ([]models.TradingCommission, error) {
var resp struct {
Result []models.TradingCommission
}
err := client.doRequest(ctx, methodSpotFees, nil, nil, &resp)
if err != nil {
return nil, err
}
return resp.Result, nil
}
// GetTradingCommissionOfSymbol gets the personal trading commission rate of a symbol
//
// https://api.exchange.cryptomkt.com/#get-spot-fee
//
// Arguments:
//
// Symbol(string) // The symbol of the commission rate
func (client *SpotTradingClient) GetSpotFee(
ctx context.Context,
arguments ...args.Argument,
) (*models.TradingCommission, error) {
var resp struct {
Result models.TradingCommission
}
err := client.doRequest(ctx, methodSpotFee, arguments, []string{internal.ArgNameSymbol}, &resp)
if err != nil {
return nil, err
}
return &resp.Result, nil
}
///////////////////
// Subscriptions //
///////////////////
// SubscribeToReports subscribe to a feed of execution reports of the user's orders
//
// recieves a snapshot and updates notifications
//
// https://api.exchange.cryptomkt.com/#socket-spot-trading
func (client *SpotTradingClient) SubscribeToReports() (notificationCh chan models.Notification[[]models.Report], err error) {
dataCh, err := client.doSubscription(methodSubscribeSpotReports, nil, nil)
if err != nil {
return nil, err
}
notificationCh = make(chan models.Notification[[]models.Report])
go func() {
defer close(notificationCh)
sendMap := map[string]func([]byte){
methodSpotOrders: func(data []byte) {
var snapshot struct {
Params []models.Report
}
json.Unmarshal(data, &snapshot)
notificationCh <- models.Notification[[]models.Report]{
Data: snapshot.Params,
NotificationType: args.NotificationSnapshot,
}
},
methodSpotOrder: func(data []byte) {
var update struct {
Params models.Report
}
json.Unmarshal(data, &update)
notificationCh <- models.Notification[[]models.Report]{
Data: []models.Report{update.Params},
NotificationType: args.NotificationUpdate,
}
},
}
// the first time it recieves a list of reports
var method struct {
Method string
}
for data := range dataCh {
json.Unmarshal(data, &method)
sendMap[method.Method](data)
}
}()
return notificationCh, nil
}
// UnsubscribeToReports stop recieveing the report feed subscription
//
// https://api.exchange.cryptomkt.com/#socket-spot-trading
func (client *SpotTradingClient) UnsubscribeToReports() (err error) {
return client.doUnsubscription(methodSpotUnsubscribe, nil, nil)
}
// Subscribes to the user's balances.
//
// Requires the "Orderbook, History, Trading balance" API key Access Right.
//
// https://api.exchange.cryptomkt.com/#subscribe-to-spot-balances
//
// Arguments:
//
// Mode(string) // The symbol of the commission rate
func (client *SpotTradingClient) SubscribeToSpotBalance(arguments ...args.Argument) (notificationCh chan models.Notification[[]models.Balance], err error) {
dataCh, err := client.doSubscription(methodSubscribeSpotBalance, arguments, []string{internal.ArgNameMode})
if err != nil {
return nil, err
}
notificationCh = make(chan models.Notification[[]models.Balance])
go func() {
defer close(notificationCh)
var snapshot struct {
Params []models.Balance
}
for data := range dataCh {
json.Unmarshal(data, &snapshot)
notificationCh <- models.Notification[[]models.Balance]{
Data: snapshot.Params,
NotificationType: args.NotificationSnapshot,
}
}
}()
return notificationCh, nil
}
// Unubscribes to the user's balances.
//
// Requires the "Orderbook, History, Trading balance" API key Access Right.
//
// https://api.exchange.cryptomkt.com/#subscribe-to-spot-balances
func (client *SpotTradingClient) UnsubscribeToSpotBalance() (err error) {
return client.doUnsubscription(methodUnsubstribeSpotBalance, nil, nil)
}