Skip to content

waxdred/bybit_websocket_go

Repository files navigation

Bybit WebSocket Go Library

GitHub Workflow Status GoDoc

This is a Go package for connecting to Bybit's WebSocket API. It provides a simple way to connect to both public and private streams, and includes support for subscribing to and processing real-time market data, trading information, and account information.

Table of Contents

Installation

To use this package, you need to have Go installed on your system. You can then install the package by running:

go get github.com/waxdred/bybit_websocket_go

Usage

To use the package, first import it:

import Bwss "github.com/waxdred/bybit_websocket_go"

Connection

To create a new connection to the Bybit WebSocket API, use the New function of the WssBybit struct:

wss := new(Bwss.WssBybit).New(true)

You can pass a boolean parameter to enable or disable debugging messages.

To close all open connections, you can use the Close function:

defer wss.Close()

Public_streams

To subscribe to a public stream, use the AddConnPublic function of the WssBybit struct, which takes the URL of the stream as its parameter:

id, _ := wss.AddConnPublic(wss.WssUrl.Perpetual(Bwss.Mainnet))

The function returns an ID that can be used to close the connection later.

To subscribe to a channel on the stream, use the AddPublicSubs function, which takes a list of channel names and a handler function as its parameters:

wss.AddPublicSubs([]string{"orderBookL2_25.BTCUSD"}, func(wss *Bwss.WssBybit, sockk *Bwss.SocketMessage) {
    fmt.Println(string(sockk.Msg))
})

Private_streams

To subscribe to a private stream, use the AddConnPrivate function of the WssBybit struct, which takes the URL of the stream, your API key, and your API secret as its parameters:

wss.AddConnPrivate(wss.WssUrl.Private(Bwss.Testnet), apiKey, apiSecret)

To subscribe to a channel on the stream, use the AddPrivateSubs function, which takes a list of channel names and a handler function for each channel as its parameters:

wss.AddPrivateSubs([]string{"wallet", "position"}, func(wss *Bwss.WssBybit, sockk *Bwss.SocketMessage) {
    wallet := Bwss.Wallet{}
    sockk.Unmarshal(&wallet)
    log.Println(wallet.PrettyFormat())
}, func(wss *Bwss.WssBybit, sockk *Bwss.SocketMessage) {
    position := Bwss.Position{}
    sockk.Unmarshal(&position)
    log.Println(position.PrettyFormat())
})

Testnet_vs_Mainnet

Bybit offers two separate environments for testing and live trading: Testnet and Mainnet. The Testnet environment allows you to test your strategies and code without risking real funds, while the Mainnet environment is for actual trading.

In the code, you can select which environment to use by passing either Bwss.Testnet or Bwss.Mainnet as the parameter to the WssUrl functions:

wss.WssUrl.Perpetual(Bwss.Testnet) // for Testnet
wss.WssUrl.Perpetual(Bwss

Testnet and Mainnet are two separate environments provided by Bybit for testing and live trading, respectively. Testnet is a sandbox environment that allows you to test your code and strategies without risking real funds, while Mainnet is the production environment used for actual trading.

In the provided code, you can select which environment to use by passing either Bwss.Testnet or Bwss.Mainnet as a parameter to the WssUrl functions. For example, to connect to the Testnet perpetual contract endpoint, you can use:

wss.AddConnPublic(wss.WssUrl.Perpetual(Bwss.Testnet)).

And to connect to the Mainnet spot trading endpoint, you can use:

wss.AddConnPublic(wss.WssUrl.Spot(Bwss.Mainnet)).

Testnet and Mainnet are two separate environments provided by Bybit for testing and live trading, respectively. Testnet is a sandbox environment that allows you to test your code and strategies without risking real funds, while Mainnet is the production environment used for actual trading. It's important to note that different API keys are required for the Testnet and Mainnet environments. Be sure to generate separate API keys for each environment and use them accordingly.

Exemple

package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"time"

	// "log"
	Bwss "github.com/waxdred/bybit_websocket_go"
)

func main() {
	apiKey := "XXXXXXXXXXXXXX"
	apiSecret := "XXXXXXXXXXXXXXXX"
	wss := new(Bwss.WssBybit).New(true)
	// close all connection open
	defer wss.Close()

	wss.AddConnPrivate(wss.WssUrl.Private(Bwss.Testnet), apiKey, apiSecret).
		AddPrivateSubs([]string{"wallet", "position"},
			func(wss *Bwss.WssBybit, sockk *Bwss.SocketMessage) {
				// handler function
				wallet := Bwss.Wallet{}
				sockk.Unmarshal(&wallet)
				log.Println(wallet.PrettyFormat())
			},
			func(wss *Bwss.WssBybit, sockk *Bwss.SocketMessage) {
				fmt.Println("handle postion start")
				position := Bwss.Position{}
				sockk.Unmarshal(&position)
				log.Println(position.PrettyFormat())
			}).Listen()
            
	id, _ := wss.AddConnPublic(wss.WssUrl.Perpetual(Bwss.Mainnet)).
		AddPublicSubs([]string{"orderbook.1.BTCUSDT"},
			func(wss *Bwss.WssBybit, sockk *Bwss.SocketMessage) {
				fmt.Println(string(sockk.Msg))
			}).Listen()
	time.Sleep(time.Duration(time.Second * 10))
	wss.CloseConn(id)
	stop := make(chan os.Signal, 1)
	signal.Notify(stop, os.Interrupt)
	<-stop
}

For more examples and detailed usage instructions, please see the GoDoc documentation.

Contributing

If you find a bug or would like to contribute to this library, please open an issue or pull request on the GitHub repository.

License

This library is licensed under the MIT License. See the LICENSE file for details.