GithubHelp home page GithubHelp logo

gdax-candles's People

Contributors

flippo24 avatar swimclan avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

gdax-candles's Issues

Why is the code so bloated?

I am sorry if this is a silly question. I went through your code and saw that you have a clock class with a setInterval that runs every 1 millisecond (very heavy on the CPU) and a websocket that opens one connection per pair (not a great idea) and a price class that is an event emitter coupled with rxjs on top of everything, why have you overcomplicated everything. The code below generates the same candles without running setInterval or rxjs fancy stuff. You can have an EventEmitter emit a candle and clear the old candle from the candles object below.

  • Is this something you tried before and had some issues because of which you went the complex route?

  • Another serious issue is inside your clock class in the setInterval, you are calling tick method every millisecond, I benchmarked and some of its runs take 1 millisecond, this is not good as the operations will add up if the duration of the tick s execution were to go beyond a millisecond

  • Also it doesnt look like you are using the timestamp of the incoming matches to set their date, your date will always be off by a small margin because of the lag between the actual event and the time you get the event

I would like to hear your opinions on this


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

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

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

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

let candles = {}

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

  if (!msg.size)
    return;

  msg.price = parseFloat(msg.price)
  msg.size = parseFloat(msg.size)
  let roundedTime = Math.floor(new Date(msg.time).getTime()/60000) * 60

  if(!candles[roundedTime]){

    let keys = Object.keys(candles).sort()
    console.log(candles[keys[keys.length - 1]])

    candles[roundedTime] = {
      time: roundedTime,
      open: msg.price,
      high: msg.price,
      low: msg.price,
      close: msg.price,
      volume: msg.size
    }
  }
  else{
    let candle = candles[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.volume += msg.size
  }

  

  setTimeout(() => {
    ws.terminate()
  }, 1200000)
})



Take a look at what happens here sometimes

  tick() {
    let start = Date.now().valueOf()
    let emit = false;
    this.now = Date.now().valueOf();
    for (var resolution in this.nextTimes) {
      if (this.now >= this.nextTimes[resolution]) {
        this.currentTicks.push(resolution);
        emit = true;
      }
    }
    emit && this.emit('tick', this.currentTicks);
    this.getNextTimes();
    this.currentTicks = [];
    console.log('ticking',Date.now().valueOf() - start);
  }

How to get more then one currency?

I am trying to get two currencies, but ich only get candles for one.
What i am doing wrong?

const Chart = require('./src/Chart');

const product = 'ETH-USD';
const product2 = 'BTC-USD';
const timeframe = '1m';
const ethereumChart = new Chart({ product, timeframe }).start();
const bitcoinChart = new Chart({ product2, timeframe }).start();

ethereumChart.on('close', candle => {
  console.log(candle);
});
bitcoinChart.on('close', candle => {
  console.log(candle);
});

and got:

Candlestick {
  timestamp: 2018-07-04T14:07:55.024Z,
  open: 474.74,
  price: 475.09,
  close: 475.09,
  high: 475.51,
  low: 474.74,
  closed: true,
  height: 0.000736702519522545,
  spread: 0.0016207455429497186,
  volume: 22.706467770000007,
  sma: {},
  ema: {},
  color: 'green',
  regression: {} }
Candlestick {
  timestamp: 2018-07-04T14:07:55.024Z,
  open: 474.74,
  price: 475.09,
  close: 475.09,
  high: 475.51,
  low: 474.74,
  closed: true,
  height: 0.000736702519522545,
  spread: 0.0016207455429497186,
  volume: 22.706467770000007,
  sma: {},
  ema: {},
  color: 'green',
  regression: {} }

volume differences in different timeframes

I tried the same product in two different timeframes. 1m and 2m. The volume in the 2m candle should be addition of the both 1m candles, or i am wrong? But this is different. Ohlc seams right to me.

const Chart = require('./src/Chart');

const product = 'ETH-USD';
const timeframe = '1m';
const timeframe2 = '2m';
const ethereumChart1 = new Chart( product, timeframe ).start();
const ethereumChart2 = new Chart( product, timeframe2 ).start();

ethereumChart1.on('close', candle => {
  console.log('timeframe 1m');
  console.log(candle);
});

ethereumChart2.on('close', candle => {
  console.log('timeframe 2m');
  console.log(candle);
});

output:

timeframe 1m
Candlestick {
  timestamp: 2018-07-04T20:42:52.620Z,
  product: 'ETH-USD',
  open: 473.72,
  price: 473.72,
  close: 473.72,
  high: 473.72,
  low: 473.72,
  closed: true,
  height: 0,
  spread: 0,
  volume: 15.489755259999999,
  sma: {},
  ema: {},
  color: 'green',
  regression: {} }
timeframe 2m
Candlestick {
  timestamp: 2018-07-04T20:43:49.910Z,
  product: 'ETH-USD',
  open: 473.72,
  price: 473.72,
  close: 473.72,
  high: 473.72,
  low: 473.71,
  closed: true,
  height: 0,
  spread: 0.00002110951616999018,
  volume: 13.47577175,
  sma: {},
  ema: {},
  color: 'green',
  regression: {} }
timeframe 1m
Candlestick {
  timestamp: 2018-07-04T20:43:49.910Z,
  product: 'ETH-USD',
  open: 473.72,
  price: 473.72,
  close: 473.72,
  high: 473.72,
  low: 473.71,
  closed: true,
  height: 0,
  spread: 0.00002110951616999018,
  volume: 5.78294257,
  sma: {},
  ema: {},
  color: 'green',
  regression: {} }

For me only the github source is working and i have to remove the bracket in Chart constructor. If i leave them, then always 'BTC-USD' and 1s timeframe will be used.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.