{"id":22608947,"url":"https://github.com/bitfinexcom/bfx-hf-backtest","last_synced_at":"2025-04-11T06:14:48.458Z","repository":{"id":37860629,"uuid":"143998030","full_name":"bitfinexcom/bfx-hf-backtest","owner":"bitfinexcom","description":null,"archived":false,"fork":false,"pushed_at":"2023-10-11T13:14:04.000Z","size":288,"stargazers_count":15,"open_issues_count":5,"forks_count":16,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-11T06:14:41.577Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitfinexcom.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-08T10:27:49.000Z","updated_at":"2022-10-04T18:54:56.000Z","dependencies_parsed_at":"2024-06-21T17:54:55.563Z","dependency_job_id":"9fef3492-142a-4376-b6d4-1d60887c1abe","html_url":"https://github.com/bitfinexcom/bfx-hf-backtest","commit_stats":{"total_commits":143,"total_committers":8,"mean_commits":17.875,"dds":0.5664335664335665,"last_synced_commit":"e931d277ceb0a2922de00ab682ca2cdb0c45c42d"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fbfx-hf-backtest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fbfx-hf-backtest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fbfx-hf-backtest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitfinexcom%2Fbfx-hf-backtest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitfinexcom","download_url":"https://codeload.github.com/bitfinexcom/bfx-hf-backtest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248351393,"owners_count":21089272,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-08T15:10:20.965Z","updated_at":"2025-04-11T06:14:48.433Z","avatar_url":"https://github.com/bitfinexcom.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Bitfinex Honey Framework Backtesting Tools for Node.JS\n\n[![Build Status](https://travis-ci.org/bitfinexcom/bfx-hf-backtest.svg?branch=master)](https://travis-ci.org/bitfinexcom/bfx-hf-backtest)\n\nThis repo provides an interface for executing backtests using either offline data, or a `bfx-hf-data-server` instance for historical Bitfinex market data.\n\n### Features\n\n* Offline backtest execution with user-supplied trade \u0026 candle data\n* Online backtest execution with data from `bfx-hf-data-server`\n* Simulates trades within a candle if none are provided\n\n### Installation\n\n```bash\nnpm i --save bfx-hf-backtest\n```\n\n### Quickstart\n\n```js\nconst HFS = require('bfx-hf-strategy')\nconst HFBT = require('bfx-hf-backtest')\nconst Strategy = ... // strategy instance\nconst candles = [/* ... */]\n\nconst candleKey = HFS.candleMarketDataKey({\n  symbol: SYMBOLS.BTC_USD,\n  tf: TIME_FRAMES.ONE_HOUR\n})\n\nHFBT.execOffline(strat, {\n  trades: {},\n  candles: {\n    [candleKey]: candles,\n  }\n}).then((btState) =\u003e {\n  const { trades = [] } = btState\n\n  // analyze backtest trades...\n})\n```\n\n### Docs\n\n[Refer to `docs/exec.md`](/docs/exec.md) for JSDoc-generated API documentation, and the [`examples/`](/examples) folder for executable examples.\n\n### Examples\n\n#### Offline Backtests\nTo execute a backtest of a trading strategy using historical data, the `execOffline` method is provided which will run the strategy against each trade \u0026 candle in-order by timestamp:\n\n```js\nconst HFS = require('bfx-hf-strategy')\nconst HFBT = require('bfx-hf-backtest')\n\nconst EMAStrategy = require('bfx-hf-strategy/examples/ema_cross')\nconst { Candle } = require('bfx-api-node-models')\nconst { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')\nconst rawCandleData = require('./btc_candle_data.json')\n\n// During real execution, candles can arrive from any market/at any time (if\n// sub'ed to multiple time frames); hence, each candle must include its origin\n// symbol/time frame pair.\nconst market = {\n  symbol: SYMBOLS.BTC_USD,\n  tf: TIME_FRAMES.ONE_HOUR\n}\n\nconst candleKey = HFS.candleMarketDataKey(market)\nconst strat = EMAStrategy(market)\nconst candles = rawCandleData\n  .sort((a, b) =\u003e a[0] - b[0])\n  .map(c =\u003e ({\n    ...(new Candle(c).toJS()),\n    ...market // attach market data\n  }))\n\nconst run = async () =\u003e {\n  await HFBT.execOffline(strat, {\n    trades: {},\n    candles: {\n      [candleKey]: candles,\n    }\n  })\n}\n\ntry {\n  run()\n} catch (e) {\n  console.error(e)\n}\n```\n\n#### Online Backtests\nOnline backtests are executed a running `bfx-hf-data-server` instance, which will automatically synchronize historical data as needed and pass it to the backtesting logic:\n\n```js\nconst HFBT = require('bfx-hf-backtest')\nconst EMAStrategy = require('bfx-hf-strategy/examples/ema_cross')\nconst { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')\n\nconst now = Date.now()\nconst market = {\n  symbol: SYMBOLS.XMR_USD,\n  tf: TIME_FRAMES.ONE_MINUTE\n}\n\nconst strat = EMAStrategy(market)\nconst run = async () =\u003e {\n  await HFBT.execOnline([strat], {\n    exchange: 'bitfinex',\n    from: now - (2 * 24 * 60 * 60 * 1000),\n    to: now,\n    trades: true,\n    candles: true,\n    ...market\n  })\n}\n\ntry {\n  run()\n} catch (e) {\n  console.error(e)\n}\n```\n\n#### Bitfinex Terminal Data\n\nBitfinex Terminal data can be used to run a strategy. There is also a [full blog article](https://github.com/bitfinexcom/bitfinex-terminal/blob/master/articles/backtesting-with-hf.md) on this.\n\n[`examples/`](/examples/bfx_terminal.js)\n```js\nconst strat = EMAStrategy(market)\nconst from = get24HoursAgo(new Date())\nconst to = new Date()\n\nconst { exec, onEnd } = await HFBT.execStream(strat, market, {\n  from,\n  to\n  // isTrade: null, // you can pass a custom `isTrade` frunction here in options\n})\n\nlet btState\n\n// db is a bitfinex terminal hyperbee stream\nconst stream = db.createReadStream({\n  gte: { candle: '5m', timestamp: from },\n  lte: { candle: '5m', timestamp: to }\n})\n\nfor await (const data of stream) {\n  const { key, value } = data\n  btState = await exec(key, value)\n}\n\nawait onEnd(btState)\n```\n\nFor the full blog article, visit [the Bitfinex Terminal repo](https://github.com/bitfinexcom/bitfinex-terminal/blob/master/articles/backtesting-with-hf.md)\n\n### Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n### Note\n\nThis package will be maintained only via github, please use latest relases from github instead of npm.\n\nExample on how to install specific version from github:\n```\nnpm i --save-prod https://github.com/bitfinexcom/bfx-hf-backtest.git#v2.0.1\n```\n\nExample on how to install it latest version from github:\n```\nnpm i --save-prod https://github.com/bitfinexcom/bfx-hf-backtest.git\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitfinexcom%2Fbfx-hf-backtest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitfinexcom%2Fbfx-hf-backtest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitfinexcom%2Fbfx-hf-backtest/lists"}