GDAX api,创建蜡烛图

时间:2017-12-22 10:19:03

标签: node.js websocket gdax-api

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

2 个答案:

答案 0 :(得分:0)

他们建议的是从此终点获取历史费率https://docs.gdax.com/#get-historic-rates,然后使用Websocket订阅源消息更新您的蜡烛 - 每当收到“匹配”/自动收报机消息时,您更新最后一支蜡烛。

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

PS。我有一个在这里实施的实时订单和一个基本的蜡烛棒图表 - 许多没有完全挂钩,但它至少可以预览,直到它完整https://github.com/robevansuk/gdax-java/

答案 1 :(得分:0)

这是使用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
  }

})