ws

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HostRealPublic     = "wss://stream.bybit.com/realtime_public"
	HostRealPrivate    = "wss://stream.bybit.com/realtime_private"
	HostTestnetPublic  = "wss://stream-testnet.bybit.com/realtime_public"
	HostTestnetPrivate = "wss://stream-testnet.bybit.com/realtime_private"
)
View Source
const (
	WSOrderBook25L1 = "orderBookL2_25" // 新版25档orderBook: order_book_25L1.BTCUSD
	WSKLine         = "kline"          // K线: kline.BTCUSD.1m
	WSKLineV2       = "klineV2"        // V2版本K线: klineV2.1.BTCUSD
	WSCandle        = "candle"         // USDT永续K线: candle.1.BTCUSDT
	WSTrade         = "trade"          // 实时交易: trade/trade.BTCUSD
	WSInsurance     = "insurance"      // 每日保险基金更新: insurance
	WSInstrument    = "instrument"     // 产品最新行情: instrument
	WSLiquidation   = "liquidation"    // 強平推送: liquidation

	WSPosition  = "position"   // 仓位变化: position
	WSExecution = "execution"  // 委托单成交信息: execution
	WSOrder     = "order"      // 委托单的更新: order
	WSStopOrder = "stop_order" // 条件单的更新: stop_order
	WSWallet    = "wallet"     // 条件单的更新: stop_order

	WSDisconnected = "disconnected" // WS断开事件
)
View Source
const (
	MaxTryTimes = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ByBitWS

type ByBitWS struct {
	Ended bool
	// contains filtered or unexported fields
}

func New

func New(config *Configuration) *ByBitWS

func (*ByBitWS) Auth

func (b *ByBitWS) Auth() error

func (*ByBitWS) CloseAndReconnect

func (b *ByBitWS) CloseAndReconnect()

func (*ByBitWS) Emit

func (b *ByBitWS) Emit(event interface{}, arguments ...interface{}) *emission.Emitter

Emit emits an event

func (*ByBitWS) IsConnected

func (b *ByBitWS) IsConnected() bool

IsConnected returns the WebSocket connection state

func (*ByBitWS) Off

func (b *ByBitWS) Off(event interface{}, listener interface{}) *emission.Emitter

Off removes a listener for an event

func (*ByBitWS) On

func (b *ByBitWS) On(event interface{}, listener interface{}) *emission.Emitter

On adds a listener to a specific event

func (*ByBitWS) Send

func (b *ByBitWS) Send(msg string) (err error)

func (*ByBitWS) SendCmd

func (b *ByBitWS) SendCmd(cmd Cmd) error

func (*ByBitWS) Start

func (b *ByBitWS) Start() error

func (*ByBitWS) Subscribe

func (b *ByBitWS) Subscribe(arg string)

type Cmd

type Cmd struct {
	Op   string        `json:"op"`
	Args []interface{} `json:"args"`
}

ws.send('{"op":"subscribe","args":["topic","topic.filter"]}');

// 同一个类型的filter有多个时,以'|'分割 // 如订阅BTCUSD一分钟和三分钟的kline ws.send('{"op":"subscribe","args":["kline.BTCUSD.1m|3m"]}');

// 订阅同一个类型filter的所有数据时请使用'*' // 如订阅所有产品的所有interval kline ws.send('{"op":"subscribe","args":["kline.*.*"]}')

type Configuration

type Configuration struct {
	Addr          string `json:"addr"`
	Proxy         string `json:"proxy"` // http://127.0.0.1:1081
	ApiKey        string `json:"api_key"`
	SecretKey     string `json:"secret_key"`
	AutoReconnect bool   `json:"auto_reconnect"`
	DebugMode     bool   `json:"debug_mode"`
}

type Execution

type Execution struct {
	Symbol      string    `json:"symbol"`        // 合约类型
	Side        string    `json:"side"`          // 方向
	OrderID     string    `json:"order_id"`      // 订单ID
	ExecID      string    `json:"exec_id"`       // 成交ID
	OrderLinkID string    `json:"order_link_id"` // 自定义订单ID
	Price       float64   `json:"price"`         // 成交价格
	OrderQty    float64   `json:"order_qty"`     // 订单数量
	ExecType    string    `json:"exec_type"`     // 交易类型,Trade/AdlTrade/BustTrade
	ExecQty     float64   `json:"exec_qty"`      // 成交数量
	ExecFee     float64   `json:"exec_fee"`      // 交易手续费
	LeavesQty   float64   `json:"leaves_qty"`    // 剩余委托数量
	IsMaker     bool      `json:"is_maker"`      // 是否是maker
	TradeTime   time.Time `json:"trade_time"`    // 交易时间
}

type Instrument

type Instrument struct {
	Symbol     string  `json:"symbol"`
	MarkPrice  float64 `json:"mark_price"`
	IndexPrice float64 `json:"index_price"`
}

type Insurance

type Insurance struct {
	Currency      string    `json:"currency"`
	Timestamp     time.Time `json:"timestamp"`
	WalletBalance int64     `json:"wallet_balance"`
}

type Item

type Item struct {
	Amount float64 `json:"amount"`
	Price  float64 `json:"price"`
}

Item stores the amount and price values

type KLine

type KLine struct {
	ID       int64   `json:"id"`        // 563
	Symbol   string  `json:"symbol"`    // BTCUSD
	OpenTime int64   `json:"open_time"` // 1539918000
	Open     float64 `json:"open"`
	High     float64 `json:"high"`
	Low      float64 `json:"low"`
	Close    float64 `json:"close"`
	Volume   float64 `json:"volume"`
	Turnover float64 `json:"turnover"` // 0.0013844
	Interval string  `json:"interval"` // 1m
}

type KLineV2

type KLineV2 struct {
	Symbol    string  `json:"symbol"`          // 合约类型,从 topic 解析得到
	Start     int64   `json:"start"`           // 开始时间戳(秒)
	End       int64   `json:"end"`             // 结束时间戳(秒)
	Open      float64 `json:"open"`            // 开盘价
	Close     float64 `json:"close"`           // 收盘价
	High      float64 `json:"high"`            // 最高价格
	Low       float64 `json:"low"`             // 最低价格
	Volume    float64 `json:"volume,string"`   // 交易量 TODO: 反向永续类型不一样
	Turnover  float64 `json:"turnover,string"` // 成交金额 0.0013844 TODO: 反向永续类型不一样
	Confirm   bool    `json:"confirm"`         // 是否确认,为 true 表明是 k 线 最后一个 tick,否则只是一个快照数据,即中间价格
	CrossSeq  int     `json:"cross_seq"`       // 版本号
	Interval  string  `json:"interval"`        // 周期,从 topic 解析得到: 1 3 5 15 30 60 120 240 360 D W M
	Timestamp int64   `json:"timestamp"`       // 结束时间戳(秒)
}

type Liquidation

type Liquidation struct {
	Symbol string       `json:"symbol"` // 合约类型
	Side   string       `json:"side"`   // 被强平仓位的方向
	Price  sjson.Number `json:"price"`  // 破产价格
	Qty    float64      `json:"qty"`    // 交易數量
	Time   int64        `json:"time"`   // 毫秒時間戳
}

type Order

type Order struct {
	OrderID        string       `json:"order_id"`            // 订单ID
	OrderLinkID    string       `json:"order_link_id"`       // 自定义订单ID
	Symbol         string       `json:"symbol"`              // 合约类型
	Side           string       `json:"side"`                // 方向
	OrderType      string       `json:"order_type"`          // 委托单价格类型,Limit/Market
	Price          sjson.Number `json:"price"`               // 委托价格
	Qty            float64      `json:"qty"`                 // 委托数量
	TimeInForce    string       `json:"time_in_force"`       // 执行策略,GoodTillCancel/ImmediateOrCancel/FillOrKill/PostOnly
	CreateType     string       `json:"create_type"`         // 下单操作的触发场景
	CancelType     string       `json:"cancel_type"`         // 取消操作的触发场景
	OrderStatus    string       `json:"order_status"`        // 订单状态
	LeavesQty      float64      `json:"leaves_qty"`          // 剩余委托数量
	CumExecQty     float64      `json:"cum_exec_qty"`        // 累计成交数量
	CumExecValue   sjson.Number `json:"cum_exec_value"`      // 累计成交价值
	CumExecFee     sjson.Number `json:"cum_exec_fee"`        // 累计成交手续费
	Timestamp      time.Time    `json:"timestamp"`           // 创建时间,only valid for inverse
	CreateTime     time.Time    `json:"create_time"`         // 创建时间,only valid for linear
	UpdateTime     time.Time    `json:"update_time"`         // 成交时间,only valid for linear
	TakeProfit     sjson.Number `json:"take_profit"`         // 止盈价格
	StopLoss       sjson.Number `json:"stop_loss"`           // 止损价格
	TrailingStop   sjson.Number `json:"trailing_stop"`       // 追踪止损(与当前价格的距离)
	TrailingActive sjson.Number `json:"trailing_active"`     // 激活价格
	LastExecPrice  sjson.Number `json:"last_exec_price"`     // 最近一次成交价格
	ReduceOnly     bool         `json:"reduce_only"`         // 只减仓
	PositionIdx    int          `json:"position_idx,string"` // 用于在不同仓位模式下标识仓位:0 - 单向持仓,1 - 双向持仓Buy,2 - 双向持仓Sell,only valid for linear
	CloseOnTrigger bool         `json:"close_on_trigger"`    // 触发后平仓,如果下平仓单,请设置为 true,避免因为保证金不足而导致下单失败
}

type OrderBook

type OrderBook struct {
	Bids      []Item    `json:"bids"`
	Asks      []Item    `json:"asks"`
	Timestamp time.Time `json:"timestamp"`
}

type OrderBookL2

type OrderBookL2 struct {
	ID     int64   `json:"id,string"`
	Price  float64 `json:"price,string"`
	Side   string  `json:"side"`
	Size   float64 `json:"size"`
	Symbol string  `json:"symbol"`
}

func (*OrderBookL2) Key

func (o *OrderBookL2) Key() string

type OrderBookL2Delta

type OrderBookL2Delta struct {
	Delete []*OrderBookL2 `json:"delete"`
	Update []*OrderBookL2 `json:"update"`
	Insert []*OrderBookL2 `json:"insert"`
}

type OrderBookLocal

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

func NewOrderBookLocal

func NewOrderBookLocal() *OrderBookLocal

func (*OrderBookLocal) GetOrderBook

func (o *OrderBookLocal) GetOrderBook() (ob OrderBook)

func (*OrderBookLocal) LoadSnapshot

func (o *OrderBookLocal) LoadSnapshot(newOrderbook []*OrderBookL2) error

func (*OrderBookLocal) Update

func (o *OrderBookLocal) Update(delta *OrderBookL2Delta)

type Position

type Position struct {
	UserID         int64   `json:"user_id,string"`      // 用户 ID
	Symbol         string  `json:"symbol"`              // 合约类型
	Size           float64 `json:"size"`                // 仓位数量
	Side           string  `json:"side"`                // 方向
	PositionValue  float64 `json:"position_value"`      // 仓位价值
	EntryPrice     float64 `json:"entry_price"`         // 平均入场价
	LiqPrice       float64 `json:"liq_price"`           // 强平价格
	BustPrice      float64 `json:"bust_price"`          // 破产价格
	Leverage       float64 `json:"leverage"`            // 逐仓模式下,值为一哦哪个好设置的杠杆;全仓模式下,值为当前风险限额下最大杠杆
	OrderMargin    float64 `json:"order_margin"`        // 委托预占用保证金
	PositionMargin float64 `json:"position_margin"`     // 仓位保证金
	OccClosingFee  float64 `json:"occ_closing_fee"`     // 仓位占用的平仓手续费
	TakeProfit     float64 `json:"take_profit"`         // 止盈价格
	TpTriggerBy    string  `json:"tp_trigger_by"`       // 止盈激活价格类型,默认为 LastPrice
	StopLoss       float64 `json:"stop_loss"`           // 止损价格
	SlTriggerBy    string  `json:"sl_trigger_by"`       // 止损激活价格类型
	RealisedPnl    float64 `json:"realised_pnl"`        // 当日已结盈亏
	CumRealisedPnl float64 `json:"cum_realised_pnl"`    // 累计已结盈亏
	PositionStatus string  `json:"position_status"`     // 仓位状态:正常、强平、减仓
	PositionSeq    int64   `json:"position_seq,string"` // 仓位变化版本号
	PositionIdx    int     `json:"position_idx,string"` // 用于在不同仓位模式下标识仓位:0 - 单向持仓,1 - 双向持仓Buy,2 - 双向持仓Sell,only valid for linear
	Mode           string  `json:"mode"`                // 仓位模式: MergedSingle or BothSide
	Isolated       bool    `json:"isolated"`            // 是否逐仓,true-逐仓 false-全仓
	RiskID         int     `json:"risk_id,string"`      // 风险限额 ID

	// 反向永续字段
	TrailingStop     float64 `json:"trailing_stop"` //
	TrailingActive   float64 `json:"trailing_active"`
	WalletBalance    float64 `json:"wallet_balance"`
	AvailableBalance float64 `json:"available_balance"`
	OccFundingFee    float64 `json:"occ_funding_fee"`
	AutoAddMargin    int     `json:"auto_add_margin,string"`
}

type StopOrder

type StopOrder struct {
	OrderID        string       `json:"order_id"`
	OrderLinkID    string       `json:"order_link_id"`
	UserID         int64        `json:"user_id"`
	Symbol         string       `json:"symbol"`
	Side           string       `json:"side"`
	OrderType      string       `json:"order_type"`
	Price          sjson.Number `json:"price"`
	Qty            float64      `json:"qty"`
	TimeInForce    string       `json:"time_in_force"` // GoodTillCancel/ImmediateOrCancel/FillOrKill/PostOnly
	CreateType     string       `json:"create_type"`
	CancelType     string       `json:"cancel_type"`
	OrderStatus    string       `json:"order_status"`
	StopOrderType  string       `json:"stop_order_type"`
	TriggerBy      string       `json:"trigger_by"`
	TriggerPrice   sjson.Number `json:"trigger_pricee"`
	CloseOnTrigger bool         `json:"close_on_trigger"`
	Timestamp      time.Time    `json:"timestamp"`
}

type Trade

type Trade struct {
	Timestamp     time.Time `json:"timestamp"`
	Symbol        string    `json:"symbol"`
	Side          string    `json:"side"`
	Size          float64   `json:"size"`
	Price         float64   `json:"price"`
	TickDirection string    `json:"tick_direction"`
	TradeID       string    `json:"trade_id"`
	CrossSeq      int       `json:"cross_seq"` // only valid for inverse
}

type Wallet added in v1.1.3

type Wallet struct {
	WalletBalance    float64 `json:"wallet_balance"`
	AvailableBalance float64 `json:"available_balance"`
}

Jump to

Keyboard shortcuts

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