交易符号和市场ID
市场ID用于在REST请求-响应过程中引用交易所内的交易对。每个交易所 都有不同的市场ID集,因此不可以跨交易所使用市场ID。例如,BTC/USD 交易对在不同的交易所中可能有不同的ID:btcusd、 BTCUSD、XBTUSD、btc/usd、 42 (数字ID)、 BTC/USD、 Btc/Usd、 tBTCUSD、 XXBTZUSD等。你不需要 记住或使用市场ID,他们的作用是在交易所模型实现的内部用于HTTP的请求 -响应目的。
CCXT库将不通用的市场ID抽象为标准化的交易符号。交易符号不同于市场ID。 每个市场都采用一个对应的符号来引用,交易符号可以跨交易所使用,这使得 交易符号更适用于跨交易所套利等其他很多应用。
交易符号通常是描述一对交易货币的大写字符串常量,以斜杠间隔两个货币代码。 货币代码是3~4位大写字母,例如 BTC, ETH, USD, GBP, CNY, LTC, JPY, DOGE, RUB, ZEC, XRP, XMR, 等等。有些交易所也有长一些的富有异国风情的货币名称。 在斜杠之前的货币被称为基础货币,之后的被称为报价货币。下面是一些符号的 示例: BTC/USD, DOGE/LTC, ETH/EUR, DASH/XRP, BTC/CNY, ZEC/XMR, ETH/JPY。
有时用户可能会注意到像'XBTM18' 或'.XRPUSDM20180101' 或r "exotic/rare symbols" 之类的交易符号。交易符号并不是一定要有斜杠或者包含货币对的代码。符号字符串 完全取决于市场类型(它是一个现货市场、期货市场、暗池市场或过期市场等等)。 CCXT不鼓励你解析交易符号字符串,你不应该依赖于交易符号的格式,CCXT推荐你 使用市场属性来达成你的应用需求。
市场结构使用符号和ID为键。交易所基类也有内置的方法可以按符号访问市场对象。大多数 API方法需要传入交易符号作为第一个参数。当查询当前价格或委托下单时,也常常 需要你指定一个交易符号。
大多数时候,CCXT用户都是与市场交易符号打交道。如果你访问字典中不存在的 键,就会收获一个异常。
JavaScript示例代码:
(async () => {
console.log (await exchange.loadMarkets ())
let btcusd1 = exchange.markets['BTC/USD'] // get market structure by symbol
let btcusd2 = exchange.market ('BTC/USD') // same result in a slightly different way
let btcusdId = exchange.marketId ('BTC/USD') // get market id by symbol
let symbols = exchange.symbols // get an array of symbols
let symbols2 = Object.keys (exchange.markets) // same as previous line
console.log (exchange.id, symbols) // print all symbols
let currencies = exchange.currencies // a list of currencies
let bitfinex = new ccxt.bitfinex ()
await bitfinex.loadMarkets ()
bitfinex.markets['BTC/USD'] // symbol → market (get market by symbol)
bitfinex.markets_by_id['XRPBTC'] // id → market (get market by id)
bitfinex.markets['BTC/USD']['id'] // symbol → id (get id by symbol)
bitfinex.markets_by_id['XRPBTC']['symbol'] // id → symbol (get symbol by id)
})
Python示例代码:
print (exchange.load_markets ())
etheur1 = exchange.markets['ETH/EUR'] # get market structure by symbol
etheur2 = exchange.market ('ETH/EUR') # same result in a slightly different way
etheurId = exchange.market_id ('BTC/USD') # get market id by symbol
symbols = exchange.symbols # get a list of symbols
symbols2 = list (exchange.markets.keys ()) # same as previous line
print (exchange.id, symbols) # print all symbols
currencies = exchange.currencies # a list of currencies
kraken = ccxt.kraken ()
kraken.load_markets ()
kraken.markets['BTC/USD'] # symbol → market (get market by symbol)
kraken.markets_by_id['XXRPZUSD'] # id → market (get market by id)
kraken.markets['BTC/USD']['id'] # symbol → id (get id by symbol)
kraken.markets_by_id['XXRPZUSD']['symbol'] # id → symbol (get symbol by id)
PHP示例代码:
$var_dump ($exchange->load_markets ());
$dashcny1 = $exchange->markets['DASH/CNY']; // get market structure by symbol
$dashcny2 = $exchange->market ('DASH/CNY'); // same result in a slightly different way
$dashcnyId = $exchange->market_id ('DASH/CNY'); // get market id by symbol
$symbols = $exchange->symbols; // get an array of symbols
$symbols2 = array_keys ($exchange->markets); // same as previous line
var_dump ($exchange->id, $symbols); // print all symbols
$currencies = $exchange->currencies; // a list of currencies
$okcoinusd = '\\ccxt\\okcoinusd';
$okcoinusd = new $okcoinusd ();
$okcoinusd->load_markets ();
$okcoinusd->markets['BTC/USD']; // symbol → market (get market by symbol)
$okcoinusd->markets_by_id['btc_usd']; // id → market (get market by id)
$okcoinusd->markets['BTC/USD']['id']; // symbol → id (get id by symbol)
$okcoinusd->markets_by_id['btc_usd']['symbol']; // id → symbol (get symbol by id)