查询账户余额 - fetchBalance
要查询账户余额,获取可用于交易的资金数量,或者锁定在委托单中的资金数量,
可以使用fetchBalance
方法。
fetchBalance (params = {})
方法返回的余额结构如下:
{
'info': { ... }, // the original untouched non-parsed reply with details
//-------------------------------------------------------------------------
// indexed by availability of funds first, then by currency
'free': { // money, available for trading, by currency
'BTC': 321.00, // floats...
'USD': 123.00,
...
},
'used': { ... }, // money on hold, locked, frozen, or pending, by currency
'total': { ... }, // total (free + used), by currency
//-------------------------------------------------------------------------
// indexed by currency first, then by availability of funds
'BTC': { // string, three-letter currency code, uppercase
'free': 321.00 // float, money available for trading
'used': 234.00, // float, money on hold, locked, frozen or pending
'total': 555.00, // float, total balance (free + used)
},
'USD': { // ...
'free': 123.00 // ...
'used': 456.00,
'total': 579.00,
},
...
}
有些交易所可能不会返回完整的余额信息。许多交易所不会返回你的空账户或者未用的账户, 这种情况下在返回的余额结构中可能会缺少某些货币的信息。
JavaScript示例代码:
(async () => {
console.log (await exchange.fetchBalance ())
}) ()
Python示例代码:
print (exchange.fetch_balance ())
PHP示例代码:
var_dump ($exchange->fetch_balance ());
余额信息的推导
有些交易所的API不会返回余额信息的完整集合,它们只会返回可用余额或者只是资金总量。 这种情况下ccxt会尝试从委托单缓存中获取缺失的数据,并根据已知的信息猜测完整的余额信息。 但是这在一些极端情况下可能不足以推导出正确的余额信息,开发者应当了解这种可能性。