Skip to content

Commit

Permalink
backtesting with bitfinex terminal
Browse files Browse the repository at this point in the history
this adjusts the examples for the bitfinex terminal release, adds
the dazaar card and updates the readme
  • Loading branch information
robertkowalski committed Sep 23, 2020
1 parent c778314 commit f38b465
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -8,4 +8,4 @@ npm-debug.log
.DS_Store
package-lock.json

examples/dbs
dbs
67 changes: 23 additions & 44 deletions README.md
Expand Up @@ -125,61 +125,40 @@ try {
}
```

#### Dazaar Market Data
#### Bitfinex Terminal Data

Dazaar Market data can be used to run a strategy:
Bitfinex 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.

[`examples/`](/examples/bfx_terminal.js)
```js
const hypercore = require('hypercore')
const Hyperbee = require('hyperbee')
const replicate = require('@hyperswarm/replicator')

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

const hopts = {}

const hbOpts = {
valueEncoding: 'json'
}

const strat = EMAStrategy(market)
const from = get24HoursAgo(new Date())
const to = new Date()

// pu in your dazaar market data key here
const key = Buffer.from('7c6057e7ac2f19fcacbee2853554b9a24da85797b1cef31b330465d0f24d7fb1', 'hex')
const feed = hypercore(path.join(__dirname, 'dbs', 'testCOPY'), key, { sparse: true })

const db = new Hyperbee(feed, hbOpts)

db.feed.ready(async () => {
replicate(feed, { lookup: true, live: true })

const { exec, onEnd } = await HFBT.execStream(strat, market, {
from,
to
})

// dazaar market data 5min candles
const kp = 'c!5m!'
const { exec, onEnd } = await HFBT.execStream(strat, market, {
from,
to
// isTrade: null, // you can pass a custom `isTrade` frunction here in options
})

const from = 1358182044000
const to = 1358342119000
let btState

const s = db.createReadStream({ gte: kp + from, lte: kp + to })
// db is a bitfinex terminal hyperbee stream
const stream = db.createReadStream({
gte: { candle: '5m', timestamp: from },
lte: { candle: '5m', timestamp: to }
})

let btState
s.on('data', async (data) => {
const { key, value } = data
btState = await exec(key, value)
})
for await (const data of stream) {
const { key, value } = data
btState = await exec(key, value)
}

s.on('end', async () => {
btState = await onEnd(btState)
})
})
await onEnd(btState)
```

For the full blog article, visit [the Bitfinex Terminal repo](https://github.com/bitfinexcom/bitfinex-terminal/blob/master/articles/backtesting-with-hf.md)

### Contributing

1. Fork it
Expand Down
72 changes: 72 additions & 0 deletions examples/bfx_terminal.js
@@ -0,0 +1,72 @@
'use strict'

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

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

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

const terms = require('bitfinex-terminal-terms-of-use')

const get24HoursAgo = (date) => {
const res = date.getTime() - (1 * 86400 * 1000)

return new Date(res)
}

const market = {
symbol: SYMBOLS.BTC_USD,
tf: TIME_FRAMES.FIVE_MINUTES
}

const strat = EMAStrategy(market)

const dmarket = dazaar('dbs/terminal-backtest')

const card = require('./bitfinex.terminal.btcusd.candles.json')
const buyer = dmarket.buy(card, { sparse: true, terms })

let replication
buyer.on('feed', async function () {
console.log('got feed')

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

await runTest(db)
})

async function runTest (db) {
const from = get24HoursAgo(new Date())
const to = new Date()

const { exec, onEnd } = await HFBT.execStream(strat, market, {
from,
to
})

const stream = db.createReadStream({
gte: { candle: '5m', timestamp: from },
lte: { candle: '5m', timestamp: to }
})

let btState
for await (const data of stream) {
const { key, value } = data
btState = await exec(key, value)
}

await onEnd(btState)

buyer.feed.close()
replication.destroy()
}

replication = swarm(buyer)
5 changes: 5 additions & 0 deletions examples/bitfinex.terminal.btcusd.candles.json
@@ -0,0 +1,5 @@
{
"name": "Bitfinex Terminal BTCUSD Candles Dazaar Card",
"id": "76ef1d766eee63ff473e33e1f231c3b65cf1042b1a353c027b48bdbeba00b969",
"payment": []
}
112 changes: 0 additions & 112 deletions examples/hyperbee.js

This file was deleted.

9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -40,8 +40,8 @@
],
"dependencies": {
"bfx-api-node-models": "^1.0.12",
"bfx-hf-strategy": "^1.0.0",
"bfx-hf-util": "^1.0.1",
"bfx-hf-strategy": "^1.1.0",
"bfx-hf-util": "^1.0.11",
"cli-table": "^0.3.1",
"colors": "^1.3.1",
"debug": "^4.1.1",
Expand All @@ -53,11 +53,14 @@
"devDependencies": {
"@hyperswarm/replicator": "^1.7.2",
"async": "^3.2.0",
"bitfinex-terminal-key-encoding": "^1.0.0",
"bitfinex-terminal-terms-of-use": "^1.0.0",
"dazaar": "^1.2.0",
"eslint": "^4.19.1",
"eslint-config-standard": "^7.0.0",
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-standard": "^2.0.1",
"hyperbee": "git+https://github.com/mafintosh/hyperbee.git",
"hyperbee": "0.0.18",
"hypercore": "^9.5.0",
"istanbul": "^0.4.5",
"jsdoc-to-markdown": "^5.0.1",
Expand Down

0 comments on commit f38b465

Please sign in to comment.