【原创】Coinbase 公共数据 API 探索(2):获取交易对订单记录


#1

简介:Coinbase,相信大家都不陌生,世界顶级数字货币交易所。Coinbase 有非常完备的 API 服务,包括允许开发人员使用 OAuth2 协议允许 Coinbase 用户授予第三方应用程序对其帐户的完全或部分访问权限,而无需共享帐户的 API 密钥或登录凭据。本篇文章介绍开发者如何使用 Coinbase 公共数据 API 获取交易对订单记录。

Coinbase,相信大家都不陌生,世界顶级数字货币交易所。Coinbase 有非常完备的 API 服务,包括允许开发人员使用 OAuth2 协议允许 Coinbase 用户授予第三方应用程序对其帐户的完全或部分访问权限,而无需共享帐户的 API 密钥或登录凭据。

Coinbase, which I believe everyone knows, is the world’s top digital currency exchange. Coinbase’s API services are very comprehensive, it allows developers to use the OAuth2 protocol to allow a Coinbase user to grant a 3rd party application full or partial access to his/her account, without sharing the account’s API key or login credentials.

本篇文章,我们展示如何利用 Coinbase 的 Market Data API 获取交易对订单记录。

In this article, we show how to use Coinbase’s Market Data API to get a list of open orders for a product.

Market Data API 是 Coinbase Pro API 中不需要身份验证的公共信息端口,用于检索市场数据。它提供市场数据的快照。

The Market Data API is an unauthenticated set of endpoints for retrieving market data. These endpoints provide snapshots of market data.

Market Data API 官方文档:https://docs.pro.coinbase.com/#market-data

Market Data API official documentation: https://docs.pro.coinbase.com/#market-data

获取交易对订单记录:

Get a list of open orders for a product:

基本查询:

Basic query:

https://api-public.sandbox.pro.coinbase.com/products/<product-id>/book

比如查询 ETH-BTC 交易对:

For example, query ETH-BTC:

https://api-public.sandbox.pro.coinbase.com/products/ETH-BTC/book

Node.js 代码示例:

Code example (Node.js):

const fetch = require('node-fetch');

fetch('https://api-public.sandbox.pro.coinbase.com/products/<product-id>/book', {
    method: 'get',
}).then(response => response.json()
    .then(data => console.log(data)));

返回的 JSON 示例:

Return JSON example:

{
  "sequence": 930722,
  "bids": [
    [
      "0.00011",
      "4142899.80123323",
      8
    ]
  ],
  "asks": [
    [
      "0.05234",
      "0.57558165",
      1
    ]
  ]
}

在这个查询中,显示的详细信息量可以使用 level 参数进行自定义。默认情况下 level 参数为1.

In this query, the amount of detail shown can be customized with the level parameter. The default level is 1.

高级查询:

Advanced Search:

对 level 进行设置可改变输出结果:

The amount of detail shown can be customized with the level parameter:

1 只显示最好的出价和 ask

2 前50的出价和 ask 汇总

3 完整订单列表

1 Only the best bid and ask

2 Top 50 bids and asks (aggregated)

3 Full order book (non aggregated)

https://api-public.sandbox.pro.coinbase.com/products/<product-id>/book?level=x

比如查询 BTC-USD 交易对并设置 level 为2:

For example, query BTC-USD and set the level to 2:

https://api-public.sandbox.pro.coinbase.com/products/BTC-USD/book?level=2

Node.js 代码示例:

Code example (Node.js):

const fetch = require('node-fetch');

fetch('https://api-public.sandbox.pro.coinbase.com/products/<product-id>/book?level=x', {
    method: 'get',
}).then(response => response.json()
    .then(data => console.log(data)));

返回的 JSON 示例(查询 BTC-USD 交易对并设置 level 为2):

Return JSON example (query BTC-USD and set the level to 2):

{
  "sequence": 41546106,
  "bids": [
    [
      "3845.42",
      "0.026",
      1
    ],
    [
      "3809",
      "0.011",
      1
    ],
    [
      "3531.21",
      "25.60766362",
      2
    ],
    [
      "3531.2",
      "0.021",
      3
    ],
    [
      "3530.53",
      "0.021",
      3
    ],
    [
      "2791.72",
      "0.021",
      3
    ],
    [
      "2787.2",
      "998.0027812",
      2
    ],
    [
      "2787.18",
      "0.03",
      3
    ],
    [
      "2786.72",
      "0.08",
      1
    ],
    [
      "2786.7",
      "0.16",
      2
    ],
    [
      "2786.67",
      "0.02",
      2
    ],
    [
      "1000",
      "0.05",
      1
    ],
    [
      "100",
      "0.005",
      5
    ],
    [
      "10.01",
      "0.01",
      1
    ],
    [
      "2",
      "67.96086037",
      7
    ],
    [
      "1",
      "1062.4",
      9
    ],
    [
      "0.1",
      "0.41",
      41
    ],
    [
      "0.03",
      "3424.95008705",
      1
    ],
    [
      "0.02",
      "10000",
      1
    ],
    [
      "0.01",
      "20103.772",
      17
    ]
  ],
  "asks": [
    [
      "3848.42",
      "96.98748121",
      1
    ],
    [
      "3851.42",
      "0.05",
      1
    ],
    [
      "5000",
      "0.001",
      1
    ],
    [
      "26329.44",
      "213.27413226",
      2
    ],
    [
      "26329.47",
      "0.371",
      2
    ],
    [
      "26331.88",
      "646.431",
      2
    ],
    [
      "26332.07",
      "0.111",
      2
    ],
    [
      "26333.7",
      "0.381",
      2
    ],
    [
      "26333.91",
      "0.8",
      1
    ],
    [
      "26338.82",
      "0.46",
      2
    ],
    [
      "26340.46",
      "2.83",
      1
    ],
    [
      "26342.08",
      "9.86",
      1
    ],
    [
      "26347.73",
      "0.52",
      1
    ],
    [
      "26349.82",
      "0.23",
      1
    ],
    [
      "26349.89",
      "5.37",
      1
    ],
    [
      "26351.15",
      "0.97",
      1
    ],
    [
      "26351.2",
      "0.72",
      1
    ],
    [
      "26351.49",
      "0.044631",
      1
    ],
    [
      "26354.29",
      "4.21",
      1
    ],
    [
      "26355.89",
      "0.05",
      1
    ],
    [
      "26358.48",
      "7.62",
      1
    ],
    [
      "26359.31",
      "2.448616",
      1
    ],
    [
      "26363.09",
      "0.97",
      1
    ],
    [
      "26364.72",
      "0.65",
      1
    ],
    [
      "26366.26",
      "0.77",
      1
    ],
    [
      "26366.34",
      "4.6",
      1
    ],
    [
      "26367.76",
      "0.43",
      1
    ],
    [
      "26369.18",
      "5.95",
      1
    ],
    [
      "26370.43",
      "0.85",
      1
    ],
    [
      "26371.14",
      "2.89",
      1
    ],
    [
      "26372.04",
      "1.37",
      1
    ],
    [
      "26373.16",
      "9.44",
      1
    ],
    [
      "26374.84",
      "0.79241",
      1
    ],
    [
      "26379.75",
      "0.98",
      1
    ],
    [
      "26381.06",
      "0.126708",
      1
    ],
    [
      "26383.4",
      "0.04",
      1
    ],
    [
      "26385.09",
      "9.46",
      1
    ],
    [
      "26385.96",
      "1.78",
      1
    ],
    [
      "26388.22",
      "0.16",
      1
    ],
    [
      "26388.56",
      "0.11",
      1
    ],
    [
      "26400.92",
      "0.81",
      1
    ],
    [
      "26401.44",
      "8.23",
      1
    ],
    [
      "26402.41",
      "2.47",
      1
    ],
    [
      "26402.54",
      "9",
      1
    ],
    [
      "26403",
      "1",
      1
    ],
    [
      "26404.9",
      "130.91",
      1
    ],
    [
      "26407.96",
      "0.15",
      1
    ],
    [
      "26410.15",
      "0.43",
      1
    ],
    [
      "26411.01",
      "0.185653",
      1
    ],
    [
      "26412.23",
      "0.717165",
      1
    ]
  ]
}

Market Data API 思维导图:

Mind map of the Market Data API:

Coinbase%20Market%20Data%20API