bybit

package module
v1.3.11 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 20 Imported by: 0

README

🚀 Bybit Go SDK: Your Ticket to the World of Lightning-Fast Trading

⚡️ A Golang Client for the V5 API, as Fast as a Rocket

Bybit Golang SDK

Go Version License WebSocket PRs Welcome

Turn your boldest trading ideas into reality with the most powerful Go SDK for the Bybit V5 API. Forget wrestling with documentation and focus on what truly matters—building profitable strategies.

📖 Full documentation available on Wiki


🎯 Why Do Traders and Developers Choose the Bybit Go SDK?

This SDK is more than just an API wrapper. It's your trusted partner in the world of algorithmic trading, built with a love for Golang and a deep understanding of traders' needs.


🤖 For Algorithmic Traders

No more battling with latency and inefficient code. Our SDK allows you to execute complex strategies with millisecond precision. Get real-time market data via WebSocket, use advanced order types, and manage risk with automatic controls. The built-in fee calculator ensures crystal-clear tracking of your profits.

👨‍💻 For Developers

We've taken care of your comfort. Type-safe operations eliminate runtime errors, and Testnet support lets you experiment without risk. The SDK has zero unnecessary dependencies, is easily configurable, and comes with comprehensive documentation and examples. It's the tool you'll love to use.

🏢 For Production Systems

Reliability is our middle name. Thread-safe operations, smart reconnection with exponential backoff, and detailed error messages make this SDK the perfect choice for 24/7 systems. Built-in rate limit handling and multi-region support guarantee your application's stability under any conditions.


✨ Key Features

We've packed everything you need for successful trading into one powerful package.

Category Features
📊 Market Data Get real-time tickers, order books, K-lines, and trade history.
💰 Trading Spot, derivatives, limit, and market orders, TP/SL—all at your fingertips.
🔐 Security HMAC-SHA256 and RSA-SHA256 support to protect your data.
🌐 WebSocket Real-time data streaming with automatic reconnection.
⚙️ Management Control positions, leverage, risk, and your wallet.
🌍 Global Access Multi-region and testnet support for low-latency trading.

📦 Installation & Configuration

Getting started is a breeze. You'll be up and running in seconds.

Step 1: Install the package using Go.

go get github.com/tigusigalpa/bybit-go

Step 2: Import it into your project.

import bybit "github.com/tigusigalpa/bybit-go"

Step 3: Configure the client using environment variables or programmatic setup.

Configuration Example:

client, err := bybit.NewClient(
    bybit.ClientConfig{
        APIKey:     os.Getenv("BYBIT_API_KEY"),
        APISecret:  os.Getenv("BYBIT_API_SECRET"),
        Testnet:    true, // Set to true for safe testing
        Region:     "global",
    },
)

🚀 Quick Start: From Zero to Your First Order

Let's take your first step into the world of automated trading together.

package main

import (
    "fmt"
    "log"
    bybit "github.com/tigusigalpa/bybit-go"
)

func main() {
    // Step 1: Initialize the client
    client, err := bybit.NewClient(bybit.ClientConfig{
        APIKey:     "your_api_key",
        APISecret:  "your_api_secret",
        Testnet:    true, // A safe environment for testing
    })
    if err != nil {
        log.Fatal(err)
    }

    // Step 2: Verify the connection to the server
    serverTime, err := client.GetServerTime()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("✅ Connected! Server Time: %v\n", serverTime)

    // Step 3: Get real-time market data
    tickers, err := client.GetTickers(map[string]interface{}{
        "category": "linear",
        "symbol":   "BTCUSDT",
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("📊 BTC Price: %v\n", tickers)

    // Step 4: Place your first order
    order, err := client.CreateOrder(map[string]interface{}{
        "category":    "linear",
        "symbol":      "BTCUSDT",
        "side":        "Buy",
        "orderType":   "Limit",
        "qty":         "0.01",
        "price":       "30000",
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("🎉 Order placed successfully! ID: %v\n", order["result"].(map[string]interface{})["orderId"])
}

Congratulations! You've just made your first trade via the API!


📚 Examples & Documentation


🤝 Make the Project Better

We always welcome community contributions! If you have ideas on how to improve the SDK or have found a bug, please create an issue or submit a pull request. Let's build the best tool for traders together!


📄 License

The project is distributed under the MIT License, giving you complete freedom to use and modify it.


⚠️ Disclaimer: Trading cryptocurrencies involves high risk. This SDK is provided "as is" without any warranty. Always test your strategies on the Testnet and never invest more than you can afford to lose.

Documentation

Index

Constants

View Source
const (
	DemoBaseURL      = "https://api-demo.bybit.com"
	DemoWebSocketURL = "wss://stream-demo.bybit.com"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(config ClientConfig) (*Client, error)

func (*Client) AddOrReduceMargin added in v1.1.16

func (c *Client) AddOrReduceMargin(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) AmendOrder

func (c *Client) AmendOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) BaseURI

func (c *Client) BaseURI() string

func (*Client) CancelAllOrders

func (c *Client) CancelAllOrders(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) CancelOrder

func (c *Client) CancelOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) ComputeFee

func (c *Client) ComputeFee(tradeType string, volume float64, level, liquidity string) float64

func (*Client) ConfirmNewRiskLimit added in v1.1.16

func (c *Client) ConfirmNewRiskLimit(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) CreateOrder

func (c *Client) CreateOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) Endpoint

func (c *Client) Endpoint() string

func (*Client) GetAccountInfo added in v1.1.16

func (c *Client) GetAccountInfo() (map[string]interface{}, error)

func (*Client) GetAccountInstrumentsInfo added in v1.1.16

func (c *Client) GetAccountInstrumentsInfo(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetClosedOptionsPositions added in v1.1.16

func (c *Client) GetClosedOptionsPositions(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetClosedPnL added in v1.1.16

func (c *Client) GetClosedPnL(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetFundingRateHistory added in v1.1.16

func (c *Client) GetFundingRateHistory(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetHistoricalVolatility added in v1.1.16

func (c *Client) GetHistoricalVolatility(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetHistoryOrders

func (c *Client) GetHistoryOrders(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetInsurance added in v1.1.16

func (c *Client) GetInsurance(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetKline added in v1.1.16

func (c *Client) GetKline(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetMovePositionHistory added in v1.1.16

func (c *Client) GetMovePositionHistory(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetOpenInterest added in v1.1.16

func (c *Client) GetOpenInterest(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetOpenOrders

func (c *Client) GetOpenOrders(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetOrderbook added in v1.1.16

func (c *Client) GetOrderbook(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetPositions

func (c *Client) GetPositions(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetRPIOrderbook added in v1.1.16

func (c *Client) GetRPIOrderbook(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetRecentTrades added in v1.1.16

func (c *Client) GetRecentTrades(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetRiskLimit added in v1.1.16

func (c *Client) GetRiskLimit(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetServerTime

func (c *Client) GetServerTime() (map[string]interface{}, error)

func (*Client) GetTickers

func (c *Client) GetTickers(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetTransactionLog added in v1.1.16

func (c *Client) GetTransactionLog(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetTransferableAmount added in v1.1.16

func (c *Client) GetTransferableAmount(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) GetWalletBalance

func (c *Client) GetWalletBalance(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) MovePosition added in v1.1.16

func (c *Client) MovePosition(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) PlaceOrder

func (c *Client) PlaceOrder(params PlaceOrderParams) (map[string]interface{}, error)

func (*Client) Request

func (c *Client) Request(method, path string, params map[string]interface{}) (map[string]interface{}, error)

func (*Client) SetAutoAddMargin added in v1.1.16

func (c *Client) SetAutoAddMargin(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) SetLeverage

func (c *Client) SetLeverage(category, symbol string, leverage float64, side *string) (map[string]interface{}, error)

func (*Client) SetTradingStop

func (c *Client) SetTradingStop(params map[string]interface{}) (map[string]interface{}, error)

func (*Client) SwitchPositionMode

func (c *Client) SwitchPositionMode(params map[string]interface{}) (map[string]interface{}, error)

type ClientConfig

type ClientConfig struct {
	APIKey        string
	APISecret     string
	Testnet       bool
	Region        string
	RecvWindow    int
	Signature     string
	RSAPrivateKey string
	HTTPClient    *http.Client
}

type DemoClient added in v1.1.16

type DemoClient struct {
	*Client
}

func NewDemoClient added in v1.1.16

func NewDemoClient(config ClientConfig) (*DemoClient, error)

func (*DemoClient) AddOrReduceMargin added in v1.2.23

func (dc *DemoClient) AddOrReduceMargin(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) AmendOrder added in v1.2.23

func (dc *DemoClient) AmendOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) ApplyForDemoFunds added in v1.1.16

func (dc *DemoClient) ApplyForDemoFunds(adjustType int, funds []DemoFundRequest) (map[string]interface{}, error)

func (*DemoClient) ApplyForDemoFundsSimple added in v1.2.23

func (dc *DemoClient) ApplyForDemoFundsSimple(coin string, amount string) (map[string]interface{}, error)

func (*DemoClient) BaseURI added in v1.1.16

func (dc *DemoClient) BaseURI() string

func (*DemoClient) BatchAmendOrder added in v1.2.23

func (dc *DemoClient) BatchAmendOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) BatchCancelOrder added in v1.2.23

func (dc *DemoClient) BatchCancelOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) BatchPlaceOrder added in v1.2.23

func (dc *DemoClient) BatchPlaceOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) CancelAllOrders added in v1.2.23

func (dc *DemoClient) CancelAllOrders(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) CancelOrder added in v1.2.23

func (dc *DemoClient) CancelOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) CreateDemoAPIKey added in v1.2.23

func (dc *DemoClient) CreateDemoAPIKey(mainnetClient *Client, demoUID string, params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) CreateDemoAccount added in v1.2.23

func (dc *DemoClient) CreateDemoAccount(mainnetClient *Client) (map[string]interface{}, error)

func (*DemoClient) CreateOrder added in v1.2.23

func (dc *DemoClient) CreateOrder(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) DeleteDemoAPIKey added in v1.2.23

func (dc *DemoClient) DeleteDemoAPIKey(mainnetClient *Client, params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetAPIKeyInfo added in v1.2.23

func (dc *DemoClient) GetAPIKeyInfo() (map[string]interface{}, error)

func (*DemoClient) GetAccountInfo added in v1.2.23

func (dc *DemoClient) GetAccountInfo() (map[string]interface{}, error)

func (*DemoClient) GetBorrowHistory added in v1.2.23

func (dc *DemoClient) GetBorrowHistory(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetClosedPnL added in v1.2.23

func (dc *DemoClient) GetClosedPnL(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetCoinGreeks added in v1.2.23

func (dc *DemoClient) GetCoinGreeks(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetCollateralInfo added in v1.2.23

func (dc *DemoClient) GetCollateralInfo(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetDeliveryRecord added in v1.2.23

func (dc *DemoClient) GetDeliveryRecord(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetOpenOrders added in v1.2.23

func (dc *DemoClient) GetOpenOrders(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetOrderHistory added in v1.2.23

func (dc *DemoClient) GetOrderHistory(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetPositions added in v1.2.23

func (dc *DemoClient) GetPositions(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetSpotMarginStatus added in v1.2.23

func (dc *DemoClient) GetSpotMarginStatus() (map[string]interface{}, error)

func (*DemoClient) GetTradeHistory added in v1.2.23

func (dc *DemoClient) GetTradeHistory(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetTransactionLog added in v1.2.23

func (dc *DemoClient) GetTransactionLog(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetUSDCSettlement added in v1.2.23

func (dc *DemoClient) GetUSDCSettlement(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) GetWalletBalance added in v1.2.23

func (dc *DemoClient) GetWalletBalance(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) Request added in v1.1.16

func (dc *DemoClient) Request(method, path string, params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetAutoAddMargin added in v1.2.23

func (dc *DemoClient) SetAutoAddMargin(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetCollateralCoin added in v1.2.23

func (dc *DemoClient) SetCollateralCoin(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetLeverage added in v1.2.23

func (dc *DemoClient) SetLeverage(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetMarginMode added in v1.2.23

func (dc *DemoClient) SetMarginMode(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetSpotHedging added in v1.2.23

func (dc *DemoClient) SetSpotHedging(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetSpotMarginLeverage added in v1.2.23

func (dc *DemoClient) SetSpotMarginLeverage(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SetTradingStop added in v1.2.23

func (dc *DemoClient) SetTradingStop(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) SwitchPositionMode added in v1.2.23

func (dc *DemoClient) SwitchPositionMode(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) ToggleMarginTrade added in v1.2.23

func (dc *DemoClient) ToggleMarginTrade(params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) UpdateDemoAPIKey added in v1.2.23

func (dc *DemoClient) UpdateDemoAPIKey(mainnetClient *Client, params map[string]interface{}) (map[string]interface{}, error)

func (*DemoClient) WebSocketURL added in v1.2.23

func (dc *DemoClient) WebSocketURL() string

type DemoFundRequest added in v1.2.23

type DemoFundRequest struct {
	Coin      string `json:"coin"`
	AmountStr string `json:"amountStr"`
}

type PlaceOrderParams

type PlaceOrderParams struct {
	Type      string
	Symbol    string
	Execution string
	Price     *float64
	Side      *string
	Leverage  *float64
	Size      float64
	SlTp      *SlTpParams
	Extra     map[string]interface{}
}

type SlTpParams

type SlTpParams struct {
	Type       string
	TakeProfit *float64
	StopLoss   *float64
}

type WebSocket

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

func NewWebSocket

func NewWebSocket(config WebSocketConfig) *WebSocket

func (*WebSocket) Close

func (ws *WebSocket) Close() error

func (*WebSocket) Connect

func (ws *WebSocket) Connect() error

func (*WebSocket) GetSubscriptions

func (ws *WebSocket) GetSubscriptions() []string

func (*WebSocket) IsConnected

func (ws *WebSocket) IsConnected() bool

func (*WebSocket) Listen

func (ws *WebSocket) Listen() error

func (*WebSocket) OnMessage

func (ws *WebSocket) OnMessage(callback func(map[string]interface{}))

func (*WebSocket) Ping

func (ws *WebSocket) Ping() error

func (*WebSocket) Send

func (ws *WebSocket) Send(message map[string]interface{}) error

func (*WebSocket) Subscribe

func (ws *WebSocket) Subscribe(topics []string) error

func (*WebSocket) SubscribeExecution

func (ws *WebSocket) SubscribeExecution() error

func (*WebSocket) SubscribeKline

func (ws *WebSocket) SubscribeKline(symbol, interval string) error

func (*WebSocket) SubscribeOrder

func (ws *WebSocket) SubscribeOrder() error

func (*WebSocket) SubscribeOrderbook

func (ws *WebSocket) SubscribeOrderbook(symbol string, depth int) error

func (*WebSocket) SubscribePosition

func (ws *WebSocket) SubscribePosition() error

func (*WebSocket) SubscribeTicker

func (ws *WebSocket) SubscribeTicker(symbol string) error

func (*WebSocket) SubscribeTrade

func (ws *WebSocket) SubscribeTrade(symbol string) error

func (*WebSocket) SubscribeWallet

func (ws *WebSocket) SubscribeWallet() error

func (*WebSocket) Unsubscribe

func (ws *WebSocket) Unsubscribe(topics []string) error

type WebSocketConfig

type WebSocketConfig struct {
	APIKey    string
	APISecret string
	Testnet   bool
	Region    string
	IsPrivate bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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