我试图使用coinbase-pro API和官方的node.js库获取当前的BTC价格和15分钟前的BTC价格。 我的目标是在过去15分钟内从API获得15个数据点的响应,每分钟一个。我假设这是每个文档的粒度参数,无论我设置的粒度值是多少,响应似乎都包含200多个项目。我只想要15 ...
now = new Date();
fifteen_minutes_ago = new Date();
fifteen_minutes_ago.setMinutes(fifteen_minutes_ago.getMinutes() - 15);
console.log(now);
console.log(fifteen_minutes_ago);
publicClient
.getProductHistoricRates('BTC-USD', { start: now, stop: fifteen_minutes_ago, granularity: 60 })
.then(BTC_USD_history_json => {
// work with data
console.log(BTC_USD_history_json)
})
.catch(error => {
// handle the error
});
当前输出:
2019-10-01T22:13:46.880Z
2019-10-01T21:58:46.880Z
[ [ 1569967920, 8309.94, 8313.98, 8309.94, 8313.98, 2.23607006 ],
[ 1569967860, 8308.68, 8309.97, 8308.68, 8309.95, 2.22526865 ],
[ 1569967800, 8308.35, 8310, 8310, 8309.89, 2.20357907 ],
[ 1569967740, 8304.51, 8310, 8304.54, 8309.99, 6.7931821 ],
[ 1569967680, 8304.13, 8306.13, 8305.96, 8305.03, 1.76882803 ],
[ 1569967620, 8304.24, 8307.13, 8305.5, 8306.09, 2.57311181 ],
[ 1569967560, 8302.26, 8307.13, 8302.26, 8304.89, 2.99700614 ],
[ 1569967500, 8301.7, 8306.68, 8306.48, 8303.88, 5.45899023 ],
[ 1569967440, 8304.35, 8307.72, 8305.47, 8304.92, 7.82532197 ],
[ 1569967380, 8305.1, 8308.98, 8305.12, 8305.47, 8.51652165 ],
[ 1569967320, 8303.01, 8311.71, 8311.71, 8305.48, 7.38486433 ],
[ 1569967260, 8311.71, 8322.55, 8322.55, 8311.71, 6.63735246 ],
[ 1569967200, 8321.05, 8327.16, 8325.53, 8322.55, 2.37100347 ],
[ 1569967140, 8322.6, 8327.19, 8322.6, 8327.19, 0.40808268 ],
[ 1569967080, 8319.81, 8322.6, 8321.51, 8321.12, 1.87082546 ],
[ 1569967020, 8321.5, 8321.51, 8321.51, 8321.51, 1.21943651 ],
[ 1569966960, 8315.84, 8321.59, 8315.85, 8321.51, 2.19931345 ],
[ 1569966900, 8312.52, 8315.85, 8313.26, 8315.85, 5.12133226 ],
[ 1569966840, 8311.41, 8313.95, 8313.9, 8312.33, 1.02898648 ],
[ 1569966780, 8307.93, 8314.99, 8311.58, 8313.9, 1.62884709 ],
[ 1569966720, 8306.62, 8318.71, 8308.43, 8315.49, 3.06220914 ],
[ 1569966660, 8309.09, 8339.08, 8339.08, 8309.09, 23.06364039 ],
[ 1569966600, 8339.07, 8341.01, 8341.01, 8339.08, 1.53194967 ],
[ 1569966540, 8341, 8341.01, 8341.01, 8341.01, 1.17700054 ],
[ 1569966480, 8341, 8341.38, 8341.38, 8341.01, 1.43096561 ],
[ 1569966420, 8341, 8344.95, 8344.95, 8341.36, 0.76637233 ],
[ 1569966360, 8341.13, 8345, 8341.48, 8343.28, 1.48420611 ],
[ 1569966300, 8341, 8341.49, 8341, 8341.44, 7.08361776 ],
[ 1569966240, 8341.05, 8345.01, 8345.01, 8341.47, 3.49227523 ],
[ 1569966180, 8345, 8346.62, 8346.62, 8345, 4.0384715 ],
[ 1569966120, 8341.01, 8346.97, 8341.08, 8346.63, 1.91480734 ],
[ 1569966060, 8337.28, 8345.25, 8338.2, 8344.38, 8.73072103 ],
[ 1569966000, 8334.86, 8338.2, 8334.86, 8338.2, 0.56247241 ],
[ 1569965940, 8330.15, 8337.9, 8333.11, 8334.86, 5.16900961 ],
[ 1569965880, 8325.01, 8333.11, 8325.01, 8331.12, 4.84518559 ],
[ 1569965820, 8325, 8328.91, 8328.28, 8325.01, 3.86054705 ],
[ 1569965760, 8329.18, 8342.24, 8342.24, 8332.73, 16.40759054 ],
[ 1569965700, 8327.37, 8341.83, 8329.72, 8341.37, 16.20994008 ],
[ 1569965640, 8329.15, 8341.37, 8341.37, 8331.42, 8.02071131 ],
[ 1569965580, 8341.37, 8348.89, 8344.3, 8341.38, 4.94595945 ],
[ 1569965520, 8343.18, 8357.47, 8355, 8344.29, 22.636711 ],
[ 1569965460, 8355, 8364.99, 8364.99, 8355, 5.74740847 ],
[ 1569965400, 8363, 8375.01, 8375.01, 8364.99, 25.86207619 ],
[ 1569965340, 8375, 8376.6, 8376.49, 8375.01, 20.96584449 ],
...
答案 0 :(得分:1)
还没有使用官方的节点客户端,但是我已经实现了数百个这样的交换API。如果我没记错的话,日期必须在ISO-8601中。似乎您的开始值和结束值也被翻转了。 Start应该是按时间顺序排列的第一个数据点。因此开始应为fifteen_minutes_ago
,结束应为now
。
这是您的客户端使用的api端点(带有我自己的日期)
https://api.pro.coinbase.com/products/BTC-USD/candles?granularity=60&start=2019-10-02T00:49:16.000Z&end=2019-10-02T01:04:16.000Z
这是我收到的输出
[
[1569978240, 8316.98, 8320, 8317.18, 8319.99, 2.00418995],
[1569978180, 8314.47, 8317.18, 8315.16, 8317.18, 2.13490103],
[1569978120, 8300.99, 8317.29, 8301, 8315.16, 0.92309748],
[1569978060, 8300.29, 8305.01, 8305.01, 8301, 1.5700159],
[1569978000, 8300.28, 8309.09, 8307.02, 8300.28, 7.96919609],
[1569977940, 8306.54, 8315.75, 8315.75, 8309.15, 10.07108197],
[1569977880, 8315.56, 8318.17, 8316.24, 8316.25, 1.66859863],
[1569977820, 8313.51, 8316.29, 8314.69, 8316.24, 0.8088554],
[1569977760, 8312.02, 8324.97, 8322.97, 8314.36, 3.00710784],
[1569977700, 8322.91, 8324.99, 8324.99, 8322.97, 1.2712065],
[1569977640, 8324.99, 8325, 8325, 8325, 0.67001059],
[1569977580, 8317.01, 8325, 8317.01, 8324.99, 4.42580389],
[1569977520, 8311.12, 8317.5, 8315.88, 8316.52, 3.47023399],
[1569977460, 8312.49, 8320.77, 8312.5, 8317.49, 5.45514162],
[1569977400, 8311.14, 8317.31, 8317.31, 8314.2, 1.81527048]
]
答案 1 :(得分:0)
确定找到了罪魁祸首,我发布的代码的第二个参数是“ stop”而不是“ end”,不知道为什么要这么做...:-)
这完全符合预期:
publicClient
.getProductHistoricRates('BTC-USD', { start: now, end: fifteen_minutes_ago, granularity: 60 })