A Python SDK for KuCoin cryptocurrency exchange API providing REST endpoints implementation, simple authentication handling, response exception handling, and websocket support for trading, market data, user account management, margin trading, lending, and earning features
—
Complete spot trading functionality for KuCoin exchange including order placement, cancellation, management, and trade history. Supports standard orders, stop orders, high-frequency trading, and OCO (One-Cancels-Other) orders.
Standard limit and market order operations.
def create_limit_order(symbol: str, side: str, size: str, price: str, clientOid: str = '', **kwargs):
"""
Place a limit order.
Args:
symbol (str): Trading symbol (e.g., 'BTC-USDT')
side (str): Order side ('buy' or 'sell')
size (str): Order size (base currency amount)
price (str): Order price
clientOid (str, optional): Client order ID (UUID recommended)
timeInForce (str, optional): Time in force ('GTC', 'GTT', 'IOC', 'FOK')
cancelAfter (int, optional): Cancel after seconds (requires GTT)
postOnly (bool, optional): Post-only flag
hidden (bool, optional): Hidden order flag
iceberg (bool, optional): Iceberg order flag
visibleSize (str, optional): Visible size for iceberg orders
remark (str, optional): Order remark
Returns:
dict: Order response with orderId
"""
def create_market_order(symbol: str, side: str, clientOid: str = '', size: str = None, funds: str = None, **kwargs):
"""
Place a market order.
Args:
symbol (str): Trading symbol
side (str): Order side ('buy' or 'sell')
clientOid (str, optional): Client order ID
size (str, optional): Order size (required for sell orders)
funds (str, optional): Order funds (required for buy orders)
**kwargs: Additional order parameters
Returns:
dict: Order response with orderId
"""
def cancel_order(orderId: str):
"""
Cancel an order by order ID.
Args:
orderId (str): Order ID to cancel
Returns:
dict: Cancellation response with cancelledOrderIds
"""
def cancel_client_order(clientId: str):
"""
Cancel an order by client order ID.
Args:
clientId (str): Client order ID to cancel
Returns:
dict: Cancellation response
"""
def cancel_all_orders(**kwargs):
"""
Cancel all orders.
Args:
symbol (str, optional): Limit to specific symbol
tradeType (str, optional): Trade type filter
Returns:
dict: List of cancelled order IDs
"""
def place_order_test(symbol: str, side: str, type: str, clientOid: str = '', **kwargs):
"""
Test order placement without actually placing the order.
Args:
symbol (str): Trading symbol
side (str): Order side
type (str): Order type ('limit' or 'market')
clientOid (str, optional): Client order ID
**kwargs: Order parameters based on type
Returns:
dict: Test result with order validation
"""Stop-loss and take-profit order functionality.
def create_limit_stop_order(symbol: str, side: str, size: str, price: str, stopPrice: str, clientOid: str = '', **kwargs):
"""
Place a limit stop order.
Args:
symbol (str): Trading symbol
side (str): Order side
size (str): Order size
price (str): Limit price
stopPrice (str): Stop trigger price
clientOid (str, optional): Client order ID
**kwargs: Additional parameters
Returns:
dict: Stop order response
"""
def create_market_stop_order(symbol: str, side: str, stopPrice: str, size: str = '', funds: str = '', clientOid: str = '', **kwargs):
"""
Place a market stop order.
Args:
symbol (str): Trading symbol
side (str): Order side
stopPrice (str): Stop trigger price
size (str, optional): Order size (for sell orders)
funds (str, optional): Order funds (for buy orders)
clientOid (str, optional): Client order ID
Returns:
dict: Stop order response
"""
def cancel_stop_order(orderId: str):
"""
Cancel a stop order.
Args:
orderId (str): Stop order ID
Returns:
dict: Cancellation response
"""
def cancel_client_stop_order(clientOid: str, symbol: str = ''):
"""
Cancel a stop order by client order ID.
Args:
clientOid (str): Client order ID
symbol (str, optional): Trading symbol
Returns:
dict: Cancellation response
"""
def get_all_stop_order_details(**kwargs):
"""
Get all stop order details.
Args:
symbol (str, optional): Filter by symbol
side (str, optional): Filter by side
type (str, optional): Filter by order type
**kwargs: Additional filters
Returns:
dict: Stop order list with pagination
"""Specialized high-frequency trading operations with enhanced performance.
def create_limit_hf_order(symbol: str, side: str, size: str, price: str, clientOid: str = '', **kwargs):
"""
Place a high-frequency limit order.
Args:
symbol (str): Trading symbol
side (str): Order side
size (str): Order size
price (str): Order price
clientOid (str, optional): Client order ID
**kwargs: Additional HF order parameters
Returns:
dict: HF order response
"""
def sync_create_limit_hf_order(symbol: str, side: str, size: str, price: str, clientOid: str = '', **kwargs):
"""
Place a synchronous high-frequency limit order.
Args:
symbol (str): Trading symbol
side (str): Order side
size (str): Order size
price (str): Order price
clientOid (str, optional): Client order ID
Returns:
dict: Synchronous order response with execution details
"""
def cancel_all_hf_orders(symbol: str):
"""
Cancel all high-frequency orders for a symbol.
Args:
symbol (str): Trading symbol
Returns:
str: Success message
"""
def get_active_hf_orders(symbol: str):
"""
Get active high-frequency orders.
Args:
symbol (str): Trading symbol
Returns:
list: Active HF orders
"""One-Cancels-Other order functionality for advanced trading strategies.
def create_oco_order(symbol: str, side: str, price: str, stopPrice: str, size: str, limitPrice: str, clientOid: str = '', remark: str = None):
"""
Place an OCO (One-Cancels-Other) order.
Args:
symbol (str): Trading symbol
side (str): Order side
price (str): Main order price
stopPrice (str): Stop order trigger price
size (str): Order size
limitPrice (str): Stop order limit price
clientOid (str, optional): Client order ID
remark (str, optional): Order remark
Returns:
dict: OCO order response
"""
def cancel_oco_order(orderId: str):
"""
Cancel an OCO order by order ID.
Args:
orderId (str): OCO order ID
Returns:
dict: List of cancelled order IDs
"""
def get_oco_orders(pageSize: int, currentPage: int, symbol: str = None, **kwargs):
"""
Get OCO order list.
Args:
pageSize (int): Page size (10-500)
currentPage (int): Page number (min 1)
symbol (str, optional): Filter by symbol
startAt (int, optional): Start time
endAt (int, optional): End time
orderIds (str, optional): Comma-separated order IDs
Returns:
dict: Paginated OCO orders
"""Retrieve order details and trading history.
def get_order_list(**kwargs):
"""
Get order list with filtering.
Args:
status (str, optional): Order status ('active', 'done')
symbol (str, optional): Trading symbol
side (str, optional): Order side
type (str, optional): Order type
tradeType (str, optional): Trade type
startAt (int, optional): Start time
endAt (int, optional): End time
currentPage (int, optional): Page number
pageSize (int, optional): Page size
Returns:
dict: Paginated order list
"""
def get_order_details(orderId: str):
"""
Get detailed information for a specific order.
Args:
orderId (str): Order ID
Returns:
dict: Complete order information
"""
def get_recent_orders():
"""
Get recent orders (last 24 hours).
Returns:
dict: Recent orders list
"""Access to fill history and trade records.
def get_fill_list(tradeType: str, **kwargs):
"""
Get fill (trade execution) history.
Args:
tradeType (str): Trade type ('TRADE', 'MARGIN_TRADE')
orderId (str, optional): Filter by order ID
symbol (str, optional): Filter by symbol
side (str, optional): Filter by side
type (str, optional): Filter by order type
startAt (int, optional): Start time
endAt (int, optional): End time
currentPage (int, optional): Page number
pageSize (int, optional): Page size
Returns:
dict: Paginated fill history
"""
def get_recent_fills():
"""
Get recent fills (last 24 hours).
Returns:
list: Recent fill records
"""Efficient bulk order operations.
def create_bulk_orders(symbol: str, orderList: list):
"""
Place multiple orders in a single request.
Args:
symbol (str): Trading symbol
orderList (list): List of order objects
Returns:
dict: Bulk order response with individual results
"""
def multi_create_hf_order(orderList: list):
"""
Place multiple high-frequency orders.
Args:
orderList (list): List of HF order objects
Returns:
list: Individual order responses
"""from kucoin.client import Trade
# Initialize trading client with authentication
trade = Trade(api_key, api_secret, api_passphrase, is_sandbox=False)
# Place a limit buy order
order = trade.create_limit_order(
symbol='BTC-USDT',
side='buy',
size='0.001',
price='30000',
timeInForce='GTC'
)
print(f"Order placed: {order['orderId']}")
# Check order status
order_details = trade.get_order_details(order['orderId'])
print(f"Order status: {order_details['isActive']}")
# Cancel the order if still active
if order_details['isActive']:
cancel_result = trade.cancel_order(order['orderId'])
print(f"Order cancelled: {cancel_result}")# Place a market buy order using funds
market_order = trade.create_market_order(
symbol='BTC-USDT',
side='buy',
funds='100' # $100 worth of BTC
)
# Place a market sell order using size
market_sell = trade.create_market_order(
symbol='BTC-USDT',
side='sell',
size='0.001' # Sell 0.001 BTC
)# Place a stop-loss order
stop_loss = trade.create_market_stop_order(
symbol='BTC-USDT',
side='sell',
stopPrice='29000', # Trigger when price hits $29,000
size='0.001'
)
# Place a take-profit limit order
take_profit = trade.create_limit_stop_order(
symbol='BTC-USDT',
side='sell',
size='0.001',
price='35000', # Sell at $35,000
stopPrice='34900' # Trigger when price hits $34,900
)# Place an OCO order (take-profit and stop-loss)
oco_order = trade.create_oco_order(
symbol='BTC-USDT',
side='sell',
price='35000', # Take-profit price
stopPrice='29000', # Stop-loss trigger
size='0.001',
limitPrice='28900' # Stop-loss limit price
)OrderResponse = dict
# {
# "orderId": str # Unique order identifier
# }
OrderInfo = dict
# {
# "id": str, # Order ID
# "symbol": str, # Trading symbol
# "type": str, # Order type
# "side": str, # Order side
# "price": str, # Order price
# "size": str, # Order size
# "funds": str, # Order funds
# "dealFunds": str, # Executed funds
# "dealSize": str, # Executed size
# "fee": str, # Trading fee
# "feeCurrency": str, # Fee currency
# "timeInForce": str, # Time in force
# "postOnly": bool, # Post-only flag
# "hidden": bool, # Hidden order flag
# "iceberg": bool, # Iceberg order flag
# "visibleSize": str, # Visible size
# "cancelAfter": int, # Cancel after seconds
# "channel": str, # Order source
# "clientOid": str, # Client order ID
# "remark": str, # Order remark
# "isActive": bool, # Order active status
# "cancelExist": bool, # Cancel record exists
# "createdAt": int, # Creation timestamp
# "tradeType": str # Trade type
# }
FillInfo = dict
# {
# "symbol": str, # Trading symbol
# "tradeId": str, # Trade ID
# "orderId": str, # Order ID
# "counterOrderId": str, # Counter-party order ID
# "side": str, # Trade side
# "liquidity": str, # Liquidity type ('taker' or 'maker')
# "forceTaker": bool, # Forced taker flag
# "price": str, # Fill price
# "size": str, # Fill size
# "funds": str, # Fill funds
# "fee": str, # Trading fee
# "feeRate": str, # Fee rate
# "feeCurrency": str, # Fee currency
# "stop": str, # Stop type
# "type": str, # Order type
# "createdAt": int, # Fill timestamp
# "tradeType": str # Trade type
# }
BulkOrderItem = dict
# {
# "clientOid": str, # Client order ID
# "side": str, # Order side
# "type": str, # Order type
# "price": str, # Order price (for limit orders)
# "size": str, # Order size
# "timeInForce": str, # Time in force
# "cancelAfter": int, # Cancel after seconds
# "postOnly": bool, # Post-only flag
# "hidden": bool, # Hidden order flag
# "iceberg": bool, # Iceberg order flag
# "visibleSize": str # Visible size for iceberg
# }Install with Tessl CLI
npx tessl i tessl/pypi-kucoin-python