ccxt:神器級的數字貨幣萬能api接口
此文檔爲 ccxt 的詳細接口解釋文檔,僅做學習用途。
先簡單介紹下ccxt
ccxt是一個“all-in-one”一站式數字貨幣萬能api接口模塊庫,目前支持120多個數字貨幣交易所,包括幣安,火幣,okex等等這些國內用戶常用的數字貨幣交易所。
下面直接進入正題
1. 自有屬性
export const version: string; // ccxt 的版本號
export const exchanges: string[]; // ccxt 當前支持的所有交易所
2. 數據結構(依賴)
- 基礎貨幣:要買入的貨幣 | 報價貨幣:要賣出的貨幣
如BTC/USDT = 10000 BTC是基礎貨幣,USDT是報價貨幣
1. 基礎變量 數據結構
該部分暫時不詳解,等到介紹交易所數據結構或者使用時再詳細解釋。
export interface MinMax {
max: number;
min: number;
}
export interface Tickers {
info: any; // info爲交易所返回的原始信息(以下都是)
[symbol: string]: Ticker; // 行情信息(詳情見下文)
}
export interface Currency {
id: string;
code: string;
}
export interface Balance {
free: number;
used: number;
total: number;
}
export interface PartialBalances {
[currency: string]: number;
}
export interface Balances {
info: any;
[key: string]: Balance;
}
export interface DepositAddress {
currency: string;
address: string;
status: string;
info: any;
}
export interface Fee {
type: 'taker' | 'maker';
currency: string;
rate: number;
cost: number;
}
export interface WithdrawalResponse {
info: any;
id: string;
}
export interface DepositAddressResponse {
currency: string;
address: string;
info: any;
tag?: string;
}
// timestamp, open, high, low, close, volume
export type OHLCV = [number, number, number, number, number, number];
2. 交易對 數據結構
原版接口:
export interface Market {
[key: string]: any
id: string;
symbol: string;
base: string;
quote: string;
active: boolean;
precision: { amount: number, price: number, cost: number };
limits: { amount: MinMax, price: MinMax, cost?: MinMax };
info: any;
}
接口介紹:
{
'id': ' btcusd', // 在交易中引用的代幣字符串
'symbol': 'BTC/USD', // 一對貨幣的大寫字符串文字
'base': 'BTC', // 大寫字符串。統一的基本貨幣代碼
'quote': 'USD', // 大寫字符串。統一的報價貨幣代碼
'active': true, // 布爾值。 市場的狀態
'precision': { // 小數點後的小數位數(精度)
'price': 8, // 整型。 如果交易所不支持,則可能丟失精度
'amount': 8, // 整型。 如果交易所不支持,則可能丟失精度
'cost': 8, // 整型。 很少交易所支持
},
'limits': { // 下單限制
'amount': {
'min': 0.01, // 訂單數量應該大於這個值
'max': 1000, // 訂單數量應該小於這個值
},
'price': { ... }, // 同上的min/max 表示訂單的最高價和最低價
'cost': { ... }, // 同上的min/max 表示訂單的金額(amount * price)
},
'info': { ... }, // 交易所原始未解析的市場信息
}
3. 訂單 數據結構
export interface Order {
id: string;
datetime: string;
timestamp: number;
lastTradeTimestamp: number;
status: 'open' | 'closed' | 'canceled';
symbol: string;
type: 'market' | 'limit';
side: 'buy' | 'sell';
price: number;
amount: number;
filled: number;
remaining: number;
cost: number;
trades: Trade[];
fee: Fee;
info: {};
}
接口介紹:
{
'id': '12345-67890:09876/54321', // 訂單的唯一標識
'datetime': '2017-08-17 12:42:48.000', // ISO8601 時間戳
'timestamp': 1502962946216, // 時間戳
'lastTradeTimestamp': 1502962956216, // 此訂單上最後交易的時間戳
'status': 'open', // 'open', 'closed', 'canceled' //訂單狀態:分別爲:打開/關閉/取消
'symbol': 'ETH/BTC', // 交易對信息
'type': 'limit', // 'market', 'limit' // 交易方式: 市價交易 現價交易
'side': 'buy', // 'buy', 'sell' // 交易方向: 買入 賣出
'price': 0.06917684, // 基礎貨幣/報價貨幣 的浮動價格
'amount': 1.5, // 下單數量(基於報價貨幣)
'filled': 1.1, // 成交數量(基於報價貨幣)
'remaining': 0.4, // 未成交數量(基於報價貨幣)
'cost': 0.076094524, // 'filled' * 'price' (總花費金額(基於報價貨幣))
'trades': [... ], // 這筆訂單交易執行的列表(可能有多筆交易)
'fee': { // 手續費
'currency': 'BTC', // 手續費計價貨幣(通常是報價)
'cost': 0.0009, // 實際手續費用
'rate': 0.002, // 費率
},
'info': { ... }, // 原始未解析的數據
}
4. 訂單列表 數據結構
export interface OrderBook {
asks: [number, number][];
bids: [number, number][];
datetime: string;
timestamp: number;
nonce: number;
}
接口介紹:
{
'asks': [ // 詢問價格 -> 買入價格 (詢問數組)
[ price, amount ], // 價格 , 數量
[ price, amount ],
...
],
'bids': [ // 投標價格 -> 賣出價格
[ price, amount ], // [ float, float ]
[ price, amount ],
...
],
'timestamp': 1499280391811, // Unix Timestamp in milliseconds (seconds * 1000)
'datetime': '2017-07-05T18:47:14.692Z', // ISO8601 datetime string with milliseconds
}
價格和金額是浮動的。投標數組按價格降序排列。最佳(最高)投標價格是第一個要素,最差(最低)投標價格是最後一個要素。詢問數組按價格升序排序。最佳(最低)要價是第一個要素,最差(最高)要價是最後一個要素。如果在交易所的訂單簿中沒有對應的訂單,則出價/要價數組可以爲空。
交易所可能會返回各個層次的訂單堆棧的詳細信息以供分析。它要麼包含每個訂單的詳細信息,要麼包含稍微少一點的詳細信息,其中訂單按價格和數量進行分組和合並。擁有更多的細節需要更多的流量和帶寬,一般來說速度較慢,但具有更高的精度。擁有更少的細節通常更快,但在某些非常特殊的情況下可能還不夠。
5. 交易信息 數據結構
export interface Trade {
amount: number; // amount of base currency
datetime: string; // ISO8601 datetime with milliseconds;
id: string; // string trade id
info: {}; // the original decoded JSON as is
order?: string; // string order id or undefined/None/null
price: number; // float price in quote currency
timestamp: number; // Unix timestamp in milliseconds
type?: 'market' | 'limit'; // order type, 'market', 'limit' or undefined/None/null
side: 'buy' | 'sell'; // direction of the trade, 'buy' or 'sell'
symbol: string; // symbol in CCXT format
takerOrMaker: 'taker' | 'maker'; // string, 'taker' or 'maker'
cost: number; // total cost (including fees), `price * amount`
fee: Fee;
}
接口介紹:
{
'info': { ... }, // 原始的JSON返回信息
'id': '12345-67890:09876/54321', // 交易ID
'timestamp': 1502962946216, // 時間戳
'datetime': '2017-08-17 12:42:48.000', // ISO8601 時間
'symbol': 'ETH/BTC', // 交易對
'order': '12345-67890:09876/54321', // string/None/None/undefined
'type': 'limit', // 訂單類型, 'market', 'limit' or undefined/None/null
'side': 'buy', // 交易方向, 'buy' or 'sell'
'takerOrMaker': 'taker', // string, 'taker' or 'maker' 訂單接受者 / 訂單創建者
'price': 0.06917684, // 基礎貨幣/報價貨幣 的浮動價格
'amount': 1.5, // 基於報價貨幣
'cost': 0.10376526, // 總花費(包括手續費) price * amount
'fee': { // 同上(3.訂單接口)
'cost': 0.0015,
'currency': 'ETH',
'rate': 0.002
},
}
6. 行情信息 數據結構
export interface Ticker {
symbol: string;
info: object;
timestamp: number;
datetime: string;
high: number;
low: number;
bid: number;
bidVolume?: number;
ask: number;
askVolume?: number;
vwap?: number;
open?: number;
close?: number;
last?: number;
previousClose?: number;
change?: number;
percentage?: number;
average?: number;
quoteVolume?: number;
baseVolume?: number;
}
{
'symbol': // eg.('BTC/USD', 'ETH/BTC', ...)
'info': // 交易所API返回原始的數據
'timestamp': // 時間戳
'datetime': // ISO8601 時間
'high': float, // 最高價
'low': float, // 最低價
'bid': float, // 當前最高買價
'bidVolume': float, // 最高買價深度(可能爲undefined)
'ask': float, // 當前最高賣價
'askVolume': float, // 最高賣價深度(可能爲undefined)
'vwap': float, // 成交量加權平均價格
'open': float, // 開盤價
'close': float, // 上次成交價格(本期收盤價)
'last': float, // 與'close'一樣,方便複製= =
'previousClose': float, // 前期收市價
'change': float, // 漲跌金額 (上次成交價-開盤價)
'percentage': float, // 漲跌幅度 (上次成交價/開盤價 * 100%)
'average': float, // 平均價格 (上次成交價+開盤價)/2
'baseVolume': float, // 24小時基本貨幣交易量
'quoteVolume': float, // 24小時報價貨幣交易量
}
7. 鏈上信息 交易結構
export interface Transaction {
info: {};
id: string;
txid?: string;
timestamp: number;
datetime: string;
address: string;
type: "deposit" | "withdrawal";
amount: number;
currency: string;
status: "pending" | "ok";
updated: number;
fee: Fee;
}
接口詳情:
{
'info': { ... }, // 交易所原始信息
'id': '123456', // 交易所提供的id
'txid': '0x68bfb29821c50ca35ef3762f887fd3211e4405aba1a94e448a4f218b850358f0',//事務ID
'timestamp': 1534081184515,
'datetime': '2018-08-12T13:39:44.515Z',
'addressFrom': '0x38b1F8644ED1Dbd5DcAedb3610301Bf5fa640D6f', // 發送者
'address': '0x02b0a9b7b4cDe774af0f8e47cb4f1c2ccdEa0806', // "from" or "to"
'addressTo': '0x304C68D441EF7EB0E2c056E836E8293BD28F8129', // 接收者
'tagFrom', '0xabcdef', // "tag" or "memo" or "payment_id" 與發送者相關
'tag': '0xabcdef' // "tag" or "memo" or "payment_id" associated with the address
'tagTo': '0xhijgklmn', // "tag" or "memo" or "payment_id" 與接收者相關
'type': 'deposit', // or 'withdrawal', string 存入或者取出
'amount': 1.2345, // float (不包含手續費)
'currency': 'ETH', // a common unified currency code, string
'status': 'pending', // 'ok', 'failed', 'canceled', string 狀態
'updated': undefined, // 狀態最後更改的UTC時間戳(精確到ms)
'comment': '由用於定義的註釋或消息',
'fee': { // 同上
'currency': 'ETH',
'cost': 0.1234,
'rate': undefined
},
}
最後還有一個最重要的
Exchange
類的數據格式,下一章再詳細翻譯