Skip to content

Commit

Permalink
backtesting with bitfinex terminal
Browse files Browse the repository at this point in the history
this adds an example for backtesting with bitfinex terminal and
updates the readme.
  • Loading branch information
robertkowalski committed Sep 15, 2020
1 parent c778314 commit 5cde254
Show file tree
Hide file tree
Showing 5 changed files with 95 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 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

### Contributing

1. Fork it
Expand Down
66 changes: 66 additions & 0 deletions examples/bfx_terminal.js
@@ -0,0 +1,66 @@
'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('bfx-hf-backtest')
const { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')
const EMAStrategy = require('bfx-hf-strategy/examples/ema_cross')

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

return new Date(res)
}

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

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 })

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)
}

swarm(buyer)
112 changes: 0 additions & 112 deletions examples/hyperbee.js

This file was deleted.

8 changes: 5 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.0.5",
"bfx-hf-util": "^1.0.11",
"cli-table": "^0.3.1",
"colors": "^1.3.1",
"debug": "^4.1.1",
Expand All @@ -53,11 +53,13 @@
"devDependencies": {
"@hyperswarm/replicator": "^1.7.2",
"async": "^3.2.0",
"bitfinex-terminal-key-encoding": "^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.16",
"hypercore": "^9.5.0",
"istanbul": "^0.4.5",
"jsdoc-to-markdown": "^5.0.1",
Expand Down

0 comments on commit 5cde254

Please sign in to comment.