-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrading.go
105 lines (84 loc) · 2.38 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
package gokraken
import (
"context"
"net/http"
"net/url"
"strconv"
"strings"
)
// Trading is responsible for communicating with all the private user trading
// endpoints on the Kraken API.
type Trading struct {
Client *Kraken
}
// AddOrder adds a standard order via the Kraken API.
// https://www.kraken.com/en-gb/help/api#add-standard-order
func (t *Trading) AddOrder(ctx context.Context, order UserOrder) (res *AddOrderResponse, err error) {
body := url.Values{
"pair": {order.Pair.String()},
"type": {string(order.Type)},
"ordertype": {string(order.OrderType)},
"volume": {strconv.FormatFloat(order.Volume, 'f', 4, 64)},
}
if order.Price != 0 {
body.Add("price", strconv.FormatFloat(order.Price, 'f', 4, 64))
}
if order.Price2 != 0 {
body.Add("price2", strconv.FormatFloat(order.Price2, 'f', 4, 64))
}
if order.Leverage != "" {
body.Add("leverage", order.Leverage)
}
if len(order.OFlags) > 0 {
flagStrings := make([]string, len(order.OFlags))
for index := range order.OFlags {
flagStrings[index] = string(order.OFlags[index])
}
body.Add("oflags", strings.Join(flagStrings, ","))
}
if order.StartTm != "" {
body.Add("starttm", order.StartTm)
}
if order.ExpireTm != "" {
body.Add("expiretm", order.ExpireTm)
}
if order.Validate {
body.Add("validate", "true")
}
if string(order.CloseOrderType) != "" {
body.Add("close[ordertype]", string(order.CloseOrderType))
}
if order.ClosedOrderPrice != 0 {
body.Add("close[price]", strconv.FormatFloat(order.ClosedOrderPrice, 'f', 4, 64))
}
if order.ClosedOrderPrice2 != 0 {
body.Add("close[price2]", strconv.FormatFloat(order.ClosedOrderPrice2, 'f', 4, 64))
}
req, err := t.Client.DialWithAuth(ctx, http.MethodPost, AddOrderResource, body)
if err != nil {
return
}
krakenResp, err := t.Client.Call(req)
if err != nil {
return
}
err = krakenResp.ExtractResult(&res)
return
}
// CancelOrder cancels an open order via the Kraken API.
// https://www.kraken.com/en-gb/help/api#cancel-open-order
func (t *Trading) CancelOrder(ctx context.Context, txid int64) (res *CancelOrderResponse, err error) {
body := url.Values{
"txid": {strconv.FormatInt(txid, 10)},
}
req, err := t.Client.DialWithAuth(ctx, http.MethodPost, CancelOrderResource, body)
if err != nil {
return
}
krakenResp, err := t.Client.Call(req)
if err != nil {
return
}
err = krakenResp.ExtractResult(&res)
return
}