30分で作る暗号通貨取引バックテスト環境 by Freqtrade

こんにちは、でめきん(@demekinnomimi)です。
タイトルには "作る" と書きましたが特に何か作るわけではないです。

github で Freqtrade という教育目的の現物専用Botを見つけたので、本日はこちらを使って Binance BTC/USDT ペアのバックテストをしてみようと思います。

環境

Ubuntu 20.04 on WSL 2
python 3.8.10

Freqtrade のインストール

以下のレポジトリのReadmeを見ながら進めていきます。

git clone -b develop https://github.com/freqtrade/freqtrade.git
cd freqtrade
./setup.sh --install

Readme記載通りに上記コマンドを打てばインストール完了。
途中で何度か質問されるのは、内容が分からなければ全て y で良いです。

バックテストの準備と実施

次にバックテストに必要なヒストリカルデータのダウンロードをしていきます。BinanceからBTC/USDTの1分足と5分足をjson形式で180日分ダウンロードします。

~/git/freqtrade$ mkdir -p user_data/data/binance
~/git/freqtrade$ freqtrade download-data --exchange binance --pairs BTC/USDT -t {1m,5m} --days 180
~/git/freqtrade$ freqtrade list-data --userdir user_data/
2021-12-12 19:54:32,107 - freqtrade.configuration.configuration - INFO - Using config: config.json ...
2021-12-12 19:54:32,109 - freqtrade.loggers - INFO - Verbosity set to 0
2021-12-12 19:54:32,110 - freqtrade.configuration.configuration - INFO - Using user-data directory: user_data ...
2021-12-12 19:54:32,110 - freqtrade.configuration.configuration - INFO - Using data directory: user_data/data/binance ...
2021-12-12 19:54:32,111 - freqtrade.configuration.check_exchange - INFO - Checking exchange...
2021-12-12 19:54:32,117 - freqtrade.configuration.check_exchange - INFO - Exchange "binance" is officially supported by the Freqtrade development team.
2021-12-12 19:54:32,118 - freqtrade.configuration.configuration - INFO - Using pairlist from configuration.
2021-12-12 19:54:32,119 - freqtrade.configuration.config_validation - INFO - Validating configuration ...
Found 2 pair / timeframe combinations.
+----------+-------------+
|     Pair |   Timeframe |
|----------+-------------|
| BTC/USDT |      1m, 5m |
+----------+-------------+

バックテストの準備をして、freqtrade backtestingを実行するとバックテストの結果が出ます。今回利用した SampleStrategyクラスは、5分足を利用して取引しているので、5分足のヒストリカルデータが必須です。

~/git/freqtrade$ freqtrade new-config -c config.json # config.json作成。適当に答える
~/git/freqtrade$ vim config.json
# 36行目のexchange->pair_whitelistにバックテストで使う通貨ペアを入れる。今回は"BTC/USTD"
# 45行目あたりのpairlists->methodを"StaticPairList"に変更する。method以外のkeyは削除
~/git/freqtrade$ cp freqtrade/templates/sample_strategy.py user_data/data/binance/strategies/ # 産所から入ってるサンプル戦略をコピー
~/git/freqtrade$ freqtrade backtesting --dry-run-wallet 500 --strategy-path user_data/data/binance/strategies/ --strategy SampleStrategy
略)
Result for strategy SampleStrategy
========================================================== BACKTESTING REPORT =========================================================
|     Pair |   Buys |   Avg Profit % |   Cum Profit % |   Tot Profit USDT |   Tot Profit % |   Avg Duration |   Win  Draw  Loss  Win% |
|----------+--------+----------------+----------------+-------------------+----------------+----------------+-------------------------|
| BTC/USDT |     99 |           0.02 |           1.99 |             1.995 |           0.40 | 1 day, 0:13:00 |    87     0    12  87.9 |
|    TOTAL |     99 |           0.02 |           1.99 |             1.995 |           0.40 | 1 day, 0:13:00 |    87     0    12  87.9 |
========================================================== BUY TAG STATS ===========================================================
|   TAG |   Buys |   Avg Profit % |   Cum Profit % |   Tot Profit USDT |   Tot Profit % |   Avg Duration |   Win  Draw  Loss  Win% |
|-------+--------+----------------+----------------+-------------------+----------------+----------------+-------------------------|
| TOTAL |     99 |           0.02 |           1.99 |             1.995 |           0.40 | 1 day, 0:13:00 |    87     0    12  87.9 |
===================================================== SELL REASON STATS =====================================================
|   Sell Reason |   Sells |   Win  Draws  Loss  Win% |   Avg Profit % |   Cum Profit % |   Tot Profit USDT |   Tot Profit % |
|---------------+---------+--------------------------+----------------+----------------+-------------------+----------------|
|           roi |      87 |     87     0     0   100 |           1.02 |          88.82 |            88.913 |          88.82 |
|     stop_loss |       8 |      0     0     8     0 |         -10.18 |         -81.44 |           -81.52  |         -81.44 |
|   sell_signal |       3 |      0     0     3     0 |          -1.73 |          -5.19 |            -5.192 |          -5.19 |
|    force_sell |       1 |      0     0     1     0 |          -0.2  |          -0.2  |            -0.205 |          -0.2  |
======================================================= LEFT OPEN TRADES REPORT =======================================================
|     Pair |   Buys |   Avg Profit % |   Cum Profit % |   Tot Profit USDT |   Tot Profit % |   Avg Duration |   Win  Draw  Loss  Win% |
|----------+--------+----------------+----------------+-------------------+----------------+----------------+-------------------------|
| BTC/USDT |      1 |          -0.20 |          -0.20 |            -0.205 |          -0.04 |        0:20:00 |     0     0     1     0 |
|    TOTAL |      1 |          -0.20 |          -0.20 |            -0.205 |          -0.04 |        0:20:00 |     0     0     1     0 |
=============== SUMMARY METRICS ================
| Metric                 | Value               |
|------------------------+---------------------|
| Backtesting from       | 2021-06-15 02:30:00 |
| Backtesting to         | 2021-12-12 10:40:00 |
| Max open trades        | 1                   |
|                        |                     |
| Total/Daily Avg Trades | 99 / 0.55           |
| Starting balance       | 500.000 USDT        |
| Final balance          | 501.995 USDT        |
| Absolute profit        | 1.995 USDT          |
| Total profit %         | 0.40%               |
| Trades per day         | 0.55                |
| Avg. daily profit %    | 0.00%               |
| Avg. stake amount      | 100.000 USDT        |
| Total trade volume     | 9900.000 USDT       |
|                        |                     |
| Best Pair              | BTC/USDT 1.99%      |
| Worst Pair             | BTC/USDT 1.99%      |
| Best trade             | BTC/USDT 2.00%      |
| Worst trade            | BTC/USDT -10.18%    |
| Best day               | 2.000 USDT          |
| Worst day              | -10.190 USDT        |
| Days win/draw/lose     | 70 / 98 / 12        |
| Avg. Duration Winners  | 17:38:00            |
| Avg. Duration Loser    | 2 days, 23:58:00    |
| Rejected Buy signals   | 0                   |
|                        |                     |
| Min balance            | 479.033 USDT        |
| Max balance            | 512.427 USDT        |
| Drawdown               | 23.94%              |
| Drawdown               | 23.967 USDT         |
| Drawdown high          | 3.000 USDT          |
| Drawdown low           | -20.967 USDT        |
| Drawdown Start         | 2021-06-17 01:10:00 |
| Drawdown End           | 2021-07-20 02:20:00 |
| Market change          | 21.15%              |
================================================

以上。

次回以降の予定

Freqtrade に手を加えて、Bybit の Linear perpetual contract の利用を最終目標にしていこうと思います。発注部分は ccxt を利用しているだけなので、たぶんいけるはず…

最後に

見ればわかりますが Freqtrade は GPL v3 です。これを組み込んだ Bot 等を配布する場合、有償無償問わずソースコードの全開示が必要なのでご注意ください。

この記事が気に入ったらサポートをしてみませんか?