pymexc is an unofficial Python library for interacting with the MEXC crypto exchange. It provides a simple and intuitive API for making requests to the MEXC API endpoints.
Base of code was taken from pybit library.
MEXC Futures API for create orders is on maintance now. But you can bypass it. See this issue for more information.
You can install pymexc using pip:
pip install pymexcTo start working with pymexc, you must import spot or futures from the library. Each of them contains 2 classes: HTTP and WebSocket. To work with simple requests, you need to initialize the HTTP class. To work with web sockets you need to initialize the WebSocket class
from pymexc import spot, futures
api_key = "YOUR API KEY"
api_secret = "YOUR API SECRET KEY"
def handle_message(message):
# handle websocket message
print(message)
# SPOT V3
# initialize HTTP client
spot_client = spot.HTTP(api_key = api_key, api_secret = api_secret)
# initialize WebSocket client
ws_spot_client = spot.WebSocket(api_key = api_key, api_secret = api_secret)
# make http request to api
print(spot_client.exchange_info())
# create websocket connection to public channel ([email protected]@BTCUSDT)
# all messages will be handled by function `handle_message`
ws_spot_client.deals_stream(handle_message, "BTCUSDT")
# FUTURES V1
# initialize HTTP client
futures_client = futures.HTTP(api_key = api_key, api_secret = api_secret)
# initialize WebSocket client
ws_futures_client = futures.WebSocket(api_key = api_key, api_secret = api_secret,
# subscribe on personal information about about account
# if not provided, will not be subscribed
# you can subsctibe later by calling ws_futures_client.personal_stream(callback) for all info
# or ws_futures_client.filter_stream(callback, params={"filters":[{"filter":"..."}]}) for specific info (https://mexcdevelop.github.io/apidocs/contract_v1_en/#filter-subscription)
personal_callback = handle_message)
# make http request to api
print(futures_client.index_price("MX_USDT"))
# create websocket connection to public channel (sub.tickers)
# all messages will be handled by function `handle_message`
ws_futures_client.tickers_stream(handle_message)
# loop forever for save websocket connection
while True:
...from pymexc import spot
spot_client = spot.HTTP()
# Get current price
ticker = spot_client.ticker_price("BTCUSDT")
print(f"BTC Price: {ticker['price']}")
# Get 24h ticker statistics
stats = spot_client.ticker_24h("BTCUSDT")
print(f"24h Change: {stats['priceChangePercent']}%")
print(f"24h Volume: {stats['volume']}")
# Get order book
orderbook = spot_client.order_book("BTCUSDT", limit=10)
print(f"Best Bid: {orderbook['bids'][0]}")
print(f"Best Ask: {orderbook['asks'][0]}")
# Get recent trades
trades = spot_client.trades("BTCUSDT", limit=10)
for trade in trades:
print(f"Price: {trade['price']}, Qty: {trade['qty']}")from pymexc import spot
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Get current price first
ticker = spot_client.ticker_price("BTCUSDT")
current_price = float(ticker['price'])
# Place a limit buy order 1% below current price
buy_price = current_price * 0.99
result = spot_client.order(
symbol="BTCUSDT",
side="BUY",
order_type="LIMIT",
quantity=0.001,
price=buy_price,
time_in_force="GTC"
)
print(f"Order placed: {result}")
# Check order status
order_status = spot_client.query_order("BTCUSDT", order_id=result['orderId'])
print(f"Order status: {order_status['status']}")
# Get all open orders
open_orders = spot_client.current_open_orders("BTCUSDT")
print(f"Open orders: {len(open_orders)}")
# Cancel an order
if open_orders:
cancel_result = spot_client.cancel_order("BTCUSDT", order_id=open_orders[0]['orderId'])
print(f"Order cancelled: {cancel_result}")from pymexc import spot
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Place a market buy order for $100 worth of BTC
result = spot_client.order(
symbol="BTCUSDT",
side="BUY",
order_type="MARKET",
quote_order_qty=100 # Buy $100 worth
)
print(f"Market order executed: {result}")from pymexc import spot
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Get account information
account = spot_client.account_information()
# Print balances for assets with non-zero amounts
for balance in account['balances']:
free = float(balance['free'])
locked = float(balance['locked'])
total = free + locked
if total > 0:
print(f"{balance['asset']}: Free={free}, Locked={locked}, Total={total}")from pymexc import spot
import time
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Get recent trades for a symbol
trades = spot_client.account_trade_list("BTCUSDT", limit=100)
# Calculate total volume and PnL
total_volume = 0
total_cost = 0
for trade in trades:
qty = float(trade['qty'])
price = float(trade['price'])
total_volume += qty
if trade['isBuyer']:
total_cost += qty * price
else:
total_cost -= qty * price
print(f"Total Volume: {total_volume} BTC")
print(f"Net Cost: {total_cost} USDT")from pymexc import spot
from pymexc.proto import PublicMiniTickerV3Api
import time
def handle_price_update(message):
"""Handle real-time price updates"""
# WebSocket uses protobuf by default, so message is a protobuf object
if isinstance(message, PublicMiniTickerV3Api):
symbol = message.symbol if hasattr(message, "symbol") else "N/A"
price = message.price if hasattr(message, "price") else "N/A"
volume = message.volume if hasattr(message, "volume") else "N/A"
print(f"{symbol}: Price={price}, Volume={volume}")
elif isinstance(message, dict) and "d" in message:
# Fallback for JSON format (if proto=False)
data = message["d"]
symbol = data.get("symbol", "N/A")
price = data.get("p", "N/A")
volume = data.get("v", "N/A")
print(f"{symbol}: Price={price}, Volume={volume}")
# Initialize WebSocket client
ws_client = spot.WebSocket()
# Subscribe to ticker updates
ws_client.mini_ticker_stream(handle_price_update, "BTCUSDT")
# Keep connection alive
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping...")
ws_client.exit()from pymexc import spot
from pymexc.proto import PublicAggreDepthsV3Api
import time
def handle_depth_update(message):
"""Handle order book depth updates"""
# WebSocket uses protobuf by default, so message is a protobuf object
if isinstance(message, PublicAggreDepthsV3Api):
bids = list(message.bids) if hasattr(message, "bids") else []
asks = list(message.asks) if hasattr(message, "asks") else []
if bids and asks:
best_bid_price = float(bids[0].price) if bids and hasattr(bids[0], "price") else None
best_ask_price = float(asks[0].price) if asks and hasattr(asks[0], "price") else None
if best_bid_price and best_ask_price:
spread = best_ask_price - best_bid_price
print(f"Bid={best_bid_price}, Ask={best_ask_price}, Spread={spread:.2f}")
elif isinstance(message, dict) and "d" in message:
# Fallback for JSON format (if proto=False)
data = message["d"]
symbol = data.get("symbol", "N/A")
bids = data.get("bids", [])
asks = data.get("asks", [])
if bids and asks:
best_bid = bids[0][0] if bids else "N/A"
best_ask = asks[0][0] if asks else "N/A"
spread = float(best_ask) - float(best_bid) if best_ask != "N/A" and best_bid != "N/A" else 0
print(f"{symbol}: Bid={best_bid}, Ask={best_ask}, Spread={spread:.2f}")
# Initialize WebSocket client
ws_client = spot.WebSocket()
# Subscribe to depth updates
ws_client.depth_stream(handle_depth_update, "BTCUSDT", interval="100ms")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
ws_client.exit()from pymexc import spot
import pandas as pd
from datetime import datetime, timedelta
spot_client = spot.HTTP()
# Get klines for the last 24 hours
end_time = int(datetime.now().timestamp() * 1000)
start_time = int((datetime.now() - timedelta(days=1)).timestamp() * 1000)
klines = spot_client.klines(
symbol="BTCUSDT",
interval="1h",
start_time=start_time,
end_time=end_time,
limit=24
)
# Convert to DataFrame for analysis
df = pd.DataFrame(klines, columns=[
'open_time', 'open', 'high', 'low', 'close', 'volume',
'close_time', 'quote_volume', 'trades', 'taker_buy_base',
'taker_buy_quote', 'ignore'
])
# Convert to numeric
for col in ['open', 'high', 'low', 'close', 'volume']:
df[col] = pd.to_numeric(df[col])
# Calculate simple moving average
df['sma_20'] = df['close'].rolling(window=20).mean()
# Print latest data
print(df[['open_time', 'open', 'high', 'low', 'close', 'volume', 'sma_20']].tail())from pymexc import spot
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Get current price
ticker = spot_client.ticker_price("BTCUSDT")
current_price = float(ticker['price'])
# Prepare multiple orders at different price levels
batch_orders = []
for i in range(5):
price = current_price * (0.99 - i * 0.001) # 1% to 0.5% below current price
batch_orders.append({
"price": str(price),
"quantity": "0.001"
})
# Place batch orders
result = spot_client.batch_orders(
batch_orders=batch_orders,
symbol="BTCUSDT",
side="BUY",
order_type="LIMIT"
)
print(f"Placed {len(result)} orders")from pymexc import spot
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
try:
# Attempt to place an order
result = spot_client.order(
symbol="BTCUSDT",
side="BUY",
order_type="LIMIT",
quantity=0.001,
price=50000
)
print(f"Order placed successfully: {result}")
except Exception as e:
print(f"Error placing order: {e}")
# Handle specific error cases
if "insufficient balance" in str(e).lower():
print("Not enough balance for this order")
elif "invalid symbol" in str(e).lower():
print("Invalid trading pair")
else:
print(f"Unknown error: {e}")from pymexc import spot
from datetime import datetime, timedelta
spot_client = spot.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Get deposit history
deposits = spot_client.deposit_history(coin="USDT", limit=50)
print("Recent Deposits:")
for deposit in deposits[:10]: # Show last 10
print(f"Amount: {deposit['amount']} {deposit['coin']}, "
f"Status: {deposit['status']}, "
f"Time: {datetime.fromtimestamp(deposit['insertTime']/1000)}")
# Get withdrawal history
withdrawals = spot_client.withdraw_history(coin="USDT", limit=50)
print("\nRecent Withdrawals:")
for withdrawal in withdrawals[:10]: # Show last 10
print(f"Amount: {withdrawal['amount']} {withdrawal['coin']}, "
f"Status: {withdrawal['status']}, "
f"Address: {withdrawal['address']}")from pymexc import spot
spot_client = spot.HTTP()
# Get prices for multiple symbols
symbols = ["BTCUSDT", "ETHUSDT", "BNBUSDT", "ADAUSDT"]
prices = spot_client.ticker_price(symbols=symbols)
print("Current Prices:")
for ticker in prices:
print(f"{ticker['symbol']}: {ticker['price']}")
# Get 24h stats for multiple symbols
stats = spot_client.ticker_24h(symbols=symbols)
print("\n24h Statistics:")
for stat in stats:
change = float(stat['priceChangePercent'])
emoji = "📈" if change > 0 else "📉"
print(f"{emoji} {stat['symbol']}: {change:+.2f}% "
f"(High: {stat['highPrice']}, Low: {stat['lowPrice']})")from pymexc import futures
futures_client = futures.HTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Get all open positions
positions = futures_client.open_positions()
print("Open Positions:")
for pos in positions.get('data', []):
print(f"Symbol: {pos['symbol']}, "
f"Side: {'Long' if pos['holdVol'] > 0 else 'Short'}, "
f"Volume: {pos['holdVol']}, "
f"Unrealized PnL: {pos['unrealisedPnl']}")
# Get account assets
assets = futures_client.assets()
print("\nAccount Assets:")
for asset in assets.get('data', []):
if float(asset['availableBalance']) > 0:
print(f"{asset['currency']}: {asset['availableBalance']} "
f"(Frozen: {asset['frozenBalance']})")import asyncio
from pymexc import spot
async def main():
# Initialize async client
async_client = spot.AsyncHTTP(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Make multiple requests concurrently
tasks = [
async_client.ticker_price("BTCUSDT"),
async_client.ticker_price("ETHUSDT"),
async_client.ticker_24h("BTCUSDT"),
async_client.account_information()
]
results = await asyncio.gather(*tasks)
btc_price, eth_price, btc_stats, account = results
print(f"BTC Price: {btc_price['price']}")
print(f"ETH Price: {eth_price['price']}")
print(f"BTC 24h Change: {btc_stats['priceChangePercent']}%")
print(f"Account Balance: {len(account['balances'])} assets")
# Run async function
asyncio.run(main())from pymexc import spot
from pymexc.proto import PrivateAccountV3Api, PrivateOrdersV3Api
import time
def handle_account_update(message):
"""Handle account balance updates"""
# WebSocket uses protobuf by default, so message is a protobuf object
if isinstance(message, PrivateAccountV3Api):
# PrivateAccountV3Api has fields: vcoinName, coinId, balanceAmount, frozenAmount, etc.
vcoin_name = message.vcoinName if hasattr(message, "vcoinName") else "N/A"
balance = float(message.balanceAmount) if hasattr(message, "balanceAmount") and message.balanceAmount else 0
frozen = float(message.frozenAmount) if hasattr(message, "frozenAmount") and message.frozenAmount else 0
if balance > 0 or frozen > 0:
print(f"{vcoin_name}: Balance={balance}, Frozen={frozen}")
elif isinstance(message, dict) and "d" in message:
# Fallback for JSON format (if proto=False)
data = message["d"]
balances = data.get("B", [])
for balance in balances:
asset = balance.get("a", "")
free = balance.get("f", "0")
locked = balance.get("l", "0")
if float(free) > 0 or float(locked) > 0:
print(f"{asset}: Free={free}, Locked={locked}")
# Initialize WebSocket with authentication
ws_client = spot.WebSocket(api_key="YOUR_KEY", api_secret="YOUR_SECRET")
# Subscribe to account updates
ws_client.account_update(handle_account_update)
# Also subscribe to order updates
def handle_order_update(message):
"""Handle order status updates"""
# WebSocket uses protobuf by default, so message is a protobuf object
if isinstance(message, PrivateOrdersV3Api):
# PrivateOrdersV3Api has fields: id, clientId, price, quantity, amount, status, etc.
order_id = message.id if hasattr(message, "id") else "N/A"
price = message.price if hasattr(message, "price") else "N/A"
quantity = message.quantity if hasattr(message, "quantity") else "N/A"
status = message.status if hasattr(message, "status") else "N/A"
print(f"Order Update: ID={order_id}, Price={price}, Quantity={quantity}, Status={status}")
elif isinstance(message, dict) and "d" in message:
# Fallback for JSON format (if proto=False)
order = message["d"]
print(f"Order Update: {order.get('s')} {order.get('S')} "
f"{order.get('q')} @ {order.get('p')} - Status: {order.get('X')}")
ws_client.account_orders(handle_order_update)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
ws_client.exit()You can find the official documentation for the MEXC API here.
Test connectivity to the Rest API.
result = spot_client.ping()Check Server Time.
result = spot_client.time()Get API default symbols.
result = spot_client.default_symbols()Get exchange trading rules and symbol information.
# Get all symbols
result = spot_client.exchange_info()
# Get specific symbol
result = spot_client.exchange_info(symbol="BTCUSDT")
# Get multiple symbols
result = spot_client.exchange_info(symbols=["BTCUSDT", "ETHUSDT"])Get order book depth.
# Using order_book
result = spot_client.order_book("BTCUSDT", limit=100)
# Using alias depth
result = spot_client.depth("BTCUSDT", limit=100)Get recent trades list.
result = spot_client.trades("BTCUSDT", limit=100)Get compressed/aggregate trades list.
result = spot_client.agg_trades("BTCUSDT", limit=100)Get kline/candlestick data.
result = spot_client.klines("BTCUSDT", interval="1h", limit=100)Get current average price.
result = spot_client.avg_price("BTCUSDT")Get 24hr ticker price change statistics.
# Get all tickers
result = spot_client.ticker_24h()
# Get specific ticker
result = spot_client.ticker_24h(symbol="BTCUSDT")
# Using alias
result = spot_client.ticker24h("BTCUSDT")Get symbol price ticker.
result = spot_client.ticker_price("BTCUSDT")Get best price/qty on the order book.
result = spot_client.ticker_book_price("BTCUSDT")Create a sub-account from the master account.
result = spot_client.create_sub_account("sub_account_name", "note")Get details of the sub-account list.
result = spot_client.sub_account_list()Create an APIKey for a sub-account.
result = spot_client.create_sub_account_api_key(
"sub_account_name",
"note",
["SPOT_ACCOUNT_READ", "SPOT_DEAL_READ"]
)Query the APIKey of a sub-account.
result = spot_client.query_sub_account_api_key("sub_account_name")Delete the APIKey of a sub-account.
result = spot_client.delete_sub_account_api_key("sub_account_name", "api_key")universal_transfer(from_account_type, to_account_type, asset, amount, from_account=None, to_account=None)
Universal Transfer (For Master Account).
result = spot_client.universal_transfer("SPOT", "FUTURES", "USDT", 100.0)query_universal_transfer_history(from_account_type, to_account_type, from_account=None, to_account=None, start_time=None, end_time=None, page=1, limit=500)
Query Universal Transfer History.
result = spot_client.query_universal_transfer_history("SPOT", "FUTURES")Query Sub-account Asset.
result = spot_client.sub_account_asset("sub_account_name", "SPOT")Query KYC status.
result = spot_client.get_kyc_status()Query UID.
result = spot_client.get_uid()Get user API default symbols.
result = spot_client.get_default_symbols()Test new order (does not place actual order).
result = spot_client.test_order(
symbol="BTCUSDT",
side="BUY",
order_type="LIMIT",
quantity=0.001,
price=50000
)Place a new order.
# Limit order
result = spot_client.order(
symbol="BTCUSDT",
side="BUY",
order_type="LIMIT",
quantity=0.001,
price=50000,
time_in_force="GTC"
)
# Market order
result = spot_client.order(
symbol="BTCUSDT",
side="BUY",
order_type="MARKET",
quote_order_qty=100
)Place batch orders (up to 20 orders).
result = spot_client.batch_orders(
batch_orders=[{"price": "50000", "quantity": "0.001"}],
symbol="BTCUSDT",
side="BUY",
order_type="LIMIT"
)Cancel an order.
result = spot_client.cancel_order("BTCUSDT", order_id="123456")Cancel all open orders on a symbol.
result = spot_client.cancel_all_open_orders("BTCUSDT")Query order status.
result = spot_client.query_order("BTCUSDT", order_id="123456")Get current open orders.
result = spot_client.current_open_orders("BTCUSDT")Get all orders (active, cancelled, or completed).
result = spot_client.all_orders("BTCUSDT", limit=100)Get account information.
result = spot_client.account_information()Get account trade list.
result = spot_client.account_trade_list("BTCUSDT", limit=100)Enable or disable MX deduct for spot commission fee.
result = spot_client.enable_mx_deduct(True)Query MX deduct status.
result = spot_client.query_mx_deduct_status()Create STP strategy group.
result = spot_client.create_stp_strategy_group("group_name")Query STP strategy group.
result = spot_client.query_stp_strategy_group()Delete STP strategy group.
result = spot_client.delete_stp_strategy_group("group_id")Add uid to STP strategy group.
result = spot_client.add_uid_to_stp_strategy_group("123456", "group_id")Delete uid from STP strategy group.
result = spot_client.delete_uid_from_stp_strategy_group("123456", "group_id")Query symbol commission.
result = spot_client.query_symbol_commission("BTCUSDT")Query the currency information.
result = spot_client.get_currency_info()withdraw(coin, address, amount, contract_address=None, withdraw_order_id=None, network=None, memo=None, remark=None)
Withdraw funds.
result = spot_client.withdraw("USDT", "address", 100.0, network="TRC20")Cancel withdraw.
result = spot_client.cancel_withdraw("withdraw_id")Get deposit history.
result = spot_client.deposit_history(coin="USDT", limit=100)Get withdraw history.
result = spot_client.withdraw_history(coin="USDT", limit=100)Generate deposit address.
result = spot_client.generate_deposit_address("USDT", "TRC20")Get deposit address.
result = spot_client.deposit_address("USDT", "TRC20")Get withdraw address.
result = spot_client.withdraw_address(coin="USDT")User Universal Transfer.
result = spot_client.user_universal_transfer("SPOT", "FUTURES", "USDT", 100.0)user_universal_transfer_history(from_account_type, to_account_type, start_time=None, end_time=None, page=1, size=10)
Query User Universal Transfer History.
result = spot_client.user_universal_transfer_history("SPOT", "FUTURES")Query User Universal Transfer History by tranId.
result = spot_client.user_universal_transfer_history_by_tranid("tran_id")Get assets that can be converted into MX.
result = spot_client.get_assets_convert_into_mx()Dust transfer (convert small amounts to MX).
result = spot_client.dust_transfer(["BTC", "ETH"])Get dust transfer log.
result = spot_client.dustlog()Internal transfer.
result = spot_client.internal_transfer("EMAIL", "[email protected]", "USDT", 100.0)Get internal transfer history.
result = spot_client.internal_transfer_history()Create a listen key for user data stream.
result = spot_client.create_listen_key()Get valid listen keys.
result = spot_client.get_listen_keys()Keep-alive a listen key.
result = spot_client.keep_alive_listen_key("listen_key")Close a listen key.
result = spot_client.close_listen_key("listen_key")Get rebate history records.
result = spot_client.get_rebate_history_records()Get rebate records detail.
result = spot_client.get_rebate_records_detail()Get self rebate records detail.
result = spot_client.get_self_rebate_records_detail()Query refer code.
result = spot_client.query_refercode()affiliate_commission_record(start_time=None, end_time=None, invite_code=None, page=None, page_size=None)
Get affiliate commission record (affiliate only).
result = spot_client.affiliate_commission_record()affiliate_withdraw_record(start_time=None, end_time=None, invite_code=None, page=None, page_size=None)
Get affiliate withdraw record (affiliate only).
result = spot_client.affiliate_withdraw_record()affiliate_commission_detail_record(start_time=None, end_time=None, invite_code=None, page=None, page_size=None, type=None)
Get affiliate commission detail record (affiliate only).
result = spot_client.affiliate_commission_detail_record()Get affiliate campaign data (affiliate only).
result = spot_client.affiliate_campaign()affiliate_referral(start_time=None, end_time=None, uid=None, invite_code=None, page=None, page_size=None)
Get affiliate referral data (affiliate only).
result = spot_client.affiliate_referral()affiliate_subaffiliates(start_time=None, end_time=None, invite_code=None, page=None, page_size=None)
Get subaffiliates data (affiliate only).
result = spot_client.affiliate_subaffiliates()Subscribe to trade streams.
def handle_deals(message):
print(message)
ws_spot_client.deals_stream(handle_deals, "BTCUSDT")Subscribe to kline streams.
def handle_kline(message):
print(message)
ws_spot_client.kline_stream(handle_kline, "BTCUSDT", "Min1")Subscribe to diff depth stream.
def handle_depth(message):
print(message)
ws_spot_client.depth_stream(handle_depth, "BTCUSDT", "100ms")Subscribe to partial book depth streams.
def handle_limit_depth(message):
print(message)
ws_spot_client.limit_depth_stream(handle_limit_depth, "BTCUSDT", 10)Subscribe to individual symbol book ticker streams.
def handle_book_ticker(message):
print(message)
ws_spot_client.book_ticker_stream(handle_book_ticker, "BTCUSDT")Subscribe to batch book ticker streams.
def handle_batch_ticker(message):
print(message)
ws_spot_client.book_ticker_batch_stream(handle_batch_ticker, ["BTCUSDT", "ETHUSDT"])Subscribe to mini tickers of all trading pairs.
def handle_mini_tickers(message):
print(message)
ws_spot_client.mini_tickers_stream(handle_mini_tickers)Subscribe to mini ticker of specified trading pair.
def handle_mini_ticker(message):
print(message)
ws_spot_client.mini_ticker_stream(handle_mini_ticker, "BTCUSDT")Subscribe to spot account updates.
def handle_account_update(message):
print(message)
ws_spot_client.account_update(handle_account_update)Subscribe to spot account deals.
def handle_account_deals(message):
print(message)
ws_spot_client.account_deals(handle_account_deals)Subscribe to spot account orders.
def handle_account_orders(message):
print(message)
ws_spot_client.account_orders(handle_account_orders)Get server time.
result = futures_client.ping()Get contract info.
result = futures_client.detail("BTC_USDT")Get transferable currencies.
result = futures_client.support_currencies()Get contract order book depth.
result = futures_client.get_depth("BTC_USDT", limit=100)Get the last N depth snapshots.
result = futures_client.depth_commits("BTC_USDT", 5)Get index price.
result = futures_client.index_price("BTC_USDT")Get fair price.
result = futures_client.fair_price("BTC_USDT")Get funding rate.
result = futures_client.funding_rate("BTC_USDT")Get candlestick data.
result = futures_client.kline("BTC_USDT", interval="Min1")Get index price candles.
result = futures_client.kline_index_price("BTC_USDT", interval="Min1")Get fair price candles.
result = futures_client.kline_fair_price("BTC_USDT", interval="Min1")Get recent trades.
result = futures_client.deals("BTC_USDT", limit=100)Get ticker (contract market data).
result = futures_client.ticker("BTC_USDT")Get insurance fund balance.
result = futures_client.risk_reverse("BTC_USDT")Get insurance fund balance history.
result = futures_client.risk_reverse_history("BTC_USDT")Get funding rate history.
result = futures_client.funding_rate_history("BTC_USDT")Get all account assets.
result = futures_client.assets()Get single currency asset information.
result = futures_client.asset("USDT")Get asset transfer records.
result = futures_client.transfer_record()Get historical positions.
result = futures_client.history_positions()Get open positions.
result = futures_client.open_positions()funding_records(position_type, start_time, end_time, symbol=None, position_id=None, page_num=1, page_size=20)
Get funding fee details.
result = futures_client.funding_records(1, start_time, end_time)Get current open orders.
result = futures_client.open_orders()history_orders(symbol=None, states=None, category=None, start_time=None, end_time=None, side=None, order_id=None, page_num=1, page_size=20)
Get all historical orders.
result = futures_client.history_orders()Get order by external ID.
result = futures_client.get_order_external("BTC_USDT", "ext_123")Get order information by order ID.
result = futures_client.get_order(123456)Batch query orders by order ID.
result = futures_client.batch_query([123456, 123457])Get trade details by order ID.
result = futures_client.deal_details("BTC_USDT", 123456)Get historical order deal details.
result = futures_client.order_deals("BTC_USDT")get_trigger_orders(symbol=None, states=None, side=None, start_time=None, end_time=None, page_num=1, page_size=20)
Get plan order list.
result = futures_client.get_trigger_orders()get_stop_limit_orders(symbol=None, is_finished=None, state=None, type=None, start_time=None, end_time=None, page_num=1, page_size=20)
Get take-profit/stop-loss order list.
result = futures_client.get_stop_limit_orders()Get risk limits.
result = futures_client.risk_limit()Get fee details.
result = futures_client.tiered_fee_rate()Modify position margin (Under Maintenance).
result = futures_client.change_margin(123, 100.0, "ADD")Get position leverage multipliers.
result = futures_client.get_leverage("BTC_USDT")change_leverage(leverage, position_id=None, open_type=None, symbol=None, position_type=None, leverage_mode=None, margin_selected=None, leverage_selected=None)
Modify leverage (Under Maintenance).
result = futures_client.change_leverage(10, symbol="BTC_USDT")Get user position mode.
result = futures_client.get_position_mode()Modify user position mode (Under Maintenance).
result = futures_client.change_position_mode(1)Place order (Under Maintenance).
result = futures_client.order(
symbol="BTC_USDT",
price=50000,
vol=0.001,
side=1,
type=1,
open_type=1,
leverage=10
)Bulk order (Under Maintenance).
result = futures_client.bulk_order(
symbol="BTC_USDT",
price=50000,
vol=0.001,
side=1,
type=1,
open_type=1
)Cancel orders (Under Maintenance).
result = futures_client.cancel_order([123456, 123457])Cancel by external order ID (Under Maintenance).
result = futures_client.cancel_order_with_external([
{"symbol": "BTC_USDT", "externalOid": "ext_123"}
])Cancel all orders under a contract (Under Maintenance).
result = futures_client.cancel_all("BTC_USDT")trigger_order(symbol, vol, side, open_type, trigger_price, trigger_type, execute_cycle, order_type, trend, leverage, ...)
Place plan order (Under Maintenance).
result = futures_client.trigger_order(
symbol="BTC_USDT",
vol=0.001,
side=1,
open_type=1,
trigger_price=50000,
trigger_type=1,
execute_cycle=1,
order_type=1,
trend=1,
leverage=10
)Cancel planned orders (Under Maintenance).
result = futures_client.cancel_trigger_order([
{"symbol": "BTC_USDT", "orderId": 123456}
])Cancel all planned orders (Under Maintenance).
result = futures_client.cancel_all_trigger_orders("BTC_USDT")Cancel TP/SL planned orders (Under Maintenance).
result = futures_client.cancel_stop_order([
{"stopPlanOrderId": 123456}
])Cancel all TP/SL orders (Under Maintenance).
result = futures_client.cancel_all_stop_orders("BTC_USDT")place_stop_order(symbol, vol, side, open_type, trigger_price, trigger_type, order_type, trend, position_id, ...)
Place TP/SL order (Under Maintenance).
result = futures_client.place_stop_order(
symbol="BTC_USDT",
vol=0.001,
side=1,
open_type=1,
trigger_price=50000,
trigger_type=1,
order_type=1,
trend=1,
position_id=123
)query_universal_transfer_history(from_account_type, to_account_type, from_account=None, to_account=None, start_time=None, end_time=None, page=1, limit=500)
Query Universal Transfer History - broker user.
result = broker_client.query_universal_transfer_history("SPOT", "FUTURES")Create a Sub-account.
result = broker_client.create_sub_account("sub_account_name", "note")Query Sub-account List.
result = broker_client.query_sub_account_list()Query Sub-account Status.
result = broker_client.query_sub_account_status("sub_account_name")Create an APIKey for a Sub-account.
result = broker_client.create_sub_account_api_key(
"sub_account_name",
["SPOT_ACCOUNT_READ"],
"note"
)Query the APIKey of a Sub-account.
result = broker_client.query_sub_account_api_key("sub_account_name")Delete the APIKey of a Sub-account.
result = broker_client.delete_sub_account_api_key("sub_account_name", "api_key")Generate Deposit Address of Sub-account.
result = broker_client.generate_deposit_address("USDT", "TRC20")Deposit Address of Sub-account.
result = broker_client.deposit_address("USDT")query_sub_account_deposit_history(coin=None, status=None, start_time=None, end_time=None, limit=20, page=1)
Query Sub-account Deposit History.
result = broker_client.query_sub_account_deposit_history()query_all_sub_account_deposit_history(coin=None, status=None, start_time=None, end_time=None, limit=100, page=1)
Query All Sub-account Deposit History (Recent 3 days).
result = broker_client.query_all_sub_account_deposit_history()Withdraw.
result = broker_client.withdraw("USDT", "TRC20", "address", 100.0)universal_transfer(from_account_type, to_account_type, asset, amount, from_account=None, to_account=None)
Universal Transfer.
result = broker_client.universal_transfer("SPOT", "FUTURES", "USDT", 100.0)Enable Futures for Sub-account.
result = broker_client.enable_futures_for_sub_account("sub_account_name")Get Broker Rebate History Records.
result = broker_client.get_broker_rebate_history_records()This library is licensed under the MIT License.