1

我想使用 GDAX api 创建一个蜡烛图。我目前正在使用 HTTP 请求历史数据https://docs.gdax.com/#get-historic-rates错误,这被标记为我应该使用 websocket API。不幸的是,我不知道如何通过 Gdax websocket api https://github.com/coinbase/gdax-node处理历史数据。有人可以帮助我吗?

4

2 回答 2

2

这是使用 GDAX websocket 从匹配通道构建的 1m 烛台

"use strict";
const
  WebSocket = require('ws'),
  PRECISION = 8

function _getPair(pair) {
  return pair.split('-')
}

let ws = new WebSocket('wss://ws-feed.pro.coinbase.com')

ws.on('open', () => {

  ws.send(JSON.stringify({
    "type": "subscribe",
    "product_ids": [
      "ETH-USD",
      "BTC-USD"
    ],
    "channels": [
      "matches"
    ]
  }))
})

let candles = {}
let lastCandleMap = {}

ws.on('message', msg => {
  msg = JSON.parse(msg);
  if (!msg.price)
    return;

  if (!msg.size)
    return;

  // Price and volume are sent as strings by the API
  msg.price = parseFloat(msg.price)
  msg.size = parseFloat(msg.size)

  let productId = msg.product_id;
  let [base, quote] = _getPair(productId);

  // Round the time to the nearest minute, Change as per your resolution
  let roundedTime = Math.floor(new Date(msg.time) / 60000.0) * 60

  // If the candles hashmap doesnt have this product id create an empty object for that id
  if (!candles[productId]) {
    candles[productId] = {}
  }


  // If the current product's candle at the latest rounded timestamp doesnt exist, create it
  if (!candles[productId][roundedTime]) {

    //Before creating a new candle, lets mark the old one as closed
    let lastCandle = lastCandleMap[productId]

    if (lastCandle) {
      lastCandle.closed = true;
      delete candles[productId][lastCandle.timestamp]
    }

    // Set Quote Volume to -1 as GDAX doesnt supply it
    candles[productId][roundedTime] = {
      timestamp: roundedTime,
      open: msg.price,
      high: msg.price,
      low: msg.price,
      close: msg.price,
      baseVolume: msg.size,
      quoteVolume: -1,
      closed: false
    }
  }

  // If this timestamp exists in our map for the product id, we need to update an existing candle
  else {
    let candle = candles[productId][roundedTime]
    candle.high = msg.price > candle.high ? msg.price : candle.high
    candle.low = msg.price < candle.low ? msg.price : candle.low
    candle.close = msg.price
    candle.baseVolume = parseFloat((candle.baseVolume + msg.size).toFixed(PRECISION))

    // Set the last candle as the one we just updated
    lastCandleMap[productId] = candle
  }

})
于 2018-10-12T04:16:39.037 回答
1

他们的建议是从该端点获取历史汇率https://docs.gdax.com/#get-historic-rates,然后使用 Websocket 提要消息使您的蜡烛保持最新 - 每当“匹配”/收到报价机消息,您相应地更新最后一根蜡烛。

来自文档:“单个请求的最大数据点数为 300 个蜡烛。如果您选择的开始/结束时间和粒度将导致超过 300 个数据点,您的请求将被拒绝。” 所以可能为什么你不能得到超过 2 天的数据。

附言。我在这里实现了一个实时订单簿和一个基本的蜡烛图 - 很多还没有完全连接在一起,但它至少可以预览,直到它完成https://github.com/robevansuk/gdax-java/

于 2018-03-20T09:21:29.213 回答