CCXT-06-市场行情

1. 价格行情

价格行情包含了最近一段时间内特定交易市场的统计信息,通常使用24小时进行统计。 查询价格行情的方法如下:

fetchTicker (symbol, params = {})   // for one ticker
fetchTickers (symbol, params = {})  // for all tickers at once

检查交易所的exchange.has['fetchTicker']exchange.has['fetchTickers']属性来决定所查询的交易所是否支持这些方法。

2. 实时行情数据结构

行情的数据结构如下:

{
    'symbol':        string symbol of the market ('BTC/USD', 'ETH/BTC', ...)
    'info':        { the original non-modified unparsed reply from exchange API },
    'timestamp':     int (64-bit Unix Timestamp in milliseconds since Epoch 1 Jan 1970)
    'datetime':      ISO8601 datetime string with milliseconds
    'high':          float, // highest price
    'low':           float, // lowest price
    'bid':           float, // current best bid (buy) price
    'bidVolume':     float, // current best bid (buy) amount (may be missing or undefined)
    'ask':           float, // current best ask (sell) price
    'askVolume':     float, // current best ask (sell) amount (may be missing or undefined)
    'vwap':          float, // volume weighed average price
    'open':          float, // opening price
    'close':         float, // price of last trade (closing price for current period)
    'last':          float, // same as `close`, duplicated for convenience
    'previousClose': float, // closing price for the previous period
    'change':        float, // absolute change, `last - open`
    'percentage':    float, // relative change, `(change/open) * 100`
    'average':       float, // average price, `(last + open) / 2`
    'baseVolume':    float, // volume of base currency traded for last 24 hours
    'quoteVolume':   float, // volume of quote currency traded for last 24 hours
}
``

注意:

  • bidVolume指的是委托账本中当前的最优买入价委托单的总量
  • askVolume指的是委托账本中当前的最优卖出价委托单的总量
  • baseVolume指的是过去24小时内基准货币的交易量(买入或卖出)
  • quoteVolume指的是过去24小时内报价货币的交易量(买入或卖出)

行情结构中的所有价格都是以报价货币计量,其中某些字段可能是undefined / None / null。

时间戳和日期都是以毫秒为单位的UTC时间值:

虽然有些交易所在其行情数据中混入了委托账本的最高买入/最低卖出价格,你不应当将行情数据视为fetchOrderBook的替代方法。行情数据的主要目的是提供统计数据,可以将其视为活跃的24小时OHLCV数据。已知的是,交易所不鼓励频繁地调用fetchTicker。如果你需要一个统一的方法去访问bids和asks,你应当使用fetchL[123]OrderBook系列的方法。 要获取历史价格和数量,使用统一API中的fetchOHLCV方法。 获取行情数据的方法如下:

  • fetchTicker (symbol[, params = {}]), symbol必须,params可选
  • fetchTickers ([symbols = undefined[, params = {}]]), 两个参数都是可选的

3. 按交易对查询实时行情

要查询指定交易对/符号的实时行情数据,调用fetchTicker(symbol)方法。 JavaScript示例代码:

if (exchange.has['fetchTicker']) {
    console.log (await (exchange.fetchTicker ('BTC/USD'))) // ticker for BTC/USD
    let symbols = Object.keys (exchange.markets)
    let random = Math.floor (Math.random () * (symbols.length - 1))
    console.log (exchange.fetchTicker (symbols[random])) // ticker for a random symbol
}

Python示例代码:

import random
if (exchange.has['fetchTicker']):
    print(exchange.fetch_ticker('LTC/ZEC')) # ticker for LTC/ZEC
    symbols = list(exchange.markets.keys())
    print(exchange.fetch_ticker(random.choice(symbols))) # ticker for a random symbol

PHP别忘了正确设置时区:

if ($exchange->has['fetchTicker']) {
    var_dump ($exchange->fetch_ticker ('ETH/CNY')); // ticker for ETH/CNY
    $symbols = array_keys ($exchange->markets);
    $random = rand () % count ($symbols);
    var_dump ($exchange->fetch_ticker ($symbols[$random])); // ticker for a random symbol
}

4. 查询所有交易对的实时行情

有些交易所(不是所有)也支持同时查询所有交易对的实时行情。请查阅交易所的文档获取详细信息。你可以在一次调用中获取所有的实时行情。 JavaScript示例代码:

if (exchange.has['fetchTickers']) {
    console.log (await (exchange.fetchTickers ())) // all tickers indexed by their symbols
}

Python示例代码:

if (exchange.has['fetchTickers']):
    print(exchange.fetch_tickers()) # all tickers indexed by their symbols

PHP示例代码:

if ($exchange->has['fetchTickers']) {
    var_dump ($exchange->fetch_tickers ()); // all tickers indexed by their symbols
}

查询所有的实时行情需要更多的流量,另外,也要注意有些交易所对后续的查询会有更严格的限流。还有一些交易所可能会对fetchTickers()的调用有更多的要求,有时你无法查询的原因是API的限制。另外一些交易所可以在URL查询参数中接受一组交易对符号,但是由于URL的长度是有限的,在极端情况下交易所可以有数千个市场 - url的长度无法容纳所有的交易对符号。 JavaScript示例代码:

if (exchange.has['fetchTickers']) {
    console.log (await (exchange.fetchTickers ([ 'ETH/BTC', 'LTC/BTC' ]))) // listed tickers indexed by their symbols
}

Python示例代码:

if (exchange.has['fetchTickers']):
    print(exchange.fetch_tickers(['ETH/BTC', 'LTC/BTC'])) # listed tickers indexed by their symbols

PHP示例代码:

if ($exchange->has['fetchTickers']) {
    var_dump ($exchange->fetch_tickers (array ('ETH/BTC', 'LTC/BTC'))); // listed tickers indexed by their symbols
}

注意在大多数情况下交易对符号列表不是必须的,但是如果你要处理所有可能的情况就需要额外的逻辑处理。 和CCXT统一API中的大多数方法一样,fetchTickers的最后一个参数是params,用来修改发送到交易所的请求参数。 返回结果的结构如下:

{
    'info':    { ... }, // the original JSON response from the exchange as is
    'BTC/USD': { ... }, // a single ticker for BTC/USD
    'ETH/BTC': { ... }, // a ticker for ETH/BTC
    ...
}