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, Bitget, Binance and Kucoin 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,BybitClientsCacheand etc. cache 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
- 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
Available clients methods
BybitClient methods (42):
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
get_account_info get_wallet_balance
get_account_instruments_info manual_borrow
get_api_key_info manual_repay
get_borrow_history manual_repay_without_asset_conversion
get_closed_pnl place_order
get_coin_greeks repay_liability
get_collateral_info reset_mmp
get_dcp_info set_collateral_coin
get_fee_rate set_leverage
get_instruments_info set_limit_price_behaviour
get_kline set_margin_mode
get_mmp_state set_mmp
get_open_and_closed_orders set_spot_hedging
get_order_history set_trading_stop
get_position_info switch_position_mode
get_risk_limit upgrade_to_unified_account_pro
BingxClient methods (47):
cancel_all_spot_open_orders get_spot_profit_overview
cancel_all_swap_open_orders get_spot_symbols
cancel_spot_batch_orders get_spot_trade_details
cancel_swap_batch_orders get_swap_account_balance
change_swap_margin_type get_swap_contracts
close_perpetual_trader_position_by_order get_swap_full_orders
close_swap_position 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
get_spot_profit_details
OkxClient methods (23):
batch_place_order get_order
cancel_algo_orders get_orders_history
cancel_batch_orders get_orders_pending
close_position get_position_tiers
get_account_config get_positions
get_algo_order get_positions_history
get_algo_orders_history get_server_time
get_algo_orders_pending place_algo_order
get_balance set_isolated_mode
get_funding_balance set_leverage
get_instruments set_position_mode
get_leverage_info
BitgetClient methods (34):
batch_cancel_futures_orders get_order_detail
batch_cancel_spot_orders get_pending_orders
batch_place_futures_orders get_pending_trigger_orders
batch_place_spot_orders get_server_time
cancel_all_futures_orders get_single_account
cancel_order get_spot_history_orders
cancel_order_by_symbol get_symbol_info
cancel_trigger_orders 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_asset_mode
get_futures_history_orders set_leverage
get_historical_position set_margin_mode
get_isolated_symbols set_position_mode
BinanceClient methods (35):
cancel_algo_order get_api_key_permissions
cancel_all_algo_open_orders get_exchange_info
cancel_all_open_orders get_income_history
cancel_batch_orders get_klines
cancel_order get_multi_assets_mode
change_leverage get_open_algo_orders
change_margin_type get_open_order
change_multi_assets_mode get_open_orders
change_position_mode get_order
create_algo_order get_position_info
create_batch_orders get_position_info_v3
create_order get_position_mode
get_account_balance get_spot_account_info
get_account_config get_spot_all_orders
get_account_info get_spot_open_orders
get_algo_order get_symbol_config
get_all_algo_orders get_user_trades
get_all_orders
KuCoinClient methods (32):
add_order get_margin_mode
add_order_test get_order_by_client_oid
add_tp_sl_order get_order_by_order_id
batch_add_orders get_order_list
batch_cancel_orders get_position_details
batch_switch_margin_mode get_position_mode
cancel_all_orders get_positions
cancel_all_stop_orders get_positions_history
cancel_order_by_client_oid get_recent_closed_orders
cancel_order_by_id get_recent_trade_history
get_all_symbols get_server_time
get_all_tickers get_service_status
get_api_key_info get_stop_orders
get_futures_account get_trade_history
get_isolated_margin_risk_limit switch_margin_mode
get_klines switch_position_mode
GateClient methods (31):
cancel_batch_future_orders get_user_leverage_currency_setting
cancel_price_triggered_order list_futures_candlesticks
create_batch_futures_order list_futures_orders
create_futures_order list_futures_risk_limit_tiers
create_price_triggered_order list_futures_tickers
create_trailing_order list_position_close
get_account_detail list_positions
get_account_main_keys list_price_triggered_orders
get_futures_account list_trailing_orders
get_futures_contract set_dual_mode
get_futures_contracts set_unified_mode
get_futures_risk_limit_table set_user_leverage_currency_setting
get_position terminate_trail_order
get_unified_account update_position_cross_mode
get_unified_mode update_position_leverage
get_user_leverage_currency_config
Installation
poetry add aiotrade-sdk
Quick Start
Option 1: Shared Session (Recommended for Production)
from aiotrade import SharedSessionManager, BybitClient
# Initialize shared session at startup (once per application)
SharedSessionManager.setup(max_connections=2000)
# Create Bybit client - will use the shared session
bybit_client = BybitClient(api_key="bybit_key", api_secret="bybit_secret", testnet=True)
try:
# Use client for API calls
bybit_tickers = await bybit_client.get_tickers(category="spot")
finally:
# Close shared session at shutdown
await SharedSessionManager.close()
Option 2: Individual Bybit Session
from aiotrade import BybitClient
# 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}")
Option 3: Cached Bybit Client
from aiotrade import BybitClientsCache
# Get cached Bybit client (creates new if doesn't exist)
bybit_client = BybitClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
testnet=True
)
# Use cached client (session management is automatic)
async with bybit_client:
tickers = await bybit_client.get_tickers(category="spot")
# Same parameters return the same cached instance
cached_bybit = BybitClientsCache.get_or_create(
api_key="your_key",
api_secret="your_secret",
testnet=True
)
assert bybit_client is cached_bybit # 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
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.27.2.tar.gz.
File metadata
- Download URL: aiotrade_sdk-0.27.2.tar.gz
- Upload date:
- Size: 144.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e81d1dd943a23fcba0ce0d9f7ad81948cf01a217191e7041dae66761f70b625
|
|
| MD5 |
912002ae23a295c5ef48a2afc88d55cd
|
|
| BLAKE2b-256 |
9f82be4a8e12ebaa30938c5dbf1f12d1e39d8e65bf363a90703bd6cf7a574e0b
|
Provenance
The following attestation bundles were made for aiotrade_sdk-0.27.2.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.27.2.tar.gz -
Subject digest:
8e81d1dd943a23fcba0ce0d9f7ad81948cf01a217191e7041dae66761f70b625 - Sigstore transparency entry: 1389188520
- Sigstore integration time:
-
Permalink:
vispar-tech/aiotrade@c4da399780d08e85d886984e963740bcf73cb24f -
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@c4da399780d08e85d886984e963740bcf73cb24f -
Trigger Event:
push
-
Statement type:
File details
Details for the file aiotrade_sdk-0.27.2-py3-none-any.whl.
File metadata
- Download URL: aiotrade_sdk-0.27.2-py3-none-any.whl
- Upload date:
- Size: 214.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32708a369250b7e4e0691c9f9de60bbb3228fb703a7e4043eb71620c79154767
|
|
| MD5 |
896ac11fff786010f3a95c13cb6e803b
|
|
| BLAKE2b-256 |
0459412a81d90b00db8ea5df223775c44ce4c921aeca5c99c896b8fbab54c355
|
Provenance
The following attestation bundles were made for aiotrade_sdk-0.27.2-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.27.2-py3-none-any.whl -
Subject digest:
32708a369250b7e4e0691c9f9de60bbb3228fb703a7e4043eb71620c79154767 - Sigstore transparency entry: 1389188524
- Sigstore integration time:
-
Permalink:
vispar-tech/aiotrade@c4da399780d08e85d886984e963740bcf73cb24f -
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@c4da399780d08e85d886984e963740bcf73cb24f -
Trigger Event:
push
-
Statement type: