|
| 1 | +package binance |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/rs/zerolog/log" |
| 14 | + |
| 15 | + "github.com/marianogappa/crypto-candles/common" |
| 16 | +) |
| 17 | + |
| 18 | +type errorResponse struct { |
| 19 | + Code int `json:"code"` |
| 20 | + Msg string `json:"msg"` |
| 21 | +} |
| 22 | + |
| 23 | +func (r errorResponse) toError() error { |
| 24 | + if r.Code == 0 && r.Msg == "" { |
| 25 | + return nil |
| 26 | + } |
| 27 | + if r.Code == eRRINVALIDSYMBOL { |
| 28 | + return common.ErrInvalidMarketPair |
| 29 | + } |
| 30 | + return fmt.Errorf("binance returned error code! Code: %v, Message: %v", r.Code, r.Msg) |
| 31 | +} |
| 32 | + |
| 33 | +// [ |
| 34 | +// [ |
| 35 | +// 1499040000000, // Open time |
| 36 | +// "0.01634790", // Open |
| 37 | +// "0.80000000", // High |
| 38 | +// "0.01575800", // Low |
| 39 | +// "0.01577100", // Close |
| 40 | +// "148976.11427815", // Volume |
| 41 | +// 1499644799999, // Close time |
| 42 | +// "2434.19055334", // Quote asset volume |
| 43 | +// 308, // Number of trades |
| 44 | +// "1756.87402397", // Taker buy base asset volume |
| 45 | +// "28.46694368", // Taker buy quote asset volume |
| 46 | +// "17928899.62484339" // Ignore. |
| 47 | +// ] |
| 48 | +// ] |
| 49 | +type successfulResponse struct { |
| 50 | + ResponseCandlesticks [][]interface{} |
| 51 | +} |
| 52 | + |
| 53 | +func interfaceToFloatRoundInt(i interface{}) (int, bool) { |
| 54 | + f, ok := i.(float64) |
| 55 | + if !ok { |
| 56 | + return 0, false |
| 57 | + } |
| 58 | + return int(f), true |
| 59 | +} |
| 60 | + |
| 61 | +func (r successfulResponse) toCandlesticks() ([]common.Candlestick, error) { |
| 62 | + candlesticks := make([]common.Candlestick, len(r.ResponseCandlesticks)) |
| 63 | + for i := 0; i < len(r.ResponseCandlesticks); i++ { |
| 64 | + raw := r.ResponseCandlesticks[i] |
| 65 | + candlestick := binanceCandlestick{} |
| 66 | + if len(raw) != 12 { |
| 67 | + return candlesticks, fmt.Errorf("candlestick %v has len != 12! Invalid syntax from Binance", i) |
| 68 | + } |
| 69 | + rawOpenTime, ok := interfaceToFloatRoundInt(raw[0]) |
| 70 | + if !ok { |
| 71 | + return candlesticks, fmt.Errorf("candlestick %v has non-int open time! Invalid syntax from Binance", i) |
| 72 | + } |
| 73 | + candlestick.openAt = time.Unix(0, int64(rawOpenTime)*int64(time.Millisecond)) |
| 74 | + |
| 75 | + rawOpen, ok := raw[1].(string) |
| 76 | + if !ok { |
| 77 | + return candlesticks, fmt.Errorf("candlestick %v has non-string open! Invalid syntax from Binance", i) |
| 78 | + } |
| 79 | + openPrice, err := strconv.ParseFloat(rawOpen, 64) |
| 80 | + if err != nil { |
| 81 | + return candlesticks, fmt.Errorf("candlestick %v had open = %v! Invalid syntax from Binance", i, openPrice) |
| 82 | + } |
| 83 | + candlestick.openPrice = openPrice |
| 84 | + |
| 85 | + rawHigh, ok := raw[2].(string) |
| 86 | + if !ok { |
| 87 | + return candlesticks, fmt.Errorf("candlestick %v has non-string high! Invalid syntax from Binance", i) |
| 88 | + } |
| 89 | + highPrice, err := strconv.ParseFloat(rawHigh, 64) |
| 90 | + if err != nil { |
| 91 | + return candlesticks, fmt.Errorf("candlestick %v had high = %v! Invalid syntax from Binance", i, highPrice) |
| 92 | + } |
| 93 | + candlestick.highPrice = highPrice |
| 94 | + |
| 95 | + rawLow, ok := raw[3].(string) |
| 96 | + if !ok { |
| 97 | + return candlesticks, fmt.Errorf("candlestick %v has non-string low! Invalid syntax from Binance", i) |
| 98 | + } |
| 99 | + lowPrice, err := strconv.ParseFloat(rawLow, 64) |
| 100 | + if err != nil { |
| 101 | + return candlesticks, fmt.Errorf("candlestick %v had low = %v! Invalid syntax from Binance", i, lowPrice) |
| 102 | + } |
| 103 | + candlestick.lowPrice = lowPrice |
| 104 | + |
| 105 | + rawClose, ok := raw[4].(string) |
| 106 | + if !ok { |
| 107 | + return candlesticks, fmt.Errorf("candlestick %v has non-string close! Invalid syntax from Binance", i) |
| 108 | + } |
| 109 | + closePrice, err := strconv.ParseFloat(rawClose, 64) |
| 110 | + if err != nil { |
| 111 | + return candlesticks, fmt.Errorf("candlestick %v had close = %v! Invalid syntax from Binance", i, closePrice) |
| 112 | + } |
| 113 | + candlestick.closePrice = closePrice |
| 114 | + |
| 115 | + rawVolume, ok := raw[5].(string) |
| 116 | + if !ok { |
| 117 | + return candlesticks, fmt.Errorf("candlestick %v has non-string volume! Invalid syntax from Binance", i) |
| 118 | + } |
| 119 | + volume, err := strconv.ParseFloat(rawVolume, 64) |
| 120 | + if err != nil { |
| 121 | + return candlesticks, fmt.Errorf("candlestick %v had volume = %v! Invalid syntax from Binance", i, volume) |
| 122 | + } |
| 123 | + candlestick.volume = volume |
| 124 | + |
| 125 | + rawCloseTime, ok := interfaceToFloatRoundInt(raw[6]) |
| 126 | + if !ok { |
| 127 | + return candlesticks, fmt.Errorf("candlestick %v has non-int close time! Invalid syntax from Binance", i) |
| 128 | + } |
| 129 | + candlestick.closeAt = time.Unix(0, int64(rawCloseTime)*int64(time.Millisecond)) |
| 130 | + |
| 131 | + rawQuoteAssetVolume, ok := raw[7].(string) |
| 132 | + if !ok { |
| 133 | + return candlesticks, fmt.Errorf("candlestick %v has non-string quote asset volume! Invalid syntax from Binance", i) |
| 134 | + } |
| 135 | + quoteAssetVolume, err := strconv.ParseFloat(rawQuoteAssetVolume, 64) |
| 136 | + if err != nil { |
| 137 | + return candlesticks, fmt.Errorf("candlestick %v had quote asset volume = %v! Invalid syntax from Binance", i, quoteAssetVolume) |
| 138 | + } |
| 139 | + candlestick.quoteAssetVolume = quoteAssetVolume |
| 140 | + |
| 141 | + rawNumberOfTrades, ok := interfaceToFloatRoundInt(raw[8]) |
| 142 | + if !ok { |
| 143 | + return candlesticks, fmt.Errorf("candlestick %v has non-int number of trades! Invalid syntax from Binance", i) |
| 144 | + } |
| 145 | + candlestick.tradeCount = rawNumberOfTrades |
| 146 | + |
| 147 | + rawTakerBaseAssetVolume, ok := raw[9].(string) |
| 148 | + if !ok { |
| 149 | + return candlesticks, fmt.Errorf("candlestick %v has non-string taker base asset volume! Invalid syntax from Binance", i) |
| 150 | + } |
| 151 | + takerBaseAssetVolume, err := strconv.ParseFloat(rawTakerBaseAssetVolume, 64) |
| 152 | + if err != nil { |
| 153 | + return candlesticks, fmt.Errorf("candlestick %v had taker base asset volume = %v! Invalid syntax from Binance", i, takerBaseAssetVolume) |
| 154 | + } |
| 155 | + candlestick.takerBuyBaseAssetVolume = takerBaseAssetVolume |
| 156 | + |
| 157 | + rawTakerQuoteAssetVolume, ok := raw[10].(string) |
| 158 | + if !ok { |
| 159 | + return candlesticks, fmt.Errorf("candlestick %v has non-string taker quote asset volume! Invalid syntax from Binance", i) |
| 160 | + } |
| 161 | + takerBuyQuoteAssetVolume, err := strconv.ParseFloat(rawTakerQuoteAssetVolume, 64) |
| 162 | + if err != nil { |
| 163 | + return candlesticks, fmt.Errorf("candlestick %v had taker quote asset volume = %v! Invalid syntax from Binance", i, takerBuyQuoteAssetVolume) |
| 164 | + } |
| 165 | + candlestick.takerBuyQuoteAssetVolume = takerBuyQuoteAssetVolume |
| 166 | + |
| 167 | + candlesticks[i] = candlestick.toCandlestick() |
| 168 | + } |
| 169 | + |
| 170 | + return candlesticks, nil |
| 171 | +} |
| 172 | + |
| 173 | +type binanceCandlestick struct { |
| 174 | + openAt time.Time |
| 175 | + closeAt time.Time |
| 176 | + openPrice float64 |
| 177 | + closePrice float64 |
| 178 | + lowPrice float64 |
| 179 | + highPrice float64 |
| 180 | + volume float64 |
| 181 | + quoteAssetVolume float64 |
| 182 | + tradeCount int |
| 183 | + takerBuyBaseAssetVolume float64 |
| 184 | + takerBuyQuoteAssetVolume float64 |
| 185 | +} |
| 186 | + |
| 187 | +func (c binanceCandlestick) toCandlestick() common.Candlestick { |
| 188 | + return common.Candlestick{ |
| 189 | + Timestamp: int(c.openAt.Unix()), |
| 190 | + OpenPrice: common.JSONFloat64(c.openPrice), |
| 191 | + ClosePrice: common.JSONFloat64(c.closePrice), |
| 192 | + LowestPrice: common.JSONFloat64(c.lowPrice), |
| 193 | + HighestPrice: common.JSONFloat64(c.highPrice), |
| 194 | + Volume: common.JSONFloat64(c.volume), |
| 195 | + NumberOfTrades: c.tradeCount, |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func (e *Binance) requestCandlesticks(baseAsset string, quoteAsset string, startTimeTs int, intervalMinutes int) ([]common.Candlestick, error) { |
| 200 | + req, _ := http.NewRequest("GET", fmt.Sprintf("%vklines", e.apiURL), nil) |
| 201 | + symbol := fmt.Sprintf("%v%v", strings.ToUpper(baseAsset), strings.ToUpper(quoteAsset)) |
| 202 | + |
| 203 | + q := req.URL.Query() |
| 204 | + q.Add("symbol", symbol) |
| 205 | + |
| 206 | + switch intervalMinutes { |
| 207 | + case 1: |
| 208 | + q.Add("interval", "1m") |
| 209 | + case 3: |
| 210 | + q.Add("interval", "3m") |
| 211 | + case 5: |
| 212 | + q.Add("interval", "5m") |
| 213 | + case 15: |
| 214 | + q.Add("interval", "15m") |
| 215 | + case 30: |
| 216 | + q.Add("interval", "30m") |
| 217 | + case 1 * 60: |
| 218 | + q.Add("interval", "1h") |
| 219 | + case 2 * 60: |
| 220 | + q.Add("interval", "2h") |
| 221 | + case 4 * 60: |
| 222 | + q.Add("interval", "4h") |
| 223 | + case 6 * 60: |
| 224 | + q.Add("interval", "6h") |
| 225 | + case 8 * 60: |
| 226 | + q.Add("interval", "8h") |
| 227 | + case 12 * 60: |
| 228 | + q.Add("interval", "12h") |
| 229 | + case 1 * 60 * 24: |
| 230 | + q.Add("interval", "1d") |
| 231 | + case 3 * 60 * 24: |
| 232 | + q.Add("interval", "3d") |
| 233 | + case 7 * 60 * 24: |
| 234 | + q.Add("interval", "1w") |
| 235 | + // TODO This one is problematic because cannot patch holes or do other calculations (because months can have 28, 29, 30 & 31 days) |
| 236 | + case 30 * 60 * 24: |
| 237 | + q.Add("interval", "1M") |
| 238 | + default: |
| 239 | + return nil, common.CandleReqError{IsNotRetryable: true, Err: common.ErrUnsupportedCandlestickInterval} |
| 240 | + } |
| 241 | + q.Add("limit", "1000") |
| 242 | + q.Add("startTime", fmt.Sprintf("%v", startTimeTs*1000)) |
| 243 | + |
| 244 | + req.URL.RawQuery = q.Encode() |
| 245 | + |
| 246 | + client := &http.Client{Timeout: 10 * time.Second} |
| 247 | + |
| 248 | + resp, err := client.Do(req) |
| 249 | + if err != nil { |
| 250 | + return nil, common.CandleReqError{IsNotRetryable: true, Err: common.ErrExecutingRequest} |
| 251 | + } |
| 252 | + defer resp.Body.Close() |
| 253 | + |
| 254 | + byts, err := ioutil.ReadAll(resp.Body) |
| 255 | + if err != nil { |
| 256 | + return nil, common.CandleReqError{IsNotRetryable: false, IsExchangeSide: true, Err: common.ErrBrokenBodyResponse} |
| 257 | + } |
| 258 | + |
| 259 | + maybeErrorResponse := errorResponse{} |
| 260 | + err = json.Unmarshal(byts, &maybeErrorResponse) |
| 261 | + errResp := maybeErrorResponse.toError() |
| 262 | + if err == nil && errResp != nil { |
| 263 | + var retryAfter time.Duration |
| 264 | + if resp.StatusCode == http.StatusTooManyRequests && len(resp.Header["Retry-After"]) == 1 { |
| 265 | + seconds, _ := strconv.Atoi(resp.Header["Retry-After"][0]) |
| 266 | + retryAfter = time.Duration(seconds) * time.Second |
| 267 | + } |
| 268 | + |
| 269 | + return nil, common.CandleReqError{ |
| 270 | + IsNotRetryable: false, |
| 271 | + IsExchangeSide: true, |
| 272 | + Code: maybeErrorResponse.Code, |
| 273 | + Err: errors.New(maybeErrorResponse.Msg), |
| 274 | + RetryAfter: retryAfter, |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + maybeResponse := successfulResponse{} |
| 279 | + err = json.Unmarshal(byts, &maybeResponse.ResponseCandlesticks) |
| 280 | + if err != nil { |
| 281 | + return nil, common.CandleReqError{IsNotRetryable: false, IsExchangeSide: true, Err: common.ErrInvalidJSONResponse} |
| 282 | + } |
| 283 | + |
| 284 | + candlesticks, err := maybeResponse.toCandlesticks() |
| 285 | + if err != nil { |
| 286 | + return nil, common.CandleReqError{IsNotRetryable: false, IsExchangeSide: true, Err: err} |
| 287 | + } |
| 288 | + |
| 289 | + if len(candlesticks) == 0 { |
| 290 | + return nil, common.CandleReqError{IsNotRetryable: false, IsExchangeSide: true, Err: common.ErrOutOfCandlesticks} |
| 291 | + } |
| 292 | + |
| 293 | + if e.debug { |
| 294 | + log.Info().Str("exchange", "Binance").Str("market", fmt.Sprintf("%v/%v", baseAsset, quoteAsset)).Int("candlestick_count", len(candlesticks)).Msg("Candlestick request successful!") |
| 295 | + } |
| 296 | + |
| 297 | + return candlesticks, nil |
| 298 | +} |
| 299 | + |
| 300 | +// Example request for klines on Binance: |
| 301 | +// https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1m&limit=3&startTime=1642329924000 |
| 302 | +// For 1m interval and date Sunday, January 16, 2022 10:45:24 AM (UTC) |
| 303 | +// |
| 304 | +// Returns |
| 305 | +// |
| 306 | +// [ |
| 307 | +// [ |
| 308 | +// 1642329960000, // Sunday, January 16, 2022 10:46:00 AM |
| 309 | +// "43086.22000000", |
| 310 | +// "43086.22000000", |
| 311 | +// "43069.48000000", |
| 312 | +// "43070.00000000", |
| 313 | +// "8.65209000", |
| 314 | +// 1642330019999, |
| 315 | +// "372709.68472200", |
| 316 | +// 384, |
| 317 | +// "2.52145000", |
| 318 | +// "108606.91496040", |
| 319 | +// "0" |
| 320 | +// ], |
| 321 | +// [ |
| 322 | +// 1642330020000, // Sunday, January 16, 2022 10:47:00 AM |
| 323 | +// "43070.00000000", |
| 324 | +// "43079.63000000", |
| 325 | +// "43069.99000000", |
| 326 | +// "43072.60000000", |
| 327 | +// "5.54560000", |
| 328 | +// 1642330079999, |
| 329 | +// "238872.65921540", |
| 330 | +// 348, |
| 331 | +// "2.52414000", |
| 332 | +// "108722.43274820", |
| 333 | +// "0" |
| 334 | +// ], |
| 335 | +// [ |
| 336 | +// 1642330080000, // Sunday, January 16, 2022 10:48:00 AM |
| 337 | +// "43072.59000000", |
| 338 | +// "43072.60000000", |
| 339 | +// "43068.13000000", |
| 340 | +// "43071.18000000", |
| 341 | +// "4.13011000", |
| 342 | +// 1642330139999, |
| 343 | +// "177888.74219360", |
| 344 | +// 344, |
| 345 | +// "1.53302000", |
| 346 | +// "66029.17746930", |
| 347 | +// "0" |
| 348 | +// ] |
| 349 | +// ] |
| 350 | +// |
| 351 | +// Binance uses the strategy of having candlesticks on multiples of an hour or a day, and truncating the requested |
| 352 | +// millisecond timestamps to the closest mutiple in the future. To test this, use the following snippet: |
| 353 | +// |
| 354 | +// curl "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=3m&limit=60&startTime=1642330104000" | jq '.[] | .[0] | . / 1000 | todate' |
| 355 | +// |
| 356 | +// And test with these millisecond timestamps: |
| 357 | +// |
| 358 | +// 1642329924000 = Sunday, January 16, 2022 10:45:24 AM |
| 359 | +// 1642329984000 = Sunday, January 16, 2022 10:46:24 AM |
| 360 | +// 1642330044000 = Sunday, January 16, 2022 10:47:24 AM |
| 361 | +// 1642330104000 = Sunday, January 16, 2022 10:48:24 AM |
| 362 | +// |
| 363 | +// On the 1m interval, candlesticks exist at every minute |
| 364 | +// On the 3m interval, candlesticks exist at: 00, 03, 06 ... |
| 365 | +// On the 5m interval, candlesticks exist at: 00, 05, 10 ... |
| 366 | +// On the 15m interval, candlesticks exist at: 00, 15, 30 & 45 |
| 367 | +// On the 30m interval, candlesticks exist at: 00 & 30 |
| 368 | +// On the 1h interval, candlesticks exist at: 00 |
| 369 | +// On the 2h interval, candlesticks exist at: 00:00, 02:00, 04:00 ... |
| 370 | +// On the 4h interval, candlesticks exist at: 00:00, 04:00, 08:00 ... |
| 371 | +// On the 8h interval, candlesticks exist at: 00:00, 08:00 & 16:00 ... |
| 372 | +// On the 12h interval, candlesticks exist at: 00:00 & 12:00 |
| 373 | +// On the 1d interval, candlesticks exist at every day |
| 374 | +// On the 3d interval, things become interesting because months can have 28, 29, 30 & 31 days, but it follows the time.Truncate(3 day) logic |
| 375 | +// On the 1w interval, it also follows the time.Truncate(7 day) logic |
| 376 | +// On the 1M interval, candlesticks exist at the beginning of each month |
0 commit comments