Navbar
English
shell

简介

API 简介

欢迎使用 BFX API! 你可以使用此 API 获得市场行情数据,进行交易,并且管理你的账户。

在文档的右侧是代码示例,目前我们仅提供针对 shell 的代码示例。

你可以通过下面的链接来切换旧版API文档:

https://www.bfxnu.com/news/other.do

更新日志

生效时间(北京时间 UTC+8) 接口 新增 / 修改 摘要
2020.07.10 16:00 域名修改为bfxnu.com 修改 更换 域名链接
2019.12.20 16:00 BFX 合约 API 英文 文档 新增 添加 合约 API 英文 文档
2019.12.03 16:50 Websocket market.$type.$symbol.trade 修改 推送消息添加订阅地址和时间戳
2019.11.01 19:00 GET /v1/history/orders.do 修改 添加“state”返回字段,表示订单状态
2019.10.12 16:00 BFX 合约 API 中文 文档 新增 添加 合约 API 中文 文档

接入说明

接入 URLs

正式版 REST API

https://api.bfxnu.com/

Websocket Feed(行情)

wss://api.bfxnu.com/v1/ws

限频规则

签名认证

签名说明

API 请求在通过 internet 传输的过程中极有可能被篡改,为了确保请求未被更改,除公共API接口和行情API数据接口外,其余接口均必须使用您的 API Key 做签名认证,以校验参数或参数值在传输途中是否发生了更改。

一个合法的请求由以下几部分组成:

创建 API Key

您可以在 这里 https://www.bfxnu.com/center/bindAppKey.do 创建 API Key。

API Key 包括以下两部分

签名步骤

规范要计算签名的请求 因为使用 MD5 进行签名计算时,使用不同内容计算得到的结果会完全不同。所以在进行签名计算前,请先对请求进行规范化处理。下面以查询某订单详情请求为例进行说明:

查询某订单详情

https://api.bfxnu.com/v1/trade/order.do?

AppKey=8e9e4b10413f49f682001c530beea232

&Timestamp=1568189479

&orderId=1082752

&symbol=bfxusdt

&Signature=a49c6053d05826f29356b189e1200f2a

1. Signature参数的生成:按照ASCII码的顺序对其余所有参数名称进行排序,并将参数值和参数名拼接在一起,例如,下面是请求参数的原始顺序,进行过编码后:

Timestamp=1568189479

symbol=bfxusdt

orderId=1082752

AppKey=8e9e4b10413f49f682001c530beea232

2. 按参数名称排序后为:

AppKey=8e9e4b10413f49f682001c530beea232

Timestamp=1568189479

orderId=1082752

symbol=bfxusdt

3. 按照以上顺序,将参数名和参数值拼接后为:

AppKey8e9e4b10413f49f682001c530beea232Timestamp1568189479orderId1082752symbolbfxusdt

4. 把上面这个拼装好的字符串,在前面和后面,都加上AppSecret(例如,AppSecret:7c304b6b86f8403abc8091720a2415f2),并进行utf-8编码,使用MD5算法对编码后的字节流进行摘要,则:

MD5(7c304b6b86f8403abc8091720a2415f2AppKey8e9e4b10413f49f682001c530beea232
Timestamp1568189479orderId1082752symbolbfxusdt7c304b6b86f8403abc8091720a2415f2)

得到的十六进制字符串签名结果为:a49c6053d05826f29356b189e1200f2a

5. 将上述签名值作为参数Signature的取值添加到 API 请求中:

最终,发送到服务器的 API 请求应该为

https://api.bfxnu.com/v1/trade/order.do?AppKey=8e9e4b10413f49f682001c530beea232&Timestamp=1568189479&orderId=1082752&symbol=bfxusdt&Signature=a49c6053d05826f29356b189e1200f2a

  1. 把所有必须的认证参数添加到接口调用的路径参数里

  2. 把数字签名在URL编码后加入到路径参数里,参数名为“Signature”。

请求格式

  1. 所有的API请求都以GET或者POST形式发出。

  2. 传参方式请使用FormData方式传参,并在Header设置User-Agent:
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.0.1471.813 Safari/537.36"

返回格式

所有的接口返回都是JSON格式。在JSON最上层有几个表示请求状态和属性的字段:"code", "data", 和 "msg"。实际的接口返回内容在"data"字段里。

返回内容格式

返回内容将会是以下格式:

{
  "code": 0,
  "msg": "",
  "data": // per API response data in nested JSON object
}
参数名称 数据类型 描述
code int 错误码(0-成功,其他-失败)
msg string 错误信息(code=0成功时,此参数为"")
data array 接口返回数据主体(失败时无此值)

错误信息

通用状态码说明

错误码 描述
Code:10000 其他错误(msg返回具体错误信息)
Code:10001 提交的参数错误
Code:10020 token失效或者错误,需要重新登录

Code:其他错误码(msg返回具体错误信息)

合约基础信息

获取所有合约名称及精度

此接口返回所有平台支持的合约名称以及精度。

HTTP 请求

curl "https://api.bfxnu.com/v1/common/symbols.do?type=contract"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "symbol":"btc",
        "showName":"BTC",
        "multiples":"20",
        "pricePrecision":1,
        "amountPrecision":2
      },
      {
        "symbol":"eth",
        "showName":"ETH",
        "multiples":"20",
        "pricePrecision":2,
        "amountPrecision":2
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
symbol string 合约名称
showName string 展示名称
multiples string 支持的杆杠,","号分隔
pricePrecision integer 价格精度位数
amountPrecision integer 数量精度位数

合约行情数据

合约 K 线数据(蜡烛图)

此接口返回历史K线数据。

HTTP 请求

curl "https://api.bfxnu.com/v1/market/kline.do?symbol=btc&interval=15min&type=contract"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
interval string true NA 时间类型 1min,3min,5min,具体说明见下方

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "code":"btc",
        "first":10205.58,
        "high":10210.01,
        "last":10192.18,
        "low":10190.28,
        "time":1568705400,
        "vol":23.23
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
code string 合约名称
first float 开盘价
high float 最高价
last float 收盘价
low float 最低价
time long 时间
vol float 以报价币种计量的交易量

获取合约市场行情(Ticker)

此接口返回合约市场行情(Ticker),最新价,最高价,最低价,成交量,买一价和卖一价。

HTTP 请求

curl "https://api.bfxnu.com/v1/market/ticker.do?symbol=btc&type=contract"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...

Response:

{
  "code": 0,
  "msg": "",
  "data": {
    "symbol":"btc",
    "buy":10191.65,
    "sell":10193.72,
    "high":10268.48,
    "last":10192.5,
    "low":10141.53,
    "trades":50584730.78,
    "vol":4954.56
  }
}

响应数据

字段名称 数据类型 描述
symbol string 合约名称
buy float 买一价
sell float 卖一价
high float 最高价
last float 最新成交价
low float 最低价
trades float 成交额
vol float 成交价

获取所有合约市场行情(Tickers)

此接口返回所有合约市场行情(Tickers),最新价,最高价,最低价,成交量,买一价和卖一价。

HTTP 请求

curl "https://api.bfxnu.com/v1/market/tickers.do?type=contract"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "symbol":"btc",
        "change":"-1.75",
        "buy":10195.79,
        "sell":10197.88,
        "high":10268.48,
        "last":10196.55,
        "low":10141.53,
        "trades":50445638.61,
        "vol":4940.96
      },
      {
        "symbol":"eth",
        "change":"-1.71",
        "buy":214.09,
        "sell":214.38,
        "high":214.92,
        "last":214.23,
        "low":196.78,
        "trades":8222181.8,
        "vol":39749.57
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
symbol string 合约名称
change string 涨跌幅
buy float 买一价
sell float 卖一价
high float 最高价
last float 最新成交价
low float 最低价
trades float 成交额
vol float 成交价

合约市场深度数据

此接口返回指定合约的当前市场深度数据(委托挂单),数量:20。

HTTP 请求

curl "https://api.bfxnu.com/v1/market/depth.do?symbol=btc&type=contract"

请求参数

参数 数据类型 必须 默认值 描述 取值范围
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
bfx string false "" 是否返回精度 ""、任意字符串

Response:

{
  "code": 0,
  "msg": "",
  "amountDigits": 4,
  "digits": 2,
  "data": {
    "bids": [
      [7964.12, 0.0678], // [price, amount]
      [7963.4, 0.9162],
      [7961, 0.1],
      [7960.6, 12.8898],
      [7958, 1.2],
      ...
    ],
    "asks": [
      [7979.55, 0.0736],
      [7980.6, 1.0292],
      [7981, 5.5652],
      [7986.1, 0.2416],
      [7990.66, 1.9970],
      ...
    ]
  }
}

响应数据

字段名称 数据类型 描述
amountDigits integer 数量保留小数位数
digits integer 价格保留小数位数
bids object 当前的所有买单, [price(成交价), amount(成交量)], 按price降序
asks object 当前的所有卖单, [price(成交价), amount(成交量)], 按price降序

获取合约历史成交记录

此接口返回合约交易记录,获取合约历史成交记录,数量:50。

HTTP 请求

curl "https://api.bfxnu.com/v1/market/trades.do?symbol=btcusdt&type=spot"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
bfx string false "" 是否返回精度 ""、任意字符串

Response:

{
  "code": 0,
  "msg": "",
  "amountDigits": 4,
  "digits": 2,
  "data": [
      {
        "type":"buy",
        "amount":0.47,
        "date":1568876917,
        "price":9862.44,
        "change":3.26,
        "tid":5509739
      },
      {
        "type":"sell",
        "amount":0.07,
        "date":1568876900,
        "price":9861.47,
        "change":0,
        "tid":5509742
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
amountDigits integer 数量保留小数位数
digits integer 价格保留小数位数
type string 成交方向(buy或sell)
amount float 成交量
date long 成交时间
price float 成交价
change float 增仓(合约交易)
tid long id

账户相关

查询所有合约资产

此接口返回查询所有合约资产。

HTTP 请求

curl "https://api.bfxnu.com/v1/account/accounts.do?AppKey=b779e1982b77478d8d23e69f7e7df10f&Timestamp=1570601407&type=spot&Signature=4469ee0fef8a34161956c42ce29746d7"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "symbol":"btc",
        "available":0,
        "frozen":0
      },
      {
        "symbol":"usdt",
        "available":214,
        "frozen":0
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
symbol string 合约名称
available float 可用资金
frozen float 冻结资金

查询单个合约资产

此接口返回查询单个合约资产。

HTTP 请求

curl "https://api.bfxnu.com/v1/account/account.do?AppKey=c5fe0cc7d3434efb817fef0ba63ea614&Timestamp=1571209967&symbol=btc&type=contract&Signature=c7d3dc4bddf8b530c355a930ce41af3e"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...

Response:

{
  "code": 0,
  "msg": "",
  "data": {
      "symbol":"btc",
      "available":1.64916,
      "frozen":0
   }
}

响应数据

字段名称 数据类型 描述
symbol string 合约名称
available float 可用资金
frozen float 冻结资金

资产划转(币币与合约之间)

合约账户转入转出资产划转(币币与合约之间)。

HTTP 请求

{
  "AppKey": "c5fe0cc7d3434efb817fef0ba63ea614",
  "Timestamp": 1571212250,
  "Signature": "c5ac81af7f18d465232652ccee98b7fa",
  "type": "contract",
  "symbol": "btc",
  "operation": "out",
  "amount": "0.1"
}

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
operation string true NA 操作方向 in转入,out转出
amount string true NA 数量

Response:

{
  "code": 0,
  "msg": "",
  "data": 1688
}

响应数据

字段名称 数据类型 描述
data long 记录id

资金划转历史记录查询

此接口查询资金划转历史记录。

HTTP 请求

curl "https://api.bfxnu.com/v1/account/transfer/records.do?AppKey=c5fe0cc7d3434efb817fef0ba63ea614&Timestamp=1571213734&direct=prev&endTime=1571213617&from=20&side=all&size=100&startTime=1557543620&symbol=btc&type=contract&Signature=3645b48620adfe0f5c784f7a1196ec7a"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
size integer false 10 订单数 [1-100]
from long false NA 起始id 需与direct参数同时搭配使用
direct string false NA 查询方向 prev向前,next向后(需与from参数同时搭配使用)
startTime long false NA 开始时间戳(s) 秒级时间戳
endTime long false NA 结束时间戳(s) 秒级时间戳

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "symbol":"btc",
        "amount":0.1,
        "operation":2,
        "recordId":1688,
        "recordTime":1571212250
      },
      {
        "symbol":"btc",
        "amount":15.6,
        "operation":2,
        "recordId":1671,
        "recordTime":1565674096
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
symbol string 合约名称
amount float 转账数量
operation integer 操作方向,1-转入,2-转出
recordId long 记录id
recordTime long 操作时间

合约交易

开仓下单

发送一个新开仓订单到BFX以进行撮合。

HTTP 请求

{
  "AppKey": "284e7228ab324e1f82a13faada3cf431",
  "Timestamp": 1570856203,
  "Signature": "bbb7fdff74908fff5e18052acce0612d",
  "type": "contract",
  "symbol": "btc",
  "price": "14000",
  "amount": "0.1",
  "multiple": 20,
  "operation": "sell"
}

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
price string true NA 下单价格
amount string true NA 下单数量
multiple integer true NA 杆杠倍数
operation string true NA 下单方向 buy-买入,sell-卖空

Response:

{
  "code": 0,
  "msg": "",
  "data": "1570856202112555"
}

响应数据

字段名称 数据类型 描述
data string 订单id

平仓下单

发送一个平仓下单请求到BFX以进行撮合。

HTTP 请求

{
  "AppKey": "284e7228ab324e1f82a13faada3cf431",
  "Timestamp": 1570858613,
  "Signature": "ad35471360bbaaeca74f0b02e2860d99",
  "type": "contract",
  "symbol": "btc",
  "price": "8761",
  "amount": "0.1",
  "positionId": 197
}

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
price string true NA 平仓价格
amount string true NA 平仓数量
positionId long true NA 持仓id

Response:

{
  "code": 0,
  "msg": "",
  "data": "1570858612569602"
}

响应数据

字段名称 数据类型 描述
data string 订单id

合约持仓查询

此接口返回指定单个订单的最新状态和详情。

HTTP 请求

curl "https://api.bfxnu.com/v1/trade/positions.do?AppKey=284e7228ab324e1f82a13faada3cf431&Timestamp=1570856750&symbol=btc&type=contract&Signature=5b133508bf308424ba9fe4ccff3752f5"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "allCount":0.2,
        "avgPrice":10945,
        "deposit":109.45,
        "direction":1,
        "floatingProfitLoss":-524.462,
        "forcedLiquidationPrice":1742.74,
        "holdCount":0.2,
        "lockCount":0,
        "multiple":20,
        "positionId":"198",
        "profitLossRatio":"-479.17%",
        "symbol":"btc",
        "stopLoss":False
      },
      {
        "allCount":0.3,
        "avgPrice":10148.96,
        "deposit":152.2344,
        "direction":2,
        "floatingProfitLoss":547.8819,
        "forcedLiquidationPrice":14650.54,
        "holdCount":0.3,
        "lockCount":0,
        "multiple":20,
        "positionId":"197",
        "profitLossRatio":"359.89%",
        "symbol":"btc",
        "stopLoss":False
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
allCount float 持仓总数量
avgPrice float 持仓均价
deposit float 保证金
direction integer 方向,1-多仓,2-空仓
floatingProfitLoss float 浮动盈亏
forcedLiquidationPrice float 强平价格
holdCount float 可用持仓数量
lockCount float 冻结持仓数量
multiple integer 杆杠倍数
positionId string 持仓id
profitLossRatio string 盈亏比
symbol string 合约名称
stopLoss boolean 是否止盈止损

撤销订单

此接口发送一个撤销订单的请求。

HTTP 请求

{
  "AppKey": "284e7228ab324e1f82a13faada3cf431",
  "Timestamp": 1570859741,
  "Signature": "e62466a824ef5025db0e8077f0cf9bb8",
  "type": "contract",
  "symbol": "btc",
  "orderId": "1570859608609294"
}

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
orderId string true NA 订单id

Response:

{
  "code": 0,
  "msg": "",
  "data": "1570859608609294"
}

响应数据

字段名称 数据类型 描述
data string 订单id

批量撤销订单

此接口发送批量撤销订单的请求。

HTTP 请求

{
  "AppKey": "284e7228ab324e1f82a13faada3cf431",
  "Timestamp": 1570859962,
  "Signature": "038d9669e0eeb4b0736c0671efde2ad1",
  "type": "contract",
  "symbol": "btc",
  "side": "sell"
}

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
side string true NA 订单类型 all所有, buy买单, sell卖单

Response:

{
  "code": 0,
  "msg": "",
  "data": {
    "successCount":4,
    "failedCount":0
  }
}

响应数据

字段名称 数据类型 描述
successCount integer 取消成功订单数
failedCount integer 取消失败订单数

查询单个订单详情

此接口返回指定单个订单的最新状态和详情。

HTTP 请求

curl "https://api.bfxnu.com/order.do?AppKey=284e7228ab324e1f82a13faada3cf431&Timestamp=1570860157&orderId=1570859608609294&symbol=btc&type=contract&Signature=6ff9c32bd3adbb5eef3cea9f5ba2c60a"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
orderId string true NA 订单id

Response:

{
  "code": 0,
  "msg": "",
  "data": {
    "amount":0.1,
    "createTime":1570859608,
    "dealAmount":0,
    "delegation_id":0,
    "deposit":70,
    "direction":2,
    "multiple":20,
    "orderId":"1570859608609294",
    "price":14000,
    "priority":1,
    "state":"canceled",
    "symbol":"btc",
    "undealAmount":0.1
  }
}

响应数据

字段名称 数据类型 描述
amount float 下单数量
createTime long 下单时间
dealAmount float 已成交数量
delegation_id long 委托编号
deposit float 保证金(合约交易才有,现货为0)
direction integer 下单方向,1-买入,2-卖空
multiple integer 杆杠倍数(合约交易才有,现货为0)
orderId string 订单id
price float 下单价格
priority integer 订单类型,1-开仓,2-平仓(合约交易才有,现货为0)
state string 订单状态,submitting提交中,submitted已提交,filled已成交,canceled已撤销
symbol string 合约名称
undealAmount float 未成交数量

查询当前未成交订单

此接口基于搜索条件查询当前未成交订单。

HTTP 请求

curl "https://api.bfxnu.com/v1/trade/orders.do?AppKey=284e7228ab324e1f82a13faada3cf431&Timestamp=1570860897&endTime=1570860882&size=100&startTime=1557543620&states=submitted%2Csubmitting&symbol=btc&type=contract&Signature=afb5d55b8bd35c471fea80bffcb0724b"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
size integer false 10 订单数 [1-100]
states string false submitted 订单状态集合(用”,”号分开) submitting提交中, submitted已提交
startTime long false NA 开始时间戳(s) 秒级时间戳
endTime long false NA 结束时间戳(s) 秒级时间戳

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "amount":0.15,
        "createTime":1570860856,
        "dealAmount":0,
        "delegation_id":55144733,
        "deposit":105.075,
        "direction":2,
        "multiple":20,
        "orderId":"1570860856236224",
        "price":14010,
        "priority":1,
        "state":"submitted",
        "symbol":"btc",
        "undealAmount":0.15
      },
      {
        "amount":0.1,
        "createTime":1570860647,
        "dealAmount":0,
        "delegation_id":55139855,
        "deposit":70.025,
        "direction":2,
        "multiple":20,
        "orderId":"1570860647390484",
        "price":14005,
        "priority":1,
        "state":"submitted",
        "symbol":"btc",
        "undealAmount":0.1
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
amount float 下单数量
createTime long 下单时间
dealAmount float 已成交数量
delegation_id long 委托编号
deposit float 保证金(合约交易才有,现货为0)
direction integer 下单方向,1-买入,2-卖空
multiple integer 杆杠倍数(合约交易才有,现货为0)
orderId string 订单id
price float 下单价格
priority integer 订单类型,1-开仓,2-平仓(合约交易才有,现货为0)
state string 订单状态,submitting提交中,submitted已提交,filled已成交,canceled已撤销
symbol string 合约名称
undealAmount float 未成交数量

查询历史订单

此接口基于搜索条件查询历史订单。

HTTP 请求

curl "https://api.bfxnu.com/v1/trade/history/orders.do?AppKey=284e7228ab324e1f82a13faada3cf431&Timestamp=1571216140&direct=prev&endTime=1571213617&from=20&size=100&startTime=1557543620&symbol=btc&type=contract&Signature=2908abbe7559379d44273f47e0299c07"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
size integer false 10 订单数 [1-100]
from long false NA 起始id 需与direct参数同时搭配使用
direct string false NA 查询方向 prev向前,next向后(需与from参数同时搭配使用)
startTime long false NA 开始时间戳(s) 秒级时间戳
endTime long false NA 结束时间戳(s) 秒级时间戳

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "amount":0.15,
        "dealAmount":105.075,
        "deposit":0,
        "direction":0,
        "multiple":20,
        "price":14010,
        "recordId":1314,
        "tradeAvgPrice":0,
        "undealAmount":0.15,
        "orderId":"1570860856236224",
        "symbol":"btc",
        "createTime":1570860856,
        "updateTime":1570860856,
        "tradeType":3
      },
      {
        "amount":0.1,
        "dealAmount":47.5,
        "deposit":0,
        "direction":0,
        "multiple":20,
        "price":9500,
        "recordId":980,
        "tradeAvgPrice":0,
        "undealAmount":0,
        "orderId":"1565581905261969",
        "symbol":"btc",
        "createTime":1565581905,
        "updateTime":1565581905,
        "tradeType":1
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
amount float 下单数量
dealAmount float 委托数量
deposit float 保证金(合约交易才有,现货为0)
direction integer 方向,1-买,2-卖
multiple integer 杆杠倍数(合约交易才有,现货为0)
price float 委托价格
recordId integer 委托挂单id
tradeAvgPrice float 成交均价
undealAmount float 未成交数量
orderId string 挂单流水号
symbol string 合约名称
createTime long 委托时间,unix时间格式
updateTime long 操作时间,unix时间格式
tradeType integer 交易类型,1-多单开仓,2-多单平仓,3-空单开仓,4-空单平仓(合约交易才有,现货为0)
state string 订单状态,submitting提交中,submitted已提交,filled已成交,canceled已撤销

查询交易记录

此接口基于搜索条件查询交易记录。

HTTP 请求

curl "https://api.bfxnu.com/v1/trade/history/trades.do?AppKey=284e7228ab324e1f82a13faada3cf431&Timestamp=1570861659&direct=prev&from=20&size=100&symbol=btc&type=contract&Signature=0a87b4d9ee8df333d6a93de95051c4a0"

请求参数

参数 数据类型 是否必须 默认值 描述 取值范围
AppKey string true NA API 访问密钥 申请到的AppKey
Timestamp long true NA 时间戳 秒级时间戳
Signature string true NA 签名 通过签名计算得出的值
type string false contract 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
size integer false 10 订单数 [1-100]
from long false NA 起始id 需与direct参数同时搭配使用
direct string false NA 查询方向 prev向前,next向后(需与from参数同时搭配使用)
startTime long false NA 开始时间戳(s) 秒级时间戳
endTime long false NA 结束时间戳(s) 秒级时间戳

Response:

{
  "code": 0,
  "msg": "",
  "data": [
      {
        "direction":1,
        "holdPrice":10148.96333333,
        "multiple":20,
        "symbol":"btc",
        "tradeBfxFee":0,
        "tradeCount":0.1,
        "tradeDeposit":50.74481667,
        "tradeFee":5.839967,
        "tradeId":210729,
        "tradeIncome":180.61533333,
        "tradePrice":8342.81,
        "tradeTime":1570858612,
        "tradeType":4
      },
      {
        "direction":2,
        "holdPrice":0,
        "multiple":20,
        "symbol":"btc",
        "tradeBfxFee":0,
        "tradeCount":0.1,
        "tradeDeposit":42.78445,
        "tradeFee":5.989823,
        "tradeId":207786,
        "tradeIncome":0,
        "tradePrice":8556.89,
        "tradeTime":1570770977,
        "tradeType":3
      },
      ...
   ]
}

响应数据

字段名称 数据类型 描述
direction integer 方向,1-买,2-卖
holdPrice float 持仓价(合约交易才有,现货为0)
multiple integer 杆杠倍数(合约交易才有,现货为0)
symbol string 合约名称
tradeBfxFee float BFX平台币抵扣交易手续费
tradeCount float 成交数量
tradeDeposit float 成交保证金(合约交易才有,现货为0)
tradeFee float 交易手续费
tradeId long 交易id
tradeIncome float 盈亏额(合约交易才有,现货为0)
tradePrice float 成交价
tradeTime long 成交时间
tradeType integer 交易类型,1-多单开仓,2-多单平仓,3-空单开仓,4-空单平仓(合约交易才有,现货为0)

Websocket行情数据

简介

接入URL

行情请求地址

wss://api.bfxnu.com/v1/ws

心跳消息

Response

{
  "ping": 1570678375
}

当用户的Websocket客户端连接到BFX的Websocket服务器后,服务器会定期(当前设为5秒)向其发送ping消息并包含一整数值如下:
  {
    "ping": 1570678375
  }

Subscribe request

{
  "pong": 1570678375
}

当用户的Websocket客户端接收到此心跳消息后,应返回pong消息并包含同一整数值:
  {
    "pong": 1570678375
  }

订阅主题

{
  "sub": "kline.contract.btc.15min"
}

成功建立与Websocket服务器的连接后,Websocket客户端发送如下请求以订阅特定主题:
  {
    "sub": "订阅地址"
  }

{
  "code": 10000,
  "msg": "订阅失败,不支持的类型: kline.contract.btccccc.15min",
  "ts": 1571378705
}

订阅失败后,Websocket客户端将收到:
  {
    "code": "错误码(0-成功,其他-失败)",
    "msg": "错误信息(code=0成功时,此参数为"")",
    "ts": "秒级时间戳"
  }

{
  "code": 0,
  "msg": "ok",
  "subbed": "kline.contract.btc.15min",
  "ts": 1571373335
}

成功订阅后,Websocket客户端将收到确认:
  {
    "code": "错误码(0-成功,其他-失败)",
    "msg": "错误信息(code=0成功时,此参数为"")",
    "subbed": "订阅地址",
    "ts": "秒级时间戳"
  }

{
  "ch": "kline.contract.btc.15min",
  "ts": 1571373361,
  "tick": [
    {
      "code": "btc",
      "first": 8070.65,
      "high": 8072.18,
      "id": 3453,
      "last": 8068.52,
      "low": 8068.52,
      "time": 1571373900,
      "vol": 3.56
    }
  ]
}

之后, 一旦所订阅的主题有更新,Websocket客户端将收到服务器推送的更新消息(push),例如:
  {
    "ch": "订阅地址",
    "ts": "秒级时间戳",
    "tick": "市场行情数据"
  }

取消订阅

{
  "unsub": "kline.contract.btc.15min"
}

取消订阅的格式如下(取消订阅成功无返回值):
  {
    "unsub": "取消订阅地址"
  }

请求数据

Websocket服务器同时支持一次性请求数据(pull)。

{
  "req": "kline.contract.btc.15min"
}

请求数据的格式如下:
  {
    "req": "请求地址"
  }

{
  "code": 0,
  "msg": "",
  "rep": "kline.contract.btc.15min",
  "ts": 1571377747,
  "tick": [
    {
      "code": "btc",
      "first": 3600,
      "high": 3900,
      "last": 3900,
      "low": 3600,
      "time": 1551685500,
      "vol": 3.5
    },
    {
      "code": "btc",
      "first": 3894.47,
      "high": 3926.1,
      "last": 3918.01,
      "low": 3870.9,
      "time": 1552024800,
      "vol": 53.28
    }
  ]
}

一次性返回的数据:
  {
    "code": "错误码(0-成功,其他-失败)",
    "msg": "错误信息(code=0成功时,此参数为"")",
    "rep": "订阅地址",
    "ts": "秒级时间戳"
    "tick": "市场行情数据"
  }

K线数据

主题订阅

一旦K线数据产生,Websocket服务器将通过此订阅主题接口推送至客户端:

kline.$type.$symbol.$interval

Subscribe request

{
  "sub": "kline.contract.btc.15min"
}

参数

参数 数据类型 是否必需 默认值 描述 取值范围
type string true NA 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...
interval string true NA K线周期 1min,3min,5min,具体说明见下方

Response

{
  "code": 0,
  "msg": "",
  "subbed": "kline.contract.btc.15min",
  "ts": 1571393156
}

响应数据

字段 数据类型 描述
subbed string 订阅地址
ts long 秒级时间戳

Update example

{
  "ch": "kline.contract.btc.15min",
  "tick": [
    {
      "code": "btc",
      "first": 8070.65,
      "high": 8072.18,
      "id": 3453,
      "last": 8068.52,
      "low": 8068.52,
      "time": 1571373900,
      "vol": 3.56
    }
  ]
}

数据更新字段列表

字段 数据类型 描述
code string 合约名称
first float 开盘价
high float 最高价
id long 记录id
last float 收盘价
low float 最低价
time long 时间
vol float 以报价币种计量的交易量

Req request

{
  "req": "kline.contract.btc.15min"
}

数据请求

用请求方式一次性获取K线数据:
  {
    "req": "kline.$type.$symbol.$interval"
  }

市场深度行情数据

当市场深度发生变化时,此主题发送最新市场深度更新数据。

主题订阅

market.$type.$symbol.depth

Subscribe request

{
  "sub": "market.contract.btc.depth"
}

参数

参数 数据类型 是否必需 默认值 描述 取值范围
type string true NA 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...

Response

{
  "code": 0,
  "msg": "",
  "subbed": "market.contract.btc.depth",
  "ts": 1571379519
}

响应数据

字段 数据类型 描述
subbed string 订阅地址
ts long 秒级时间戳

Update example

{
  "ch": "market.contract.btc.depth",
  "ts": 1571379520,
  "tick": {
    "bids": [
    [7897.38,0.02], // [price, amount]
    [7897.18,0.13]
    // more Market Depth data here
    ],
    "asks": [
    [7901.37,0.2],
    [7901.16,0.05]
    // more Market Depth data here
    ]
  }
}

数据更新字段列表

字段 数据类型 描述
bids object 当前的所有买单, [price(成交价), amount(成交量)], 按price降序
asks object 当前的所有卖单, [price(成交价), amount(成交量)], 按price降序

Req request

{
  "req": "market.contract.btc.depth"
}

数据请求

支持数据请求方式一次性获取市场深度数据:
  {
    "req": "market.$type.$symbol.depth"
  }

成交明细

此主题提供市场最新成交明细。

主题订阅

market.$type.$symbol.trade

Subscribe request

{
  "sub": "market.contract.btc.trade"
}

参数

参数 数据类型 是否必需 默认值 描述 取值范围
type string true NA 市场类型 contract: 合约, spot: 现货, vcontract: 模拟
symbol string true NA 合约名称 btc, eth, ht...

Response

{
  "code": 0,
  "msg": "",
  "subbed": "market.contract.btc.trade",
  "ts": 1571383431
}

响应数据

字段 数据类型 描述
subbed string 订阅地址
ts long 秒级时间戳

Update example

{
  "ch": "market.contract.btc.trade",
  "ts": 1571379520,
  "tick": [
    {
    "amount": 0.85,
    "change": 1.7,
    "date": 1571383457,
    "price": 7895.59,
    "tid": 114202,
    "type": "buy"
    }
  ]
}

数据更新字段列表

字段 数据类型 描述
amount float 成交量
change float 涨跌幅
date long 成交时间
price float 成交价
tid long id
type string 成交方向(buy或sell)

Req request

{
  "req": "market.contract.btc.trade"
}

数据请求

支持数据请求方式一次性获取成交明细数据:
  {
    "req": "market.$type.$symbol.trade"
  }