Skip to content
Cris Mihalache edited this page Oct 2, 2018 · 1 revision

This repo contains an algorithmic order system built on top of the Bitfinex node API, along with three algo orders: Iceberg, TWAP, and Accumulate/Distribute

Usage

To use, create an AOHost instance and call startAO(id, args):

const { AOHost, Iceberg, TWAP, AccumulateDistribute } = require('bfx-hf-algo')

const host = new AOHost({
  aos: [Iceberg, TWAP, AccumulateDistribute],
  apiKey: '...',
  apiSecret: '...',
  wsURL: '...',
  restURL: '...',
})

host.on('ao:start', (instance) => {
  const { state = {} } = instance
  const { id, gid } = state
  console.log('started AO %s [gid %s]', id, gid)
})

host.on('ao:stop', (instance) => {
  const { state = {} } = instance
  const { id, gid } = state
  console.log('stopped AO %s [gid %s]', id, gid)
})

host.on('ws2:auth:error', (packet) => {
  console.log('error authenticating: %j', packet)
})

host.on('error', (err) => {
  console.log('error: %s', err)
})

host.once('ws2:auth:success', async () => {

  // Start an Iceberg order instance
  const gid = await host.startAO('bfx.iceberg', {
    symbol: 'tBTCUSD',
    price: 21000,
    amount: -0.5,
    sliceAmount: -0.1,
    excessAsHidden: true,
    orderType: 'LIMIT',
    submitDelay: 150,
    cancelDelay: 150,
    _margin: false,
  })

  // later, host.stopAO(gid)
})

Algo Order Host

The AOHost class provides a wrapper around the algo order system, and manages lifetime events/order execution. Internally it hosts a Manager instance from bfx-api-node-core for communication with the Bitfinex API, and listens for websocket stream events in order to update order state/trigger algo order events.

Execution is handled by an event system, with events being triggered by Bitfinex API websocket stream payloads, and the algo orders themselves.

The host must be instantiated with a valid API key/secret pair, and websocket/REST endpoints, along with an optional proxy agent. These parameters are the same as those passed to the bfx-api-node-core Manager constructor. Note that the Manager is instantiated with the dead-man-switch active (dms: 4).

To start/stop algo orders, gid = startAO(id, args) and stopAO(gid) methods are provided, with the generated group ID (gid) being the same as that used for all atomic orders created by the individual algo orders.

See the above usage example for instantiation

Clone this wiki locally