HF Strategy & Backtesting Libraries Alpha

Cris Mihalache
Bitfinex
Published in
3 min readOct 24, 2018

As part of the Honey Framework, today we are releasing a new set of open source libraries that together provide a full trading strategy & backtesting framework: bfx-hf-strategy, bfx-hf-backtest, and bfx-hf-data-server.

Using these libraries, you can create trading strategies that respond to market updates and utilize the indicators in bfx-hf-indicators. We are releasing these libraries as Alpha, so keep in mind their internal structure may still change significantly moving forwards. However, we feel they are stable enough to already warrant usage & testing.

Strategies

Trading strategies created usingbfx-hf-strategy are compatible with bfx-hf-backtest for running tests on historical data, and the Bitfinex APIs for executing on the live markets.

Strategies are defined by a set of indicators that they will utilize, and a set of update methods to be called on each price tick as received by the API:

const { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')
const { EMA } = require('bfx-hf-indicators')

HFS.define({
id: 'ema_cross',
name: 'ema_cross',
symbol: SYMBOLS.BTC_USD,
tf: TIME_FRAMES.ONE_MINUTE,

indicators: {
emaL: new EMA([100]),
emaS: new EMA([20])
},

onEnter: require('./on_enter'),
onUpdateLong: require('./on_update_long'),
onUpdateShort: require('./on_update_short')
})

The various update methods all receive the tick timestamp, price, and data type/full data object (trade or candle). For more information, head over the the README or take a look at the EMA cross strategy tutorial.

Backtests

After writing a strategy with bfx-hf-strategy, instantiate it and pass it to one ofbfx-hf-backtest's execOnline or execOffline methods in order to execute against historical data.

Online backtests require a running instance of the bfx-hf-data-server for synchronizing Bitfinex trade/candle data locally. The server will automatically download the data needed for the backtest, and will maintain a dataset over time to speed up future tests:

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

const now = Date.now()
const market = {
symbol: SYMBOLS.XMR_USD,
tf: TIME_FRAMES.ONE_MINUTE
}

const strat = EMAStrategy(market)
const run = async () => {
await HFBT.execOnline([strat], {
from: now - (90 * 24 * 60 * 60 * 1000),
to: now,
trades: false,
...market
})
}

try {
run()
} catch (e) {
console.error(e)
}

Data Server

The HF data server exposes a websocket API in order to receive backtest commands. When a backtest request is received, any missing data is first downloaded via the Bitfinex API, after which the full backtest dataset is streamed over the websocket connection to the client.

Backtest requests can omit trade data as it can take awhile to sync, and operate purely on candle data.

For data storage an embedded MongoDB instance is used, but this may be changed in the future for performance reasons. To use the data server, clone it locally and run npm start, or host an instance yourself with a short script:

const { RESTv2 } = require('bfx-api-node-rest')
const { logDataDBInfo, startDataServer } = require('bfx-hf-data-server')

const run = async () => {
const rest = new RESTv2(...) // create a REST API interface
const ds = startDataServer({
port: 8899, // command port number
rest
})

await logDataDBInfo()
}

try {
run()
} catch (e) {
debug(e.stack)
}

Overview

Taken together, along with the recent release of bfx-hf-algo and the algo server, these libraries provide a full suite of automated trading tools collectively known as the Honey Framework.

In the coming months we will release a central website with documentation about each library, and will work to add further useful features & components to the HF.

For now, the strategy & backtest portion are considered have been released as Alpha. As such, the three libraries outlined above can be expected to change over the coming months, as we further refine the strategy platform and add full support for trading on multiple symbols, along with algorithmic order integration.

In the meantime, we hope you find this toolset useful & constructive. If you have any comments or suggestions, feel free to open issues on the Github pages for these projects.

To get started, head over to the EMA cross strategy tutorial page to learn more about the trading system.

--

--