0%

Backtest your Trading Strategies with Bitfinex Terminal & Honey Framework

2020년 9월 24일 6 분 읽기
뉴스 기사 배너 이미지

We will run our backtest on 5-minute candles from BTCUSD trading data. That data is, like in a blockchain, stored in a Merkle Tree and can be cryptographically verified. The data is shared over a peer-to-peer (P2P) network and can be live-streamed. That means the backtest can start to run while we are fetching the data and resume the downloads after a connection reset is done for us. All these features make Terminal a compelling choice for sharing trading data and trading signals.

Bitfinex Terminal supports Dazaar Cards, which is an easy way to access the data streams. Every data stream has a unique ID, which is verified cryptographically, similar to a Bitcoin address. The Dazaar card contains a description of the content and the stream ID. Imagine it like a torrent file, but for encrypted, real-time data streams.

Let’s try it

To begin, we install the required dependencies:

npm install dazaar hyperbee bitfinex-terminal-key-encoding bfx-hf-util bfx-hf-backtest
bfx-hf-strategy bitfinex-terminal-terms-of-use

We also have to create a file; let’s say backtest.js. We can now start to write our code.

To see the results of our backtest on the console we have to enable debug output:

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

The EMA-Cross-strategy we run today is one of the example strategies included in the Honey Framework. We load that strategy with our other dependencies. One dependency of it are the Terms of Use for Bitfinex Terminal – we will have to load them into Dazaar after reading and acknowledging them:

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 terms = require('bitfinex-terminal-terms-of-use')

We also define a small helper function to give us the time from exactly 24 hours in the past:

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

return new Date(res) }

For backtesting, we have to define the market we test and pass it to the EMA Strategy:

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

const strat = EMAStrategy(market)

Then we can initialise Dazaar:

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

With the above command, Dazaar will create a local database in dbs/terminal-backtest. All of our data will be stored in this folder, so if you want to start over, you can simply delete it.

As the next step we download the Dazaar Card for BTCUSD from https://raw.githubusercontent.com/bitfinexcom/bitfinex-terminal/master/cards/bitfinex.terminal.btcusd.trades.json and load it:

wget https://raw.githubusercontent.com/bitfinexcom/bitfinex-terminal/master/cards/bitfinex.terminal.btcusd.trades.json

const card = require('./bitfinex.terminal.btcusd.candles.json')

Dazaar also supports paid feeds, e.g. for selling trading signals of a successful strategy, but the Bitfinex Terminal data is free. Our card is loaded into Dazaar. We enable sparse mode, with sparse mode set, it will just download data requested from us. We also load the terms of service we required above, after we read and acknowledged them:

const buyer = dmarket.buy(card, { sparse: true, terms })

If we wanted to download a full copy of all candles in the background, we would set sparse to false.

Once the data is ready, Dazaar will emit a feed event. After that is emitted, we can set up Hyperbee on top of the Dazaar feed. Hyperbee provides us with a B-Tree structure for easy querying of the data. For Terminal, we use a special key encoding module, which makes querying the data easier. We already loaded the key encoding module with const keyEncoding = require('bitfinex-terminal-key-encoding'). Once the Hyperbee database is set up, we can call runTest and work with the database:

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

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

runTest(db) })

The last part of our backtest is the actual testing. For that, we have to define the function runTest:

async function runTest (db) {

}

Inside the function runTest we have to define the timeframe we want to use for our test. For the beginning of the timeframe, we use the helper function we created at the beginning of our tutorial. The value for to points to the current time:

const from = get24HoursAgo(new Date()) const to = new Date()

Together with our strategy and the market, we are passing the timeframe definitions to HFBT.execStream. It returns a function called exec and a function called onEnd:

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

The function exec will be applied to each element of the stream, but first we have to initiate the stream. We will run our test on 5m candles. To get all data within that 24 hour timeframe we use our variables from and to in the respective fields lte (less than equal) and gte (greater than equal):

const stream = db.createReadStream({ gte: { candle: TIME_FRAMES.FIVE_MINUTES, timestamp: from }, lte: { candle: TIME_FRAMES.FIVE_MINUTES, timestamp: to } })

With the help of an Async Iterator we call exec on every entry and store the result in btState:

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

Then, once the iterator gives us no results any more, we call the function onEnd, which will print our results to the console:

await onEnd(btState)

Here is the whole function runTest:

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: TIME_FRAMES.FIVE_MINUTES, timestamp: from }, lte: { candle: TIME_FRAMES.FIVE_MINUTES, timestamp: to } })

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

await onEnd(btState) }

After finishing the function runTest, we are almost done. To start everything, we have to go online:

swarm(buyer)

Here is the whole code in one file for our backtest.

Now, when we run our file, we get the result printed to the console:

results
results

As you can see, we would have made a loss with this strategy during the timeframe 9/6/2020, 3:18:58 PM to 9/7/2020, 3:18:58 PM, when this article was written.

Conclusion

In this article, we’ve taken a look at how backtesting a Honey Framework trading strategy works with Bitfinex Terminal. We were able to create a backtest in under 70 lines of code with an easy and reliable way to sync large amounts of data. Everything was running on top of Dazaar, which can be used to sell trading signals too. One of our next articles will cover exactly that topic, so stay tuned!

The post Backtest your Trading Strategies with Bitfinex Terminal & Honey Framework appeared first on Bitfinex blog.

인기 뉴스

How to Set Up and Use Trust Wallet for Binance Smart Chain
#Bitcoin#Bitcoins#Config+2 더 많은 태그

How to Set Up and Use Trust Wallet for Binance Smart Chain

Your Essential Guide To Binance Leveraged Tokens

Your Essential Guide To Binance Leveraged Tokens

How to Sell Your Bitcoin Into Cash on Binance (2021 Update)
#Subscriptions

How to Sell Your Bitcoin Into Cash on Binance (2021 Update)

What is Grid Trading? (A Crypto-Futures Guide)

What is Grid Trading? (A Crypto-Futures Guide)

Cryptohopper에서 무료로 거래를 시작하세요!

무료 사용 - 신용카드 필요 없음

시작하기
Cryptohopper appCryptohopper app

면책 조항: Cryptohopper는 규제 기관이 아닙니다. 암호화폐 봇 거래에는 상당한 위험이 수반되며 과거 실적이 미래 결과를 보장하지 않습니다. 제품 스크린샷에 표시된 수익은 설명용이며 과장된 것일 수 있습니다. 봇 거래는 충분한 지식이 있거나 자격을 갖춘 재무 고문의 조언을 구한 경우에만 참여하세요. Cryptohopper는 어떠한 경우에도 (a) 당사 소프트웨어와 관련된 거래로 인해, 그로 인해 또는 이와 관련하여 발생하는 손실 또는 손해의 전부 또는 일부 또는 (b) 직접, 간접, 특별, 결과적 또는 부수적 손해에 대해 개인 또는 단체에 대한 어떠한 책임도 지지 않습니다. Cryptohopper 소셜 트레이딩 플랫폼에서 제공되는 콘텐츠는 Cryptohopper 커뮤니티 회원이 생성한 것이며 Cryptohopper 또는 그것을 대신한 조언이나 추천으로 구성되지 않는다는 점에 유의하시기 바랍니다. 마켓플레이스에 표시된 수익은 향후 결과를 나타내지 않습니다. Cryptohopper의 서비스를 사용함으로써 귀하는 암호화폐 거래와 관련된 내재적 위험을 인정하고 수락하며 발생하는 모든 책임이나 손실로부터 Cryptohopper를 면책하는 데 동의합니다. 당사의 소프트웨어를 사용하거나 거래 활동에 참여하기 전에 당사의 서비스 약관 및 위험 공개 정책을 검토하고 이해하는 것이 필수적입니다. 특정 상황에 따른 맞춤형 조언은 법률 및 재무 전문가와 상담하시기 바랍니다.

©2017 - 2024 저작권: Cryptohopper™ - 판권 소유.