go-binance is Binance public API implementation in golang
go get github.com/bloc4ain/go-binance
err := binance.Ping()
time, err := binance.GetServerTime()
info, err := binance.GetExchangeInfo()
book, err := binance.GetOrderBook("TRXBTC", "1000")
trades, err := binance.GetRecentTrades("TRXBTC", "500")
trades, err := binance.GetRecentTrades("TRXBTC", "500")
trades, err := binance.GetAggregateTrades("TRXBTC", "100", "", "", "")
Orders are assigned with order ID when issued and can later be queried using it
trades, err := binance.GetKlines("TRXBTC", "100", "", "", "")
// The Aggregate Trade Streams push trade information that is aggregated for a single taker order.
stream, err := binance.OpenAggregateTradeStream("TRXBTC")
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
// The Trade Streams push raw trade information; each trade has a unique buyer and seller.
stream, err := binance.OpenTradeStream("TRXBTC")
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
// The Kline/Candlestick Stream push updates to the current klines/candlestick every second.
stream, err := binance.OpenChartStream("TRXBTC", binance.ChartIntervalOneMin)
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
// 24hr Ticker statistics for a single symbol pushed every second
stream, err := binance.OpenTickerStream("TRXBTC")
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
// 24hr Ticker statistics for all symbols in an array pushed every second
stream, err := binance.OpenTickersStream()
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
// Top <levels> bids and asks, pushed every second. Valid <levels> are 5, 10, or 20.
stream, err := binance.OpenPartialBookStream("TRXBTC", "20")
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
// Order book price and quantity depth updates used to locally manage an order book pushed every second.
stream, err := binance.OpenDiffDepthStream("TRXBTC")
if err != nil {
fmt.Printf("Stream open error: %s\n", err)
return
}
defer stream.Close()
for {
update, err := stream.Read()
if err != nil {
fmt.Printf("Stream read error: %s\n", err)
return
}
fmt.Printf("%+v\n", update)
}
- Open a stream using binance.OpenDiffDepthStream
- Buffer the events you receive from the stream
- Get a depth snapshot using binance.GetOrderBook
- Drop any event where
FinalUpdateID
is <=LastUpdateID
in the snapshot - The first processed should have
FirstUpdateID
<=LastUpdateID
+1 ANDFinalUpdateID
>=LastUpdateID
+1 - While listening to the stream, each new event's
FirstUpdateID
should be equal to the previous event'sFinalUpdateID
+1 - The data in each event is the absolute quantity for a price level
- If the quantity is 0, remove the price level
- Receiving an event that removes a price level that is not in your local order book can happen and is normal.
This project is licensed under the MIT License. See the LICENSE file for more info.