How to use bfx-api-node-models - 10 common examples

To help you get started, we’ve selected a few bfx-api-node-models examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bitfinexcom / bitfinex-api-node / examples / rest2 / submit_order.js View on Github external
'use strict'

process.env.DEBUG = 'bfx:examples:*'

const debug = require('debug')('bfx:examples:submit_order')
const bfx = require('../bfx')
const { Order } = require('bfx-api-node-models')
const rest = bfx.rest(2)

debug('Submitting new order...')

// Build new order
const o = new Order({
  cid: Date.now(),
  symbol: 'tBTCUSD',
  price: 18000,
  amount: -0.02,
  type: Order.type.LIMIT,
  lev: 10
}, rest)

o.submit().then(() => {
  debug(
    'got submit confirmation for order %d [%d] [tif: %d]',
    o.cid, o.id, o.mtsTIF
  )
})
  .catch((err) => console.log(err))
github bitfinexcom / bitfinex-api-node / examples / rest2 / submit_order.js View on Github external
process.env.DEBUG = 'bfx:examples:*'

const debug = require('debug')('bfx:examples:submit_order')
const bfx = require('../bfx')
const { Order } = require('bfx-api-node-models')
const rest = bfx.rest(2)

debug('Submitting new order...')

// Build new order
const o = new Order({
  cid: Date.now(),
  symbol: 'tBTCUSD',
  price: 18000,
  amount: -0.02,
  type: Order.type.LIMIT,
  lev: 10
}, rest)

o.submit().then(() => {
  debug(
    'got submit confirmation for order %d [%d] [tif: %d]',
    o.cid, o.id, o.mtsTIF
  )
})
  .catch((err) => console.log(err))

// update order

setTimeout(() => {
  o.update({
    price: 17000
github bitfinexcom / bitfinex-api-node / examples / rest2 / submit_funding_offer.js View on Github external
'use strict'

process.env.DEBUG = 'bfx:examples:*'

const debug = require('debug')('bfx:examples:submit_order')
const bfx = require('../bfx')
const { FundingOffer } = require('bfx-api-node-models')
const rest = bfx.rest(2, { transform: true })

debug('Submitting new order...')

// Build new order
const fo = new FundingOffer({
  type: 'LIMIT',
  symbol: 'fUSD',
  rate: 0.0120000,
  amount: 120,
  period: 2
}, rest)

fo.submit().then((fo) => {
  debug('Submitted funding offer', fo.id)
})
  .catch((err) => console.log(err))

// cancel offer

// setTimeout(() => {
//   fo.cancel()
github bitfinexcom / bitfinex-api-node / examples / ws2 / atomic_order_update.js View on Github external
ws.onOrderBook({ symbol: SYMBOL }, (ob) => {
  const topBidL = ob.topBidLevel()

  if (topBidL === null || orderSent) {
    return
  }

  debug('taking out price level: %j', topBidL)

  const o = new Order({
    symbol: SYMBOL,
    type: Order.type.EXCHANGE_LIMIT,
    price: topBidL[0],
    amount: topBidL[2] * -1.1 // sell through top bid
  }, ws)

  o.registerListeners()
  o.submit().then(() => debug('order submitted'))
  o.once('update', (o) => {
    debug('got order update: %s', o.status)

    if (o.isPartiallyFilled()) {
      debug('order is partially filled, amount %f', o.amount)
      debug('increasing amount w/ delta %f', o.amount * 2)

      o.update({ delta: `${o.amount * 2}` }).then(() => {
github bitfinexcom / bitfinex-api-node / examples / ws2 / atomic_order_update.js View on Github external
ws.onOrderBook({ symbol: SYMBOL }, (ob) => {
  const topBidL = ob.topBidLevel()

  if (topBidL === null || orderSent) {
    return
  }

  debug('taking out price level: %j', topBidL)

  const o = new Order({
    symbol: SYMBOL,
    type: Order.type.EXCHANGE_LIMIT,
    price: topBidL[0],
    amount: topBidL[2] * -1.1 // sell through top bid
  }, ws)

  o.registerListeners()
  o.submit().then(() => debug('order submitted'))
  o.once('update', (o) => {
    debug('got order update: %s', o.status)

    if (o.isPartiallyFilled()) {
      debug('order is partially filled, amount %f', o.amount)
      debug('increasing amount w/ delta %f', o.amount * 2)

      o.update({ delta: `${o.amount * 2}` }).then(() => {
        debug('order updated, new amount %f', o.amount)
        debug('setting price to %f', o.price * 1.05)
github bitfinexcom / bitfinex-api-node / examples / ws2 / order_tif.js View on Github external
ws.once('auth', () => {
  debug('authenticated')

  // Build new order
  const o = new Order({
    cid: Date.now(),
    symbol: 'tBTCUSD',
    price: 17833.5,
    amount: -0.02,
    type: Order.type.LIMIT,
    tif: '2019-03-08 15:00:00'
  }, ws)

  o.registerListeners()

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

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

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

  o.submit().then(() => {
github bitfinexcom / bitfinex-api-node / lib / transports / ws2.js View on Github external
let data = getMessagePayload(msg)

    if (this._manageOrderBooks) {
      const err = this._updateManagedOB(symbol, data, raw)

      if (err) {
        this.emit('error', err)
        return
      }

      data = this._orderBooks[symbol]
    }

    // Always transform an array of entries
    if (this._transform) {
      data = new OrderBook((Array.isArray(data[0]) ? data : [data]), raw)
    }

    const internalMessage = [chanData.chanId, 'orderbook', data]
    internalMessage.filterOverride = [
      chanData.symbol,
      chanData.prec,
      chanData.len
    ]

    this._propagateMessageToListeners(internalMessage, chanData, false)
    this.emit('orderbook', symbol, data)
  }
github bitfinexcom / bitfinex-api-node / lib / transports / ws2.js View on Github external
if (this._manageCandles) {
      const err = this._updateManagedCandles(key, data)

      if (err) {
        this.emit('error', err)
        return
      }

      data = this._candles[key]
    } else if (data.length > 0 && !Array.isArray(data[0])) {
      data = [data] // always pass on an array of candles
    }

    if (this._transform) {
      data = Candle.unserialize(data)
    }

    const internalMessage = [chanData.chanId, 'candle', data]
    internalMessage.filterOverride = [chanData.key]

    this._propagateMessageToListeners(internalMessage, chanData, false)
    this.emit('candle', data, key)
  }
github bitfinexcom / bitfinex-api-node / lib / transports / ws2.js View on Github external
submitOrder (order) {
    if (!this._isAuthenticated) {
      return Promise.reject(new Error('not authenticated'))
    }

    const packet = Array.isArray(order)
      ? order
      : order instanceof Order
        ? order.toNewOrderPacket()
        : new Order(order).toNewOrderPacket()

    if (this._affCode) {
      if (!packet.meta) {
        packet.meta = {}
      }

      packet.meta.aff_code = packet.meta.aff_code || this._affCode // eslint-disable-line
    }

    this._sendOrderPacket([0, 'on', null, packet])

    return this._getEventPromise(`order-new-${packet.cid}`)
  }
github bitfinexcom / bitfinex-api-node / examples / ws2 / oco-order.js View on Github external
ws.once('auth', () => {
  debug('authenticated')

  // Build new order
  const o = new Order({
    cid: Date.now(),
    symbol: 'tBTCUSD',
    type: Order.type.EXCHANGE_LIMIT,
    amount: -0.05,

    oco: true,
    price: 2000,
    priceAuxLimit: 1000
  }, ws)

  let closed = false

  // Enable automatic updates
  o.registerListeners()

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