github.com/frankrap/okex-api@v1.0.4/swap_api.go (about) 1 package okex 2 3 /* 4 OKEX Swap Api 5 @author Lingting Fu 6 @date 2018-12-27 7 @version 1.0.0 8 */ 9 10 import ( 11 "errors" 12 "log" 13 "strings" 14 ) 15 16 /* 17 获取某个合约的持仓信息 18 GET /api/swap/v3/<instrument_id>/position 19 */ 20 func (client *Client) GetSwapPositionByInstrument(instrumentId string) (SwapPosition, error) { 21 22 sp := SwapPosition{} 23 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_POSITION, instrumentId), nil, &sp); err != nil { 24 return SwapPosition{}, err 25 } 26 return sp, nil 27 } 28 29 /* 30 所有合约持仓信息 31 获取所有合约的持仓信息 32 限速规则:1次/10s 33 GET /api/swap/v3/position 34 */ 35 func (client *Client) GetSwapPositions() (*SwapPositionList, error) { 36 37 sp := SwapPositionList{} 38 if _, _, err := client.Request(GET, SWAP_POSITION, nil, &sp); err != nil { 39 return nil, err 40 } 41 return &sp, nil 42 } 43 44 func (client *Client) getSwapAccounts(uri string) (SwapAccounts, error) { 45 sa := SwapAccounts{} 46 if _, _, err := client.Request(GET, uri, nil, &sa); err != nil { 47 return SwapAccounts{}, err 48 } 49 return sa, nil 50 } 51 52 /* 53 获取所有币种合约的账户信息 54 HTTP请求 55 GET /api/swap/v3/accounts 56 */ 57 func (client *Client) GetSwapAccounts() (SwapAccounts, error) { 58 return client.getSwapAccounts(SWAP_ACCOUNTS) 59 } 60 61 /* 62 单个币种合约账户信息 63 HTTP请求 64 GET /api/swap/v3/<instrument_id>/accounts 65 */ 66 func (client *Client) GetSwapAccount(instrumentId string) (SwapAccount, error) { 67 68 sa := SwapAccount{} 69 uri := GetInstrumentIdUri(SWAP_INSTRUMENT_ACCOUNT, instrumentId) 70 if _, _, err := client.Request(GET, uri, nil, &sa); err != nil { 71 return SwapAccount{}, err 72 } 73 return sa, nil 74 } 75 76 /* 77 获取某个合约的杠杆倍数,持仓模式 78 79 HTTP请求 80 GET /api/swap/v3/accounts/<instrument_id>/settings 81 */ 82 func (client *Client) GetSwapAccountsSettingsByInstrument(instrumentId string) (SwapAccountsSetting, error) { 83 as := SwapAccountsSetting{} 84 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_ACCOUNTS_SETTINGS, instrumentId), nil, &as); err != nil { 85 return SwapAccountsSetting{}, err 86 } 87 return as, nil 88 } 89 90 /* 91 设定某个合约的杠杆倍数 92 93 HTTP请求 94 POST /api/swap/v3/accounts/<instrument_id>/leverage 95 */ 96 func (client *Client) PostSwapAccountsLeverage(instrumentId string, leverage string, side string) ([]byte, SwapAccountsSetting, error) { 97 params := make(map[string]string) 98 params["leverage"] = leverage 99 params["side"] = side 100 as := SwapAccountsSetting{} 101 var respBody []byte 102 var err error 103 if respBody, _, err = client.Request(POST, GetInstrumentIdUri(SWAP_ACCOUNTS_LEVERAGE, instrumentId), params, &as); err != nil { 104 return respBody, SwapAccountsSetting{}, err 105 } 106 return respBody, as, nil 107 } 108 109 /* 110 账单流水查询 111 列出账户资产流水,账户资产流水是指导致账户余额增加或减少的行为。流水会分页,每页100条数据,并且按照时间倒序排序和存储,最新的排在最前面。 112 113 HTTP请求 114 GET /api/swap/v3/accounts/<instrument_id>/ledger 115 */ 116 func (client *Client) GetSwapAccountLedger(instrumentId string, optionalParams map[string]string) (*SwapAccountsLedgerList, error) { 117 baseUri := GetInstrumentIdUri(SWAP_ACCOUNTS_LEDGER, instrumentId) 118 uri := baseUri 119 if optionalParams != nil { 120 uri = BuildParams(baseUri, optionalParams) 121 } 122 ll := SwapAccountsLedgerList{} 123 if _, _, err := client.Request(GET, uri, nil, &ll); err != nil { 124 return nil, err 125 } 126 return &ll, nil 127 } 128 129 /* 130 API交易提供限价单下单模式,只有当您的账户有足够的资金才能下单。一旦下单,您的账户资金将在订单生命周期内被冻结,被冻结的资金以及数量取决于订单指定的类型和参数。 131 132 HTTP请求 133 POST /api/swap/v3/order 134 */ 135 func (client *Client) PostSwapOrder(instrumentId string, order BasePlaceOrderInfo) ([]byte, SwapOrderResult, error) { 136 or := SwapOrderResult{} 137 info := PlaceOrderInfo{order, instrumentId} 138 var respBody []byte 139 var err error 140 if respBody, _, err = client.Request(POST, SWAP_ORDER, info, &or); err != nil { 141 return respBody, SwapOrderResult{}, err 142 } 143 return respBody, or, nil 144 } 145 146 /* 147 批量进行下单请求。 148 149 HTTP请求 150 POST /api/swap/v3/orders 151 */ 152 func (client *Client) PostSwapOrders(instrumentId string, orders []*BasePlaceOrderInfo) ([]byte, *SwapOrdersResult, error) { 153 sor := SwapOrdersResult{} 154 orderData := PlaceOrdersInfo{InstrumentId: instrumentId, OrderData: orders} 155 var respBody []byte 156 var err error 157 if respBody, _, err = client.Request(POST, SWAP_ORDERS, orderData, &sor); err != nil { 158 return respBody, nil, err 159 } 160 return respBody, &sor, nil 161 } 162 163 /* 164 撤销之前下的未完成订单。 165 166 HTTP请求 167 POST /api/swap/v3/cancel_order/<instrument_id>/<order_id> 168 */ 169 func (client *Client) PostSwapCancelOrder(instrumentId string, orderId string) ([]byte, SwapCancelOrderResult, error) { 170 uri := "/api/swap/v3/cancel_order/" + instrumentId + "/" + orderId 171 or := SwapCancelOrderResult{} 172 var respBody []byte 173 var err error 174 if respBody, _, err = client.Request(POST, uri, nil, &or); err != nil { 175 return respBody, SwapCancelOrderResult{}, err 176 } 177 return respBody, or, nil 178 } 179 180 /* 181 批量撤销之前下的未完成订单。 182 183 HTTP请求 184 POST /api/swap/v3/cancel_batch_orders/<instrument_id> 185 */ 186 func (client *Client) PostSwapBatchCancelOrders(instrumentId string, orderIds []string) ([]byte, *SwapCancelOrderResult, error) { 187 uri := GetInstrumentIdUri(SWAP_CANCEL_BATCH_ORDERS, instrumentId) 188 or := SwapCancelOrderResult{} 189 190 params := map[string]interface{}{} 191 params["ids"] = orderIds 192 193 var respBody []byte 194 var err error 195 if respBody, _, err = client.Request(POST, uri, params, &or); err != nil { 196 return respBody, nil, err 197 } 198 return respBody, &or, nil 199 } 200 201 /* 202 列出您当前所有的订单信息。 203 204 HTTP请求 205 GET /api/swap/v3/orders/<instrument_id> 206 207 请求示例 208 GET /api/swap/v3/orders/BTC-USD-SWAP?status=2&from=4&limit=30 209 */ 210 func (client *Client) GetSwapOrderByInstrumentId(instrumentId string, paramMap map[string]string) (*SwapOrdersInfo, error) { 211 if paramMap["status"] == "" || len(instrumentId) == 0 { 212 return nil, errors.New("Request Parameter's not correct, instrument_id and status is required.") 213 } 214 215 baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_ORDER_LIST, instrumentId) 216 kvParams := BuildOrderParams(paramMap) 217 uri := baseUri + "?" + kvParams 218 soi := SwapOrdersInfo{} 219 220 if _, _, err := client.Request(GET, uri, nil, &soi); err != nil { 221 return nil, err 222 } 223 return &soi, nil 224 } 225 226 /* 227 通过订单id获取单个订单信息。 228 229 HTTP请求 230 GET /api/swap/v3/orders/<instrument_id>/<order_id> 231 232 请求示例 233 GET /api/swap/v3/orders/BTC-USD-SWAP/64-2a-26132f931-3 234 */ 235 func (client *Client) GetSwapOrderByOrderId(instrumentId string, orderId string) (BaseOrderInfo, error) { 236 return client.GetSwapOrderById(instrumentId, orderId) 237 } 238 239 /* 240 获取订单信息 241 通过订单id获取单个订单信息。 242 243 限速规则:40次/2s 244 HTTP请求 245 GET /api/swap/v3/orders/<instrument_id>/<order_id> 246 or 247 GET /api/swap/v3/orders/<instrument_id>/<client_oid> 248 */ 249 func (client *Client) GetSwapOrderById(instrumentId, orderOrClientId string) (BaseOrderInfo, error) { 250 orderInfo := BaseOrderInfo{} 251 baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_ORDER_BY_ID, instrumentId) 252 uri := strings.Replace(baseUri, "{order_client_id}", orderOrClientId, -1) 253 254 if r, _, err := client.Request(GET, uri, nil, &orderInfo); err != nil { 255 log.Printf("r: %v", string(r)) 256 return BaseOrderInfo{}, err 257 } 258 259 return orderInfo, nil 260 } 261 262 /* 263 获取最近的成交明细列表。 264 265 HTTP请求 266 GET /api/swap/v3/fills 267 268 请求示例 269 GET /api/swap/v3/fills?order_id=64-2b-16122f931-3&instrument_id=BTC-USD-SWAP&from=1&limit=50(返回BTC-USD-SWAP中order_id为64-2b-16122f931-3的订单中第1页前50笔成交信息) 270 */ 271 func (client *Client) GetSwapFills(instrumentId string, orderId string, options map[string]string) (interface{}, error) { 272 m := make(map[string]string) 273 m["instrument_id"] = instrumentId 274 m["order_id"] = orderId 275 276 m["from"] = options["from"] 277 m["to"] = options["to"] 278 m["limit"] = options["limit"] 279 280 uri := BuildParams(SWAP_FILLS, m) 281 sfi := SwapFillsInfo{} 282 283 if _, _, err := client.Request(GET, uri, nil, &sfi); err != nil { 284 return nil, err 285 } 286 287 return &sfi, nil 288 } 289 290 /* 291 获取可用合约的列表,这组公开接口提供了行情数据的快照,无需认证即可调用。 获取可用合约的列表,查询各合约的交易限制和价格步长等信息。 292 293 HTTP请求 294 GET /api/swap/v3/instruments 295 */ 296 func (client *Client) GetSwapInstruments() (SwapInstrumentList, error) { 297 sil := SwapInstrumentList{} 298 if _, _, err := client.Request(GET, SWAP_INSTRUMENTS, nil, &sil); err != nil { 299 return SwapInstrumentList{}, err 300 } 301 302 return sil, nil 303 } 304 305 /* 306 获取合约的深度列表。 307 308 HTTP请求 309 GET /api/swap/v3/instruments/<instrument_id>/depth 310 311 请求示例 312 GET /api/swap/v3/instruments/<instrument_id>/depth?size=50 313 */ 314 func (client *Client) GetSwapDepthByInstrumentId(instrumentId string, optionalParams map[string]string) (SwapInstrumentDepth, error) { 315 sid := SwapInstrumentDepth{} 316 params := NewParams() 317 if optionalParams != nil { 318 if v, ok := optionalParams["size"]; ok { 319 params["size"] = v 320 } 321 if v, ok := optionalParams["depth"]; ok { 322 params["depth"] = v 323 } 324 } 325 requestPath := BuildParams(GetInstrumentIdUri(SWAP_INSTRUMENT_DEPTH, instrumentId), params) 326 327 if _, _, err := client.Request(GET, requestPath, nil, &sid); err != nil { 328 return SwapInstrumentDepth{}, err 329 } 330 331 return sid, nil 332 } 333 334 /* 335 获取平台全部合约的最新成交价、买一价、卖一价和24交易量。 336 337 HTTP请求 338 GET /api/swap/v3/instruments/ticker 339 */ 340 func (client *Client) GetSwapInstrumentsTicker() (*SwapTickerList, error) { 341 stl := SwapTickerList{} 342 if _, _, err := client.Request(GET, SWAP_INSTRUMENTS_TICKER, nil, &stl); err != nil { 343 return nil, err 344 } 345 346 return &stl, nil 347 } 348 349 /* 350 获取合约的最新成交价、买一价、卖一价和24交易量。 351 352 HTTP请求 353 GET /api/swap/v3/instruments/<instrument_id>/ticker 354 */ 355 func (client *Client) GetSwapTickerByInstrument(instrumentId string) (*BaseTickerInfo, error) { 356 bti := BaseTickerInfo{} 357 uri := GetInstrumentIdUri(SWAP_INSTRUMENT_TICKER, instrumentId) 358 if _, _, err := client.Request(GET, uri, nil, &bti); err != nil { 359 return nil, err 360 } 361 362 bti.InstrumentId = instrumentId 363 return &bti, nil 364 } 365 366 /* 367 获取合约的成交记录。 368 369 HTTP请求 370 GET /api/swap/v3/instruments/<instrument_id>/trades 371 372 请求示例 373 GET /api/swap/v3/instruments/BTC-USD-SWAP/trades?from=1&limit=50 374 */ 375 func (client *Client) GetSwapTradesByInstrument(instrumentId string, optionalParams map[string]string) (*SwapTradeList, error) { 376 stl := SwapTradeList{} 377 baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_TRADES, instrumentId) 378 uri := BuildParams(baseUri, optionalParams) 379 if _, _, err := client.Request(GET, uri, nil, &stl); err != nil { 380 return nil, err 381 } 382 return &stl, nil 383 } 384 385 /* 386 获取合约的K线数据。 387 388 HTTP请求 389 GET /api/swap/v3/instruments/<instrument_id>/candles 390 391 请求示例 392 GET /api/swap/v3/instruments/BTC-USD-SWAP/candles?start=2018-10-26T02:31:00.000Z&end=2018-10-26T02:55:00.000Z&granularity=60(查询BTC-USD-SWAP的2018年10月26日02点31分到2018年10月26日02点55分的1分钟K线数据) 393 */ 394 func (client *Client) GetSwapCandlesByInstrument(instrumentId string, optionalParams map[string]string) (*SwapCandleList, error) { 395 scl := SwapCandleList{} 396 baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_CANDLES, instrumentId) 397 uri := baseUri 398 if len(optionalParams) > 0 { 399 uri = BuildParams(baseUri, optionalParams) 400 } 401 if _, _, err := client.Request(GET, uri, nil, &scl); err != nil { 402 return nil, err 403 } 404 return &scl, nil 405 406 } 407 408 /* 409 获取币种指数。 410 411 HTTP请求 412 GET /api/swap/v3/instruments/<instrument_id>/index 413 414 请求示例 415 GET /api/swap/v3/instruments/BTC-USD-SWAP/index 416 */ 417 func (client *Client) GetSwapIndexByInstrument(instrumentId string) (*SwapIndexInfo, error) { 418 sii := SwapIndexInfo{} 419 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_INDEX, instrumentId), nil, &sii); err != nil { 420 return nil, err 421 } 422 return &sii, nil 423 } 424 425 /* 426 获取合约整个平台的总持仓量。 427 428 HTTP请求 429 GET /api/swap/v3/instruments/<instrument_id>/open_interest 430 */ 431 func (client *Client) GetSwapOpenInterestByInstrument(instrumentId string) (*SwapOpenInterest, error) { 432 sii := SwapOpenInterest{} 433 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_OPEN_INTEREST, instrumentId), nil, &sii); err != nil { 434 return nil, err 435 } 436 return &sii, nil 437 } 438 439 /* 440 获取合约当前开仓的最高买价和最低卖价。 441 442 HTTP请求 443 GET /api/swap/v3/instruments/<instrument_id>/price_limit 444 */ 445 func (client *Client) GetSwapPriceLimitByInstrument(instrumentId string) (*SwapPriceLimit, error) { 446 sii := SwapPriceLimit{} 447 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_PRICE_LIMIT, instrumentId), nil, &sii); err != nil { 448 return nil, err 449 } 450 return &sii, nil 451 } 452 453 /* 454 获取合约爆仓单。 455 456 HTTP请求 457 GET /api/swap/v3/instruments/<instrument_id>/liquidation 458 459 请求示例 460 GET /api/swap/v3/instruments/BTC-USD-SWAP/liquidation?status=0&from=1&limit=50 461 */ 462 func (client *Client) GetSwapLiquidationByInstrument(instrumentId string, status string, optionalParams map[string]string) (*SwapLiquidationList, error) { 463 scl := SwapLiquidationList{} 464 baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_LIQUIDATION, instrumentId) 465 uri := baseUri 466 if optionalParams != nil { 467 optionalParams["status"] = status 468 uri = BuildParams(baseUri, optionalParams) 469 } else { 470 oParams := map[string]string{} 471 oParams["status"] = status 472 uri = BuildParams(baseUri, oParams) 473 } 474 if _, _, err := client.Request(GET, uri, nil, &scl); err != nil { 475 return nil, err 476 } 477 return &scl, nil 478 } 479 480 /* 481 获取合约挂单冻结数量。 482 483 HTTP请求 484 GET /api/swap/v3/accounts/<instrument_id>/holds 485 */ 486 func (client *Client) GetSwapAccountsHoldsByInstrument(instrumentId string) (*SwapAccountHolds, error) { 487 r := SwapAccountHolds{} 488 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_ACCOUNTS_HOLDS, instrumentId), nil, &r); err != nil { 489 return nil, err 490 } 491 return &r, nil 492 } 493 494 /* 495 获取合约下一次的结算时间。 496 497 HTTP请求 498 GET /api/swap/v3/instruments/<instrument_id>/funding_time 499 */ 500 func (client *Client) GetSwapFundingTimeByInstrument(instrumentId string) (*SwapFundingTime, error) { 501 r := SwapFundingTime{} 502 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_FUNDING_TIME, instrumentId), nil, &r); err != nil { 503 return nil, err 504 } 505 return &r, nil 506 } 507 508 /* 509 获取合约标记价格。 510 511 HTTP请求 512 GET /api/swap/v3/instruments/<instrument_id>/mark_price 513 */ 514 func (client *Client) GetSwapMarkPriceByInstrument(instrumentId string) (*SwapMarkPrice, error) { 515 r := SwapMarkPrice{} 516 if _, _, err := client.Request(GET, GetInstrumentIdUri(SWAP_INSTRUMENT_MARK_PRICE, instrumentId), nil, &r); err != nil { 517 return nil, err 518 } 519 return &r, nil 520 } 521 522 /* 523 获取合约历史资金费率。 524 525 HTTP请求 526 GET /api/swap/v3/instruments/<instrument_id>/historical_funding_rate 527 528 请求示例 529 GET /api/swap/v3/instruments/BTC-USD-SWAP/historical_funding_rate?from=1&limit=50 530 */ 531 func (client *Client) GetSwapHistoricalFundingRateByInstrument(instrumentId string, optionalParams map[string]string) (*SwapHistoricalFundingRateList, error) { 532 r := SwapHistoricalFundingRateList{} 533 baseUri := GetInstrumentIdUri(SWAP_INSTRUMENT_HISTORICAL_FUNDING_RATE, instrumentId) 534 uri := baseUri 535 if optionalParams != nil { 536 uri = BuildParams(baseUri, optionalParams) 537 } 538 539 if _, _, err := client.Request(GET, uri, nil, &r); err != nil { 540 return nil, err 541 } 542 return &r, nil 543 } 544 545 /* 546 获取法币汇率。 547 548 HTTP请求 549 GET /api/swap/v3/rate 550 */ 551 func (client *Client) GetSwapRate() (*SwapRate, error) { 552 sr := SwapRate{} 553 if _, _, err := client.Request(GET, SWAP_RATE, nil, &sr); err != nil { 554 return nil, err 555 } 556 return &sr, nil 557 }