CCXT — Cryptocurrency Exchange Trading
You have access to the ccxt CLI tool which lets you interact with 100+ cryptocurrency exchanges (Binance, Bybit, OKX, Kraken, Coinbase, and many more). You can fetch market data, place orders, check balances, and stream live data.
Core Syntax
CODEBLOCK0
Before Calling Any Method
If you're unsure about the required arguments for a method, run:
CODEBLOCK1
This will show you the required and optional arguments with descriptions.
Available Options
| Flag | Purpose |
|---|
| INLINECODE1 | Show raw request/response data |
| INLINECODE2 |
Use testnet/sandbox environment |
|
--raw | Output clean JSON without formatting |
|
--swap | Target swap/perpetuals account |
|
--future | Target futures account |
|
--spot | Target spot account |
|
--option | Target options account |
|
--param key=value | Pass extra exchange-specific params (repeatable) |
|
--no-keys | Skip API key loading |
Common Operations
Market Data (Public — No API Keys Required)
Fetch markets (list all trading pairs on an exchange):
CODEBLOCK2
Fetch a single ticker:
CODEBLOCK3
Fetch multiple tickers:
CODEBLOCK4
Fetch order book:
CODEBLOCK5
Fetch OHLCV candles:
CODEBLOCK6
Fetch recent trades:
CODEBLOCK7
Fetch exchange status:
CODEBLOCK8
Fetch currencies:
CODEBLOCK9
Trading (Private — Requires API Keys)
Create an order:
CODEBLOCK10
Create order with extra params:
CODEBLOCK11
Cancel an order:
CODEBLOCK12
Fetch open orders:
CODEBLOCK13
Fetch closed orders:
CODEBLOCK14
Fetch a specific order:
CODEBLOCK15
Account (Private — Requires API Keys)
Fetch balance:
CODEBLOCK16
Fetch balance for derivatives:
CODEBLOCK17
Fetch my trades:
CODEBLOCK18
Fetch positions (derivatives):
CODEBLOCK19
Fetch deposit address:
CODEBLOCK20
Derivatives
Fetch funding rate:
CODEBLOCK21
Fetch funding rate history:
CODEBLOCK22
Fetch mark price / index price:
CODEBLOCK23
Important Rules
- 1. Always quote symbols that contain
/ or : — e.g., "BTC/USDT", "BTC/USDT:USDT". - Use
undefined as a positional placeholder to skip optional arguments while providing later ones. For example: ccxt binance fetchOHLCV "BTC/USDT" 1h undefined 10 skips since but provides limit. - Use
--raw when you need to parse the output programmatically or when the user needs clean JSON. - Use
--sandbox for testing with testnet environments. Always recommend sandbox mode when the user is experimenting with orders. - ISO8601 datetimes (e.g.,
"2025-01-01T00:00:00Z") are auto-converted to milliseconds. - API keys must be configured via environment variables (e.g.,
BINANCE_APIKEY, BINANCE_SECRET) or the config file. If a private method fails due to missing credentials, instruct the user to set them up. - Derivatives symbols use the format
"BASE/QUOTE:SETTLE" — e.g., "BTC/USDT:USDT" for USDT-margined perpetuals. - Be careful with order methods — always confirm amounts and prices with the user before executing
createOrder. The CLI executes immediately with no confirmation prompt. - When the output is large (e.g.,
fetchMarkets returns hundreds of entries), consider piping through | head or filtering, or suggest the user narrows their query. - For the list of supported exchanges, you can check:
ccxt exchanges or refer to https://docs.ccxt.com.
Authentication Setup
Tell users to configure credentials in one of two ways:
Option 1 — Environment variables:
CODEBLOCK24
Option 2 — Config file (path shown in ccxt --help):
CODEBLOCK25
Error Handling
- - If you get an
AuthenticationError, the API keys are missing or invalid. - If you get an
ExchangeNotAvailable or NetworkError, the exchange may be down or rate-limiting. - If you get an
BadSymbol, the trading pair doesn't exist on that exchange — use fetchMarkets to check available pairs. - If you get an
InsufficientFunds, the account doesn't have enough balance for the operation.
CCXT — 加密货币交易所交易
你可以使用 ccxt CLI 工具与 100 多个加密货币交易所(Binance、Bybit、OKX、Kraken、Coinbase 等)进行交互。你可以获取市场数据、下单、查询余额以及实时流式传输数据。
核心语法
bash
ccxt <交易所ID> <方法名> [参数...] [选项]
调用任何方法之前
如果不确定某个方法所需的参数,请运行:
bash
ccxt explain <方法名>
这将显示必需的参数和可选参数及其说明。
可用选项
| 标志 | 用途 |
|---|
| --verbose | 显示原始请求/响应数据 |
| --sandbox |
使用测试网/沙箱环境 |
| --raw | 输出干净的 JSON,不进行格式化 |
| --swap | 针对永续合约账户 |
| --future | 针对期货账户 |
| --spot | 针对现货账户 |
| --option | 针对期权账户 |
| --param key=value | 传递额外的交易所特定参数(可重复) |
| --no-keys | 跳过 API 密钥加载 |
常见操作
市场数据(公开 — 无需 API 密钥)
获取市场(列出交易所所有交易对):
bash
ccxt <交易所> fetchMarkets --raw
获取单个行情:
bash
ccxt <交易所> fetchTicker BTC/USDT --raw
获取多个行情:
bash
ccxt <交易所> fetchTickers --raw
获取订单簿:
bash
ccxt <交易所> fetchOrderBook BTC/USDT --raw
获取 OHLCV 蜡烛图:
bash
ccxt <交易所> fetchOHLCV BTC/USDT 1h undefined 10 --raw
获取近期交易:
bash
ccxt <交易所> fetchTrades BTC/USDT --raw
获取交易所状态:
bash
ccxt <交易所> fetchStatus --raw
获取货币信息:
bash
ccxt <交易所> fetchCurrencies --raw
交易(私有 — 需要 API 密钥)
创建订单:
bash
ccxt <交易所> createOrder BTC/USDT limit buy 0.001 50000 --raw
ccxt <交易所> createOrder BTC/USDT market buy 0.001 --raw
使用额外参数创建订单:
bash
ccxt <交易所> createOrder BTC/USDT limit buy 0.001 50000 --param stopPrice=49000 --raw
取消订单:
bash
ccxt <交易所> cancelOrder <订单ID> BTC/USDT --raw
获取未成交订单:
bash
ccxt <交易所> fetchOpenOrders BTC/USDT --raw
获取已成交订单:
bash
ccxt <交易所> fetchClosedOrders BTC/USDT --raw
获取特定订单:
bash
ccxt <交易所> fetchOrder <订单ID> BTC/USDT --raw
账户(私有 — 需要 API 密钥)
获取余额:
bash
ccxt <交易所> fetchBalance --raw
获取衍生品余额:
bash
ccxt <交易所> fetchBalance --swap --raw
获取我的交易:
bash
ccxt <交易所> fetchMyTrades BTC/USDT --raw
获取持仓(衍生品):
bash
ccxt <交易所> fetchPositions --swap --raw
获取充值地址:
bash
ccxt <交易所> fetchDepositAddress BTC --raw
衍生品
获取资金费率:
bash
ccxt <交易所> fetchFundingRate BTC/USDT:USDT --raw
获取资金费率历史:
bash
ccxt <交易所> fetchFundingRateHistory BTC/USDT:USDT --raw
获取标记价格/指数价格:
bash
ccxt <交易所> fetchMarkOHLCV BTC/USDT:USDT 1h --raw
ccxt <交易所> fetchIndexOHLCV BTC/USDT:USDT 1h --raw
重要规则
- 1. 始终对包含 / 或 : 的交易对符号加引号 — 例如 BTC/USDT、BTC/USDT:USDT。
- 使用 undefined 作为位置占位符,在提供后续参数时跳过可选参数。例如:ccxt binance fetchOHLCV BTC/USDT 1h undefined 10 跳过了 since 但提供了 limit。
- 使用 --raw 当需要以编程方式解析输出或用户需要干净的 JSON 时。
- 使用 --sandbox 在测试网环境中进行测试。当用户尝试下单时,始终推荐沙箱模式。
- ISO8601 日期时间(例如 2025-01-01T00:00:00Z)会自动转换为毫秒。
- API 密钥必须通过环境变量(例如 BINANCEAPIKEY、BINANCESECRET)或配置文件进行配置。如果私有方法因缺少凭据而失败,请指导用户进行设置。
- 衍生品交易对符号使用 BASE/QUOTE:SETTLE 格式 — 例如 USDT 保证金永续合约为 BTC/USDT:USDT。
- 谨慎处理订单方法 — 在执行 createOrder 之前,始终与用户确认数量和价格。CLI 会立即执行,没有确认提示。
- 当输出内容较多时(例如 fetchMarkets 返回数百条记录),考虑通过 | head 管道过滤,或建议用户缩小查询范围。
- 如需查看支持的交易所列表,可以运行:ccxt exchanges 或参考 https://docs.ccxt.com。
身份验证设置
告知用户通过以下两种方式之一配置凭据:
选项 1 — 环境变量:
bash
export BINANCEAPIKEY=yourapi_key
export BINANCESECRET=yoursecret
选项 2 — 配置文件(路径在 ccxt --help 中显示):
json
{
binance: {
apiKey: yourapikey,
secret: your_secret
}
}
错误处理
- - 如果遇到 AuthenticationError,说明 API 密钥缺失或无效。
- 如果遇到 ExchangeNotAvailable 或 NetworkError,说明交易所可能宕机或触发了频率限制。
- 如果遇到 BadSymbol,说明该交易对在该交易所不存在 — 使用 fetchMarkets 检查可用交易对。
- 如果遇到 InsufficientFunds,说明账户余额不足以执行该操作。