• Stars
    star
    459
  • Rank 95,365 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

BITFINEX NodeJS trading API - Bitcoin, Litecoin, and Ether exchange

Bitfinex WSv2 Trading API for Node.JS - Bitcoin, Ethereum, Ripple and more

Build Status

A Node.JS reference implementation of the Bitfinex API

Features

  • Official implementation
  • REST v2 API
  • WebSockets v2 API
  • WebSockets v1 API

Installation

  npm i --save bitfinex-api-node

Quickstart

const { WSv2 } = require('bitfinex-api-node')
const ws = new WSv2({ transform: true })

// do something with ws client

Docs

Refer to the docs/ folder for JSDoc-generated HTML documentation, and the examples/ folder for executable examples covering common use cases.

Official API documentation at https://docs.bitfinex.com/v2/reference

Examples

Sending an order & tracking status:

const ws = bfx.ws()

ws.on('error', (err) => console.log(err))
ws.on('open', ws.auth.bind(ws))

ws.once('auth', () => {
  const o = new Order({
    cid: Date.now(),
    symbol: 'tETHUSD',
    amount: 0.1,
    type: Order.type.MARKET
  }, ws)

  // Enable automatic updates
  o.registerListeners()

  o.on('update', () => {
    console.log(`order updated: ${o.serialize()}`)
  })

  o.on('close', () => {
    console.log(`order closed: ${o.status}`)
    ws.close()
  })

  o.submit().then(() => {
    console.log(`submitted order ${o.id}`)
  }).catch((err) => {
    console.error(err)
    ws.close()
  })
})

ws.open()

Cancel all open orders

const ws = bfx.ws(2)

ws.on('error', (err) => console.log(err))
ws.on('open', ws.auth.bind(ws))

ws.onOrderSnapshot({}, (orders) => {
  if (orders.length === 0) {
    console.log('no open orders')
    return
  }

  console.log(`recv ${orders.length} open orders`)

  ws.cancelOrders(orders).then(() => {
    console.log('cancelled orders')
  })
})

ws.open()

Subscribe to trades by pair

const ws = bfx.ws(2)

ws.on('error', (err) => console.log(err))
ws.on('open', () => {
  ws.subscribeTrades('BTCUSD')
})

ws.onTrades({ symbol: 'tBTCUSD' }, (trades) => {
  console.log(`trades: ${JSON.stringify(trades)}`)
})
ws.onTradeEntry({ symbol: 'tBTCUSD' }, (trades) => {
  console.log(`te: ${JSON.stringify(trades)}`)
})

ws.open()

Version 2.0.0 Breaking changes

constructor takes only an options object now, including the API keys

Old:

new BFX(API_KEY, API_SECRET, { version: 2 })

since 2.0.0:

new BFX({ apiKey: '', apiSecret: '' })

trade and orderbook snapshots are emitted as nested lists

To make dealing with snapshots better predictable, snapshots are emitted as an array.

normalized orderbooks for R0

Lists of raw orderbooks (R0) are ordered in the same order as P0, P1, P2, P3

Testing

npm test

FAQ

Order Creation Limits

The base limit per-user is 1,000 orders per 5 minute interval, and is shared between all account API connections. It increases proportionally to your trade volume based on the following formula:

1000 + (TOTAL_PAIRS_PLATFORM * 60 * 5) / (250000000 / USER_VOL_LAST_30d)

Where TOTAL_PAIRS_PLATFORM is the number of pairs shared between Ethfinex/Bitfinex (currently ~101) and USER_VOL_LAST_30d is in USD.

'on' Packet Guarantees

No; if your order fills immediately, the first packet referencing the order will be an oc signaling the order has closed. If the order fills partially immediately after creation, an on packet will arrive with a status of PARTIALLY FILLED...

For example, if you submit a LIMIT buy for 0.2 BTC and it is added to the order book, an on packet will arrive via ws2. After a partial fill of 0.1 BTC, an ou packet will arrive, followed by a final oc after the remaining 0.1 BTC fills.

On the other hand, if the order fills immediately for 0.2 BTC, you will only receive an oc packet.

Nonce too small

I make multiple parallel request and I receive an error that the nonce is too small. What does it mean?

Nonces are used to guard against replay attacks. When multiple HTTP requests arrive at the API with the wrong nonce, e.g. because of an async timing issue, the API will reject the request.

If you need to go parallel, you have to use multiple API keys right now.

te vs tu Messages

A te packet is sent first to the client immediately after a trade has been matched & executed, followed by a tu message once it has completed processing. During times of high load, the tu message may be noticably delayed, and as such only the te message should be used for a realtime feed.

Sequencing

If you enable sequencing on v2 of the WS API, each incoming packet will have a public sequence number at the end, along with an auth sequence number in the case of channel 0 packets. The public seq numbers increment on each packet, and the auth seq numbers increment on each authenticated action (new orders, etc). These values allow you to verify that no packets have been missed/dropped, since they always increase monotonically.

Differences Between R* and P* Order Books

Order books with precision R0 are considered 'raw' and contain entries for each order submitted to the book, whereas P* books contain entries for each price level (which aggregate orders).

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

More Repositories

1

bitfinex-api-go

BITFINEX Go trading API - Bitcoin, Litecoin, and Ether exchange
Go
296
star
2

bfx-hf-ui

JavaScript
212
star
3

grenache

DHT based high-performance microservices framework, by Bitfinex
206
star
4

bitfinex-api-py

Python
200
star
5

bfx-hf-algo

JavaScript
90
star
6

bitfinex-api-rb

BITFINEX Ruby trading API - Bitcoin, Litecoin, and Ether exchange
Ruby
80
star
7

bitfinex-terminal

JavaScript
69
star
8

bfx-hf-indicators

JavaScript
58
star
9

grenache-grape

Grenache Grape implementation
JavaScript
53
star
10

sunbeam

JavaScript
47
star
11

bfxfixgw

Bitfinex FIX Gateway
Go
45
star
12

dazaar

JavaScript
45
star
13

bfx-hf-algo-server

JavaScript
44
star
14

smidgen

JavaScript
43
star
15

bfx-hf-strategy-py

Python
40
star
16

bfx-hf-indicators-py

Python
31
star
17

bfx-hf-strategy

JavaScript
25
star
18

bfx-report-electron

JavaScript
23
star
19

hypertele

A swiss-knife proxy powered by Hyperswarm DHT
JavaScript
20
star
20

grenache-nodejs-ws

Grenache Node.JS WebSocket implementation
JavaScript
19
star
21

dazaar-vision

JavaScript
18
star
22

eosfinex-custody-contract

C++
18
star
23

antani

Proof of Liabilities and Vote Delegation
JavaScript
16
star
24

bfx-hf-backtest

JavaScript
15
star
25

bfx-pay-woocommerce

PHP
14
star
26

pub

14
star
27

bfx-hf-chart

JavaScript
14
star
28

bfx-hf-data-server

JavaScript
13
star
29

bfx-stuff-ui

Bitfinex UI Goodies
JavaScript
13
star
30

grenache-nodejs-http

Grenache Node.JS WebSocket implementation
JavaScript
13
star
31

bfx-api-node-models

JavaScript
12
star
32

bfx-api-node-rest

JavaScript
12
star
33

bfx-reports-framework

JavaScript
10
star
34

bfx-report-ui

JavaScript
10
star
35

grenache-cli

The Grenache Command Line Interface.
M4
8
star
36

svc-js-cli

JavaScript
8
star
37

bfx-api-excel-demo

Visual Basic 6.0
7
star
38

bfx-report

JavaScript
7
star
39

bfx-hf-server

JavaScript
7
star
40

grenache-nodejs-utp

JavaScript
7
star
41

caron

Atomic Job enqueuer from Redis lists to popular Job Queues (Sidekiq, Resque, Bull, ...)
JavaScript
7
star
42

ufx-ui

JavaScript
7
star
43

hypercore-benchmark

JavaScript
6
star
44

grenache-rust

Rust
6
star
45

grenache-ruby-http

Ruby
6
star
46

mobile-pub

6
star
47

dazaar-payment

JavaScript
6
star
48

bip

6
star
49

hypercore-bisect

JavaScript
6
star
50

bfx-api-node-core

JavaScript
5
star
51

bfx-api-mock-srv

JavaScript
5
star
52

bfx-util-net-js

JavaScript
5
star
53

bfx-hf-models

JavaScript
5
star
54

ulisse

MySQL table snapshots and real time updates to Redis ordered queue
JavaScript
5
star
55

bfx-hf-util

JavaScript
5
star
56

bfx-ost-streamer

The Bitfinex osTicket Streamer Plugin.
PHP
5
star
57

dazaar-payment-lightning

JavaScript
5
star
58

lokue

LokiJS Job Queue
JavaScript
4
star
59

grenache-nodejs-zmq

Grenache Node.JS ZeroMQ implementation, by Bitfinex
JavaScript
4
star
60

bfx-svc-js

Shell
4
star
61

wasteland

JavaScript
4
star
62

bfx-api-node-util

JavaScript
3
star
63

bfx-hf-models-adapter-template

JavaScript
3
star
64

bfx-report-express

JavaScript
3
star
65

pesto-rb

Redis based distributed lock for ruby
Ruby
3
star
66

mutail

Simple and fast file(s) tail. Works with wildcards.
JavaScript
3
star
67

bfx-cli

3
star
68

dazaar-cli

JavaScript
3
star
69

bfx-api-node-plugin-example

JavaScript
3
star
70

cheesebox

JavaScript
3
star
71

grenache-ruby-ws

Ruby
3
star
72

bfx-hf-strategy-exec

JavaScript
3
star
73

grenache-nodejs-base

JavaScript
3
star
74

moonbeam

JavaScript
3
star
75

dazaar-guild

JavaScript
3
star
76

bitfinex-api-php

PHP
3
star
77

bfx-ext-eos-multisig

JavaScript
3
star
78

bfx-hf-ext-plugin-dummy

JavaScript
3
star
79

grenache-nodejs-link

JavaScript
3
star
80

bfx-facs-auth-google

JavaScript
2
star
81

bfx-api-node-plugin-managed-ob

JavaScript
2
star
82

moonbeam-history

JavaScript
2
star
83

bfx-lib-server-js

JavaScript
2
star
84

eos-block-stream

JavaScript
2
star
85

grenache-nodejs-ws-tls

JavaScript
2
star
86

affiliates-nicknames

JavaScript
2
star
87

bitfinex-terminal-order-book

JavaScript
2
star
88

bfx-api-node-plugin-managed-candles

JavaScript
2
star
89

dazaar-www

CSS
2
star
90

hypercore-tutorial

2
star
91

lib-js-util-schema

2
star
92

bfx-api-node-plugin-ob-checksum

JavaScript
2
star
93

bfx-hf-strategy-dazaar

JavaScript
2
star
94

bfx-hf-ui-config

JavaScript
2
star
95

bfx-api-node-plugin-wd

JavaScript
2
star
96

bfx-util-js

Shell
2
star
97

bfx-api-node-plugin-seq-audit

JavaScript
2
star
98

dazaar-scatter-pay

JavaScript
2
star
99

bfx-api-node-mock-data

2
star
100

lib-js-util-math

JavaScript
2
star