/
binance_coin_futures_api.go
317 lines (283 loc) · 10.6 KB
/
binance_coin_futures_api.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
package go_binance
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"golang.org/x/net/http2"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
const (
mainNetBaseURLCoin = "https://dapi.binance.com"
testNetBaseURLCoin = "https://testnet.binancefuture.com"
ticker24HrEndPointCoin = "/dapi/v1/ticker/24hr"
orderBookEndPointCoin = "/dapi/v1/depth"
exchangeInformationEndPointCoin = "/dapi/v1/exchangeInfo"
klinesEndpointCoin = "/dapi/v1/klines"
listenKeyEndPointCoin = "/dapi/v1/listenKey"
orderEndPointCoin = "/dapi/v1/order"
allOpenOrdersEndPointCoin = "/dapi/v1/allOpenOrders"
futuresAccountBalanceEndpointCoin = "/dapi/v1/balance"
accountInformationEndpointCoin = "/dapi/v1/account"
)
type BinanceCoinFuturesApi struct {
Client *http.Client
BaseUrl string
PublicKey string
SecretKey string
Logger *logrus.Logger
}
func (bcfa *BinanceCoinFuturesApi) PrepareLoggers() {
bcfa.Logger = logrus.New()
bcfa.Logger.Formatter = new(logrus.JSONFormatter)
apilogs, err := os.OpenFile("logs/binance_api.log", os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
bcfa.Logger.SetOutput(apilogs)
} else {
fmt.Println("Failed to log to file for binance api calls, using default stderr")
}
}
func (bcfa *BinanceCoinFuturesApi) SetApiKeys(public, secret string) {
bcfa.PublicKey = public
bcfa.SecretKey = secret
}
func (bcfa *BinanceCoinFuturesApi) NewNetClient() {
// Todo: make sure timeout and handshake won't cause any problems.
var netTransport = &http.Transport{
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
}).DialContext,
DisableKeepAlives: false,
TLSHandshakeTimeout: 2 * time.Second,
ForceAttemptHTTP2: true,
}
bcfa.Client = &http.Client{
Timeout: 0,
Transport: netTransport,
}
}
func (bcfa *BinanceCoinFuturesApi) NewNetClientHTTP2() {
var netTransport = &http2.Transport{}
bcfa.Client = &http.Client{
Timeout: 0,
Transport: netTransport,
}
}
func(bcfa *BinanceCoinFuturesApi) UseMainNet() {
bcfa.BaseUrl = mainNetBaseURLCoin
}
func(bcfa *BinanceCoinFuturesApi) UseTestNet() {
bcfa.BaseUrl = testNetBaseURLCoin
}
// Adds timestamp and creates a signature according to binance rules
func (bcfa BinanceCoinFuturesApi) signParameters(parameters *url.Values) string {
tonce := strconv.FormatInt(time.Now().UnixNano(), 10)[0:13]
parameters.Add("timestamp", tonce)
signature, _ := bcfa.getSha256Signature(parameters.Encode())
return signature
}
// Signs given parameter values
func (bcfa BinanceCoinFuturesApi) getSha256Signature(parameters string) (string, error) {
mac := hmac.New(sha256.New, []byte(bcfa.SecretKey))
_, err := mac.Write([]byte(parameters))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
func (bcfa BinanceCoinFuturesApi) parseResponseBody(body io.ReadCloser) ([]byte, error) {
data, err := ioutil.ReadAll(body)
if err != nil {
bcfa.Logger.Println("Something went wrong during reading request body, ", err)
}
return data, nil
}
func (bcfa BinanceCoinFuturesApi) doPublicRequest(httpVerb, endPoint string, parameters url.Values) ([]byte, error) {
fullURL := bcfa.BaseUrl + endPoint
if parameters != nil {
fullURL += "?" + parameters.Encode()
}
request, _ := http.NewRequest(httpVerb, fullURL, nil)
response, err := bcfa.Client.Do(request)
if err != nil {
// Failure to speak HTTP - Connectivity error
// Non 2XX status does not produce errors
bcfa.Logger.Error("Connectivity error while Client.Do ", err)
return nil, err
}
// Log rate limit for debug purposes
// Even if request results in non 2xx status it provides
// rate limit information
bcfa.Logger.Println(endPoint + ", rate limit used: ",
response.Header.Get("X-Mbx-Used-Weight-1m"))
data, err := bcfa.parseResponseBody(response.Body)
if err != nil {
return nil, err
}
// Non 2xx status does not return error
if response.StatusCode != 200 {
bem := new(BinanceErrorMessage)
err = json.Unmarshal(data, &bem)
if err != nil {
bem = nil
}
err = &RequestError{
StatusCode: response.StatusCode,
UrlUsed: fullURL,
Message: *bem,
}
bcfa.Logger.Error(endPoint, err.Error())
return nil, err
}
return data, nil
}
func (bcfa BinanceCoinFuturesApi) doSignedRequest(httpVerb, endPoint string, parameters url.Values) ([]byte, error) {
signature := bcfa.signParameters(¶meters)
headers := make(http.Header)
headers.Add("X-MBX-APIKEY", bcfa.PublicKey)
fullURL := bcfa.BaseUrl + endPoint + "?" + parameters.Encode() +"&signature=" + signature
request, _ := http.NewRequest(httpVerb, fullURL, nil)
request.Header = headers
response, err := bcfa.Client.Do(request)
if err != nil {
// Failure to speak HTTP - Connectivity error
// Non 2XX status does not produce errors
bcfa.Logger.Error("Connectivity error while Client.Do ", err)
return nil, err
}
// Log rate limit for debug purposes
// Even if request results in non 2xx status it provides
// rate limit information
bcfa.Logger.Println(endPoint + ", rate limit used: ",
response.Header.Get("X-Mbx-Used-Weight-1m"))
data, err := bcfa.parseResponseBody(response.Body)
if err != nil {
return nil, err
}
// Non 2xx status does not return error
if response.StatusCode != 200 {
bem := new(BinanceErrorMessage)
err = json.Unmarshal(data, &bem)
if err != nil {
bem = nil
}
err = &RequestError{
StatusCode: response.StatusCode,
UrlUsed: fullURL,
Message: *bem,
}
bcfa.Logger.Error(endPoint, err.Error())
return nil, err
}
return data, nil
}
func(bcfa BinanceCoinFuturesApi) GetExchangeInformation() ([]byte, error) {
return bcfa.doPublicRequest("GET", exchangeInformationEndPointCoin, nil)
}
// Contains weighted average price (vwap)
func (bcfa BinanceCoinFuturesApi) Get24HourTickerPriceChangeStatistics(symbol string) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
return bcfa.doPublicRequest("GET", ticker24HrEndPointCoin, parameters)
}
func (bcfa BinanceCoinFuturesApi) GetOrderBook(symbol string, limit int) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("limit", fmt.Sprintf("%d", limit))
return bcfa.doPublicRequest("GET", orderBookEndPointCoin, parameters)
}
// limit can be just 1 if only current candle is needed.
func(bcfa BinanceCoinFuturesApi) GetKlines(symbol, interval string, limit int) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("interval", interval)
parameters.Add("limit", fmt.Sprintf("%d", limit))
return bcfa.doPublicRequest("GET", klinesEndpointCoin, parameters)
}
// ======================= SIGNED API CALLS ================================
func (bcfa BinanceCoinFuturesApi) GetUserStreamKey() ([]byte, error) {
return bcfa.doSignedRequest("POST", listenKeyEndPointCoin, url.Values{})
}
// Keepalive a user data stream to prevent a time out. User data streams will close after 60 minutes.
// It's recommended to send a ping about every 60 minutes.
// returns no information, it is completely fine to ignore the byte slice
func (bcfa BinanceCoinFuturesApi) UpdateKeepAliveUserStream() ([]byte, error) {
return bcfa.doSignedRequest("PUT", listenKeyEndPointCoin, url.Values{})
}
// returns no information, it is completely fine to ignore the byte slice
func (bcfa BinanceCoinFuturesApi) DeleteUserStream() ([]byte, error) {
return bcfa.doSignedRequest("DELETE", listenKeyEndPointCoin, url.Values{})
}
func (bcfa BinanceCoinFuturesApi) PlaceLimitOrder(symbol, side string, price, qty float64, reduceOnly bool) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("side", side)
parameters.Add("type", OrderTypeLimit)
parameters.Add("timeInForce", GoodTillCancel)
parameters.Add("reduceOnly", strconv.FormatBool(reduceOnly))
parameters.Add("quantity", strconv.FormatFloat(qty, 'f', -1, 64))
parameters.Add("price", strconv.FormatFloat(price, 'f', -1, 64))
return bcfa.doSignedRequest("POST", orderEndPointCoin, parameters)
}
func (bcfa BinanceCoinFuturesApi) PlacePostOnlyLimitOrder(symbol, side string, price, qty float64, reduceOnly bool) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("side", side)
parameters.Add("type", OrderTypeLimit)
parameters.Add("timeInForce", GoodTillCrossing)
parameters.Add("reduceOnly", strconv.FormatBool(reduceOnly))
parameters.Add("quantity", strconv.FormatFloat(qty, 'f', -1, 64))
parameters.Add("price", strconv.FormatFloat(price, 'f', -1, 64))
return bcfa.doSignedRequest("POST", orderEndPointCoin, parameters)
}
func (bcfa BinanceCoinFuturesApi) PlaceMarketOrder(symbol, side string, qty float64, reduceOnly bool) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("side", side)
parameters.Add("type", OrderTypeMarket)
parameters.Add("reduceOnly", strconv.FormatBool(reduceOnly))
parameters.Add("quantity", strconv.FormatFloat(qty, 'f', -1, 64))
return bcfa.doSignedRequest("POST", orderEndPointCoin, parameters)
}
// PlaceStopMarketOrder Generally used for trailing profit orders.
// Binance only allows one stop market order to be active
// after initial order any secondary orders will replace the first one.
// In order to use this method as take profit tool
// use the same side as your position side.
func (bcfa BinanceCoinFuturesApi) PlaceStopMarketOrder(symbol, side string, stopPrice, qty float64) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("side", side)
parameters.Add("type", OrderTypeStopMarket)
parameters.Add("reduceOnly", "true")
parameters.Add("quantity", strconv.FormatFloat(qty, 'f', -1, 64))
parameters.Add("stopPrice", strconv.FormatFloat(stopPrice, 'f', -1, 64))
return bcfa.doSignedRequest("POST", orderEndPointCoin, parameters)
}
func (bcfa BinanceCoinFuturesApi) CancelSingleOrder(symbol, origClientOrderId string, orderId int64) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
parameters.Add("orderId", strconv.FormatInt(orderId, 10))
parameters.Add("origClientOrderId", origClientOrderId)
return bcfa.doSignedRequest("DELETE", orderEndPointCoin, parameters)
}
func (bcfa BinanceCoinFuturesApi) CancelAllOrders(symbol string) ([]byte, error) {
parameters := url.Values{}
parameters.Add("symbol", symbol)
return bcfa.doSignedRequest("DELETE", allOpenOrdersEndPointCoin, parameters)
}
func (bcfa BinanceCoinFuturesApi) GetAccountBalance() ([]byte, error) {
return bcfa.doSignedRequest("GET", futuresAccountBalanceEndpointCoin, url.Values{})
}
func (bcfa BinanceCoinFuturesApi) GetAccountInformation() ([]byte, error) {
return bcfa.doSignedRequest("GET", accountInformationEndpointCoin, url.Values{})
}