Giter VIP home page Giter VIP logo

go-bitmex's Introduction

Build Status Go Report Card

go-bitmex

Event driven BitMEX client library for Golang which provides a full access to both the REST and websocket APIs.

Authentication is handled automatically for you by both clients. If there is credentials passed it will authenticate. Credentials are optional and if you are only using public endpoints, then there is no need to supply them.

If you find this useful, be cool and use my affilate link https://www.bitmex.com/register/0Oo09J and get 10% off trading fees for 6 months.

Or tip me:

btc: 3HmLfHJvrJuM48zHFY6HstUCxbuwV3dvxd

eth: 0x9Dc9129185E79211534D0039Af1C6f1ff585F5e3

ltc: MEpFjCdR3uXd6QjuJTSu3coLtcSWY3S2Hg

Further Documentation

Read the tests rest_test.go and websocket_test.go

GoDoc package documentation is here

Using the REST client

This is a basic go-swagger client generated from the official swagger documents provided by BitMEX with a custom http.RoundTripper to calculate and inject the authorisation signature into the headers.

Example

package main

import (
    "fmt"

    "github.com/adampointer/go-bitmex"
    "github.com/adampointer/go-bitmex/swagger/client/trade"
    "github.com/wacul/ptr"
)

func main() {
    cfg := bitmex.NewClientConfig().
        WithURL("https://testnet.bitmex.com").
        WithAuth("api_key", "api_secret") // Not required for public endpoints
    restClient := bitmex.NewBitmexClient(cfg)

    params := trade.NewTradeGetBucketedParams()
    params.SetSymbol(ptr.String("XBTUSD"))
    params.SetBinSize(ptr.String("1d"))
    params.SetReverse(ptr.Bool(true))

    res, err := restClient.Trade.TradeGetBucketed(params)
    if err != nil {
        panic(err)
    }
    fmt.Println(res)
}

WebSocket Client

The websocket implementation is slightly more involved. The core of it is an event driven websocket library. Events/mesages are then shipped out to an event bus where clients can subscribe to specific topics.

There are two types of subscriptions available. Websocket events, such as connect, disconnect etc. and API subscriptions where parsed messages are pushed. API subscriptions can be filtered by instrument.

Both kind of subscription can be added/removed at anytime either before Start() is called or after.

Websocket Events

err := wsClient.SubscribeToEvents(types.EventWebsocketConnected, func(interface{}) {
    fmt.Println("connected")
})

Subscriptions

// Subscribe to all orders
wsClient.SubscribeToApiTopic(types.SubscriptionTopicOrder, func(res *types.SubscriptionResponse) {
    // Decode the response into an OrderData model
    data, err := res.OrderData()
    if err != nil {
        log.Errorf("error getting order data: %s", err)
	}
    fmt.Println(data)
})
// API subscription with instrument filter
wsClient.SubscribeToApiTopic(types.SubscriptionTopicOrderBookL2.WithInstrument("XRPZ19"), func(res *types.SubscriptionResponse) {
    data, err := res.OrderBookL2Data()
    if err != nil {
        log.Errorf("error getting orderbook data: %s", err)
    }
    fmt.Println(data)
})

Full Example

package main

import (
    "log"

    "github.com/adampointer/go-bitmex"
    "github.com/adampointer/go-bitmex/types"
)

func main() {
    cfg := bitmex.NewWebsocketClientConfig().
        WithURL("wss://testnet.bitmex.com/realtime").
        WithAuth("api_key", "api_secret") // Not required for public endpoints
    wsClient := bitmex.NewWebsocketClient(cfg)

    err := wsClient.SubscribeToApiTopic(types.SubscriptionTopicOrderBookL2.WithInstrument("XRPZ19"), func(res *types.SubscriptionResponse) {
        data, err := res.OrderBookL2Data()
        if err != nil {
            log.Fatalf("error getting orderbook data: %s", err)
        }
        log.Println(data)
    })
    if err != nil {
        panic(err)
    }
    wsClient.Start()
}

Heartbeat and Reconnection

Due to an issue in the underlying websocket library, the disconnect handler may not get called if the connection is silently dropped by the remote. Heartbeat is implemented and pings the remote every 5 seconds. A 10 second timer is reset on every message received. If the timer expires, the library will automatically call Restart(). On a successful reconnect, any configured API subscription will be re-subscribed.

go-bitmex's People

Contributors

adampointer avatar adampointer-form3 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.