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场景编辑器

市价买入委托的特殊情况 - createMarketBuyOrderRequiresPrice

总的说来,当市价委托买入或卖出时,用户只需要指定要买入或卖出的基准货币 的数量。但是然而,有些交易所的市价买入委托单处理采用了不同的方式来计算 委托单价值。

假设你在交易BTC/USD,目前的BTC市场价格是超过9000 USD。要按市价买入或 卖出,你可以指定数量为 2 BTC,取决于你的委托方向,成交结果将是你的账户 增加或减少18000 USD左右。

但是有些交易所要求按报价货币指定委托单的总价!这背后的逻辑其实很简单, 不是说我要买入或卖出多少基准货币,而是”我想消费多少报价货币“。

在这些交易所进行市价买入委托,你不能将委托单的数量指定为 2 BTC,而是 应当指定委托单的总价,在这个例子中,也就是 18000 USD。采用这种方式处理 市价委托单的交易所,有一个选项createMarketBuyOrderRequiresPrice,你 可以用它以两种方式指定市价买入委托单的总花费:

第一种是默认的,如果你同时设置了委托数量和价格,那么在ccxt内部将会 简单地按照这个公式(cost = amount * price)计算出委托单总价格,得到 的总花费就会设置为该市价委托单的报价货币总花费,也就是USD总额。示例 代码如下:

// this example is oversimplified and doesn't show all the code that is
// required to handle the errors and exchange metadata properly
// it shows just the concept of placing a market buy order

const exchange = new ccxt.cex ({
    'apiKey': YOUR_API_KEY,
    'secret': 'YOUR_SECRET',
    'enableRateLimit': true,
    // 'options': {
    //     'createMarketBuyOrderRequiresPrice': true, // default
    // },
})

;(async () => {

    // when `createMarketBuyOrderRequiresPrice` is true, we can pass the price
    // so that the total cost of the order would be calculated inside the library
    // by multiplying the amount over price (amount * price)

    const symbol = 'BTC/USD'
    const amount = 2 // BTC
    const price = 9000 // USD
    // cost = amount * price = 2 * 9000 = 18000 (USD)

    // note that we don't use createMarketBuyOrder here, instead we use createOrder
    // createMarketBuyOrder will omit the price and will not work when
    // exchange.options['createMarketBuyOrderRequiresPrice'] = true
    const order = await exchange.createOrder (symbol, 'market', 'buy', amount, price)

    console.log (order)
})

如果希望自己指定委托单的总花费,那么可以使用第二种方式。这需要先 关闭createMarketBuyOrderRequiresPrice选项,然后进行设置。示例代码 如下:

const exchange = new ccxt.cex ({
    'apiKey': YOUR_API_KEY,
    'secret': 'YOUR_SECRET',
    'enableRateLimit': true,
    'options': {
        'createMarketBuyOrderRequiresPrice': false, // switch off
    },
})

// or, to switch it off later, after the exchange instantiation, you can do
exchange.options['createMarketBuyOrderRequiresPrice'] = false

;(async () => {

    // when `createMarketBuyOrderRequiresPrice` is true, we can pass the price
    // so that the total cost of the order would be calculated inside the library
    // by multiplying the amount over price (amount * price)

    const symbol = 'BTC/USD'
    const amount = 2 // BTC
    const price = 9000 // USD
    cost = amount * price // ← instead of the amount cost goes ↓ here
    const order = await exchange.createMarketBuyOrder (symbol, cost)
    console.log (order)
})

进一步阅读请参考: