GithubHelp home page GithubHelp logo

bfx-api-node-models's Introduction

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
  • PulseMessage
  • 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

bfx-api-node-models's People

Contributors

avsek477 avatar daniel1984 avatar ezewer avatar f3rno avatar jacobplaster avatar leehillz avatar prdn avatar robertkowalski avatar tarcisiozf avatar vigan-abd avatar zimkaru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bfx-api-node-models's Issues

npm warnings

Hi - are these warnings something I should be worried about? No idea what bfx-hf-util is, I don't have in in my dependencies.

warning "bfx-api-node-models > bfx-hf-util > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".
warning "bfx-api-node-models > bfx-hf-util > @babel/preset-es2015 > @babel/[email protected]" has incorrect peer dependency "@babel/core@>=7.0.0-beta.50 <7.0.0-rc.0".

Refactor: Server-Valid Order Types & Status Enums

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

The Order model comes with 2 object keys, types and statuses built here: https://github.com/bitfinexcom/bfx-api-node-models/blob/master/lib/order.js#L728

Spaces are converted to underscores as convenience for the keys, but the original value is also saved as a key, and both copies point to the value. i.e. we have this:

Order.type.EXCHANGE_LIMIT = 'EXCHANGE LIMIT'
Order.type['EXCHANGE LIMIT'] = 'EXCHANGE LIMIT'

This needs to be refactored to only provide valid types (no underscores); it was done like this for ease of use but the duplicate keys make iteration difficult/filtering necessary in some places. Also becomes confusing seeing underscores in some places and spaces in others.

removeListeners doesn't work as expected for newly created orders, causing callback leak

Summary

There's an issue causing removeListeners to not remove all the order callbacks that were registered in registerListeners causing a leak, as new orders accumulate this greatly affects performance eventually killing he process

Issue Breakdown

  • Upon initial creation of an order with cid the cbGID will be undefined.{cid} since gid was never provided
  • Calling registerListeners will register the mentioned cbGID which later is used with removeListeners
  • Submitting the order will invoke an update message updating the gid to null (we listen to update to verify the order was successfully submitted
  • cbGID will now be null.{cid} mismatching the initial cbGID, rendering removeListeners useless which will increase callbacks size infinitely - this specifically affects systems automatically creating orders.

Sample code

(I took the sample code from https://github.com/bitfinexcom/bfx-api-node-models/#order marking where the issue is prominent)

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.removeListeners() // ISSUE - `cbGID` is not the same as before the `update`
})

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

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)
})

Incorrect funding ticker fields bidSize and bidPeriod, askSize and askPeriod. frrAmountAvailable is missing

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

Funding ticker fields bidSize and bidPeriod are swapped around.
https://github.com/bitfinexcom/bfx-api-node-models/blob/master/lib/funding_ticker.js#L9-L24

Correct position for bidSize is 4 and for bidPeriod is 3.
Similarly, correct position for askSize is 7 and for askPeriod is 6.

Also, field frrAmountAvailable is missing.

Const fields should be:

const fields = {
  symbol: 0,
  frr: 1,
  bid: 2,
  bidSize: 4,
  bidPeriod: 3,
  ask: 5,
  askSize: 7,
  askPeriod: 6,
  dailyChange: 8,
  dailyChangePerc: 9,
  lastPrice: 10,
  volume: 11,
  high: 12,
  low: 13,
  frrAmountAvailable: 14,
}

Reference: https://docs.bitfinex.com/reference#ws-public-ticker

Steps to reproduce

Additional Notes:

Faulty link in README.md

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

Cannot browse the documentation.

In README.md file, section Docs, the docs/ link points to https://cdn.statically.io/gh/bitfinexcom/bfx-api-node-models/master/docs/index.html and when clicking on the link it gives error 400 Bad Request. The user is not registered with the HTML service. To enable the service please contact https://twitter.com/staticallyio

Steps to reproduce

  • Click on docs/ link in Chrome

Position ID

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

I'm trying to get position ID but it seems to be missing I get something like this form socket.

["0":0,"1":"pu","2":["tETHUSD","ACTIVE",0.1499,1999.2,0,0,-0.2103996400000136,0.13005202080831876,0,0.009251598279286587,null,146081924,null,null,null,0,null,0,0,{"reason":"TRADE","order_id":59065482179,"order_id_oppo":59064777149,"liq_stage":null,"trade_price":"1999.2","trade_amount":"0.1499"}]}

Is position ID missing or is it 146081924?

0.009251598279286587 is PL_PERC and then according to documentation 146081924 should be LEVERAGE?

But it looks like some kind of broken timestamp. How do I make sense of this?

Orderbook checksum is incorrect with prices smaller than 0.000001

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

When an orderbook containing prices smaller than 0.000001, i.e. those which are stringified in e-notation, is passed to Orderbook.checksumArr(), it takes pains to ensure that those are stringified without e-notation. The resulting checksum value doesn't match the checksum from the Bitfinex websocket 'book' stream. In my own rather naive implementation I just let Node.js stringify those numbers in e-notation and the checksum does match.

I only tested Orderbook.checksumArr(), not Orderbook.checksum(), but the latter looks it probably behaves in the same way.

Steps to reproduce

const rawArr = [
    [5.6e-7, 1, 60712.4250231],
    [5.5e-7, 8, 1047414.30449527],
    [5.4e-7, 3, 26992.30308495],
    [5.3e-7, 1, 1597.7],
    [5.1e-7, 1, 300],
    [5e-7, 1, 4000],
    [4.9e-7, 1, 1000],
    [4.3e-7, 1, 2000],
    [4.2e-7, 1, 2000],
    [4.1e-7, 1, 2000],
    [4e-7, 3, 4452.125],
    [3.9e-7, 1, 2000],
    [3.8e-7, 1, 2000],
    [3.4e-7, 32, 10290.67],
    [3.3e-7, 2, 734.82],
    [3e-7, 1, 800],
    [2.9e-7, 1, 867],
    [2.8e-7, 1, 300],
    [2.7e-7, 1, 400.6],
    [2.6e-7, 1, 257],
    [2.5e-7, 2, 3600],
    [2.4e-7, 1, 108.5],
    [2.3e-7, 1, 8050],
    [2.1e-7, 1, 308],
    [2e-7, 1, 100],
    [5.7e-7, 6, -1106130.75562937],
    [5.8e-7, 1, -383.98454496],
    [5.9e-7, 1, -2501.6405316],
    [6.1e-7, 2, -416.6],
    [8.2e-7, 1, -2000],
    [8.9e-7, 1, -100],
    [9.2e-7, 1, -22222],
    [9.3e-7, 1, -500],
    [9.9e-7, 1, -500],
    [0.000001, 1, -250.37],
    [0.00000108, 1, -500],
    [0.0000011, 1, -129.576636],
    [0.00000111, 1, -29412.414691],
    [0.00000118, 1, -500],
    [0.00000119, 1, -100],
    [0.0000012, 1, -4000],
    [0.00000141, 1, -332.93],
    [0.00000165, 1, -152.76229702],
    [0.00000171, 1, -265.88],
    [0.00000175, 1, -653],
    [0.00000176, 1, -2.19],
    [0.0000018, 1, -4000],
    [0.000002, 1, -2722.65],
    [0.0000022, 1, -500],
    [0.0000024, 1, -4000]
];

console.log(bfx.OrderBook.checksumArr(rawArr));

Expected: -776929505
Result: 758960098

Subscribing to a market such as tVSYBTC where the prices are very small should give you your own real-world examples if you doubt.

Additional Notes:
  • Node v14.16.0
  • wss://api-pub.bitfinex.com/ws/2
  • Checksums enabled with ws.send(JSON.stringify({event: 'conf', flags: 131072}));

Symbol missing from transform Tickerdata

bid, bidSize, ask, askSize, dailyChange, dailyChangePerc,

Is this code awaiting a new server version?

Because this does not work for current production, as the first value in the ticker array is symbol.
Using this 'bid' becomes 'tBTCEUR', etc.

The bfx-api-node v2.0.2 is depending on this change and is as such not usable atm.

Regards
Wim

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.