Skip to main content

A Python client for the Bitget API with comprehensive position management

Project description

🚀 Bitget API V2 Python Client

A comprehensive Python client for the Bitget API V2, providing extensive functionality for futures trading, account management, and market data access.

✨ Features

  • 📊 Complete futures market trading capabilities
  • 💼 Account management and settings
  • 📈 Real-time and historical market data
  • 🔄 Automatic rate limiting and request handling
  • 🛡️ Comprehensive error handling and validation
  • 📝 Detailed debug logging capabilities
  • 🎯 Type hints and dataclass models for better code completion

🛠️ Installation

# Install using PYPI
pip install bitget-python

🔧 Quick Start

from bitpy import BitgetAPI

# For market data only - no API keys needed
client = BitgetAPI(
    api_key=None,
    secret_key=None,
    api_passphrase=None
)

# Get market data without authentication
ticker = client.market.get_ticker(
    symbol="BTCUSDT",
    product_type="USDT-FUTURES"
)

# Get candlestick data
candles = client.market.get_candlestick(
    symbol="BTCUSDT",
    product_type="USDT-FUTURES",
    granularity="1m",
    limit=100
)

For account and position operations, API keys are required:

# For trading operations - API keys required
client = BitgetAPI(
    api_key="your_api_key",
    secret_key="your_secret_key",
    api_passphrase="your_passphrase",
    debug=True
)

# Get account information (requires authentication)
account = client.account.get_account(
    symbol="BTCUSDT",
    product_type="USDT-FUTURES",
    margin_coin="USDT"
)

🌐 WebSocket Support (Only Public)

from bitpy import BitgetWebsocketAPI
import asyncio

async def handle_ticker(data: dict):
    if "data" in data and len(data["data"]) > 0:
        ticker = data["data"][0]
        print(f"Symbol: {ticker['instId']}")
        print(f"Last Price: {ticker['lastPr']}")
        print(f"24h High: {ticker['high24h']}")
        print(f"24h Low: {ticker['low24h']}")
        print(f"24h Change %: {ticker['change24h']}")
        print("-" * 50)
        
async def main():
    # Initialize WebSocket client
    api = BitgetWebsocketAPI(is_private=False, debug=False)
    ws_client = api.websocket
    # Subscribe to channels
    subscriptions = [
        {
            "instType": "SPOT",
            "channel": "ticker",
            "instId": "BTCUSDT"
        }
    ]
    try:
        await ws_client.connect()
        print("Connected to WebSocket")
        await ws_client.subscribe(subscriptions, handle_ticker)
        # Keep connection alive
        while ws_client.connected:
            await asyncio.sleep(1)
            
    except KeyboardInterrupt:
        await ws_client.close()
        
if __name__ == "__main__":
    asyncio.run(main())

🔑 Core Components

Account Management

  • Account information and settings
  • Leverage and margin configuration
  • Position mode management
  • Asset mode settings
  • Interest and bill history

Position Management

  • Position tracking and history
  • Position tier information
  • Multiple position modes support

Market Data

  • Real-time tickers and depth
  • Candlestick data with multiple timeframes
  • Funding rates and open interest
  • Historical transaction data
  • Contract specifications

💹 Supported Markets

Market Type Description
USDT-FUTURES USDT margined futures
COIN-FUTURES Coin margined futures
USDC-FUTURES USDC margined futures
SUSDT-FUTURES Simulated USDT futures
SCOIN-FUTURES Simulated coin futures
SUSDC-FUTURES Simulated USDC futures

⚠️ Error Handling

from bitpy.exceptions import InvalidProductTypeError, BitgetAPIError

try:
    positions = client.position.get_all_positions("INVALID-TYPE")
except InvalidProductTypeError as e:
    print(f"Invalid product type: {e}")
except BitgetAPIError as e:
    print(f"API Error {e.code}: {e.message}")

🔄 Rate Limiting

The client implements a smart token bucket algorithm for rate limiting, automatically tracking and managing request limits per endpoint to ensure optimal API usage.

📊 Advanced Market Data

# Get candlestick data
candles = client.market.get_candlestick(
    symbol="BTCUSDT",
    product_type="USDT-FUTURES",
    granularity="1m",
    limit=100
)

# Get market depth
depth = client.market.get_merge_depth(
    symbol="BTCUSDT",
    product_type="USDT-FUTURES",
    precision="0.1"
)

🤝 Contributing

Contributions are welcome! Feel free to submit a Pull Request. For feature requests or bug reports, please open an issue.

📄 License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitget_python-1.0.4.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bitget_python-1.0.4-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file bitget_python-1.0.4.tar.gz.

File metadata

  • Download URL: bitget_python-1.0.4.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for bitget_python-1.0.4.tar.gz
Algorithm Hash digest
SHA256 7e39d1e3ec64cee9b6aeeb3db8466084cb3ce86abbc92c03e7c2f4df5c47ef54
MD5 263214f6767be7a1112723d9be701ef8
BLAKE2b-256 283b2d93f9d68b07dfae09fd3f28f9ecc691b86779b7da7769eaa83e295960e4

See more details on using hashes here.

File details

Details for the file bitget_python-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: bitget_python-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for bitget_python-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 74117e7cefb804fee7ef3143902b31884437c4219da861d509fea6346a69c2cd
MD5 489f76d511db58def6809763b477f72d
BLAKE2b-256 f38f3742eb096006efd35c3435b51d0dde44f92422cbf03f3c2a6e74fa8ca035

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page