Unofficial Python wrapper for the Binance cryptocurrency exchange REST API v3 and WebSocket APIs with comprehensive trading, market data, and account management functionality
—
Asset conversion functionality for instant and limit conversions between cryptocurrencies. Provides quote-based conversions, limit order conversions, and comprehensive conversion history tracking across spot, margin, and futures accounts.
Get quotes and execute instant conversions between supported assets.
def convert_request_quote(self, **params): ...
def convert_accept_quote(self, **params): ...
def get_convert_trade_history(self, **params): ...# Request a conversion quote
quote = client.convert_request_quote(
fromAsset='BTC',
toAsset='USDT',
fromAmount=0.1 # Convert 0.1 BTC to USDT
)
print(f"Quote ID: {quote['quoteId']}")
print(f"Rate: {quote['ratio']}")
print(f"From amount: {quote['fromAmount']} {quote['fromAsset']}")
print f"To amount: {quote['toAmount']} {quote['toAsset']}")
print(f"Valid until: {quote['validTimestamp']}")
# Accept the quote to execute conversion
conversion = client.convert_accept_quote(
quoteId=quote['quoteId']
)
print(f"Conversion status: {conversion['status']}")
print(f"Transaction ID: {conversion['orderId']}")
# Alternative: Convert by specifying target amount
quote_target = client.convert_request_quote(
fromAsset='USDT',
toAsset='BTC',
toAmount=0.05 # Get 0.05 BTC
)
# Get conversion history
history = client.get_convert_trade_history(
startTime=int(time.time() * 1000) - (7 * 24 * 60 * 60 * 1000), # 7 days ago
endTime=int(time.time() * 1000),
limit=100
)
for trade in history['list']:
print(f"Order ID: {trade['orderId']}")
print(f"From: {trade['fromAmount']} {trade['fromAsset']}")
print(f"To: {trade['toAmount']} {trade['toAsset']}")
print(f"Price: {trade['price']}")
print(f"Status: {trade['orderStatus']}")
print(f"Create time: {trade['createTime']}")Convert API functionality specific to margin accounts with limit orders and exchange information.
def margin_v1_get_convert_exchange_info(self, **params): ...
def margin_v1_get_convert_asset_info(self, **params): ...
def margin_v1_post_convert_limit_place_order(self, **params): ...
def margin_v1_get_convert_limit_query_open_orders(self, **params): ...
def margin_v1_post_convert_limit_cancel_order(self, **params): ...
def margin_v1_get_convert_order_status(self, **params): ...# Get margin convert exchange information
margin_convert_info = client.margin_v1_get_convert_exchange_info()
for asset_pair in margin_convert_info['assetPairs']:
print(f"Pair: {asset_pair['fromAsset']} -> {asset_pair['toAsset']}")
print(f"Min from amount: {asset_pair['minFromAmount']}")
print(f"Max from amount: {asset_pair['maxFromAmount']}")
# Get convert asset information
asset_info = client.margin_v1_get_convert_asset_info(asset='BTC')
print(f"BTC convertible: {asset_info['convertible']}")
print(f"Precision: {asset_info['precision']}")
# Place margin limit convert order
limit_order = client.margin_v1_post_convert_limit_place_order(
fromAsset='BTC',
toAsset='USDT',
fromAmount=0.1,
price=50000 # Target conversion rate
)
print(f"Order ID: {limit_order['orderId']}")
print(f"Status: {limit_order['status']}")
# Check open limit convert orders
open_orders = client.margin_v1_get_convert_limit_query_open_orders()
for order in open_orders['orders']:
print(f"Order ID: {order['orderId']}")
print(f"From: {order['fromAmount']} {order['fromAsset']}")
print(f"To: {order['toAsset']}")
print(f"Price: {order['price']}")
print(f"Status: {order['status']}")
# Cancel limit convert order
cancel_result = client.margin_v1_post_convert_limit_cancel_order(
orderId=limit_order['orderId']
)
# Get specific order status
order_status = client.margin_v1_get_convert_order_status(
orderId=limit_order['orderId']
)Convert API functionality for futures accounts.
def futures_v1_get_convert_exchange_info(self, **params): ...
def futures_v1_post_convert_get_quote(self, **params): ...
def futures_v1_post_convert_accept_quote(self, **params): ...
def futures_v1_get_convert_order_status(self, **params): ...# Get futures convert exchange information
futures_convert_info = client.futures_v1_get_convert_exchange_info()
print(f"Supported conversions: {futures_convert_info['assetPairs']}")
# Get futures conversion quote
futures_quote = client.futures_v1_post_convert_get_quote(
fromAsset='BTC',
toAsset='USDT',
fromAmount=0.1
)
print(f"Quote ID: {futures_quote['quoteId']}")
print(f"Conversion rate: {futures_quote['price']}")
print(f"Valid until: {futures_quote['expiredTimestamp']}")
# Accept futures conversion quote
futures_conversion = client.futures_v1_post_convert_accept_quote(
quoteId=futures_quote['quoteId']
)
print(f"Order ID: {futures_conversion['orderId']}")
print(f"Status: {futures_conversion['status']}")
# Check futures conversion order status
futures_status = client.futures_v1_get_convert_order_status(
orderId=futures_conversion['orderId']
)Convert assets during transfers between different account types.
def margin_v1_post_asset_convert_transfer(self, **params): ...
def margin_v1_post_asset_convert_transfer_query_by_page(self, **params): ...# Convert asset during transfer
convert_transfer = client.margin_v1_post_asset_convert_transfer(
clientTranId='unique_transfer_id_123',
asset='BTC',
amount=0.1,
targetAsset='USDT',
accountType='SPOT' # Target account type
)
print(f"Transfer ID: {convert_transfer['tranId']}")
print(f"Status: {convert_transfer['status']}")
# Query convert transfer history
transfer_history = client.margin_v1_post_asset_convert_transfer_query_by_page(
startTime=int(time.time() * 1000) - (30 * 24 * 60 * 60 * 1000), # 30 days ago
endTime=int(time.time() * 1000),
current=1, # Page number
size=20 # Page size
)
for transfer in transfer_history['rows']:
print(f"Transfer ID: {transfer['tranId']}")
print(f"From: {transfer['amount']} {transfer['asset']}")
print(f"To: {transfer['targetAmount']} {transfer['targetAsset']}")
print(f"Status: {transfer['status']}")
print(f"Create time: {transfer['createTime']}")Manage convertible coin contracts and balances.
def margin_v1_get_capital_contract_convertible_coins(self, **params): ...
def margin_v1_post_capital_contract_convertible_coins(self, **params): ...# Get convertible coins information
convertible_coins = client.margin_v1_get_capital_contract_convertible_coins()
for coin in convertible_coins['convertibleCoins']:
print(f"Coin: {coin['coin']}")
print(f"Convertible amount: {coin['convertibleAmount']}")
print(f"Convertible: {coin['convertible']}")
print(f"Exchange rate: {coin['exchangeRate']}")
# Execute convertible coin conversion
conversion_result = client.margin_v1_post_capital_contract_convertible_coins(
coin='USDC',
amount=100,
targetCoin='USDT'
)
print(f"Conversion ID: {conversion_result['convertId']}")
print(f"Status: {conversion_result['status']}")# Convert order status constants
CONVERT_ORDER_STATUS_PROCESSING = "PROCESSING"
CONVERT_ORDER_STATUS_SUCCESS = "SUCCESS"
CONVERT_ORDER_STATUS_CONVERT_SUCCESS = "CONVERT_SUCCESS"
CONVERT_ORDER_STATUS_PROCESS_FAIL = "PROCESS_FAIL"
CONVERT_ORDER_STATUS_ACCEPTED = "ACCEPTED"from binance import BinanceAPIException
try:
quote = client.convert_request_quote(
fromAsset='BTC',
toAsset='USDT',
fromAmount=0.1
)
conversion = client.convert_accept_quote(quoteId=quote['quoteId'])
except BinanceAPIException as e:
if e.code == -1200:
print("Invalid conversion parameters")
elif e.code == -1201:
print("Quote expired or invalid")
elif e.code == -1202:
print("Insufficient balance for conversion")
else:
print(f"Conversion error: {e.message}")Convert API operations are subject to rate limits:
The Convert API provides comprehensive asset conversion capabilities across all Binance account types with both instant and limit order functionality, extensive history tracking, and advanced transfer conversion features.
Install with Tessl CLI
npx tessl i tessl/pypi-python-binance