stream

package
v0.0.0-...-fc0de6e Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2026 License: Apache-2.0 Imports: 9 Imported by: 11

Documentation

Index

Constants

View Source
const (
	TypeLimit                   = 1
	TypeMarket                  = 2
	TypeStopLimit               = 3
	TypeStopMarket              = 4
	TypeConditionalLimit        = 5
	TypeConditionalMarket       = 6
	TypeMarginMarket            = 8
	TypeMarginStopLimit         = 9
	TypeMarginTriggerStopMarket = 10
	TypeCollateralNormalization = 14
	TypeMarketStock             = 202
)

Order type constants.

View Source
const (
	PingRequest = "ping"
	TimeRequest = "time"

	KlineRequest     = "candles_request"
	KlineSubscribe   = "candles_subscribe"
	KlineUpdate      = "candles_update"
	KlineUnsubscribe = "candles_unsubscribe"

	DepthRequest     = "depth_request"
	DepthSubscribe   = "depth_subscribe"
	DepthUpdate      = "depth_update"
	DepthUnsubscribe = "depth_unsubscribe"

	LastPriceRequest     = "lastprice_request"
	LastPriceSubscribe   = "lastprice_subscribe"
	LastPriceUpdate      = "lastprice_update"
	LastPriceUnsubscribe = "lastprice_unsubscribe"

	MarketStatRequest     = "market_request"
	MarketStatSubscribe   = "market_subscribe"
	MarketStatUpdate      = "market_update"
	MarketStatUnsubscribe = "market_unsubscribe"

	MarketStatTodayRequest     = "marketToday_query"
	MarketStatTodaySubscribe   = "marketToday_subscribe"
	MarketStatTodayUpdate      = "marketToday_update"
	MarketStatTodayUnsubscribe = "marketToday_unsubscribe"

	TradesRequest     = "trades_request"
	TradesSubscribe   = "trades_subscribe"
	TradesUpdate      = "trades_update"
	TradesUnsubscribe = "trades_unsubscribe"

	OrderPendingRequest      = "ordersPending_request"
	OrdersPendingSubscribe   = "ordersPending_subscribe"
	OrdersPendingUpdate      = "ordersPending_update"
	OrdersPendingUnsubscribe = "ordersPending_unsubscribe"

	DealsRequest     = "deals_request"
	DealsSubscribe   = "deals_subscribe"
	DealsUpdate      = "deals_update"
	DealsUnsubscribe = "deals_unsubscribe"

	SpotBalanceRequest     = "balanceSpot_request"
	SpotBalanceSubscribe   = "balanceSpot_subscribe"
	SpotBalanceUpdate      = "balanceSpot_update"
	SpotBalanceUnsubscribe = "balanceSpot_unsubscribe"

	OrdersExecutedRequest     = "ordersExecuted_request"
	OrdersExecutedSubscribe   = "ordersExecuted_subscribe"
	OrdersExecutedUpdate      = "ordersExecuted_update"
	OrdersExecutedUnsubscribe = "ordersExecuted_unsubscribe"

	MarginBalanceRequest     = "balanceMargin_request"
	MarginBalanceSubscribe   = "balanceMargin_subscribe"
	MarginBalanceUpdate      = "balanceMargin_update"
	MarginBalanceUnsubscribe = "balanceMargin_unsubscribe"

	WsTypeQuery     = true
	WsTypeSubscribe = false
)

WebSocket method constants.

View Source
const (
	// ReadTimeout is the maximum duration for read operations on the WebSocket connection.
	ReadTimeout = 60 * time.Second
	// WriteTimeout is the maximum duration for write operations on the WebSocket connection.
	WriteTimeout = 30 * time.Second
)

Variables

This section is empty.

Functions

func TransformEvent

func TransformEvent[E any](event Event) (E, error)

TransformEvent converts a generic Event into a typed event struct.

Types

type AsksAndBids

type AsksAndBids struct {
	Asks []Pair `json:"asks"`
	Bids []Pair `json:"bids"`
}

AsksAndBids contains asks and bids from the order book.

type AssetBalance

type AssetBalance struct {
	Available string `json:"available"`
	Freeze    string `json:"freeze"`
}

AssetBalance represents the balance of an asset.

type Command

type Command struct {
	ID      int64  `json:"id"`
	Method  string `json:"method"`
	Params  []any  `json:"params"`
	IsQuery bool   `json:"-"`
}

Command represents a WebSocket command to send to the server.

func NewAuthorizeCommand

func NewAuthorizeCommand(token string) Command

NewAuthorizeCommand creates an authorization command with the given token.

func NewDealsCommand

func NewDealsCommand(market string, offset int64, limit int64) Command

NewDealsCommand creates a deals query command.

func NewDealsUnsubscribe

func NewDealsUnsubscribe() Command

NewDealsUnsubscribe creates a deals unsubscribe command.

func NewDepthUnsubscribe

func NewDepthUnsubscribe() Command

NewDepthUnsubscribe creates a market depth unsubscribe command.

func NewKlineCommand

func NewKlineCommand(market string, startTime int64, endTime int64, interval int64) Command

NewKlineCommand creates a kline query command.

func NewKlineUnsubscribe

func NewKlineUnsubscribe() Command

NewKlineUnsubscribe creates a kline unsubscribe command.

func NewLastPriceCommand

func NewLastPriceCommand(market string) Command

NewLastPriceCommand creates a last price query command.

func NewLastPriceUnsubscribe

func NewLastPriceUnsubscribe() Command

NewLastPriceUnsubscribe creates a last price unsubscribe command.

func NewMarginBalanceCommand

func NewMarginBalanceCommand(assets []string) Command

NewMarginBalanceCommand creates a margin balance query command.

func NewMarginBalanceUnsubscribe

func NewMarginBalanceUnsubscribe() Command

NewMarginBalanceUnsubscribe creates a margin balance unsubscribe command.

func NewMarketDepthCommand

func NewMarketDepthCommand(market string, limit int64, priceInterval string) Command

NewMarketDepthCommand creates a market depth query command.

func NewMarketStatCommand

func NewMarketStatCommand(market string, period int64) Command

NewMarketStatCommand creates a market statistics query command.

func NewMarketStatTodayCommand

func NewMarketStatTodayCommand(market string) Command

NewMarketStatTodayCommand creates a market statistics today query command.

func NewMarketStatTodayUnsubscribe

func NewMarketStatTodayUnsubscribe() Command

NewMarketStatTodayUnsubscribe creates a market statistics today unsubscribe command.

func NewMarketStatUnsubscribe

func NewMarketStatUnsubscribe() Command

NewMarketStatUnsubscribe creates a market statistics unsubscribe command.

func NewMarketTradesCommand

func NewMarketTradesCommand(market string, limit int64, startTradeID int64) Command

NewMarketTradesCommand creates a market trades query command.

func NewMarketTradesUnsubscribe

func NewMarketTradesUnsubscribe() Command

NewMarketTradesUnsubscribe creates a market trades unsubscribe command.

func NewOrdersExecutedCommand

func NewOrdersExecutedCommand(market string, orderTypes []int64, limit int64, offset int64) Command

NewOrdersExecutedCommand creates an executed orders query command.

func NewOrdersExecutedUnsubscribe

func NewOrdersExecutedUnsubscribe() Command

NewOrdersExecutedUnsubscribe creates an executed orders unsubscribe command.

func NewOrdersPendingUnsubscribe

func NewOrdersPendingUnsubscribe() Command

NewOrdersPendingUnsubscribe creates a pending orders unsubscribe command.

func NewPendingOrdersCommand

func NewPendingOrdersCommand(market string, offset int64, limit int64) Command

NewPendingOrdersCommand creates a pending orders query command.

func NewPingCommand

func NewPingCommand() Command

NewPingCommand creates a ping command.

func NewSpotBalanceCommand

func NewSpotBalanceCommand(assets []string) Command

NewSpotBalanceCommand creates a spot balance query command.

func NewSpotBalanceUnsubscribe

func NewSpotBalanceUnsubscribe() Command

NewSpotBalanceUnsubscribe creates a spot balance unsubscribe command.

func NewStreamCommand

func NewStreamCommand(id int64, method string, params []any) Command

NewStreamCommand creates a generic stream command.

func NewTimeCommand

func NewTimeCommand() Command

NewTimeCommand creates a server time query command.

func NewUnsubscribeCommand

func NewUnsubscribeCommand(method string) Command

NewUnsubscribeCommand creates a generic unsubscribe command.

type CommandHandler

type CommandHandler struct {
	Command Command
	Handler func(command Command, response []byte)
}

CommandHandler associates a Command with its response handler callback.

type CommandReply

type CommandReply struct {
	ID     int `json:"id"`
	Error  any `json:"error"`
	Result any `json:"result"`
}

CommandReply represents a response to a Command from the server.

type Deal

type Deal struct {
	Time        float64 `json:"time"`
	ID          int     `json:"id"`
	Side        int     `json:"side"`
	Role        int     `json:"role"`
	Price       string  `json:"price"`
	Amount      string  `json:"amount"`
	Deal        string  `json:"deal"`
	Fee         string  `json:"fee"`
	Market      string  `json:"market"`
	DealOrderID int64   `json:"deal_order_id"`
}

Deal represents a single deal/trade.

type Deals

type Deals struct {
	Response
	Result DealsResult `json:"result"`
}

Deals represents a deals response.

type DealsResult

type DealsResult struct {
	ResponsePagination
	Records []Deal `json:"records"`
}

DealsResult contains a paginated list of deals.

type DealsUpdateEvent

type DealsUpdateEvent struct {
	Params []any `json:"params"`
}

DealsUpdateEvent represents a deals update event.

type Event

type Event struct {
	Method string `json:"method"`
	Params []any  `json:"params"`
	ID     int64  `json:"id"`
}

Event represents a WebSocket event received from the server.

type ExecutedOrders

type ExecutedOrders struct {
	Response
	Result Orders `json:"result"`
}

ExecutedOrders represents an executed orders response.

type Kline

type Kline struct {
	Response
	Result []KlineResult `json:"result"`
}

Kline represents a kline response.

type KlineResult

type KlineResult struct {
	Time        int64
	Open        string
	Close       string
	Highest     string
	Lowest      string
	StockVolume string
	DealVolume  string
	Market      string
}

KlineResult represents a single kline/candlestick data point.

type KlineUpdateEvent

type KlineUpdateEvent struct {
	Params []any `json:"params"`
}

KlineUpdateEvent represents a kline update event.

type LastPrice

type LastPrice struct {
	Response
	Result string `json:"result"`
}

LastPrice represents a last price response.

type LastPriceUpdateEvent

type LastPriceUpdateEvent struct {
	Params any `json:"params"`
}

LastPriceUpdateEvent represents a last price update event.

type MarginBalance

type MarginBalance map[string]MarginBalanceQueryInfo

MarginBalance is a map of asset name to margin balance info.

type MarginBalanceEventInfo

type MarginBalanceEventInfo struct {
	Balance                string `json:"B"`
	Asset                  string `json:"a"`
	AvailableWithBorrow    string `json:"ab"`
	AvailableWithoutBorrow string `json:"av"`
	Borrow                 string `json:"b"`
}

MarginBalanceEventInfo contains margin balance event details.

type MarginBalanceQueryInfo

type MarginBalanceQueryInfo struct {
	AvailableWithBorrow    string `json:"available_with_borrow"`
	AvailableWithoutBorrow string `json:"available_without_borrow"`
	Balance                string `json:"balance"`
	Borrow                 string `json:"borrow"`
}

MarginBalanceQueryInfo contains margin balance query details.

type MarginBalanceResponse

type MarginBalanceResponse struct {
	Response
	Result map[string]string
}

MarginBalanceResponse represents a margin balance response.

type MarginBalanceUpdateEvent

type MarginBalanceUpdateEvent struct {
	Balances []MarginBalanceEventInfo `json:"params"`
}

MarginBalanceUpdateEvent represents a margin balance update event.

type MarketDepth

type MarketDepth struct {
	Response
	Result []AsksAndBids `json:"result"`
}

MarketDepth represents a market depth response.

type MarketDepthUpdateEvent

type MarketDepthUpdateEvent struct {
	Params []any `json:"params"`
}

MarketDepthUpdateEvent represents a market depth update event.

type MarketStat

type MarketStat struct {
	Response
	Result MarketStatResult `json:"result"`
}

MarketStat represents a market statistics response.

type MarketStatResult

type MarketStatResult struct {
	Period int    `json:"period"`
	Last   string `json:"last"`
	Open   string `json:"open"`
	Close  string `json:"close"`
	High   string `json:"high"`
	Low    string `json:"low"`
	Volume string `json:"volume"`
	Deal   string `json:"deal"`
}

MarketStatResult contains market statistics data.

type MarketStatTodayUpdateEvent

type MarketStatTodayUpdateEvent struct {
	Params []any `json:"params"`
}

MarketStatTodayUpdateEvent represents a market statistics today update event.

type MarketStatUpdateEvent

type MarketStatUpdateEvent struct {
	Params []any `json:"params"`
}

MarketStatUpdateEvent represents a market statistics update event.

type MarketTrades

type MarketTrades struct {
	Response
	Result MarketTradesResult `json:"result"`
}

MarketTrades represents a market trades response.

type MarketTradesResult

type MarketTradesResult struct {
	ID     int     `json:"id"`
	Time   float64 `json:"time"`
	Price  string  `json:"price"`
	Amount string  `json:"amount"`
	Type   string  `json:"type"`
}

MarketTradesResult contains a single market trade.

type MarketTradesUpdateEvent

type MarketTradesUpdateEvent struct {
	Params []any `json:"params"`
}

MarketTradesUpdateEvent represents a market trades update event.

type OrderExecutedUpdateEvent

type OrderExecutedUpdateEvent struct {
	Params []OrdersRecords `json:"params"`
}

OrderExecutedUpdateEvent represents an executed order update event.

type Orders

type Orders struct {
	ResponsePagination
	Records []OrdersRecords `json:"records"`
}

Orders contains a paginated list of order records.

type OrdersExecuted

type OrdersExecuted struct {
	Limit   int             `json:"limit"`
	Offset  int             `json:"offset"`
	Records []OrdersRecords `json:"records"`
}

OrdersExecuted contains executed order records with pagination.

type OrdersRecords

type OrdersRecords struct {
	ID            int     `json:"id"`
	Market        string  `json:"market"`
	Type          int     `json:"type"`
	Side          int     `json:"side"`
	PostOnly      bool    `json:"post_only"`
	IOC           bool    `json:"ioc"`
	Ctime         float64 `json:"ctime"`
	Mtime         float64 `json:"mtime"`
	Price         string  `json:"price"`
	Amount        string  `json:"amount"`
	Left          string  `json:"left"`
	DealStock     string  `json:"deal_stock"`
	DealMoney     string  `json:"deal_money"`
	DealFee       string  `json:"deal_fee"`
	ClientOrderID string  `json:"client_order_id"`
}

OrdersRecords contains order details.

type Pair

type Pair [2]string

Pair represents a price-amount pair in the order book.

type Pending

type Pending struct {
	ID                   int64
	PendingOrdersRecords OrdersRecords
}

Pending represents a pending order.

type PendingOrders

type PendingOrders struct {
	Response
	Result Orders `json:"result"`
}

PendingOrders represents a pending orders response.

type PendingOrdersUpdateEvent

type PendingOrdersUpdateEvent struct {
	Balances []any `json:"params"`
}

PendingOrdersUpdateEvent represents a pending orders update event.

type Ping

type Ping struct {
	Response
	Result string `json:"result"`
}

Ping represents a ping response.

type Response

type Response struct {
	ID    int `json:"id"`
	Error any `json:"error"`
}

Response represents a basic WebSocket response.

type ResponsePagination

type ResponsePagination struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
	Total  int `json:"total"`
}

ResponsePagination contains pagination info from paginated responses.

type SpotBalance

type SpotBalance struct {
	Response
	Result map[string]AssetBalance
}

SpotBalance represents a spot balance response.

type SpotBalanceUpdateEvent

type SpotBalanceUpdateEvent struct {
	Balances []map[string]AssetBalance `json:"params"`
}

SpotBalanceUpdateEvent represents a spot balance update event.

type Stream

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

Stream manages a WebSocket connection to the WhiteBit API, handling subscriptions, queries, automatic reconnection, and keepalive pings.

func NewStream

func NewStream(ctx context.Context, token string, errorHandler func(err error)) (*Stream, error)

NewStream creates a new WebSocket stream connection to the WhiteBit API. If token is non-empty, the connection will be authorized. The errorHandler is called for any asynchronous errors and must not be nil. The provided context controls the lifetime of the connection.

func (*Stream) Close

func (stream *Stream) Close() error

Close closes the WebSocket connection.

func (*Stream) Query

func (stream *Stream) Query(command Command, callback func(command Command, response []byte)) error

Query sends a one-shot command and invokes the callback with the response.

func (*Stream) Subscribe

func (stream *Stream) Subscribe(command *Subscription) error

Subscribe sends a subscription command and registers the event handler.

func (*Stream) Unsubscribe

func (stream *Stream) Unsubscribe(sub *Subscription) error

Unsubscribe sends an unsubscribe command and removes the subscription from the stream.

type Subscription

type Subscription struct {
	Command
	EventMethod       string
	OnEvent           func(event Event)
	UnsubscribeMethod Command
	// contains filtered or unexported fields
}

Subscription represents a WebSocket subscription with an event handler.

func NewDealsSubscription

func NewDealsSubscription(handle func(event DealsUpdateEvent), market []string) *Subscription

NewDealsSubscription creates a subscription for deal updates.

func NewKlineSubscription

func NewKlineSubscription(handle func(event KlineUpdateEvent), market string, interval int64) *Subscription

NewKlineSubscription creates a subscription for kline/candlestick updates.

func NewLastPriceSubscription

func NewLastPriceSubscription(handle func(event LastPriceUpdateEvent), market []string) *Subscription

NewLastPriceSubscription creates a subscription for last price updates.

func NewMarginBalanceSubscription

func NewMarginBalanceSubscription(handle func(event MarginBalanceUpdateEvent), assets ...string) *Subscription

NewMarginBalanceSubscription creates a subscription for margin balance updates.

func NewMarketDepthSubscription

func NewMarketDepthSubscription(handle func(MarketDepthUpdateEvent), market string, limit int64, priceInterval string, multiSubscription bool) *Subscription

NewMarketDepthSubscription creates a subscription for market depth updates.

func NewMarketStatSubscription

func NewMarketStatSubscription(handle func(MarketStatUpdateEvent), market []string) *Subscription

NewMarketStatSubscription creates a subscription for market statistics updates.

func NewMarketStatTodaySubscription

func NewMarketStatTodaySubscription(handle func(MarketStatTodayUpdateEvent), market []string) *Subscription

NewMarketStatTodaySubscription creates a subscription for today's market statistics updates.

func NewMarketTradesSubscription

func NewMarketTradesSubscription(handle func(MarketTradesUpdateEvent), market []string) *Subscription

NewMarketTradesSubscription creates a subscription for market trade updates.

func NewOrderExecutedSubscription

func NewOrderExecutedSubscription(handle func(event OrderExecutedUpdateEvent), market []string, filter int64) *Subscription

NewOrderExecutedSubscription creates a subscription for executed order updates.

func NewPendingOrdersSubscription

func NewPendingOrdersSubscription(handle func(event PendingOrdersUpdateEvent), market ...string) *Subscription

NewPendingOrdersSubscription creates a subscription for pending order updates.

func NewSpotBalanceSubscription

func NewSpotBalanceSubscription(handle func(event SpotBalanceUpdateEvent), assets ...string) *Subscription

NewSpotBalanceSubscription creates a subscription for spot balance updates.

type Time

type Time struct {
	Response
	Result int64 `json:"result"`
}

Time represents a time response.

Jump to

Keyboard shortcuts

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