首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GDAX api,创建烛图

GDAX api,创建烛图
EN

Stack Overflow用户
提问于 2017-12-22 18:19:03
回答 2查看 1.1K关注 0票数 1

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

EN

回答 2

Stack Overflow用户

发布于 2018-10-12 12:16:39

下面是使用GDAX websocket从match频道构建的1m个烛台

代码语言:javascript
复制
"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
  }

})
票数 2
EN

Stack Overflow用户

发布于 2018-03-20 17:21:29

他们的建议是从这个端点获取历史利率https://docs.gdax.com/#get-historic-rates,然后使用Websocket feed消息使您的蜡烛保持最新-每当收到'match'/ticker消息时,您就会相应地更新最后一根蜡烛。

文档中写道:“单次请求的最大数据点数为300支。如果您选择的开始/结束时间和粒度将导致超过300个数据点,则您的请求将被拒绝。”这可能就是为什么你不能获得超过2天的数据。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47939781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档