Giter VIP home page Giter VIP logo

go-binance's Introduction

Go Binance API

Summary

Go client for Binance

Installation

go get github.com/pdepip/go-binance/binance

Documentation

Full API Documentation can be found at https://www.binance.com/restapipub.html

Setup

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)

Examples

Get Current Positions

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)
    }
}

Place a Limit Order

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)
}

Place a Market Order

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)
}

Check Order Status

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)
}

Cancel an Order

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)
}

Get Open Orders

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)
}

Get the Order Book

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)

}

Get Latest Price of a Symbol

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)

}

Local Depth Cache

See examples/depth.go. Script connects to Binance websocket and maintains a simple local depth cache.

go-binance's People

Contributors

matthewwoop avatar dionbosschieter avatar pdepip avatar 7ac avatar tyrion85 avatar

Watchers

Keita Kitamura avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.