bitfinexcom / bitfinex-api-node

BITFINEX NodeJS trading API - Bitcoin, Litecoin, and Ether exchange
https://www.bitfinex.com/
MIT License
462 stars 213 forks source link

REST API v2 candles always return [] ! #223

Closed jwchen119 closed 6 years ago

jwchen119 commented 6 years ago

Here's part of my code below. However, it always return empty array.

const BFX = require('bitfinex-api-node')
const bfxRest = new BFX('1234', '4321', {version: 2}).rest

var opts = {};
opts.timeframe= "1m";
opts.symbol= "tIOTUSD";
opts.section= "hist";

bfxRest.candles(opts, (err, res) => {
    if (err) console.log(err)
    console.log(res)
})

I also tried : bfxRest.candles((err, res) => {

Nothing helped, it still return []. I'm pretty sure there's some bugs, somehow I'm not able to point it out.

f3rno commented 6 years ago

@jwchen119 are you using the latest version of the library (v2 beta)? If so, the constructor signature changed, it should be:

const bfxRest = new BFX({
  apiKey: '...'
  apiSecret: '...'
}).rest(2)
jwchen119 commented 6 years ago

Yes @f3rno , I'm trying to use the candles function which only available in v2 beta. By migrating v1 to v2, there's some new problem here. I think it might be the problem of my environment? My full code is below, I'm using Heroku to build my app for testing:

'use strict';

const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
const BFX = require('bitfinex-api-node');

const bfx = new BFX({ 
    apiKey: '1234',
    apiSecret: '4321',
})

const bfxRest = bfx.rest(2);

const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');

const server = express()
  .use((req, res) => res.sendFile(INDEX) )
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));

const wss = new SocketServer({ server });

wss.on('connection', (ws) => {
  console.log('Client connected');
  ws.on('close', () => console.log('Client disconnected'));
});

var Crypto = "請稍後....";

var opts = {timeframe:"30m", symbol:"tIOTUSD", section:"hist"};

bfxRest.candles(opts, (err, res) => {
    if (err) console.log(err)
    console.log(res)
})

setInterval(() => {
    wss.clients.forEach((client) => {
    bfxRest.ticker('tIOTUSD', (err, res) => {
        if (err) console.log(err)
        console.log(res)
        Crypto = res
    })
      client.send(JSON.stringify(Crypto));
    });
}, 3000);

Build succeeded but shows this error when start up:

const rest = bfx.rest(2)
                  ^
 TypeError: bfx.rest is not a function

I know here's not the place like StackOverflow, I'm also new to node.js. If this is not related to the sourcecode please let me know and I'll close this issue, thank you.

jwchen119 commented 6 years ago

Alright, I did make a mistake. I was using old version of "bitfinex-api-node" declare in package.json which lead to the problem. After using the version of "2.0.0-beta" all the problem solved.

f3rno commented 6 years ago

@jwchen119 Good, I'm glad you figured it out :) Let me know if you have any other questions