binance

package
v0.0.0-...-ea1cebd Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BINANCE_FUTURES_LINK_ID_PREFIX = "x-9Oc4JrZL"
)
View Source
const BINANCE_PREFIX = "BINANCE-"
View Source
const (
	BINANCE_SPOT_LINK_ID_PREFIX = "x-INHON5QW"
)

Variables

This section is empty.

Functions

func NewBinanceClient

func NewBinanceClient(baseURL, apiKey, secretKey string, l *zap.Logger) *api.Client

func NewBinanceFuturesClient

func NewBinanceFuturesClient(baseURL, apiKey, secretKey string, l *zap.Logger) *apiFutures.Client

func SubscribeToOrders

func SubscribeToOrders(
	ctx context.Context,
	urls BinanceURLs,
	client *api.Client,
	lg *zap.Logger,
) (<-chan exchanges.OrderEvent, error)

func SubscribeToOrdersFutures

func SubscribeToOrdersFutures(
	ctx context.Context,
	wsEndpoint string,
	lg *zap.Logger,
) (<-chan exchanges.OrderEvent, error)

func SubscribeToOrdersV2

func SubscribeToOrdersV2(
	ctx context.Context,
	wsEndpoint string,
	lg *zap.Logger,
) (<-chan exchanges.OrderEvent, error)

Similar with SubscribeToOrders but it accepts ws endpoint instead of urls object

func SubscribeToPositions

func SubscribeToPositions(
	ctx context.Context,
	wsEndpoint string,
	lg *zap.Logger,
) (<-chan exchanges.PositionEvent, error)

func SubscribeToPositionsFutures

func SubscribeToPositionsFutures(
	ctx context.Context,
	wsEndpoint string,
	lg *zap.Logger,
) (<-chan exchanges.PositionEvent, error)

func SubscribeToPrice

func SubscribeToPrice(ctx context.Context, urls BinanceURLs, symbol string, lg *zap.Logger) (<-chan exchanges.PriceEvent, error)

SubscribeToPrice No reconnection in case of error Returns control after connect

func SubscribeToPriceV2

func SubscribeToPriceV2(ctx context.Context, wsEndpoint string, symbol string, lg *zap.Logger) (<-chan exchanges.PriceEvent, error)

Similar with SubscribeToPrice but it accepts ws endpoint instead of urls object

func ToBinanceSymbol

func ToBinanceSymbol(symbol string) string

func ToFullSymbol

func ToFullSymbol(binanceSymbol string) string

Types

type AccountUpdate

type AccountUpdate struct {
	EventType     string                    `json:"e"`
	EventTime     int64                     `json:"E"`
	BalancesArray []BinanceBalancePositions `json:"B"`
}

type BinanceBalancePositions

type BinanceBalancePositions struct {
	Asset  string `json:"a"`
	Free   string `json:"f"`
	Locked string `json:"l"`
}

type BinanceFutures

type BinanceFutures struct {
	Client *api.Client
	// contains filtered or unexported fields
}

BinanceLong TODO: probably it's better to switch to orderID instead of clientOrderID Order ID in the methods input and output is clientOrderID internally.

func NewBinanceFutures

func NewBinanceFutures(urls BinanceURLs, apiKey, secretKey string, lg *zap.Logger) *BinanceFutures

func (*BinanceFutures) CancelOrder

func (b *BinanceFutures) CancelOrder(ctx context.Context, symbol, id string) error

func (*BinanceFutures) GenerateClientOrderID

func (b *BinanceFutures) GenerateClientOrderID(ctx context.Context, identifierID string) (string, error)

func (*BinanceFutures) GetAccount

func (b *BinanceFutures) GetAccount(ctx context.Context) (exchanges.Account, error)

func (*BinanceFutures) GetName

func (b *BinanceFutures) GetName() string

func (*BinanceFutures) GetOpenOrders

func (b *BinanceFutures) GetOpenOrders(ctx context.Context) ([]exchanges.OrderDetailInfo, error)

func (*BinanceFutures) GetOrderInfo

func (b *BinanceFutures) GetOrderInfo(ctx context.Context, symbol, id string, _ *time.Time) (exchanges.OrderInfo, error)

func (*BinanceFutures) GetOrderInfoByClientOrderID

func (b *BinanceFutures) GetOrderInfoByClientOrderID(ctx context.Context, symbol, clientOrderID string, _ *time.Time) (exchanges.OrderInfo, error)

func (*BinanceFutures) GetOrders

func (*BinanceFutures) GetPrefix

func (b *BinanceFutures) GetPrefix() string

func (*BinanceFutures) GetPrice

func (b *BinanceFutures) GetPrice(ctx context.Context, symbol string) (*apd.Decimal, error)

func (*BinanceFutures) GetTradableSymbols

func (b *BinanceFutures) GetTradableSymbols(ctx context.Context) ([]exchanges.SymbolInfo, error)

func (*BinanceFutures) PlaceBuyOrder

func (b *BinanceFutures) PlaceBuyOrder(ctx context.Context,
	_ bool, symbol string, price, quantity *apd.Decimal, prefferedID string,
) (id string, e error)

func (*BinanceFutures) PlaceBuyOrderV2

func (b *BinanceFutures) PlaceBuyOrderV2(ctx context.Context, _ bool, symbol string, price, qty *apd.Decimal, preferredID string, orderType string) (id string, e error)

PlaceBuyOrderV2 Place Buy Order with OrderType param

func (*BinanceFutures) PlaceSellOrder

func (b *BinanceFutures) PlaceSellOrder(ctx context.Context,
	_ bool, symbol string, price, quantity *apd.Decimal, prefferedID string,
) (id string, e error)

func (*BinanceFutures) PlaceSellOrderV2

func (b *BinanceFutures) PlaceSellOrderV2(ctx context.Context, _ bool, symbol string, price, qty *apd.Decimal, preferredID string, orderType string) (id string, e error)

PlaceSellOrderV2 Place Sell Order with OrderType param

func (*BinanceFutures) ReleaseOrder

func (b *BinanceFutures) ReleaseOrder(_ context.Context, symbol, id string) error

func (*BinanceFutures) RoundPrice

func (b *BinanceFutures) RoundPrice(
	_ context.Context,
	symbol string,
	price *apd.Decimal,
	tickSize *string,
) (*apd.Decimal, error)

func (*BinanceFutures) RoundQuantity

func (b *BinanceFutures) RoundQuantity(_ context.Context, symbol string, qty *apd.Decimal) (*apd.Decimal, error)

func (*BinanceFutures) WatchAccountPositions

func (b *BinanceFutures) WatchAccountPositions(ctx context.Context) (<-chan exchanges.PositionEvent, error)

func (*BinanceFutures) WatchOrdersStatuses

func (b *BinanceFutures) WatchOrdersStatuses(ctx context.Context) (<-chan exchanges.OrderEvent, error)

func (*BinanceFutures) WatchSymbolPrice

func (b *BinanceFutures) WatchSymbolPrice(ctx context.Context, symbol string) (<-chan exchanges.PriceEvent, error)

type BinanceLong

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

BinanceLong TODO: probably it's better to switch to orderID instead of clientOrderID Order ID in the methods input and output is clientOrderID internally.

func NewBinanceLong

func NewBinanceLong(urls BinanceURLs, apiKey, secretKey string, lg *zap.Logger) *BinanceLong

func (*BinanceLong) CancelOrder

func (b *BinanceLong) CancelOrder(ctx context.Context, symbol, id string) error

func (*BinanceLong) GenerateClientOrderID

func (b *BinanceLong) GenerateClientOrderID(ctx context.Context, identifierID string) (string, error)

func (*BinanceLong) GetAccount

func (b *BinanceLong) GetAccount(ctx context.Context) (exchanges.Account, error)

func (*BinanceLong) GetName

func (b *BinanceLong) GetName() string

func (*BinanceLong) GetOpenOrders

func (b *BinanceLong) GetOpenOrders(ctx context.Context) ([]exchanges.OrderDetailInfo, error)

func (*BinanceLong) GetOrderInfo

func (b *BinanceLong) GetOrderInfo(ctx context.Context, symbol, id string, _ *time.Time) (exchanges.OrderInfo, error)

func (*BinanceLong) GetOrderInfoByClientOrderID

func (b *BinanceLong) GetOrderInfoByClientOrderID(ctx context.Context, symbol, clientOrderID string, _ *time.Time) (exchanges.OrderInfo, error)

func (*BinanceLong) GetOrders

func (b *BinanceLong) GetOrders(ctx context.Context, filter exchanges.OrderFilter) (res []exchanges.OrderDetailInfo, err error)

func (*BinanceLong) GetPrefix

func (b *BinanceLong) GetPrefix() string

func (*BinanceLong) GetPrice

func (b *BinanceLong) GetPrice(ctx context.Context, symbol string) (*apd.Decimal, error)

func (*BinanceLong) GetTradableSymbols

func (b *BinanceLong) GetTradableSymbols(ctx context.Context) ([]exchanges.SymbolInfo, error)

func (*BinanceLong) PlaceBuyOrder

func (b *BinanceLong) PlaceBuyOrder(ctx context.Context,
	_ bool, symbol string, price, quantity *apd.Decimal, prefferedID string,
) (id string, e error)

func (*BinanceLong) PlaceBuyOrderV2

func (b *BinanceLong) PlaceBuyOrderV2(ctx context.Context, _ bool, symbol string, price, qty *apd.Decimal, preferredID string, orderType string) (id string, e error)

PlaceBuyOrderV2 Place Buy Order with OrderType param

func (*BinanceLong) PlaceSellOrder

func (b *BinanceLong) PlaceSellOrder(ctx context.Context,
	_ bool, symbol string, price, quantity *apd.Decimal, prefferedID string,
) (id string, e error)

func (*BinanceLong) PlaceSellOrderV2

func (b *BinanceLong) PlaceSellOrderV2(ctx context.Context, _ bool, symbol string, price, qty *apd.Decimal, preferredID string, orderType string) (id string, e error)

PlaceSellOrderV2 Place Sell Order with OrderType param

func (*BinanceLong) ReleaseOrder

func (b *BinanceLong) ReleaseOrder(_ context.Context, symbol, id string) error

func (*BinanceLong) RoundPrice

func (b *BinanceLong) RoundPrice(_ context.Context, symbol string, price *apd.Decimal, tickSize *string) (*apd.Decimal, error)

func (*BinanceLong) RoundQuantity

func (b *BinanceLong) RoundQuantity(_ context.Context, symbol string, qty *apd.Decimal) (*apd.Decimal, error)

func (*BinanceLong) WatchAccountPositions

func (b *BinanceLong) WatchAccountPositions(ctx context.Context) (<-chan exchanges.PositionEvent, error)

func (*BinanceLong) WatchOrdersStatuses

func (b *BinanceLong) WatchOrdersStatuses(ctx context.Context) (<-chan exchanges.OrderEvent, error)

WatchOrdersStatuses Returns control immediately

func (*BinanceLong) WatchSymbolPrice

func (b *BinanceLong) WatchSymbolPrice(ctx context.Context, symbol string) (<-chan exchanges.PriceEvent, error)

WatchSymbolPrice OPTIMIZATION: subscribe to single symbol on client side not to all symbols.

type BinanceOrderCanceller

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

func NewBinanceOrderCanceller

func NewBinanceOrderCanceller(client *api.Client) *BinanceOrderCanceller

func (*BinanceOrderCanceller) CancelOrder

func (b *BinanceOrderCanceller) CancelOrder(ctx context.Context, symbol, clientOrderID string) error

type BinanceURLs

type BinanceURLs struct {
	APIURL                 string
	WebSocketBaseURL       string
	USAPIURL               string
	FutureAPIURL           string
	USWebSocketBaseURL     string
	FutureWebSocketBaseURL string
}
var OriginalBinanceURLs BinanceURLs = BinanceURLs{
	APIURL:                 "https://api.binance.com",
	WebSocketBaseURL:       "wss://stream.binance.com:9443/ws",
	USAPIURL:               "https://api.binance.us",
	FutureAPIURL:           "https://fapi.binance.com",
	USWebSocketBaseURL:     "wss://stream.binance.us:9443/ws",
	FutureWebSocketBaseURL: "wss://fstream.binance.com/ws",
}
var TestnetBinanceURLs BinanceURLs = BinanceURLs{
	APIURL:                 "https://testnet.binance.vision/api",
	WebSocketBaseURL:       "wss://testnet.binance.vision/ws",
	FutureWebSocketBaseURL: "wss://stream.binancefuture.com/ws",
	FutureAPIURL:           "https://testnet.binancefuture.com",
}

func (BinanceURLs) WSAllMiniMarketsStatURL

func (u BinanceURLs) WSAllMiniMarketsStatURL() string

WSAllMiniMarketsStatServe serve websocket that push mini version of 24hr statistics for all market every second

func (BinanceURLs) WSFuturesAllMiniMarketStatsURL

func (u BinanceURLs) WSFuturesAllMiniMarketStatsURL() string

func (BinanceURLs) WSFuturesUserDataURL

func (u BinanceURLs) WSFuturesUserDataURL(listenKey string) string

func (BinanceURLs) WSUSAllMiniMarketsStatURL

func (u BinanceURLs) WSUSAllMiniMarketsStatURL() string

func (BinanceURLs) WSUSUserDataURL

func (u BinanceURLs) WSUSUserDataURL(listenKey string) string

func (BinanceURLs) WSUserDataURL

func (u BinanceURLs) WSUserDataURL(listenKey string) string

WSUserDataServe serve user data handler with listen key

type BinanceUS

type BinanceUS struct {
	Client *api.Client
	// contains filtered or unexported fields
}

BinanceLong TODO: probably it's better to switch to orderID instead of clientOrderID Order ID in the methods input and output is clientOrderID internally.

func NewBinanceUS

func NewBinanceUS(urls BinanceURLs, apiKey, secretKey string, lg *zap.Logger) *BinanceUS

func (*BinanceUS) CancelOrder

func (b *BinanceUS) CancelOrder(ctx context.Context, symbol, id string) error

func (*BinanceUS) GenerateClientOrderID

func (b *BinanceUS) GenerateClientOrderID(ctx context.Context, identifierID string) (string, error)

func (*BinanceUS) GetAccount

func (b *BinanceUS) GetAccount(ctx context.Context) (exchanges.Account, error)

func (*BinanceUS) GetName

func (b *BinanceUS) GetName() string

func (*BinanceUS) GetOpenOrders

func (b *BinanceUS) GetOpenOrders(ctx context.Context) ([]exchanges.OrderDetailInfo, error)

func (*BinanceUS) GetOrderInfo

func (b *BinanceUS) GetOrderInfo(ctx context.Context, symbol, id string, _ *time.Time) (exchanges.OrderInfo, error)

func (*BinanceUS) GetOrderInfoByClientOrderID

func (b *BinanceUS) GetOrderInfoByClientOrderID(ctx context.Context, symbol, clientOrderID string, _ *time.Time) (exchanges.OrderInfo, error)

func (*BinanceUS) GetOrders

func (b *BinanceUS) GetOrders(ctx context.Context, filter exchanges.OrderFilter) (res []exchanges.OrderDetailInfo, err error)

func (*BinanceUS) GetPrefix

func (b *BinanceUS) GetPrefix() string

func (*BinanceUS) GetPrice

func (b *BinanceUS) GetPrice(ctx context.Context, symbol string) (*apd.Decimal, error)

func (*BinanceUS) GetTradableSymbols

func (b *BinanceUS) GetTradableSymbols(ctx context.Context) ([]exchanges.SymbolInfo, error)

func (*BinanceUS) PlaceBuyOrder

func (b *BinanceUS) PlaceBuyOrder(ctx context.Context,
	_ bool, symbol string, price, quantity *apd.Decimal, prefferedID string,
) (id string, e error)

func (*BinanceUS) PlaceBuyOrderV2

func (b *BinanceUS) PlaceBuyOrderV2(ctx context.Context, _ bool, symbol string, price, qty *apd.Decimal, preferredID string, orderType string) (id string, e error)

PlaceBuyOrderV2 Place Buy Order with OrderType param

func (*BinanceUS) PlaceSellOrder

func (b *BinanceUS) PlaceSellOrder(ctx context.Context,
	_ bool, symbol string, price, quantity *apd.Decimal, prefferedID string,
) (id string, e error)

func (*BinanceUS) PlaceSellOrderV2

func (b *BinanceUS) PlaceSellOrderV2(ctx context.Context, _ bool, symbol string, price, qty *apd.Decimal, preferredID string, orderType string) (id string, e error)

PlaceSellOrderV2 Place Sell Order with OrderType param

func (*BinanceUS) ReleaseOrder

func (b *BinanceUS) ReleaseOrder(_ context.Context, symbol, id string) error

func (*BinanceUS) RoundPrice

func (b *BinanceUS) RoundPrice(_ context.Context, symbol string, price *apd.Decimal, tickSize *string) (*apd.Decimal, error)

func (*BinanceUS) RoundQuantity

func (b *BinanceUS) RoundQuantity(_ context.Context, symbol string, qty *apd.Decimal) (*apd.Decimal, error)

func (*BinanceUS) WatchAccountPositions

func (b *BinanceUS) WatchAccountPositions(ctx context.Context) (<-chan exchanges.PositionEvent, error)

func (*BinanceUS) WatchOrdersStatuses

func (b *BinanceUS) WatchOrdersStatuses(ctx context.Context) (<-chan exchanges.OrderEvent, error)

func (*BinanceUS) WatchSymbolPrice

func (b *BinanceUS) WatchSymbolPrice(ctx context.Context, symbol string) (<-chan exchanges.PriceEvent, error)

type FuturesAccountUpdate

type FuturesAccountUpdate struct {
	EventType string                   `json:"e"`
	EventTime int64                    `json:"E"`
	Data      FuturesAccountUpdateData `json:"a"`
}

type FuturesAccountUpdateData

type FuturesAccountUpdateData struct {
	EventReasonType string                    `json:"m"`
	Positions       []FuturesBinancePositions `json:"P"`
}

type FuturesBinancePositions

type FuturesBinancePositions struct {
	Symbol         string `json:"s"`
	PositionAmount string `json:"pa"`
}

type OrderGetter

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

func (*OrderGetter) GetBinanceOrder

func (og *OrderGetter) GetBinanceOrder(ctx context.Context, symbol, clientOrderID string) (*api.Order, error)

func (*OrderGetter) GetHistoryOrders

func (og *OrderGetter) GetHistoryOrders(
	ctx context.Context,
	symbol string,
	orderID *string,
	clientOrderID *string,
) (res []exchanges.OrderDetailInfo, err error)

func (*OrderGetter) GetOpenOrders

func (og *OrderGetter) GetOpenOrders(ctx context.Context) (res []exchanges.OrderDetailInfo, err error)

func (*OrderGetter) GetOrderBinanceStatus

func (og *OrderGetter) GetOrderBinanceStatus(ctx context.Context, symbol, clientOrderID string) (api.OrderStatusType, error)

func (*OrderGetter) GetOrderInfoByClientOrderID

func (og *OrderGetter) GetOrderInfoByClientOrderID(ctx context.Context, symbol, clientOrderID string) (exchanges.OrderInfo, error)

type OrderPlacer

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

func NewOrderPlacer

func NewOrderPlacer(client *api.Client) *OrderPlacer

func (*OrderPlacer) CreateOrderRequest

func (op *OrderPlacer) CreateOrderRequest(
	symbol string, price, quantity *apd.Decimal, prefferedID string,
	side api.SideType,
) (*orderFields, error)

CreateOrderRequest Don't forget to floor `price` and `quantity`

func (*OrderPlacer) CreateOrderRequestV2

func (op *OrderPlacer) CreateOrderRequestV2(symbol string, price, quantity *apd.Decimal, preferredID string, side api.SideType, orderType api.OrderType) (*orderFields, error)

func (*OrderPlacer) PlaceOrder

func (op *OrderPlacer) PlaceOrder(ctx context.Context,
	symbol string, price, quantity *apd.Decimal, prefferedID string,
	side api.SideType,
) (id string, e error)

func (*OrderPlacer) PlaceOrderV2

func (op *OrderPlacer) PlaceOrderV2(ctx context.Context, symbol string, price, quantity *apd.Decimal, preferredID string,
	side api.SideType, orderType api.OrderType) (id string, e error)

func (*OrderPlacer) TestOrder

func (op *OrderPlacer) TestOrder(ctx context.Context,
	symbol string, price, quantity *apd.Decimal, prefferedID string,
	side api.SideType,
) error

type OrderUpdate

type OrderUpdate struct {
	// !WARNING Keep upper and lower case letters -- this is workaround to make case-sensetive JSON
	EventType string `json:"e"` // "e": "executionReport", // Event type
	EventTime int64  `json:"E"` // "E": 1499405658658,     // Event time

	ClientOrderID         string  `json:"c"` // "c": "mUvoqJxFIILMdfAW5iGSOW", // Client order ID
	OriginalClientOrderID *string `json:"C"` // "C": null,                     // Original client order ID; This is the ID of the order being canceled

	CurrentExecutionType string `json:"x"` // "x": "NEW", // Current execution type
	CurrentOrderStatus   string `json:"X"` // "X": "NEW", // Current order status

	Symbol string `json:"s"` // "s": "ETHBTC", // Symbol
	Side   string `json:"S"` // "S": "BUY",    // Side

}

type OrderUpdateDataFutures

type OrderUpdateDataFutures struct {
	ClientOrderID string `json:"c"` // "c": "mUvoqJxFIILMdfAW5iGSOW", // Client order ID              // Original client order ID; This is the ID of the order being canceled

	CurrentExecutionType string `json:"x"` // "x": "NEW", // Current execution type
	CurrentOrderStatus   string `json:"X"` // "X": "NEW", // Current order status

	Symbol string `json:"s"` // "s": "ETHBTC", // Symbol
	Side   string `json:"S"` // "S": "BUY",    // Side

}

type OrderUpdateFutures

type OrderUpdateFutures struct {
	EventType string                 `json:"e"` // "e": "ORDER_TRADE_UPDATE", // Event type
	EventTime int64                  `json:"E"` // "E": 1499405658658,     // Event time
	OrderData OrderUpdateDataFutures `json:"o"`
}

type PositionGetter

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

func (*PositionGetter) GetAccountBalances

func (pg *PositionGetter) GetAccountBalances(ctx context.Context) (res exchanges.Account, err error)

type WSAllMiniMarketsStatEvent

type WSAllMiniMarketsStatEvent []*WSMiniMarketsStatEvent

type WSMiniMarketsStatEvent

type WSMiniMarketsStatEvent struct {
	// ! DON'T REMOVE NullJSONValue fields. They are used to make JSON case-senitive
	Event string `json:"e"`
	Time  int64  `json:"E"`

	Symbol  string              `json:"s"`
	Ignore1 utils.NullJSONValue `json:"S"`

	LastPrice string              `json:"c"`
	Ignore2   utils.NullJSONValue `json:"C"`

	OpenPrice string              `json:"o"`
	Ignore3   utils.NullJSONValue `json:"O"`

	HighPrice string              `json:"h"`
	Ignore4   utils.NullJSONValue `json:"H"`

	LowPrice string              `json:"l"`
	Ignore5  utils.NullJSONValue `json:"L"`

	BaseVolume string              `json:"v"`
	Ignore6    utils.NullJSONValue `json:"V"`

	QuoteVolume string              `json:"q"`
	Ignore7     utils.NullJSONValue `json:"Q"`
}

WSMiniMarketsStatEvent define websocket market mini-ticker statistics event

Jump to

Keyboard shortcuts

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