Execute your Buying and selling Technique with the Honey Framework and Bitfinex Terminal

Not too long ago we discovered how backtesting a method can work with Bitfinex Terminal information streams. In its core, the info streams are free Dazaar feeds shared over a P2P know-how just like BitTorrent. However in contrast to BitTorrent, the info might be cryptographically verified and is streamable. We are able to request frames of a dataset proper from the center or determine to only comply with a stay stream with the most recent additions. After all, downloading the entire dataset is feasible too.

On this article, we are going to use information that’s streamed on Bitfinex Terminal to execute a stay technique. For demonstration functions, the generated buying and selling alerts are despatched to the Bitfinex Websocket API. In follow, nevertheless, we are able to do something with them. Even if you wish to energy on a espresso machine with a Raspberry Pi each time your technique would commerce, you would achieve this!

For our tutorial, we’re utilizing a well-liked technique, the EMA Cross Technique. It emits a buying and selling sign when two EMA indicators cross one another. The EMA Cross technique is included as one of many examples within the bfx-hf-strategy library.

Let’s begin

To start we now have to put in the required dependencies:

npm set up dazaar hyperbee bitfinex-terminal-key-encoding bfx-hf-util bfx-hf-strategy bfx-hf-strategy-dazaar  
  bitfinex-api-node bfx-api-node-models bitfinex-terminal-terms-of-use

We additionally have to create a file, which can comprise the code we write, let’s name it exec-strategy.js:

contact exec-strategy.js

Now we are able to load the required dependencies. Readers of our backtesting tutorial will spot just a few similarities:

const dazaar = require('dazaar')
const swarm = require('dazaar/swarm')
const Hyperbee = require('hyperbee')
const keyEncoding = require('bitfinex-terminal-key-encoding')
const phrases = require('bitfinex-terminal-terms-of-use')

const { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')
const EMAStrategy = require('bfx-hf-strategy/examples/ema_cross')
const execDazaar = require('bfx-hf-strategy-dazaar')

We additionally load and initialise the Bitfinex Websocket consumer. To make it work, you must change the placeholders along with your credentials:

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

const apiKey = 'SECRET'
const apiSecret="SECRETSECRET"
const ws = new WSv2({
  apiKey,
  apiSecret
})

Subsequent we initialise our technique:

const market = {
  image: SYMBOLS.BTC_USD,
  tf: TIME_FRAMES.ONE_MINUTE
}

const strat = EMAStrategy(market)

And initialise Dazaar:

const dmarket = dazaar('dbs/terminal-live') // shops obtained information in `dbs/terminal-live`

With wget we are able to pull the Dazaar Card for the info feed we wish to devour. An summary of information feeds might be discovered right here.

wget https://uncooked.githubusercontent.com/bitfinexcom/bitfinex-terminal/grasp/playing cards/bitfinex.terminal.btcusd.trades.json

That card is loaded into Dazaar and the Phrases of Companies are agreed by loading them into Dazaar after we now have learn them:

const card = require('./bitfinex.terminal.btcusd.candles.json')
const purchaser = dmarket.purchase(card, { sparse: true, phrases })

And if Dazaar emits a feed occasion, we arrange Hyperbee and name a perform referred to as runStrategy:

purchaser.on('feed', perform () {
  console.log('acquired feed')

  const db = new Hyperbee(purchaser.feed, {
    keyEncoding,
    valueEncoding: 'json'
  })

  runStrategy(db)
})

To this point, most of our setup is just like the setup we used for backtests within the final article. Now we now have to outline the perform runStrategy, which is enabling us to do one thing with the buying and selling alerts from our technique:

async perform runStrategy (db) {

}

The perform runStrategy will arrange the logic that runs our technique on every obtained candle. First, we open the Websocket to the Bitfinex API.

await ws.open()
await ws.auth()

We additionally arrange the info stream. We name execDazaar with our technique, the outlined market and the Hyperbee database. As choices, we move in submitOrder, a customized perform we are going to write in a bit. submitOrder can be referred to as for each buying and selling sign that’s emitted from our technique. The simulateFill possibility permits us to not await the order to be absolutely stuffed by the alternate, which is helpful for our tutorial. The built-in submitOrder-function within the Honey Framework additionally submits orders by WebSocket and is ready for an order to be stuffed earlier than persevering with. In manufacturing, it’s possible you’ll wish to use the built-in one, relying in your technique and use case. We additionally seed the technique state, we use a rely of 120 candles.

const { exec, stream } = await execDazaar(strat, market, db, {
  submitOrder,
  simulateFill: true,
  includeTrades: false,
  seedCandleCount: 120
})

execDazaar returns a stream and a perform referred to as exec. We’re calling exec on each entry that’s despatched within the stream:

for await (const information of stream) {
  const { key, worth } = information
  await exec(key, worth)
}

Our perform runStrategy is now full, right here is the complete perform:

async perform runStrategy (db) {
  await ws.open()
  await ws.auth()

  const { exec, stream } = await execDazaar(strat, market, db, {
    submitOrder,
    simulateFill: true,
    includeTrades: false,
    seedCandleCount: 10
  })

  for await (const information of stream) {
    const { key, worth } = information
    await exec(key, worth)
  }
}

We nonetheless should outline the perform submitOrder. For each sign emitted from our EMA Cross technique, submitOrder is named with the present state of the technique and the order information. In our case, we take the order information and ship it to Bitfinex. We write a simplified model of the

async perform submitOrder (strategyState = {}, order = {}) {
  const _o = {
    cid: Date.now(),
    ...order
  }

  console.log('submitting order', _o)

  const o = new Order(_o, ws)
  o.registerListeners()

  o.on('replace', () => {
    console.log(`order up to date: ${o.serialize()}`)
  })

  const res = await o.submit()
  return res
}

To start out the whole lot, we now have to connect with the community, which can set off a feed occasion:

swarm(purchaser)

Once we run the file now, it’s going to take the final 120 candles and pre-seed our technique algorithm with it. Then it’s going to comply with the stay stream of recent candles coming in and run our technique on it. In case a buying and selling sign is generated, it’s submitted to the Bitfinex API. You will discover the complete file we wrote on this article right here.

And that’s it! On this article, we’ve discovered how we are able to take a Honey Framework technique, feed in information from Bitfinex Terminal, and submit the buying and selling alerts for buying and selling. With the assistance of customized submitOrder features, we are able to construct customized performance our buying and selling technique acts upon. We hope you loved the article.

PS: Bitfinex is hiring! 🙂

LEAVE A REPLY

Please enter your comment!
Please enter your name here