Skip to content
Go wrapper for Binance API
Go
Branch: master
Clone or download
#16 Compare This branch is 6 commits ahead of rootpd:master.
rootpd Merge pull request #2 from yoneapp/master
Fixed issue where OrderBook quantity returns price
Latest commit 1af0343 May 18, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Adding account tests, mocker and fixing example imports. Nov 11, 2017
vendor/github.com
Gopkg.lock Adding account tests, mocker and fixing example imports. Nov 11, 2017
Gopkg.toml Initial implementation of all calls, simple example ready. Oct 23, 2017
LICENSE
README.md Fixing example import after move to github, updating README. Oct 23, 2017
binance.go Initial implementation of all calls, simple example ready. Oct 23, 2017
interval.go Initial implementation of all calls, simple example ready. Oct 23, 2017
mock_test.go Adding account tests, mocker and fixing example imports. Nov 11, 2017
order_status.go Initial implementation of all calls, simple example ready. Oct 23, 2017
service_account.go Fix ‘Precision is over the maximum defined for this asset’ Dec 29, 2017
service_account_test.go Adding account tests, mocker and fixing example imports. Nov 11, 2017
service_def.go Initial implementation of all calls, simple example ready. Oct 23, 2017
service_def_test.go Adding account tests, mocker and fixing example imports. Nov 11, 2017
service_market.go Update service_market.go Mar 15, 2018
service_user_stream.go Initial implementation of all calls, simple example ready. Oct 23, 2017
service_websocket.go Initial implementation of all calls, simple example ready. Oct 23, 2017
signer.go Initial implementation of all calls, simple example ready. Oct 23, 2017
signer_test.go Initial implementation of all calls, simple example ready. Oct 23, 2017
util.go Initial implementation of all calls, simple example ready. Oct 23, 2017

README.md

Binance API

To read full documentation, specs and find out which request params are required/optional, please visit the official documentation page.

Getting started

var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "time", log.DefaultTimestampUTC, "caller", log.DefaultCaller)

hmacSigner := &binance.HmacSigner{
    Key: []byte("API secret"),
}
ctx, _ := context.WithCancel(context.Background())
// use second return value for cancelling request when shutting down the app

binanceService := binance.NewAPIService(
    "https://www.binance.com",
    "API key",
    hmacSigner,
    logger,
    ctx,
)
b := binance.NewBinance(binanceService)

Examples

Following provides list of main usages of library. See example package for testing application with more examples.

Each call has its own Request structure with data that can be provided. The library is not responsible for validating the input and if non-zero value is used, the param is sent to the API server.

In case of an standard error, instance of binance.Error is returned with additional info.

NewOrder

newOrder, err := b.NewOrder(binance.NewOrderRequest{
    Symbol:      "BNBETH",
    Quantity:    1,
    Price:       999,
    Side:        binance.SideSell,
    TimeInForce: binance.GTC,
    Type:        binance.TypeLimit,
    Timestamp:   time.Now(),
})
if err != nil {
    panic(err)
}
fmt.Println(newOrder)

CancelOrder

canceledOrder, err := b.CancelOrder(binance.CancelOrderRequest{
    Symbol:    "BNBETH",
    OrderID:   newOrder.OrderID,
    Timestamp: time.Now(),
})
if err != nil {
    panic(err)
}
fmt.Printf("%#v\n", canceledOrder)

Klines

kl, err := b.Klines(binance.KlinesRequest{
    Symbol:   "BNBETH",
    Interval: binance.Hour,
})
if err != nil {
    panic(err)
}
fmt.Printf("%#v\n", kl)

Trade Websocket

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

kech, done, err := b.TradeWebsocket(binance.TradeWebsocketRequest{
    Symbol: "ETHBTC",
})
if err != nil {
    panic(err)
}
go func() {
    for {
        select {
        case ke := <-kech:
            fmt.Printf("%#v\n", ke)
        case <-done:
            break
        }
    }
}()

fmt.Println("waiting for interrupt")
<-interrupt
fmt.Println("canceling context")
cancelCtx()
fmt.Println("waiting for signal")
<-done
fmt.Println("exit")
return

Known issues

  • Websocket error handling is not perfect and occasionally attempts to read from closed connection.
You can’t perform that action at this time.