bitfinexcom / bitfinex-api-node

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

How to submit new order? #76

Closed nedievas closed 7 years ago

nedievas commented 7 years ago

Please, help me submit order, as documentation here is poor. My code (secret and key are hidden):

'use strict'
const BFX = require('bitfinex-api-node')
const API_KEY = 'my-secret'
const API_SECRET = 'my-secret'

const opts = {
  version: 2,
  transform: true
}

const bws = new BFX(API_KEY, API_SECRET, opts).ws

bws.on('open', () => {
  bws.auth()
  bws.subscribeTrades('BTCUSD')
//  bws.subscribeOrderBook('BTCUSD')
//  bws.subscribeTicker('BTCUSD')
})

var trades = msg.map(function (msg) {
  return {
        trade_id: msg.ID,
        time: msg.MTS,
        size: Math.abs(msg.AMOUNT),
        price: msg.PRICE,
        side: msg.AMOUNT > 0 ? 'buy' : 'sell'
      }
    })
//        console.log('Trades', trades)

})

bws.on('ticker', (pair, ticker) => {
  var ticks = {bid: ticker.BID, ask: ticker.ASK}
//  console.log('Ticker:', ticks)
})

bws.on('subscribed', (data) => {
  console.log('New subscription', data)
})

var order = ([
  0,
  'on',
  null,
  {
    type: 'EXCHANGE LIMIT',
    symbol: 'btcusd',
    amount: '0.01',
    price: 0.01,
    hidden: 0,
    postonly: 0
  }
])
bws.on('open', () => {
  bws.submitOrder(order)
  console.log(order);
})

bws.on('error', console.error)

I get this error:

[ 0,
  'on',
  null,
  { type: 'EXCHANGE LIMIT',
    symbol: 'btcusd',
    amount: '0.01',
    price: 0.01,
    hidden: 0,
    postonly: 0 } ]
{ event: 'error', msg: 'auth: invalid', code: 10100 }

Thanks!

robertkowalski commented 7 years ago

Hi nedievas,

it seems you forgot to authenticate yourself.

try to change your code to this:

bws.on('open', () => {
  bws.subscribeTrades('BTCUSD')
//  bws.subscribeOrderBook('BTCUSD')
//  bws.subscribeTicker('BTCUSD')

  bws.auth()  // authenticate with credentials
})

hope that helps.

nedievas commented 7 years ago

Hi. As you can see, I did. Please test code. Thanks

robertkowalski commented 7 years ago

ah ok, gotcha! thank you for your patience with me!

seems I replied to older code I got by emai from github.

try to send the order after the auth event is emitted. sent a PR in https://github.com/bitfinexcom/bitfinex-api-node/pull/78 with a slightly better example.