binance

package
v0.0.0-...-e7775c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2022 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package binance provides the connection driver for the binance API.

Index

Constants

View Source
const (
	EndpointWsBase   = "wss://stream.binance.com:9443"
	EndpointWsRaw    = EndpointWsBase + "/ws"
	EndpointWsStream = EndpointWsBase + "/stream"
)

Endpoint paths

View Source
const (
	MethodWsSubscribe         = "SUBSCRIBE"
	MethodWsUnsubscribe       = "UNSUBSCRIBE"
	MethodWsListSubscriptions = "LIST_SUBSCRIPTIONS"
	MethodWsSetProperty       = "SET_PROPERTY"
	MethodWsGetProperty       = "GET_PROPERTY"
)

Method names for websocket

Variables

View Source
var (
	ErrStreamSubscribed = errors.New("stream already subscribed")
)
View Source
var (
	// Global IP based back-off WaitGroup.
	// The WaitGroup will be blocked after any 429 or 418,
	// for the time set in the `Retry-After` reponse header.
	IPBackOff sync.WaitGroup
)

Functions

This section is empty.

Types

type BackOffError

type BackOffError struct {
	StatusCode int
	Duration   time.Duration
}

BackOffError is returned after a 429 or 418 status code is received from the API.

func (BackOffError) Error

func (e BackOffError) Error() string

type Kline

type Kline struct {
	Start            int64  `json:"t"` // Kline start time
	Finish           int64  `json:"T"` // Kline close time
	Symbol           string `json:"s"` // Symbol
	Interval         string `json:"i"` // Interval
	First            int64  `json:"f"` // First trade ID
	Last             int64  `json:"L"` // Last trade ID
	Open             string `json:"o"` // Open price
	Close            string `json:"c"` // Close price
	High             string `json:"h"` // High price
	Low              string `json:"l"` // Low price
	BaseVolume       string `json:"v"` // Base asset volume
	Trades           int    `json:"n"` // Number of trades
	Closed           bool   `json:"x"` // Is this kline closed?
	QuoteVolume      string `json:"q"` // Quote asset volume
	TakerBaseVolume  string `json:"V"` // Taker buy base asset volume
	TakerQuoteVolume string `json:"Q"` // Taker buy quote asset volume
	Ignore           string `json:"B"` // Ignore
}

type KlineEvent

type KlineEvent struct {
	Event  string `json:"e"` // Event type ("kline")
	Time   int64  `json:"E"` // Event time
	Symbol string `json:"s"` // Symbol
	Kline  Kline  `json:"k"`
}

type KlineHandler

type KlineHandler interface {
	Event(KlineEvent)
	Done()
}

type KlineInterval

type KlineInterval string
const (
	Minute   KlineInterval = "1m"
	Minute3  KlineInterval = "3m"
	Minute5  KlineInterval = "5m"
	Minute15 KlineInterval = "15m"
	Minute30 KlineInterval = "30m"
	Hour     KlineInterval = "1h"
	Hour2    KlineInterval = "2h"
	Hour4    KlineInterval = "4h"
	Hour6    KlineInterval = "6h"
	Hour8    KlineInterval = "8h"
	Hour12   KlineInterval = "12h"
	Day      KlineInterval = "1d"
	Day3     KlineInterval = "3d"
	Week     KlineInterval = "1w"
	Month    KlineInterval = "1M"
)

type MarketData

type MarketData struct {
	*driver.Client
	// contains filtered or unexported fields
}

func (*MarketData) GetJSON

func (m *MarketData) GetJSON(ctx context.Context, path string, data, target interface{}) error

GetJSON performs a GET request on paths, with data encoded to URL values. The response body is expected to be JSON and will be unmarshalled into target. In case the call succeeds and the satus code is not 200, a BackOffError or RequestError will be returned.

In case a status code 429 or 418 is received, a timer is started based on the 'Retry-After' response header. Subsequent calls will block untill this timer expires. (Uses the global IPBackOff WaitGroup)

type OrderBookLimit

type OrderBookLimit int
const (
	OrderBookLimit_5    OrderBookLimit = 5
	OrderBookLimit_10   OrderBookLimit = 10
	OrderBookLimit_20   OrderBookLimit = 20
	OrderBookLimit_50   OrderBookLimit = 50
	OrderBookLimit_100  OrderBookLimit = 100
	OrderBookLimit_500  OrderBookLimit = 500
	OrderBookLimit_1000 OrderBookLimit = 1000
	OrderBookLimit_5000 OrderBookLimit = 5000
	OrderBookDefault                   = OrderBookLimit_100
)

type OrderBookReq

type OrderBookReq struct {
	Symbol string         `schema:"symbol,required,omitempty"`
	Limit  OrderBookLimit `schema:"limit,omitempty"`
}

type OrderBookResp

type OrderBookResp struct {
	LastUpdateId int64      `json:"lastUpdateId"`
	Bids         [][]string `json:"bids"`
	Asks         [][]string `json:"asks"`
}

type PingResp

type PingResp struct{}

type RequestError

type RequestError struct {
	StatusCode int
	Status     string
}

RequestError is returned on any status code that's not 200, 418 or 429.

func (RequestError) Error

func (e RequestError) Error() string

type ServerTimeResp

type ServerTimeResp struct {
	ServerTime int64 `json:"serverTime"`
}

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream implements the binance cobined stream protocol.

func NewStream

func NewStream(ctx context.Context) (*Stream, error)

NewStream dails the websocket endpoint for binance combined streams. The returned stream is closed when the context is canceled. On any error, the stream closes and terminates. Calling methods on the Stream after closingwill results in errors to be returned.

func (*Stream) Subscribe

func (s *Stream) Subscribe(stream string, handler driver.JSONHandler) error

Subscribe to a named binanace websocket stream. Raw JSON will be send to the returned channel for every complete message. The order of messages is serialized in order of arrival, and the Stream's listener will block untill the channel write completes. The receiver must prevent exessive blocking of the channel. The channel can be buffered with the size of bufLen, to accomodate for short bursts of data.

func (*Stream) SubscribeClosingPrices

func (s *Stream) SubscribeClosingPrices(symbol string, interval string, handler driver.ClosingPriceHandler) error

func (*Stream) SubscribeKlines

func (s *Stream) SubscribeKlines(symbol string, interval KlineInterval, handler KlineHandler) error

func (*Stream) Unsubscribe

func (s *Stream) Unsubscribe(stream string) error

func (*Stream) UnsubscribeClosingPrices

func (s *Stream) UnsubscribeClosingPrices(symbol string, interval string) error

func (*Stream) UnsubscribeKlines

func (s *Stream) UnsubscribeKlines(symbol string, interval KlineInterval) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL