-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzb_trade.go
215 lines (197 loc) · 6.46 KB
/
zb_trade.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
package zb
import (
"fmt"
"github.com/tidwall/gjson"
"log"
"net/url"
"strings"
"time"
)
// PlaceOrder 委托下单
// tradeType: 交易类型1/0[buy/sell]
// acctType: 杠杆 1/0[杠杆/现货](选填,默认为: 0 现货)
func (z *ZB) PlaceOrder(symbol string, price float64, amount float64, tradeType int, acctType int, customOrderID string) (result OrderResponse, err error) {
params := url.Values{}
params.Set("method", "order")
params.Set("price", fmt.Sprint(price))
params.Set("amount", fmt.Sprint(amount))
params.Set("currency", symbol)
params.Set("tradeType", fmt.Sprint(tradeType))
params.Set("acctType", fmt.Sprint(acctType))
if customOrderID != "" {
params.Set("customerOrderId", customOrderID)
}
z.buildSignParams(¶ms)
url := z.tradeURL + "order?" + params.Encode()
_, err = z.HttpGet(url, "", nil, &result)
// {"code":2009,"message":"账户余额不足"}
return
}
// PlaceOrderMore 批量委托下单
// 交易参数,数组格式[[价格,数量],[价格,数量]],最多20个
// tradeType: 交易类型1/0[buy/sell]
// acctType: 杠杆 1/0[杠杆/现货](选填,默认为: 0 现货)
func (z *ZB) PlaceOrderMore(symbol string, tradeParams [][]float64, tradeType int, acctType int) (result OrderMoreV2Response, err error) {
var tradeParamsData []byte
tradeParamsData, err = json.Marshal(tradeParams)
if err != nil {
return
}
params := url.Values{}
params.Set("method", "orderMoreV2")
params.Set("market", symbol)
params.Set("tradeType", fmt.Sprint(tradeType))
params.Set("acctType", fmt.Sprint(acctType))
params.Set("tradeParams", "_tradeParams_")
params.Set("accesskey", z.accessKey)
payload := params.Encode()
payload = strings.ReplaceAll(payload, "_tradeParams_", string(tradeParamsData))
secretKeySha, _ := GetSHA(z.secretKey)
var sign string
sign, err = GetParamHmacMD5Sign(secretKeySha, payload)
if err != nil {
return
}
url := z.tradeURL + "orderMoreV2?" + payload + "&sign=" + sign + "&reqTime=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
if z.debugMode {
log.Printf("url: %v", url)
}
_, err = z.HttpGet(url, "", nil, &result)
// {"code":1003,"message":"验证不通过"}
// {"code":2009,"message":"账户余额不足"}
return
}
// CancelOrder 取消委托
func (z *ZB) CancelOrder(symbol string, id int64, customOrderID string) (result OrderResponse, err error) {
params := url.Values{}
params.Set("method", "cancelOrder")
params.Set("currency", symbol)
if customOrderID != "" {
params.Set("customerOrderId", customOrderID)
} else {
params.Set("id", fmt.Sprint(id))
}
z.buildSignParams(¶ms)
url := z.tradeURL + "cancelOrder?" + params.Encode()
_, err = z.HttpGet(url, "", nil, &result)
// {"code":3001,"message":"挂单没有找到或已完成"}
return
}
// GetOrder 获取委托单
func (z *ZB) GetOrder(symbol string, id int64, customOrderID string) (result Order, err error) {
params := url.Values{}
params.Set("method", "getOrder")
params.Set("currency", symbol)
if customOrderID != "" {
params.Set("customerOrderId", customOrderID)
} else {
params.Set("id", fmt.Sprint(id))
}
z.buildSignParams(¶ms)
url := z.tradeURL + "getOrder?" + params.Encode()
var resp []byte
resp, err = z.HttpGet(url, "", nil, nil)
if err != nil {
return
}
// {"code":3001,"message":"挂单没有找到或已完成"}
g := gjson.ParseBytes(resp)
if codeValue := g.Get("code"); codeValue.Exists() {
err = fmt.Errorf("code=%v message=%v",
codeValue.Int(), g.Get("message").String())
return
}
err = json.Unmarshal(resp, &result)
return
}
// GetOrders 获取多个委托单
func (z *ZB) GetOrders(symbol string, tradeType int, pageIndex int) (result []Order, err error) {
params := url.Values{}
params.Set("method", "getOrders")
params.Set("currency", symbol)
params.Set("tradeType", fmt.Sprint(tradeType))
params.Set("pageIndex", fmt.Sprint(pageIndex))
z.buildSignParams(¶ms)
url := z.tradeURL + "getOrders?" + params.Encode()
var resp []byte
resp, err = z.HttpGet(url, "", nil, nil)
if err != nil {
return
}
g := gjson.ParseBytes(resp)
if codeValue := g.Get("code"); codeValue.Exists() {
err = fmt.Errorf("code=%v message=%v",
codeValue.Int(), g.Get("message").String())
return
}
err = json.Unmarshal(resp, &result)
return
}
// GetOrdersIgnoreTradeType 获取委托单忽略类型
func (z *ZB) GetOrdersIgnoreTradeType(symbol string, pageIndex int, pageSize int) (result []Order, err error) {
params := url.Values{}
params.Set("method", "getOrdersIgnoreTradeType")
params.Set("currency", symbol)
params.Set("pageIndex", fmt.Sprint(pageIndex))
params.Set("pageSize", fmt.Sprint(pageSize))
z.buildSignParams(¶ms)
url := z.tradeURL + "getOrdersIgnoreTradeType?" + params.Encode()
var resp []byte
resp, err = z.HttpGet(url, "", nil, nil)
if err != nil {
return
}
g := gjson.ParseBytes(resp)
if codeValue := g.Get("code"); codeValue.Exists() {
err = fmt.Errorf("code=%v message=%v",
codeValue.Int(), g.Get("message").String())
return
}
err = json.Unmarshal(resp, &result)
return
}
// GetUnfinishedOrdersIgnoreTradeType 未成交或部份成交挂单
func (z *ZB) GetUnfinishedOrdersIgnoreTradeType(symbol string, pageIndex int, pageSize int) (result []Order, err error) {
params := url.Values{}
params.Set("method", "getUnfinishedOrdersIgnoreTradeType")
params.Set("currency", symbol)
params.Set("pageIndex", fmt.Sprint(pageIndex))
params.Set("pageSize", fmt.Sprint(pageSize))
z.buildSignParams(¶ms)
url := z.tradeURL + "getUnfinishedOrdersIgnoreTradeType?" + params.Encode()
var resp []byte
resp, err = z.HttpGet(url, "", nil, nil)
if err != nil {
return
}
// {"code":3001,"message":"挂单没有找到或已完成"}
g := gjson.ParseBytes(resp)
if codeValue := g.Get("code"); codeValue.Exists() {
err = fmt.Errorf("code=%v message=%v",
codeValue.Int(), g.Get("message").String())
return
}
err = json.Unmarshal(resp, &result)
return
}
// GetAccountInfo 获取用户信息
func (z *ZB) GetAccountInfo() (result AccountInfo, err error) {
params := url.Values{}
params.Set("method", "getAccountInfo")
z.buildSignParams(¶ms)
url := z.tradeURL + "getAccountInfo?" + params.Encode()
var resp []byte
resp, err = z.HttpGet(url, "", nil, nil)
if err != nil {
return
}
// {"code":3001,"message":"挂单没有找到或已完成"}
g := gjson.ParseBytes(resp)
if codeValue := g.Get("code"); codeValue.Exists() {
err = fmt.Errorf("code=%v message=%v",
codeValue.Int(), g.Get("message").String())
return
}
err = json.Unmarshal(resp, &result)
return
}