Skip to content

opencrypter/binance-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binance-Go

Build Status codecov License: MIT Join the chat at https://gitter.im/opencrypter/binance-go

An open source sdk for Binance exchange written in Golang. There are endpoints/streams that are not implemented yet. Feel free to collaborate with me if you need them now :)

THIS LIBRARY IS NOT READY FOR PRODUCTION YET

Installation

After you've configured your local Go package:

go get github.com/opencrypter/binance-go

Usage

This SDK is based on the official binance api docs

You only have to call the constructor function in order to use it:

import "github.com/opencrypter/binance-go"

sdk := binance.New("Your-api-key", "Your secret api-key")

exchangeInfo, err := sdk.ExchangeInfo()

Available api endpoints

ExchangeInfo

Current exchange trading rules and symbol information

Official doc: exchange-information

Example

exchangeInfo, err := sdk.ExchangeInfo()

Order book

Order depth for a specific symbol

Official doc: Order book

Example

// Example with limit by default
query := binance.NewDepthQuery("ETHBTC")
depth, err := sdk.Depth(query)

// Example with limit parameter
query := binance.NewDepthQuery("ETHBTC").Limit(5)
depth, err := sdk.Depth(query)

Historical trades list

Get historical trades for a specific symbol

Official doc: Recent trades list

Example

// Example to retrieve last trades with the limit by default
query := binance.NewTradesQuery("ETHBTC")
response, err := sdk.Trades(query)

// Example to retrieve historical trades from the given id and a result limit
query := binance.NewTradesQuery("ETHBTC").Limit(350).FromId(3500)
trades, err := sdk.Trades(query)

Compressed/Aggregate trades list

Get compressed, aggregate trades. Trades that fill at the time, from the same order, with the same price will have the quantity aggregated.

Official doc: Compressed/Aggregate trades list

Example

// Example with parameters by default
query := binance.NewCompressedTradesQuery("ETHBTC")
response, err := sdk.CompressedTrades(query)

// Example with all parameters. Keep in mind that you cannot use all parameters at the same time (read the official doc)
query := binance.NewCompressedTradesQuery("ETHBTC").Limit(10).FromId(1).StartTime(1498793709153).EndTime(1498793709163)
trades, err := sdk.CompressedTrades(query)

Kline/Candlestick data

Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.

Official doc: Kline/Candlestick data

Example

// Example with parameters by default
query := binance.NewKLinesQuery("ETHBTC", binance.Interval12h)
response, err := sdk.KLines(query)

// Example with all parameters.
query := NewKLinesQuery("ETHBTC", Interval1m).Limit(10).StartTime(1498793709153).EndTime(1498793709163)
trades, err := sdk.KLines(query)

Symbol price ticker

Latest price for a symbol.

Official doc: Symbol price ticker

Example

query := binance.NewSymbolPriceTickerQuery("ETHBTC")
response, err := sdk.SymbolPriceTicker(query)

All symbol price tickers

Latest price for all symbols.

Official doc: Symbol price ticker

Example

response, err := sdk.AllSymbolPriceTickers()

Symbol order book ticker

Best price/qty on the order book for a symbol.

Official doc: Symbol order book ticker

Example

query := binance.NewSymbolOrderBookTickerQuery("ETHBTC")
response, err := sdk.SymbolOrderBookTicker(query)

All symbol order book tickers

Best price/qty on the order book for all symbols.

Official doc: Symbol order book ticker

Example

response, err := sdk.AllSymbolOrderBookTickers()

New order (TRADE)

Send a new order.

Official doc: New order (TRADE)

Example

request := binance.NewOrderRequest("BTCUSDT", "SELL", "MARKET", 10)
response, err := sdk.NewOrder(request)

// With all optional parameters (See official doc)
request := NewOrderRequest("BTCUSDT", "SELL", "MARKET", 10).
	TimeInForce("GTC").
	Price(0.1).
	NewClientOrderId("6gCrw2kRUAF9CvJDGP16IP").
	StopPrice(0.1).
	IcebergQuantity(0.1).
	NewOrderResponseType("ACK").
	RecvWindow(2)
response, err := sdk.NewOrder(request)

Query order (USER_DATA)

Get order detail.

Official doc: Query order (USER_DATA)

Example

query := binance.NewGetOrderQuery("LTCBTC")
response, err := sdk.GetOrder(query)

// With all optional parameters (See official doc)
query := binance.NewGetOrderQuery("LTCBTC").
	OrderId(1).
	OrigClientOrderId("myOrder1").
	RecvWindow(2)
response, err := sdk.GetOrder(query)

Cancel order (TRADE)

Cancel an active order.

Official doc: Cancel order (TRADE)

Example

request := binance.NewCancelOrderRequest("LTCBTC")
response, err := sdk.CancelOrder(request)

// With all optional parameters (See official doc)
request := binance.NewCancelOrderRequest("LTCBTC").
    OrderId(1).
    OrigClientOrderId("myOrder1").
    RecvWindow(2000)
response, err := sdk.CancelOrder(request)

Current open orders (USER_DATA)

Get all open orders on a symbol.

Official doc: Current open orders (USER_DATA)

Example

query := binance.NewGetOpenOrdersQuery("LTCBTC")
response, err := sdk.GetOpenOrders(query)

// With all optional parameters (See official doc)
query := binance.NewGetOpenOrdersQuery("LTCBTC").RecvWindow(2)
response, err := sdk.GetOpenOrders(query)

All orders (USER_DATA)

Get all account orders; active, canceled, or filled.

Official doc: All orders (USER_DATA)

Example

query := binance.NewGetAllOrdersQuery("LTCBTC")
response, _ := sdk.GetAllOrders(query)

// With all optional parameters (See official doc)
query := binance.NewGetAllOrdersQuery("LTCBTC").
    OrderId(1).
    Limit(200).
    RecvWindow(2000)
response, _ := sdk.GetAllOrders(query)

Account information (USER_DATA)

Get current account information.

Official doc: Account information (USER_DATA)

Example

query := binance.NewAccountQuery()
response, _ := sdk.Account(query)

// With all optional parameters (See official doc)
query := binance.NewAccountQuery().RecvWindow(2000)
response, _ := sdk.Account(query)

Account trade list (USER_DATA)

Get trades for a specific account and symbol.

Official doc: Account trade list (USER_DATA)

Example

query := binance.NewMyTradesQuery("ETHBTC")
response, _ := sdk.MyTrades(query)

// With all optional parameters (See official doc)
query := binance.NewMyTradesQuery("ETHBTC").Limit(10).RecvWindow(2).FromId(200)
response, _ := sdk.MyTrades(query)

Available web socket streams:

Not available yet.

Tests

All is covered 100%. You can run all tests as normally you do it:

go test -test.v

License

MIT licensed. See the LICENSE file for details.