ba
github.com/eranyanay/binance-api
go get github.com/eranyanay/binance-api
ba

github.com/eranyanay/binance-api

by Eran Yanay

v0.0.0-20190408204030-b55a418abc86 (see all)License:MIT
go get github.com/eranyanay/binance-api
Readme

Golang Binance API

binance-api is a lightweight Golang implementation for Binance API, providing complete API coverage, and supports both REST API and websockets API

This project is designed to help you interact with the Binance API, streaming candlestick charts data, market depth, or use other advanced features binance exposes via API.

Installation

go get github.com/eranyanay/binance-api

Getting started

// Generate default client
client := binance.NewBinanceClient("API-KEY", "SECRET")

// Generate client with custom window size
client := binance.NewBinanceClientWindow("API-KEY", "SECRET", 5000)

Examples

REST API usage examples

Get depth for symbol

depth, err := client.Depth(&binance.DepthOpts{Symbol: "ETHBTC"})

Get candlesticks for symbol

klines, err := client.Klines(&binance.KlinesOpts{
        Symbol: "ETHBTC",
        Interval: binance.KlineInterval15m,
})

Get 24 hour price change statistics for symbol

stats, err := client.Ticker(&binance.TickerOpts{Symbol: "ETHBTC"})

Get latest prices for all symbols

prices, err := client.Prices()

Create new order for ETHBTC, purchase 1 quantity at price 0.05BTC

order, err := client.NewOrder(&binance.NewOrderOpts{
        Symbol: "ETHBTC",
        Type: binance.OrderTypeLimit,
        Price: "0.05",
        Quantity: "1",
        Side: binance.OrderSideBuy,
        TimeInForce: binance.TimeInForceGTC,
    })

Test new order options

order, err := client.NewOrderTest(&binance.NewOrderOpts{
        Symbol: "ETHBTC",
        Type: binance.OrderTypeLimit,
        Price: "0.05",
        Quantity: "1",
        Side: binance.OrderSideBuy,
        TimeInForce: binance.TimeInForceGTC,
    })

Create stop loss order

order, err := client.NewOrder(&binance.NewOrderOpts{
        Symbol: "ETHBTC",
        Type: binance.OrderTypeLimit,
        Price: "0.05",
        StopPrice: "0.049",
        Quantity: "1",
        Side: binance.OrderSideBuy,
        TimeInForce: binance.TimeInForceGTC,
    })

Query order status

Orders are assigned with order ID when issued and can later be queried using it

query, err := client.QueryOrder(&binance.QueryOrderOpts{Symbol:"ETHBTC", OrderID:order.OrderID})

Cancel an open order

cancel, err := client.CancelOrder(&binance.CancelOrderOpts{Symbol:"ETHBTC", OrderID:order.OrderID})

Open orders for a symbol

orders, err := client.OpenOrders(&binance.OpenOrdersOpts{Symbol:"ETHBTC"})

Get all account orders for symbol ETHBTC, with ID greater than 5

orders, err := client.AllOrders(&binance.AllOrdersOpts{Symbol:"ETHBTC", OrderID: 5})

Get account information

info, err := client.Account()

Get account trades for symbol

trades, err := client.Trades(&binance.TradesOpts{Symbol:"ETHBTC"})

Get aggreagated trades for symbol

trades, err := client.AggregatedTrades(&binance.AggregatedTradeOpts{Symbol:"ETHBTC"})

Get best tickers for all symbols

tickers, err := client.AllBookTickers()

Create a new datastream key

key, err := client.DataStream()

Set datastream key keep-alive to prevent from it to timeout

err := client.DataStreamKeepAlive(key)

Close datastream

err := client.DataStreamClose(key)

Websockets API usage examples

Depth for symbol

conn, err := client.DepthWS("ETHBTC")
if err != nil {
    // Handle error
}
defer conn.Close()
for {
    update, err := conn.Read()
    if err != nil {
        // Handle error
    }
    fmt.Printf("Depth update: %v", update)
}

Klines for symbol with interval of 15 minutes per candlestick

conn, err := client.Klines("ETHBTC", binance.KlineInterval15m)
if err != nil {
    // Handle error
}
defer conn.Close()
for {
    update, err := conn.Read()
    if err != nil {
        // Handle error
    }
    fmt.Printf("Klines update: %v", update)
}

Trades for symbol

conn, err := client.Trades("ETHBTC")
if err != nil {
    // Handle error
}
defer conn.Close()
for {
    update, err := conn.Read()
    if err != nil {
        // Handle error
    }
    fmt.Printf("Trades update: %v", update)
}

Account info updates

// Retrieve new datastream key
key, err := client.DataStream()
if err != nil {
    // Handle error
}
defer client.DataStreamClose(key)
conn, err := client.AccountInfoWS(key)
if err != nil {
    // Handle error
}
defer conn.Close()
for {
    accountUpdate, orderUpdate, err := conn.Read()
    if err != nil {
        // Handle error
    }
    fmt.Printf("Account info update: %v %v", accountUpdate, orderUpdate)
}

License

This project is licensed under the MIT License. See the LICENSE file for more info.

GitHub Stars

11

LAST COMMIT

3yrs ago

MAINTAINERS

0

CONTRIBUTORS

2

OPEN ISSUES

1

OPEN PRs

0
VersionTagPublished
v0.0.0-20190408204030-b55a418abc86
2yrs ago
No alternatives found
No tutorials found
Add a tutorial