High-performance async trading API client for Python supporting BingX and Bybit exchanges with intelligent session and cache management
Project description
aiotrade
High-performance async trading API client for Python supporting BingX, Bybit, OKX, and Bitget exchanges with intelligent session and cache management.
Architecture
The library uses a sophisticated architecture for optimal performance:
Session Management
- Shared Session:
SharedSessionManagercreates a single aiohttp session with high-performance connection pooling - Individual Sessions: Clients automatically create individual sessions if shared session isn't initialized
- Connection Pooling: Up to 2000 concurrent connections with smart distribution per host
Client Caching
- TTL Cache:
BingxClientsCache,BybitClientsCache,OkxClientsCache, andBitgetClientsCachecache client instances with 10-minute lifetime - Lock-Free: No blocking operations for maximum performance
- Lazy Cleanup: Expired entries removed on access, not proactively
Implemented methods
BybitClient methods (43):
batch_cancel_order get_server_time
batch_place_order get_smp_group_id
batch_set_collateral_coin get_trade_behaviour_setting
cancel_all_orders get_transaction_log
cancel_order get_transferable_amount
decode_str get_wallet_balance
get_account_info manual_borrow
get_account_instruments_info manual_repay
get_api_key_info manual_repay_without_asset_conversion
get_borrow_history place_order
get_closed_pnl repay_liability
get_coin_greeks reset_mmp
get_collateral_info set_collateral_coin
get_dcp_info set_leverage
get_fee_rate set_limit_price_behaviour
get_instruments_info set_margin_mode
get_kline set_mmp
get_mmp_state set_spot_hedging
get_open_and_closed_orders set_trading_stop
get_order_history switch_position_mode
get_position_info upgrade_to_unified_account_pro
get_risk_limit
BingxClient methods (48):
cancel_all_spot_open_orders get_spot_profit_details
cancel_all_swap_open_orders get_spot_profit_overview
cancel_spot_batch_orders get_spot_symbols
cancel_swap_batch_orders get_spot_trade_details
change_swap_margin_type get_swap_account_balance
close_perpetual_trader_position_by_order get_swap_contracts
close_swap_position get_swap_full_orders
decode_str get_swap_klines
get_account_asset_overview get_swap_leverage_and_available_positions
get_account_uid get_swap_margin_type
get_api_permissions get_swap_open_orders
get_perpetual_copy_trading_pairs get_swap_order_details
get_perpetual_current_trader_order get_swap_order_history
get_perpetual_personal_trading_overview get_swap_position_history
get_perpetual_profit_details get_swap_position_mode
get_perpetual_profit_overview get_swap_positions
get_server_time place_spot_order
get_spot_account_assets place_swap_batch_orders
get_spot_history_orders place_swap_order
get_spot_klines sell_spot_asset_by_order
get_spot_open_orders set_perpetual_commission_rate
get_spot_order_details set_perpetual_trader_tpsl_by_order
get_spot_order_history set_swap_leverage
get_spot_personal_trading_overview set_swap_position_mode
OkxClient methods (18):
batch_place_order get_order
cancel_batch_orders get_orders_history
close_position get_orders_pending
decode_str get_position_tiers
get_account_config get_positions
get_balance get_positions_history
get_funding_balance set_isolated_mode
get_instruments set_leverage
get_leverage_info set_position_mode
BitgetClient methods (34):
batch_cancel_futures_orders get_isolated_symbols
batch_cancel_spot_orders get_order_detail
batch_place_futures_orders get_pending_orders
batch_place_spot_orders get_pending_trigger_orders
cancel_all_futures_orders get_server_time
cancel_order get_single_account
cancel_order_by_symbol get_spot_history_orders
cancel_trigger_orders get_symbol_info
decode_str get_trade_rate
flash_close_position get_unfilled_orders
get_account_assets place_futures_order
get_account_info place_spot_order
get_account_list place_tpsl_plan_order
get_all_positions place_trigger_order
get_contract_config set_leverage
get_futures_history_orders set_margin_mode
get_historical_position set_position_mode
Installation
poetry add aiotrade-sdk
Quick Start
Option 1: Shared Session (Recommended for Production)
from aiotrade import SharedSessionManager, BingxClient, BybitClient, OkxClient, BitgetClient
# Initialize shared session at startup (once per application)
SharedSessionManager.setup(max_connections=2000)
# Create clients for different exchanges - they automatically use the shared session
bingx_client = BingxClient(api_key="bingx_key", api_secret="bingx_secret", demo=True)
bybit_client = BybitClient(api_key="bybit_key", api_secret="bybit_secret", testnet=True)
okx_client = OkxClient(api_key="okx_key", api_secret="okx_secret", passphrase="okx_passphrase")
bitget_client = BitgetClient(api_key="bitget_key", api_secret="bitget_secret", passphrase="bitget_passphrase")
try:
# Use clients for API calls
bingx_assets = await bingx_client.get_spot_account_assets()
bybit_tickers = await bybit_client.get_tickers(category="spot")
okx_balance = await okx_client.get_balance()
bitget_assets = await bitget_client.get_account_assets()
finally:
# Close shared session at shutdown
await SharedSessionManager.close()
Option 2: Individual Sessions
from aiotrade import BingxClient, BybitClient, OkxClient, BitgetClient
# BingX client with individual session
async with BingxClient(api_key="your_key", api_secret="your_secret", demo=True) as client:
assets = await client.get_spot_account_assets()
print(f"BingX assets: {assets}")
# Bybit client with individual session
async with BybitClient(api_key="your_key", api_secret="your_secret", testnet=True) as client:
tickers = await client.get_tickers(category="spot")
print(f"Bybit tickers: {tickers}")
# OKX client with individual session
async with OkxClient(api_key="your_key", api_secret="your_secret", passphrase="your_passphrase") as client:
balance = await client.get_balance()
print(f"OKX balance: {balance}")
# Bitget client with individual session
async with BitgetClient(api_key="your_key", api_secret="your_secret", passphrase="your_passphrase") as client:
assets = await client.get_account_assets()
print(f"Bitget assets: {assets}")
Option 3: Cached Clients
from aiotrade import BingxClientsCache, BybitClientsCache, OkxClientsCache, BitgetClientsCache
# Get cached BingX client (creates new if doesn't exist)
bingx_client = BingxClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
demo=True
)
# Get cached Bybit client
bybit_client = BybitClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
testnet=True
)
# Get cached OKX client
okx_client = OkxClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
passphrase="your_passphrase"
)
# Get cached Bitget client
bitget_client = BitgetClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
passphrase="your_passphrase"
)
# Use clients (session management is automatic)
async with bingx_client:
assets = await bingx_client.get_spot_account_assets()
async with bybit_client:
tickers = await bybit_client.get_tickers(category="spot")
async with okx_client:
balance = await okx_client.get_balance()
async with bitget_client:
assets = await bitget_client.get_account_assets()
# Same parameters return the same cached instance
cached_okx = OkxClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
passphrase="your_passphrase"
)
assert okx_client is cached_okx # True
cached_bitget = BitgetClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
passphrase="your_passphrase"
)
assert bitget_client is cached_bitget # True
Session Behavior
| Scenario | Session Type | When Used |
|---|---|---|
SharedSessionManager.setup() called |
Shared session | All clients |
| No shared session initialized | Individual session | Each client |
| Cached clients | Depends on initialization | Cached per credentials |
Cache Features
- Automatic TTL: 10 minutes default, configurable
- Memory Safe: Prevents client accumulation
- High Performance: Lock-free operations
- Background Cleanup: Optional periodic cleanup task
# Configure cache lifetime for each exchange
BingxClientsCache.configure(lifetime_seconds=1800) # 30 minutes
BybitClientsCache.configure(lifetime_seconds=1800) # 30 minutes
OkxClientsCache.configure(lifetime_seconds=1800) # 30 minutes
BitgetClientsCache.configure(lifetime_seconds=1800) # 30 minutes
# Start background cleanup
bingx_cleanup = BingxClientsCache.create_cleanup_task(interval_seconds=300)
bybit_cleanup = BybitClientsCache.create_cleanup_task(interval_seconds=300)
okx_cleanup = OkxClientsCache.create_cleanup_task(interval_seconds=300)
bitget_cleanup = BitgetClientsCache.create_cleanup_task(interval_seconds=300)
# Manual cleanup
bingx_removed = BingxClientsCache.cleanup_expired()
bybit_removed = BybitClientsCache.cleanup_expired()
okx_removed = OkxClientsCache.cleanup_expired()
bitget_removed = BitgetClientsCache.cleanup_expired()
Requirements
- Python >= 3.12
- aiohttp
- High-performance connection pooling for production use
Performance Tips
- Use Shared Session for applications creating many clients
- Enable Caching for repeated API credential usage
- Configure Connection Limits based on your throughput needs
- Use Background Cleanup for long-running applications
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiotrade_sdk-0.10.0.tar.gz.
File metadata
- Download URL: aiotrade_sdk-0.10.0.tar.gz
- Upload date:
- Size: 63.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9699e62809badd0f92e484dfee5acb592cae5ee826d80c33af7de46fb1d587d3
|
|
| MD5 |
201794f5de351ebce1644d9f4c5633e4
|
|
| BLAKE2b-256 |
24726de35b603341271d0385c6c4fc3161c14f9ff6fc3875333c5d1b70b11e88
|
Provenance
The following attestation bundles were made for aiotrade_sdk-0.10.0.tar.gz:
Publisher:
release.yml on vispar-tech/aiotrade
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiotrade_sdk-0.10.0.tar.gz -
Subject digest:
9699e62809badd0f92e484dfee5acb592cae5ee826d80c33af7de46fb1d587d3 - Sigstore transparency entry: 983349694
- Sigstore integration time:
-
Permalink:
vispar-tech/aiotrade@9728dcc2abd212d71cd885dad29d2b8d35d33069 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/vispar-tech
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9728dcc2abd212d71cd885dad29d2b8d35d33069 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiotrade_sdk-0.10.0-py3-none-any.whl.
File metadata
- Download URL: aiotrade_sdk-0.10.0-py3-none-any.whl
- Upload date:
- Size: 96.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
615e3e231a95160f6ac18eca60f1b123d7600c2c42d9fb3475b71cfd773ac924
|
|
| MD5 |
ea37b5430213402d07ea6eec8dbedcd0
|
|
| BLAKE2b-256 |
62260fbf8b20a3585644cdf8560e0b0d92817947ecba5dd66136c4b6154994d8
|
Provenance
The following attestation bundles were made for aiotrade_sdk-0.10.0-py3-none-any.whl:
Publisher:
release.yml on vispar-tech/aiotrade
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiotrade_sdk-0.10.0-py3-none-any.whl -
Subject digest:
615e3e231a95160f6ac18eca60f1b123d7600c2c42d9fb3475b71cfd773ac924 - Sigstore transparency entry: 983349717
- Sigstore integration time:
-
Permalink:
vispar-tech/aiotrade@9728dcc2abd212d71cd885dad29d2b8d35d33069 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/vispar-tech
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9728dcc2abd212d71cd885dad29d2b8d35d33069 -
Trigger Event:
push
-
Statement type: