From 4a09043ffccda7becbef4f7eb4f17cd3b1553c3d Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 16:22:47 +0300 Subject: [PATCH 01/11] Fixed issue #9 quick hack to keep the notebook in user_data/notebooks --- RollingBacktestNotebook.ipynb | 42 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index c309710..edd4c43 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -7,9 +7,9 @@ "source": [ "# FreqTrade backtesting analysis and plotting notebook\n", "\n", - "Version: v1.1\n", + "Version: v1.2\n", "\n", - "Date: 2023-07-08\n", + "Date: 2023-07-22\n", "\n", "GitHub: https://github.com/froggleston/freqtrade_analysis_notebook\n", "\n", @@ -117,8 +117,12 @@ "## main configuration for everything ##\n", "\n", "# main freqtrade dir\n", - "# use '.' if you copy the notebook files into your ft dir\n", - "freqtrade_dir = \".\"\n", + "\n", + "# If using docker:\n", + "freqtrade_dir = \"/freqtrade\"\n", + "os.chdir(\"/freqtrade/\")\n", + "# else, comment the 2 lines and use '.' if you copy the notebook files into your ft dir\n", + "# freqtrade_dir = \".\"\n", "\n", "if 'executed' not in globals():\n", " executed = True # Guard against running this multiple times\n", @@ -158,13 +162,13 @@ "# # set your config file\n", "config_file = f\"{freqtrade_dir}/your_config.json\"\n", "\n", - "# # set your format and path to downloaded data\n", + "# set your format and path to downloaded data\n", "# data_format = \"json\"\n", - "# data_location = Path('/path', 'to', 'your', 'data', f'{exchange}')\n", + "# data_location = Path(freqtrade_dir, 'path', 'to', 'your', 'data', f'{exchange}')\n", "\n", "# set your stake currency and stake format\n", "stake_currency = \"USDT\"\n", - " \n", + "\n", "# set your chosen stoploss\n", "stoploss = -0.125\n", "\n", @@ -183,7 +187,6 @@ "# uncomment for 1m detail\n", "# timeframe_detail = \"1m\"\n", "# ft_config['timeframe_detail'] = timeframe_detail\n", - "\n", "ft_config['datadir'] = data_location\n", "\n", "if short:\n", @@ -261,8 +264,7 @@ "metadata": { "pycharm": { "name": "#%%t\n" - }, - "scrolled": false + } }, "outputs": [], "source": [ @@ -326,9 +328,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "# run the test strategy for the same timerange as benchmark \n", @@ -536,9 +536,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "import sqlite3\n", @@ -708,9 +706,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "enter_tags=\"all\"\n", @@ -743,9 +739,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "# construct the plotting framework\n", @@ -917,7 +911,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.11.4" }, "vscode": { "interpreter": { @@ -926,5 +920,5 @@ } }, "nbformat": 4, - "nbformat_minor": 1 + "nbformat_minor": 4 } From 6321f77349d6996c43c9cff580af0eb519522e39 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 18:14:53 +0300 Subject: [PATCH 02/11] Fixed issue #11 can't run benchmark strategy when using a config file without entry and exit pricing for futures --- RollingBacktestNotebook.ipynb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index edd4c43..40e05ba 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -208,6 +208,22 @@ " 'dataformat_ohlcv':data_format,\n", " 'stoploss': stoploss,\n", " 'minimal_roi': minimal_roi,\n", + " \"entry_pricing\": {\n", + " \"price_side\": \"same\",\n", + " \"use_order_book\": True,\n", + " \"order_book_top\": 1,\n", + " \"price_last_balance\": 0.0,\n", + " \"check_depth_of_market\": {\n", + " \"enabled\": False,\n", + " \"bids_to_ask_delta\": 1\n", + " }\n", + " },\n", + " \"exit_pricing\": {\n", + " \"price_side\": \"same\",\n", + " \"use_order_book\": True,\n", + " \"order_book_top\": 1,\n", + " \"price_last_balance\": 0.0\n", + " } \n", " }\n", "else:\n", " trading_mode = CandleType.SPOT\n", @@ -227,6 +243,22 @@ " 'dataformat_ohlcv':data_format,\n", " 'minimal_roi': minimal_roi,\n", " 'enable_protections': enable_protections,\n", + " \"entry_pricing\": {\n", + " \"price_side\": \"same\",\n", + " \"use_order_book\": True,\n", + " \"order_book_top\": 1,\n", + " \"price_last_balance\": 0.0,\n", + " \"check_depth_of_market\": {\n", + " \"enabled\": False,\n", + " \"bids_to_ask_delta\": 1\n", + " }\n", + " },\n", + " \"exit_pricing\": {\n", + " \"price_side\": \"same\",\n", + " \"use_order_book\": True,\n", + " \"order_book_top\": 1,\n", + " \"price_last_balance\": 0.0\n", + " }\n", " }\n", "\n", "bench_config = {\n", From c0a179d7f5827cb0ec66ee32b87a552a1b35b370 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 18:15:20 +0300 Subject: [PATCH 03/11] Added swap files to .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index b6e4761..720084f 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# Swap files +*.swp From 6ae22125041f59c8654416a50a21073da42d4cbd Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 19:21:37 +0300 Subject: [PATCH 04/11] Fixed issue #9 when getting No such file or directory: 'freqtrade_dir' when using the notebook from another directory --- RollingBacktestNotebook.ipynb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index 40e05ba..abdaced 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -119,10 +119,8 @@ "# main freqtrade dir\n", "\n", "# If using docker:\n", - "freqtrade_dir = \"/freqtrade\"\n", - "os.chdir(\"/freqtrade/\")\n", - "# else, comment the 2 lines and use '.' if you copy the notebook files into your ft dir\n", - "# freqtrade_dir = \".\"\n", + "freqtrade_dir = \".\" # use '.' if you've copied the notebook files into your ft dir\n", + "# freqtrade_dir = \"/freqtrade\" # if using docker\n", "\n", "if 'executed' not in globals():\n", " executed = True # Guard against running this multiple times\n", @@ -130,7 +128,7 @@ " # cwd = cwd[0] # only use if using !pwd above\n", " sys.path.append(cwd) # Add notebook dir to python path for utility imports\n", " # cd to root directory to make relative paths in config valid\n", - " %cd freqtrade_dir\n", + " os.chdir(\"/freqtrade/\")\n", "\n", "# parallelise backtests by month\n", "parallel = True\n", From b39af3339998164286eafc42712114d15c885827 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 20:18:59 +0300 Subject: [PATCH 05/11] Added a common situation that can happen when your data_format is set to the wrong value. --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 430b643..a8a2c85 100644 --- a/README.md +++ b/README.md @@ -94,4 +94,8 @@ Mouseover main plot and subplot data series to see individual values. ### plotly - works but slow -- not as fancy \ No newline at end of file +- not as fancy + +### "No data found. Terminating." when running backtests + +Usually happens if you are using a different data_format than the one downloaded. From 763ac3047295342164eca49a903a352c3621c4f8 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 20:30:34 +0300 Subject: [PATCH 06/11] Added error and workaround for a situation when you're using too many pairs to test. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index a8a2c85..3633b4a 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,7 @@ Mouseover main plot and subplot data series to see individual values. ### "No data found. Terminating." when running backtests Usually happens if you are using a different data_format than the one downloaded. + +### "IOPub data rate exceeded." when running backtests + +Lower the number of pairs in your pairlist. From c070204f23d93230a2ae124afdeb7eafead18eaf Mon Sep 17 00:00:00 2001 From: vaidab Date: Sat, 22 Jul 2023 21:10:55 +0300 Subject: [PATCH 07/11] Added instructions for docker install and setup --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3633b4a..c5205f8 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,23 @@ pip install -r /path/to/freqtrade_analysis_notebook/requirements.txt ### Docker -This has not been tested in any docker environments, so YMMV, but some instructions that might be useful are in [this issue](https://github.com/froggleston/freqtrade_analysis_notebook/issues/1) +This has been tested under docker by @vaidab . + +Use the instructions [here](https://www.freqtrade.io/en/stable/data-analysis/) for setting up Jupyter for freqtrade under docker. + +Some instructions that might be useful are in [this issue](https://github.com/froggleston/freqtrade_analysis_notebook/issues/1) + ## Installation Follow one of the two methods below: +### Docker installation + +- Copy `.py` files and the `RollingBacktestNotebook.ipynb` to user_data/notebooks. +- Set `freqtrade_dir` variable to `"/freqtrade"` + + ### Easiest installation - Copy all `.py` files and the `RollingBacktestNotebook.ipynb` into your base freqtrade folder, **not** the user_data/notebooks folder. @@ -67,6 +78,10 @@ Pick any of these to open up a new Jupyter file browser tab in your preferred br If using an IDE like vscode, install an available jupyter extension and open the freqtrade folder. Then open the ipynb file and run the cells as normal. +### Via Docker + +Connect to JupyterLab by following the instructions on the Freqtrade site. + ## Usage Use the toolbar at the top of the plot to change behaviour or select/deselect data series. From 838e148ca9a2108e501cc40edcdac1fa43d8398a Mon Sep 17 00:00:00 2001 From: vaidab Date: Sun, 23 Jul 2023 11:04:28 +0300 Subject: [PATCH 08/11] Now raises exception if trying to connect to a non-existant database. Something that sqlite3.connect doesn't do by default --- RollingBacktestNotebook.ipynb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index abdaced..493708d 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -573,6 +573,7 @@ "from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats\n", "\n", "# specify your actual trades from a dry/live DB\n", + "# db_path = f\"{freqtrade_dir}/user_data/your_db_name.sqlite\"\n", "db_path = \"your_db_name.sqlite\"\n", "\n", "# if backtest_dir points to a directory, it'll automatically load the last backtest file\n", @@ -581,7 +582,14 @@ "# or specify a specific backtest results file\n", "# backtest_dir = config[\"user_data_dir\"] / \"backtest_results/backtest-result-2020-07-01_20-04-22.json\"\n", "\n", - "dat = sqlite3.connect(db_path)\n", + "try:\n", + " if os.path.exists(db_path):\n", + " dat = sqlite3.connect(db_path)\n", + " else:\n", + " raise FileNotFoundError(db_path + \" does not exist\")\n", + "except sqlite3.Error as e:\n", + " print(f\"An error occurred connecting to the database: {e}\")\n", + "\n", "sel_cols = \"pair,open_date,close_date,min_rate,max_rate,enter_tag,exit_reason,open_rate,close_rate,close_profit,close_profit_abs\"\n", "query = dat.execute(f\"SELECT {sel_cols} FROM trades\")\n", "cols = [column[0] for column in query.description]\n", From 22bccb65ec9c5dd1d237833785313210f4d88f57 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sun, 23 Jul 2023 12:17:49 +0300 Subject: [PATCH 09/11] Made dry/live - backtest comparison more verbose. Now it also raises exception if the timeranges between backtesting and dry/live don't match --- RollingBacktestNotebook.ipynb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index 493708d..247e115 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -566,7 +566,13 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, "outputs": [], "source": [ "import sqlite3\n", @@ -626,8 +632,12 @@ "bt_trades = bt_trades.loc[(bt_trades['open_date'] >= compare_start_date) & (bt_trades['open_date'] <= compare_end_date)]\n", "sql_trades = sql_trades.loc[(sql_trades['open_date'] <= bt_end_date)]\n", "\n", - "print(\"REAL\\n\", tabulate(sql_trades, headers='keys', tablefmt='psql', showindex=True))\n", - "print(\"BT\\n\", tabulate(bt_trades[sel_cols.split(\",\")], headers='keys', tablefmt='psql', showindex=True))\n", + "print(f\"Trades that match both timeranges: {sql_trades.shape[0]}/{num_real_trades} trades in the dry/live DB and {bt_trades.shape[0]}/{num_bt_trades} trades in the backtest file.\")\n", + "if sql_trades.shape[0] == 0 or bt_trades.shape[0] == 0:\n", + " raise Exception(\"No matching trades, check if backtesting period matches dry/live run\")\n", + "\n", + "print(\"DRY/LIVE\\n\", tabulate(sql_trades, headers='keys', tablefmt='psql', showindex=True))\n", + "print(\"BACKTESTING\\n\", tabulate(bt_trades[sel_cols.split(\",\")], headers='keys', tablefmt='psql', showindex=True))\n", "\n", "merged_df = pd.merge(sql_trades, bt_trades, how ='outer', on =['pair', 'open_date'], suffixes=('_sql', '_bt')).sort_values(by='open_date')\n", "\n", From fb4753db7a7f2e224114acc185016f59d7bbdaf9 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sun, 23 Jul 2023 12:26:56 +0300 Subject: [PATCH 10/11] Added myself as a contributor. --- RollingBacktestNotebook.ipynb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index 247e115..637bf6d 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -17,6 +17,9 @@ "* **@froggleston** (for the notebook and some of the notebook_helper code)\n", "* **@rk** (for most of the notebook_helper code)\n", "\n", + "Contributor:\n", + "* **@vaidab** (various bugfixes and docker setup & testing)\n", + "\n", "### Welcome to the Notebook\n", "\n", "This notebook should make it easier to run sequential test backtests and compare them against a single benchmark backtest.\n", From 328ae1bfb71be1a084b1a436a2d00119c5c41869 Mon Sep 17 00:00:00 2001 From: vaidab Date: Sun, 23 Jul 2023 12:26:56 +0300 Subject: [PATCH 11/11] Added myself as a contributor. --- RollingBacktestNotebook.ipynb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RollingBacktestNotebook.ipynb b/RollingBacktestNotebook.ipynb index 247e115..637bf6d 100644 --- a/RollingBacktestNotebook.ipynb +++ b/RollingBacktestNotebook.ipynb @@ -17,6 +17,9 @@ "* **@froggleston** (for the notebook and some of the notebook_helper code)\n", "* **@rk** (for most of the notebook_helper code)\n", "\n", + "Contributor:\n", + "* **@vaidab** (various bugfixes and docker setup & testing)\n", + "\n", "### Welcome to the Notebook\n", "\n", "This notebook should make it easier to run sequential test backtests and compare them against a single benchmark backtest.\n",