Go client for Binance
go get github.com/pdepip/go-binance/binance
Full API Documentation can be found at https://www.binance.com/restapipub.html
Creating a client:
import (
"os"
"go-binance/binance"
)
// Secure method
secret := os.Getenv("BINANCE_SECRET")
key := os.Getenv("BINANCE_KEY")
// Unsecure method
secret := "mySecret"
key := "myKey"
client := binance.New(secret, key)
package main
import (
"os"
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
client := binance.New(os.Getenv("BINANCE_KEY"), os.Getenv("BINANCE_SECRET"))
positions, err := client.GetPositions()
if err != nil {
panic(err)
}
for _, p := range positions {
fmt.Println(p.Asset, p.Free, p.Locked)
}
}
package main
import (
"os"
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
order := binance.LimitOrder {
Symbol: "BNBBTC",
Side: "BUY",
Type: "LIMIT",
TimeInForce: "GTC",
Quantity: 50.0,
Price: 0.00025,
}
client := binance.New(os.Getenv("BINANCE_KEY"), os.Getenv("BINANCE_SECRET"))
res, err := client.PlaceLimitOrder(order)
if err != nil {
panic(err)
}
fmt.Println(res)
}
package main
import (
"os"
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
order := binance.MarketOrder {
Symbol: "BNBBTC",
Side: "BUY",
Type: "MARKET",
Quantity: 50.0,
}
client := binance.New(os.Getenv("BINANCE_KEY"), os.Getenv("BINANCE_SECRET"))
res, err := client.PlaceMarketOrder(order)
if err != nil {
panic(err)
}
fmt.Println(res)
}
import (
"os"
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
orderQuery := binance.OrderQuery {
Symbol: "BNBBTC",
OrderId: "yourOrderId",
}
client := binance.New(os.Getenv("BINANCE_KEY"), os.Getenv("BINANCE_SECRET"))
res, err := client.CheckOrder(orderQuery)
if err != nil {
panic(err)
}
fmt.Println(res)
}
import (
"os"
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
orderQuery := binance.OrderQuery {
Symbol: "BNBBTC",
OrderId: "yourOrderId",
}
client := binance.New(os.Getenv("BINANCE_KEY"), os.Getenv("BINANCE_SECRET"))
res, err := client.CancelOrder(orderQuery)
if err != nil {
panic(err)
}
fmt.Println(res)
}
import (
"os"
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
orderQuery := binance.OpenOrdersQuery {
Symbol: "BNBBTC",
}
client := binance.New(os.Getenv("BINANCE_KEY"), os.Getenv("BINANCE_SECRET"))
res, err := client.GetOpenOrders(orderQuery)
if err != nil {
panic(err)
}
fmt.Println(res)
}
import (
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
query := binance.OrderBookQuery {
Symbol: "BNBBTC",
Limit: 100,
}
client := binance.New("", "")
res, err := client.GetOrderBook(query)
if err != nil {
panic(err)
}
fmt.Println(res)
}
import (
"fmt"
"github.com/pdepip/go-binance/binance"
)
func main() {
// Params
query := binance.SymbolQuery {
Symbol: "BNBBTC",
}
client := binance.New("", "")
res, err := client.GetLastPrice(query)
if err != nil {
panic(err)
}
fmt.Println(res)
}
See examples/depth.go
. Script connects to Binance websocket and maintains a simple local depth cache.