Skip to main content

Weex SDK for Python

PyPI - Version PyPI - License PyPI - Status

A comprehensive Python SDK for Weex exchange API, supporting both HTTP REST API and WebSocket connections with automatic reconnection and heartbeat mechanisms.

Features

  • Complete API Coverage: Full support for Account, Market, Trade, and AI log upload APIs
  • Sync & Async Support: Both synchronous and asynchronous clients
  • WebSocket Support: Real-time market data and account updates with auto-reconnect
  • Robust Error Handling: Comprehensive exception handling with detailed error messages
  • Type Safety: Full type hints with strict typing (no Any types)
  • Logging: Built-in logging with configurable levels
  • Auto Reconnection: WebSocket automatic reconnection with exponential backoff
  • Heartbeat: Automatic ping/pong handling for WebSocket connections

Installation

pip install weex-sdk

Or install from source:

git clone https://github.com/discountry/weex-sdk-python.git
cd weex-sdk-python
pip install -e .

Quick Start

Synchronous Usage

from weex_sdk import WeexClient

# Initialize client
client = WeexClient(
    api_key="your_api_key",
    secret_key="your_secret_key",
    passphrase="your_passphrase"
)

# Get account information
account = client.account.get_accounts()
print(account)

# Get market ticker
ticker = client.market.get_ticker("cmt_btcusdt")
print(f"Current price: {ticker['last']}")

# Place an order
order = client.trade.place_order(
    symbol="cmt_btcusdt",
    client_oid="unique_order_id_123",
    size="0.01",
    order_type="0",  # Normal order
    match_price="0",  # Limit price
    price="50000",
    type="1"  # Open long
)
print(f"Order placed: {order['order_id']}")

# Upload AI log
client.ai.upload_ai_log(
    stage="Decision Making",
    model="GPT-5-mini",
    input_data={"prompt": "Analyze BTC trend"},
    output={"signal": "Buy", "confidence": 0.82},
    explanation="AI analyzed market data and generated buy signal",
    order_id=int(order['order_id'])
)

Asynchronous Usage

import asyncio
from weex_sdk import AsyncWeexClient

async def main():
    async with AsyncWeexClient(
        api_key="your_api_key",
        secret_key="your_secret_key",
        passphrase="your_passphrase"
    ) as client:
        # Get account information
        account = await client.account.get_accounts()
        print(account)

        # Get market ticker
        ticker = await client.market.get_ticker("cmt_btcusdt")
        print(f"Current price: {ticker['last']}")

        # Place an order
        order = await client.trade.place_order(
            symbol="cmt_btcusdt",
            client_oid="unique_order_id_123",
            size="0.01",
            order_type="0",
            match_price="0",
            price="50000",
            type="1"
        )
        print(f"Order placed: {order['order_id']}")

asyncio.run(main())

WebSocket Usage

Public Channels (Market Data)

from weex_sdk import WeexWebSocket
import json

def on_ticker(data):
    """Handle ticker updates."""
    print(f"Ticker update: {json.dumps(data, indent=2)}")

# Connect to public WebSocket
ws = WeexWebSocket(is_private=False)

# Subscribe to ticker
ws.subscribe_ticker("cmt_btcusdt", callback=on_ticker)

# Connect (runs in background thread)
ws.connect()

# Keep running
import time
time.sleep(60)

# Close connection
ws.close()

Private Channels (Account Updates)

from weex_sdk import WeexWebSocket

def on_account_update(data):
    """Handle account updates."""
    print(f"Account update: {data}")

def on_order_update(data):
    """Handle order updates."""
    print(f"Order update: {data}")

# Connect to private WebSocket
ws = WeexWebSocket(
    api_key="your_api_key",
    secret_key="your_secret_key",
    passphrase="your_passphrase",
    is_private=True
)

# Subscribe to account and order updates
ws.subscribe_account(callback=on_account_update)
ws.subscribe_order(callback=on_order_update)

# Connect
ws.connect()

# Keep running
import time
time.sleep(60)

ws.close()

Async WebSocket

import asyncio
from weex_sdk import AsyncWeexWebSocket

async def on_ticker(data):
    """Handle ticker updates."""
    print(f"Ticker: {data}")

async def main():
    ws = AsyncWeexWebSocket(is_private=False)
    await ws.connect()
    await ws.subscribe_ticker("cmt_btcusdt", callback=on_ticker)
    
    # Keep running
    await asyncio.sleep(60)
    await ws.close()

asyncio.run(main())

API Reference

Account API

# Get all accounts
accounts = client.account.get_accounts()

# Get account for specific coin
account = client.account.get_account("USDT")

# Get assets
assets = client.account.get_assets()

# Get bills
bills = client.account.get_bills(
    coin="USDT",
    symbol="cmt_btcusdt",
    limit=10
)

# Get settings
settings = client.account.get_settings(symbol="cmt_btcusdt")

# Set leverage
client.account.set_leverage(
    symbol="cmt_btcusdt",
    margin_mode=1,  # 1: Cross, 3: Isolated
    long_leverage="10",
    short_leverage="10"
)

# Get positions
positions = client.account.get_all_positions()
position = client.account.get_single_position("cmt_btcusdt")

Market API

# Get server time
time_info = client.market.get_server_time()

# Get contracts
contracts = client.market.get_contracts(symbol="cmt_btcusdt")

# Get market depth
depth = client.market.get_depth("cmt_btcusdt", limit=15)

# Get tickers
tickers = client.market.get_tickers()
ticker = client.market.get_ticker("cmt_btcusdt")

# Get trades
trades = client.market.get_trades("cmt_btcusdt", limit=100)

# Get K-line data
candles = client.market.get_candles(
    symbol="cmt_btcusdt",
    granularity="1m",
    limit=100
)

# Get funding rate
fund_rate = client.market.get_current_fund_rate("cmt_btcusdt")

Trade API

# Place order
order = client.trade.place_order(
    symbol="cmt_btcusdt",
    client_oid="unique_id",
    size="0.01",
    order_type="0",  # 0: Normal, 1: Post-Only, 2: FOK, 3: IOC
    match_price="0",  # 0: Limit, 1: Market
    price="50000",
    type="1"  # 1: Open long, 2: Open short, 3: Close long, 4: Close short
)

# Cancel order
result = client.trade.cancel_order(order_id=order['order_id'])

# Get order details
order_detail = client.trade.get_order_detail(order['order_id'])

# Get current orders
current_orders = client.trade.get_current_orders(symbol="cmt_btcusdt")

# Get order fills
fills = client.trade.get_order_fills(order_id=order['order_id'])

# Close all positions
close_result = client.trade.close_positions(symbol="cmt_btcusdt")

# Cancel all orders
cancel_result = client.trade.cancel_all_orders(
    cancel_order_type="normal",
    symbol="cmt_btcusdt"
)

AI API

# Upload AI log
result = client.ai.upload_ai_log(
    stage="Decision Making",
    model="GPT-5-mini",
    input_data={
        "prompt": "Analyze BTC/USDT price trend for the next 3 hours",
        "data": {
            "RSI_14": 36.8,
            "EMA_20": 68950.4,
            "FundingRate": -0.0021,
            "OpenInterest": 512.3
        }
    },
    output={
        "signal": "Buy",
        "confidence": 0.82,
        "target_price": 69300,
        "reason": "Negative funding + rising open interest implies short squeeze potential."
    },
    explanation="Low RSI and price near the EMA20 suggest weakening downside momentum. Negative funding with rising open interest points to short-side pressure and potential squeeze risk, indicating a bullish bias for BTC over the next three hours.",
    order_id=123456789
)

Error Handling

The SDK provides comprehensive error handling with custom exceptions:

from weex_sdk import (
    WeexAPIException,
    WeexAuthenticationError,
    WeexRateLimitError,
    WeexNetworkError,
    WeexWebSocketError,
    WeexValidationError,
)

try:
    order = client.trade.place_order(...)
except WeexAuthenticationError as e:
    print(f"Authentication failed: {e}")
except WeexRateLimitError as e:
    print(f"Rate limit exceeded. Retry after {e.retry_after} seconds")
except WeexValidationError as e:
    print(f"Invalid parameters: {e}")
except WeexAPIException as e:
    print(f"API error: {e}")

Logging

Configure logging level:

from weex_sdk.logger import setup_logger
import logging

# Set logging level
setup_logger(level=logging.DEBUG)

WebSocket Features

Auto Reconnection

WebSocket connections automatically reconnect on disconnect with exponential backoff:

ws = WeexWebSocket(
    is_private=False,
    reconnect_attempts=10,  # Max reconnection attempts
    reconnect_delay=1,  # Initial delay (seconds)
    max_reconnect_delay=60  # Max delay (seconds)
)

Heartbeat

The SDK automatically handles ping/pong messages to keep connections alive.

Subscription Management

Subscriptions are automatically restored after reconnection:

ws.subscribe_ticker("cmt_btcusdt", callback=on_ticker)
ws.subscribe_depth("cmt_btcusdt", limit=15, callback=on_depth)

# After reconnection, all subscriptions are automatically restored

Requirements

  • Python >= 3.8
  • requests >= 2.31.0
  • aiohttp >= 3.9.0
  • websocket-client >= 1.6.0
  • websockets >= 12.0

License

MIT License

Support

For issues and questions:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Metadata

Release files for weex-sdk 1.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weex-sdk 1.0.10
File Size Uploaded
weex_sdk-1.0.10.tar.gz 30.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for weex-sdk 1.0.10
File Interpreter ABI Platform
weex_sdk-1.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 61.3 kB

Release files / weex_sdk-1.0.10.tar.gz

Download URL weex_sdk-1.0.10.tar.gz
Size 30.2 kB
Tags Source
SHA-256 checksum
How to use checksums
76f99b14dd712d48d7643792b42a7012b955e582cb43975ca76b9b14503d8952
BLAKE2b-256 checksum
How to use checksums
d5c1883d075b7fe5d0c5562cdd44e2f53109943f8b03b13e4775ad1b74539ccd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / weex_sdk-1.0.10-py3-none-any.whl

Download URL weex_sdk-1.0.10-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0fde1f0258efcf538700dd66550a891c44f99631d4f5f4ffd2048a491821b4e
BLAKE2b-256 checksum
How to use checksums
47d8f9e02c83c138071ef3681d89f962ed6f5c2b02f73dacf88d5265d5a4e47f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

1.0.10 This release

2 release files

1.0.9

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page