Skip to content

Latest commit

 

History

History
197 lines (175 loc) · 5.58 KB

README.md

File metadata and controls

197 lines (175 loc) · 5.58 KB

Golang SDK for bitmex

inspired by https://github.com/BitMEX/api-connectors, https://www.bitmex.com/api/explorer and https://github.com/jxc6698/bitcoin-exchange-api

all structs and APIs are from the bitmex official api connectors, based on that, add authentication and fix bugs.

Why

the generated connectors by bitmex have too many mistakes to use, this SDK fix these bugs to ensure bitmex API can get right results.

Installation

go get github.com/qct/bitmex-go

Examples

  1. Get order book
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
orderBookApi := restful.NewOrderBookApi(apiClient.OrderBookApi)
orderBooks, err := orderBookApi.OrderBookGetL2("XBTUSD", 5)
if err != nil {
    log.Println("error wihle get orderbook: ", err)
}
for _, v := range orderBooks.AskList {
    log.Printf("%+v\n", v)
}
for _, v := range orderBooks.BidList {
    log.Printf("%+v\n", v)
}
  1. Get your position
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
auth      = context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
        Key:    apiKey,
        Secret: secretKey,
    })

positionApi := apiClient.PositionApi
params := map[string]interface{}{
    "filter":  "{\"symbol\": \"XBTUSD\"}",
    "columns": "",
    "count":   float32(10),
}
positions, response, err := positionApi.PositionGet(auth, params)
if err != nil {
    log.Println("error: ", err)
}
log.Println(response.Status)
log.Printf("%+v\n", positions)
  1. Get your wallet
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
auth      = context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
        Key:    apiKey,
        Secret: secretKey,
    })

userApi := apiClient.UserApi
params := map[string]interface{}{
    "currency": "",
}
wallet, response, err := userApi.UserGetWallet(auth, params)
if err != nil {
    log.Println("error: ", err)
}
log.Println(response.Status)
log.Printf("wallet: %+v\n", wallet)
  1. Get margin info
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
auth      = context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
        Key:    apiKey,
        Secret: secretKey,
    })

userApi := apiClient.UserApi
margin, response, err := userApi.UserGetMargin(auth, nil)
if err != nil {
    log.Println("error: ", err)
}
log.Println(response.Status)
log.Printf("margin: %+v\n", margin)
  1. Buy & Sell
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
auth      = context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
        Key:    apiKey,
        Secret: secretKey,
    })

// buy
orderApi := restful.NewOrderApi(apiClient.OrderApi, auth)
resp, orderId, err := orderApi.LimitBuy("XBTUSD", 1.0, 13000, "qct_f_f_")
if err != nil {
    log.Println("error: ", err)
}
log.Printf("response: %s, orderId: %s\n", resp.Status, orderId)

// sell
resp, orderId, err := orderApi.LimitSell("XBTUSD", 1.0, 20000, "qct_f_f_")
if err != nil {
    log.Println("error: ", err)
}
log.Printf("result: %s, orderId: %s\n", resp.Status, orderId)
  1. Get your order
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
auth      = context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
        Key:    apiKey,
        Secret: secretKey,
    })

orderApi := apiClient.OrderApi
params := map[string]interface{}{
    "symbol":    "XBTUSD",
    "filter":    "",
    "columns":   "",
    "count":     float32(5),
    "start":     nil,
    "reverse":   true,
    "startTime": nil,
    "endTime":   nil,
}
orders, response, err := orderApi.OrderGetOrders(auth, params)
if err != nil {
    log.Println("error: ", err)
}
log.Println(response.Status)
log.Printf("orders: %+v\n", orders)
  1. Even chat through RESTFul api
apiClient = swagger.NewAPIClient(swagger.NewConfiguration())
auth      = context.WithValue(context.TODO(), swagger.ContextAPIKey, swagger.APIKey{
        Key:    apiKey,
        Secret: secretKey,
    })

chatApi := apiClient.ChatApi
params := map[string]interface{}{
    "channelID": 2.0,
}
chat, response, err := chatApi.ChatNew(auth, "hello", params)
if err != nil {
    log.Println("error: ", err)
}
log.Println(response.Status)
log.Printf("%+v\n", chat)

Roadmap

  • I only tested some API I will use, in the near future, I will keep adding and fixing to make more API available.
  • make a PR to bitmex api collectors to generate a ready to use golang client.

Changelog

1.swagger.json, in definitions:

"OrderBookL2": {
      "properties": {
        "id": { "type": "number", "format": "int64" },
        "symbol": { "type": "string" },
        "side": { "type": "string" },
        "size": { "type": "number", "format": "int64" },
        "price": { "format": "double", "type": "number" }
      },
      "required": ["id", "symbol", "side"],
      "type": "object"
    },

2.api_order.go, cancel multi orders

if localVarOptionals != nil && localVarOptionals.OrderID.IsSet() {
    //localVarFormParams.Add("orderID", parameterToString(localVarOptionals.OrderID.Value(), ""))
    ids := strings.Split(localVarOptionals.OrderID.Value(), ",")
    for _, id := range ids {
        localVarFormParams.Add("orderID", id)
    }
}
if localVarOptionals != nil && localVarOptionals.ClOrdID.IsSet() {
    //localVarFormParams.Add("clOrdID", parameterToString(localVarOptionals.ClOrdID.Value(), ""))
    ids := strings.Split(localVarOptionals.ClOrdID.Value(), ",")
    for _, id := range ids {
        localVarFormParams.Add("clOrdID", id)
    }
}