CCXT中文开发手册

CCXT开发概述
CCXT概述 支持的交易所 实例化交易所类 设置交易所的属性
CCXT交易所模型
交易所的数据结构 交易所API访问的限流 DDoS保护异常及处理办法
CCXT市场模型
市场数据结构 数据精度和极限值 委托单数值要求和格式化方法 载入市场清单 交易符号和市场ID 符号命名的一致性 符号命名冲突的解决流程 符号命名常见问题及解答 市场缓存强制重载
CCXT API
方法与访问端结点 隐式API方法 公开/私有API 同步调用与异步调用 API方法参数与返回值 API方法命名规范 统一API 改写统一API的参数 统一API结果的分页
CCXT委托账本模型
交易委托账本 委托账本模型的结构 查询市场深度 查询市场价格
CCXT市场行情
实时行情 实时行情数据结构 查询指定交易对实时行情 查询所有交易对实时行情
CCXT烛线图数据
OHLCV烛线图 OHLCV数据结构 OHLCV数据模拟
CCXT数字货币交易
查询交易 - fetchTrade 交易身份验证 API密钥设置 查询账户余额 - fetchBalance 查询委托单 - fetchOrders 查询交易 - fetchTrades 委托单缓存 清理缓存的委托单 - purgeCachedOrders 查询指定ID的委托单 - fetchOrder 查询全部委托单 - fetchOrders 查询全部敞口委托单 - fetchOpenOrders 查询全部已完结委托单 - fetchClosedOrders 委托单数据结构 委托下单 市价委托 - createMarketSellOrder/createMarketBuyOrder 市价买入委托的特殊情况 - createMarketBuyOrderRequiresPrice 用限价单模拟市价单 限价委托 - createLimitBuyOrder/createLimitSellOrder 委托单的自定义参数 其他类型的委托单 取消委托单 - cancelOrder 委托单与交易的关系 查询个人的历史交易 - fetchMyTrade 交易的数据结构 查询指定委托单的交易 获取充值地址 - fetchDepositAddress/createDepositAddress 地址的数据结构 提现 - withdraw 链上交易数据结构 查询充值记录 - fetchDoposits 查询提现记录 - fetchWithdrawals 查询链上交易 - fetchTransactions 查询手续费 - fetchFees 查询交易所状态 - fetchStatus 预算交易费 - calculateFee 资金操作费 - currencies 查询账本 - fetchLedger 账本记录结构 修改Nonce值 - seconds/milliseconds/microseconds
CCXT错误处理
错误处理概述 - try/catch 异常类的体系 交易所异常 网络异常
在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

错误处理概述 - try/catch

ccxt采用各种语言中原生的异常机制进行错误处理。

要处理错误,你需要使用try代码块来保护调用ccxt统一API的代码, 然后使用catch代码块捕捉异常。示例代码如下。

JavaScript:

// try to call a unified method
try {
    const response = await exchange.fetchTicker ('ETH/BTC')
    console.log (response)
} catch (e) {
    // if the exception is thrown, it is "caught" and can be handled here
    // the handling reaction depends on the type of the exception
    // and on the purpose or business logic of your application
    if (e instanceof ccxt.NetworkError) {
        console.log (exchange.id, 'fetchTicker failed due to a network error:', e.message)
        // retry or whatever
        // ...
    } else if (e instanceof ccxt.ExchangeError) {
        console.log (exchange.id, 'fetchTicker failed due to exchange error:', e.message)
        // retry or whatever
        // ...
    } else {
        console.log (exchange.id, 'fetchTicker failed with:', e.message)
        // retry or whatever
        // ...
    }
}

Python:

try:
    response = await exchange.fetch_order_book('ETH/BTC')
    print(response)
except ccxt.NetworkError as e:
    print(exchange.id, 'fetch_order_book failed due to a network error:', str(e))
    # retry or whatever
    # ...
except ccxt.ExchangeError as e:
    print(exchange.id, 'fetch_order_book failed due to exchange error:', str(e))
    # retry or whatever
    # ...
except Exception as e:
    print(exchange.id, 'fetch_order_book failed with:', str(e))
    # retry or whatever
    # ...

PHP:

// try to call a unified method
try {
    $response = $exchange->fetch_trades('ETH/BTC');
    print_r($response);
} catch (\ccxt\NetworkError $e) {
    echo $exchange->id . ' fetch_trades failed due to a network error: ' . $e->getMessage () . "\n";
    // retry or whatever
    // ...
} catch (\ccxt\ExchangeError $e) {
    echo $exchange->id . ' fetch_trades failed due to exchange error: ' . $e->getMessage () . "\n";
    // retry or whatever
    // ...
} catch (Exception $e) {
    echo $exchange->id . ' fetch_trades failed with: ' . $e->getMessage () . "\n";
    // retry or whatever
    // ...
}