Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

bfx-api-node-models

bitfinexcom1.7kMIT2.0.1

Object models for usage with the Bitfinex node API

bitfinex, bitcoin, BTC

readme

Bitfinex Data Models for Node.JS

Build Status

This repo contains model classes for working with the data structures returned by the Bitfinex REST & WebSocket APIs. The models can all be initialized with an array-format payload as returned by an API call, and can be unserialized back to the array format when needed.

Some models, such as Order and OrderBook provide higher level methods which operate on the underlying data sets.

All models provide serialize() and unserialize() methods, which convert to/from array-format payloads respectively. All model constructors can take either array-format payloads, or objects/other model instances. A helper toJS() method is also provided for converting models to plain JS objects (POJOs).

Features

  • Convert between array, object, and class representations of API data
  • Class methods for operating on model data where applicable (i.e. OrderBook)

Classes for the following Bitfinex API data types:

  • Alert
  • BalanceInfo
  • Candle
  • Currency
  • FundingCredit
  • FundingInfo
  • FundingLoan
  • FundingOffer
  • FundingTickerHist
  • FundingTicker
  • FundingTrade
  • LedgerEntry
  • Liquidations
  • MarginInfo
  • Movement
  • Notification
  • OrderBook
  • Order
  • Position
  • PublicTrade
  • StatusMessagesDeriv
  • Trade
  • TradingTicker
  • TradingTickerHist
  • UserInfo
  • Wallet
  • WalletHist
  • Currency
  • SymbolDetails
  • TransactionFee
  • AccountSummary
  • AuthPermission

Installation

npm i --save bfx-api-node-models

Quickstart

const { Order } = require('bfx-api-node-models')

const o = new Order({
  cid: Date.now(),
  symbol: 'tBTCUSD',
  price: 7000.0,
  amount: -0.02,
  type: Order.type.EXCHANGE_LIMIT
})

// Generate an API-compatible order creation packet for later submit
console.log(o.toNewOrderPacket())

Docs

Refer to the docs/ folder for JSDoc-generated API documentation covering each model class.

Examples

The order model provides helper methods for order submission, updates, and cancellation. These methods are compatible with version 2.0.0 of bitfinex-api-node, and return promises which resolve upon receival of the relevant success/error notifications.

Orders are matched with their API packets by one/all of id, gid, and cid.

Example usage:

const { Order } = require('bfx-api-node-models')
const ws = ... // setup WSv2 instance for order updates/submission

// Build new order
const o = new Order({
  cid: Date.now(),
  symbol: 'tBTCUSD',
  price: 7000.0,
  amount: -0.02,
  type: Order.type.EXCHANGE_LIMIT
}, ws) // note WSv2 client passed in here

let closed = false

// Enable automatic updates
o.registerListeners()

o.on('update', () => {
  debug('order updated: %j', o.serialize())
})

o.on('close', () => {
  debug('order closed: %s', o.status)
  closed = true
})

debug('submitting order %d', o.cid)

o.submit().then(() => {
  debug('got submit confirmation for order %d [%d]', o.cid, o.id)
}).catch((err) => {
  debug('failed to submit order: %s', err.message)
})

The order book model constructor takes either entire book snapshots as returned by the WSv2 API, or individual update packets with single bids/asks. Once constructed, order books may be updated either with complete snapshots via updateFromSnapshot(snapshot) or individual update packets via updateWidth(entry).

Static helpers are also provided for working with array-format order books, in the form of updateArrayOBWith(ob, entry, raw), arrayOBMidPrice(ob, raw), and checksumArr(ob, raw).

Checksums may be calculated for normal books via checksum(), for comparison with the checksums reported by the WSv2 API.

Example usage:

const ob = new OrderBook([
  [140, 1, 10],
  [145, 1, 10],
  [148, 1, 10],
  [149, 1, 10],
  [151, 1, -10],
  [152, 1, -10],
  [158, 1, -10],
  [160, 1, -10]
])

ob.updateWith([145, 3, 15]) // update bid
ob.updateWith([158, 3, -15]) // update ask

console.log(ob.serialize())

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

changelog

2.0.1

  • chore: bump bfx-api-node-rest to 6.0.0

2.0.0

  • chore: deprecated support for pulse endpoints

1.8.2

  • feat: add IOC order type

1.8.1

  • fix: Add extra Weighted Averages fields for trades timestamps

1.8.0

  • feature: Weighted Averages model

1.7.1

  • fix: Change FundingOffer, Order, Position model to support new rest api signature
  • fix: Test coverage to support new rest api signature

1.7.0

  • feature: Currency Movement Info model

1.6.3

  • fix: public trade unserializing from snapshot

1.6.2

  • feature: visible on hit option supported for hidden orders

1.6.0

  • feature: Core settings model

1.5.0

  • feature: added isPaperTradeEnabled info in user info model

1.4.1

  • fix: validate inputs on updateWith method in order book model

1.4.0

  • chore: standard upgrade
  • chore: remove babel

1.3.3

  • feature: add extra keys to account summary model

1.3.2

  • feature: add note to movement model

1.3.1

  • feature: enriched pulse message model to include pulse comments

1.3.0

  • feature: add multi level parser to base model class
  • feature: add symbol details model
  • feature: add transaction fee model
  • feature: add account summary model
  • feature: add auth permission model

1.2.11

  • feature: add clampMin to StatusMessagesDeriv model
  • feature: add clampMax to StatusMessagesDeriv model

1.2.10

  • feature: add walletFx to currency model
  • fix: currencies explorer is an array not a string

1.2.9

  • feature: add Invoice model

1.2.8

  • feature: enriched pulse message model to include pulse profile

1.2.7

  • feature: add pulse model

1.2.6

  • feature: add public pulse profile model

1.2.5

  • feature: add change log model

1.2.4

  • meta: added JSDoc-generated HTML docs
  • meta: standardized linter config

1.2.3

  • fix: index lookup in validation of array-format models

1.2.2

  • fix: make bfx-api-node-rest dev-dep to break circular dependency

1.2.1

  • fix: move bfx-hf-util and bfx-api-node-rest to deps from dev deps

1.2.0

  • meta: bumped dependencies
  • meta: added husky pre-commit test hook
  • feature: add validation support to models
  • refactor: core Model() class method signatures to reduce boilerplate
  • Position feature: orderToClose() method
  • Model fix: toJS() unserialize method invocation parameters

1.1.10

  • feature: add ability to specify lev in new Order object

1.1.9

  • feature: Add type to FundingCredit and FundingLoan

1.1.8

  • Order feature: add toString() method

1.1.7

  • fix: increase test timeout in a few places

1.1.6

1.1.5

  • feature: support affiliateCode field in Order class

1.1.4

  • feature: Login model

1.1.3

  • models/Order: fix OCO field population on on packets

1.1.2

  • models/order: add fields hidden, routing and meta

1.1.1

  • docs: create/update

1.1.0

  • adds function Position.claim
  • adds function FundingOffer.submit
  • adds function FundingOffer.close
  • adds function FundingOffer.cancel

1.0.13

  • fix: (Model bool field handling) un-quote key in indexOf call
  • manifest: add build script
  • manifest: update dependencies
  • meta: standardize travis conig
  • meta: add github issue/pr templates
  • meta: init changelog

1.0.12

  • Order fix: prepare prices & amounts in new order packet
  • Order refactor: require gid to match to apply an update

1.0.11

  • Order fix: init reduce-only flag in constructor
  • Position fix: add missing fields
  • feature: StatusMessagesDeriv model
  • feature: Liquidation model

1.0.10

  • feature: make all model collections iterable

1.0.9

  • Currency: add symbol field

1.0.8

  • feature: populate tif & mtsTIF on Order

1.0.7

  • refactor: convert PositionHist to Position

1.0.6

  • feature: WalletHistory model
  • feature: PositionHistory model
  • feature: TradingTickerHist model
  • feature: FundingTickerHist model
  • fix: tests for FundingTicker & TradingTicker

1.0.5

  • OrderBook refactor: assumy OB snapshots are sorted in update method
  • OrderBook fix: support update with empty snapshot
  • feature: add live REST tests where possible

1.0.4

  • OrderBook fix: guard against empty array OB updates in updateArrayOBWith

1.0.3

  • fix: update FundingTicker & TradingTicker formats

1.0.2

  • fix: allow creating orders w/ amount or amountOrig 0

1.0.1

  • feature: Currency model
  • feature: UserInfo model
  • LedgerEntry feature: add wallet field

1.0.0

  • Initial version