/
trading.go
127 lines (106 loc) · 3.94 KB
/
trading.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
package kraken
import (
"context"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Trading handles communication with the trading related
// methods of the Kraken API.
type Trading service
// AddOrderOpts represents the parameters to create an Order.
type AddOrderOpts struct {
UserRef string `url:"userref,omitempty"`
OrderType OrderType `url:"ordertype,omitempty"`
Type OrderDirection `url:"type,omitempty"`
Volume string `url:"volume,omitempty"`
DisplayVol string `url:"displayvol,omitempty"`
Pair string `url:"pair,omitempty"`
Price string `url:"price,omitempty"`
Price2 string `url:"price2,omitempty"`
Trigger OrderTrigger `url:"trigger,omitempty"`
Leverage string `url:"leverage,omitempty"`
ReduceOnly bool `url:"reduce_only,omitempty"`
StopType StopType `url:"stptype,omitempty"`
OrderFlags string `url:"oflags,omitempty"`
TimeInForce TimeInForce `url:"timeinforce,omitempty"`
Starttm string `url:"starttm,omitempty"`
Expiretm string `url:"expiretm,omitempty"`
CloseOrderType OrderType `url:"close[ordertype],omitempty"`
ClosePrice string `url:"close[price],omitempty"`
ClosePrice2 string `url:"close[price2],omitempty"`
Deadline string `url:"deadline,omitempty"`
Validate bool `url:"validate,omitempty"`
}
// AddOrder places a new order.
// Docs: https://docs.kraken.com/rest/#tag/Trading/operation/addOrder
func (t *Trading) AddOrder(ctx context.Context, opts AddOrderOpts) (*OrderCreation, error) {
body, err := query.Values(opts)
if err != nil {
return nil, err
}
req, err := t.client.newPrivateRequest(ctx, http.MethodPost, "AddOrder", newFormURLEncodedBody(body))
if err != nil {
return nil, err
}
var v OrderCreation
if err := t.client.do(req, &v); err != nil {
return nil, err
}
return &v, nil
}
// CancelOrderOpts represents the parameters to cancel an Order.
type CancelOrderOpts struct {
TransactionID string `url:"txid,omitempty"`
}
// CancelOrder cancels an order.
// Docs: https://docs.kraken.com/rest/#tag/Trading/operation/cancelOrder
func (t *Trading) CancelOrder(ctx context.Context, opts CancelOrderOpts) (*OrderCancelation, error) {
body, err := query.Values(opts)
if err != nil {
return nil, err
}
req, err := t.client.newPrivateRequest(ctx, http.MethodPost, "CancelOrder", newFormURLEncodedBody(body))
if err != nil {
return nil, err
}
var v OrderCancelation
if err := t.client.do(req, &v); err != nil {
return nil, err
}
return &v, nil
}
// CancelAllOrders cancels all open orders.
// Docs: https://docs.kraken.com/rest/#tag/Trading/operation/cancelAll
func (t *Trading) CancelAllOrders(ctx context.Context) (*OrderCancelation, error) {
req, err := t.client.newPrivateRequest(ctx, http.MethodPost, "CancelAll", newFormURLEncodedBody(nil))
if err != nil {
return nil, err
}
var v OrderCancelation
if err := t.client.do(req, &v); err != nil {
return nil, err
}
return &v, nil
}
// CancelAllOrdersAfterOpts represents the parameters to cancel all orders after a timeout.
type CancelAllOrdersAfterOpts struct {
Timeout time.Duration `url:"timeout,omitempty"` // Duration (in seconds) to set/extend the timer by
}
// CancelAllOrdersAfter cancels all open orders after a timeout.
// Docs: https://docs.kraken.com/rest/#tag/Trading/operation/cancelAllOrdersAfter
func (t *Trading) CancelAllOrdersAfter(ctx context.Context, opts CancelAllOrdersAfterOpts) (*TriggeredOrderCancellation, error) {
body, err := query.Values(opts)
if err != nil {
return nil, err
}
req, err := t.client.newPrivateRequest(ctx, http.MethodPost, "CancelAllOrdersAfter", newFormURLEncodedBody(body))
if err != nil {
return nil, err
}
var v TriggeredOrderCancellation
if err := t.client.do(req, &v); err != nil {
return nil, err
}
return &v, nil
}