代码拉取完成,页面将自动刷新
package bybit
import (
"fmt"
"log"
"time"
. "gitee.com/378077287/exchanges"
"github.com/chuckpreslar/emission"
bws "github.com/frankrap/bybit-api/ws"
)
type BybitWebSocket struct {
ws *bws.ByBitWS
params *Parameters
emitter *emission.Emitter
}
func (s *BybitWebSocket) SubscribeTrades(market Market, callback func(trades []*Trade)) error {
s.emitter.On(WSEventTrade, callback)
var arg = bws.WSTrade
if market.Symbol != "" {
arg += "." + market.Symbol
}
s.ws.Subscribe(arg)
return nil
}
func (s *BybitWebSocket) SubscribeLevel2Snapshots(market Market, callback func(ob *OrderBook)) error {
s.emitter.On(WSEventL2Snapshot, callback)
arg := bws.WSOrderBook25L1 + "." + market.Symbol
s.ws.Subscribe(arg)
return nil
}
func (s *BybitWebSocket) SubscribeOrders(market Market, callback func(orders []*Order)) error {
s.emitter.On(WSEventOrder, callback)
s.ws.Subscribe(bws.WSOrder)
return nil
}
func (s *BybitWebSocket) SubscribePositions(market Market, callback func(positions []*Position)) error {
s.emitter.On(WSEventPosition, callback)
s.ws.Subscribe(bws.WSPosition)
return nil
}
func (s *BybitWebSocket) handleOrderBook(symbol string, data bws.OrderBook) {
//log.Printf("handleOrderBook symbol: %v", symbol)
ob := &OrderBook{
Symbol: symbol,
}
for _, v := range data.Asks {
ob.Asks = append(ob.Asks, Item{
Price: v.Price,
Amount: v.Amount,
})
}
for _, v := range data.Bids {
ob.Bids = append(ob.Bids, Item{
Price: v.Price,
Amount: v.Amount,
})
}
ob.Time = data.Timestamp
s.emitter.Emit(WSEventL2Snapshot, ob)
}
func (s *BybitWebSocket) handleTrade(symbol string, data []*bws.Trade) {
var trades []*Trade
for _, v := range data {
var direction Direction
if v.Side == "Buy" {
direction = Buy
} else if v.Side == "Sell" {
direction = Sell
}
trades = append(trades, &Trade{
ID: v.TradeID,
Direction: direction,
Price: v.Price,
Amount: float64(v.Size),
Ts: v.Timestamp.UnixNano() / int64(time.Millisecond),
Symbol: v.Symbol,
})
}
s.emitter.Emit(WSEventTrade, trades)
}
func (s *BybitWebSocket) handlePosition(data []*bws.Position) {
var eventData []*Position
now := time.Now()
for _, v := range data {
var o Position
o.Symbol = v.Symbol
o.OpenTime = now
o.OpenPrice = v.EntryPrice
switch v.Side {
case "Buy":
o.Size = v.Size
case "Sell":
o.Size = -v.Size
}
o.AvgPrice = v.EntryPrice
eventData = append(eventData, &o)
}
s.emitter.Emit(WSEventPosition, eventData)
}
func (s *BybitWebSocket) handleOrder(data []*bws.Order) {
if s.params.DebugMode {
log.Printf("handleOrder data=%#v", data)
}
var orders []*Order
for _, v := range data {
var o Order
o.ID = fmt.Sprint(v.OrderID)
o.Symbol = v.Symbol
o.Price = v.Price
//o.AvgPrice = 0
// o.StopPx = 0
o.Amount = v.Qty
o.FilledAmount = v.CumExecQty
if v.Side == "Buy" {
o.Direction = Buy
} else if v.Side == "Sell" {
o.Direction = Sell
}
switch v.OrderType {
case "Limit":
o.Type = OrderTypeLimit
case "Market":
o.Type = OrderTypeMarket
}
if v.TimeInForce == "PostOnly" {
o.PostOnly = true
}
o.Status = s.orderStatus(v.OrderStatus)
orders = append(orders, &o)
}
s.emitter.Emit(WSEventOrder, orders)
}
func (s *BybitWebSocket) orderStatus(orderStatus string) OrderStatus {
switch orderStatus {
case "Created":
return OrderStatusCreated
case "NewBybit":
return OrderStatusNew
case "PartiallyFilled":
return OrderStatusPartiallyFilled
case "Filled":
return OrderStatusFilled
case "PendingCancel":
return OrderStatusCancelPending
case "Cancelled":
return OrderStatusCancelled
case "Rejected":
return OrderStatusRejected
case "Untriggered":
return OrderStatusUntriggered
case "Triggered":
return OrderStatusTriggered
default:
return OrderStatusCreated
}
}
func NewBybitWebSocket(params *Parameters) *BybitWebSocket {
wsURL := "wss://stream.bybit.com/realtime"
if params.Testnet {
wsURL = "wss://stream-testnet.bybit.com/realtime"
}
s := &BybitWebSocket{
params: params,
emitter: emission.NewEmitter(),
}
cfg := &bws.Configuration{
Addr: wsURL,
ApiKey: params.AccessKey,
SecretKey: params.SecretKey,
AutoReconnect: true,
DebugMode: params.DebugMode,
}
ws := bws.New(cfg)
s.ws = ws
ws.On(bws.WSOrderBook25L1, s.handleOrderBook)
ws.On(bws.WSTrade, s.handleTrade)
ws.On(bws.WSOrder, s.handleOrder)
ws.On(bws.WSPosition, s.handlePosition)
ws.Start()
return s
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。