【原创】Coinbase 公共数据 API 探索(5):获取交易对历史价格


#1

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 the historic rates 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 the historic rates for a product.:

使用沙箱测试端口:

Use sandbox URL:

https://api-public.sandbox.pro.coinbase.com/products//candles

使用生产端口:

Use rest API endpoint URL:

https://api.pro.coinbase.com/products//candles

比如查询 ETH-BTC 交易对:

For example, query ETH-BTC:

使用沙箱测试端口:

Use sandbox URL:

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

使用生产端口:

Use rest API endpoint URL:

https://api.pro.coinbase.com/products/ETH-BTC/candles

Node.js 代码示例:

Code example (Node.js):

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

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

端口接受三个可选参数:

Parameters:

start: ISO 8601规则的开始时间
end: ISO 8601规则的结束时间
granularity: 时间段大小(输入秒数)

start: Start time in ISO 8601
end: End time in ISO 8601
granularity: Desired timeslice in seconds

granularity 参数只能是这些值:{60, 300, 900, 3600, 21600, 86400}。否则请求将被拒绝。这些值都是秒数,分别代表一分钟,五分钟,十五分钟,一小时,六小时和一天。

The granularity field must be one of the following values: {60, 300, 900, 3600, 21600, 86400}. Otherwise, your request will be rejected. These values correspond to timeslices representing one minute, five minutes, fifteen minutes, one hour, six hours, and one day, respectively.

返回的 JSON 示例:

Return JSON example:

[
    [ time, low, high, open, close, volume ],
    [ 1415398768, 0.32, 4.2, 0.35, 4.2, 12.3 ],
    ...
]

返回 JSON 释义:

Each bucket is an array of the following information:

time: 时间段开始的时间;
low: 时间段中的最低价格;
high: 时间段中的最高价格;
open: 时间段中第一笔交易的开盘价;
close: 时间段中最后一笔交易的收盘价;
volume: 时间段中的交易活动量。

time: bucket start time
low: lowest price during the bucket interval
high: highest price during the bucket interval
open: opening price (first trade) in the bucket interval
close: closing price (last trade) in the bucket interval
volume: volume of trading activity during the bucket interval

Market Data API 思维导图:

Mind map of the Market Data API:

Coinbase Market Data API