From d56f9655e2b019cf95af75c613257b9049661944 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 3 Jul 2020 07:20:43 +0200 Subject: [PATCH] Update notebook with new statistics example --- docs/plotting.md | 2 +- docs/strategy_analysis_example.md | 44 +++++++++++++++-- freqtrade/data/btanalysis.py | 2 +- .../templates/strategy_analysis_example.ipynb | 47 +++++++++++++++++-- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/docs/plotting.md b/docs/plotting.md index 7de5626b2..09eb6ddb5 100644 --- a/docs/plotting.md +++ b/docs/plotting.md @@ -285,7 +285,7 @@ Examples: Use custom backtest-export file ``` bash -freqtrade plot-profit -p LTC/BTC --export-filename user_data/backtest_results/backtest-result-Strategy005.json +freqtrade plot-profit -p LTC/BTC --export-filename user_data/backtest_results/backtest-result.json ``` Use custom database diff --git a/docs/strategy_analysis_example.md b/docs/strategy_analysis_example.md index 6b4ad567f..8915ebe37 100644 --- a/docs/strategy_analysis_example.md +++ b/docs/strategy_analysis_example.md @@ -18,7 +18,7 @@ config = Configuration.from_files([]) # config = Configuration.from_files(["config.json"]) # Define some constants -config["timeframe"] = "5m" +config["ticker_interval"] = "5m" # Name of the strategy class config["strategy"] = "SampleStrategy" # Location of the data @@ -33,7 +33,7 @@ pair = "BTC_USDT" from freqtrade.data.history import load_pair_history candles = load_pair_history(datadir=data_location, - timeframe=config["timeframe"], + timeframe=config["ticker_interval"], pair=pair) # Confirm success @@ -85,10 +85,44 @@ Analyze a trades dataframe (also used below for plotting) ```python -from freqtrade.data.btanalysis import load_backtest_data +from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats -# Load backtest results -trades = load_backtest_data(config["user_data_dir"] / "backtest_results/backtest-result.json") +# if backtest_dir points to a directory, it'll automatically load the last backtest file. +backtest_dir = config["user_data_dir"] / "backtest_results" +# backtest_dir can also point to a specific file +# backtest_dir = config["user_data_dir"] / "backtest_results/backtest-result-2020-07-01_20-04-22.json" +``` + + +```python +# You can get the full backtest statistics by using the following command. +# This contains all information used to generate the backtest result. +stats = load_backtest_stats(backtest_dir) + +strategy = 'SampleStrategy' +# All statistics are available per strategy, so if `--strategy-list` was used during backtest, this will be reflected here as well. +# Example usages: +print(stats['strategy'][strategy]['results_per_pair']) +# Get pairlist used for this backtest +print(stats['strategy'][strategy]['pairlist']) +# Get market change (average change of all pairs from start to end of the backtest period) +print(stats['strategy'][strategy]['market_change']) +# Maximum drawdown () +print(stats['strategy'][strategy]['max_drawdown']) +# Maximum drawdown start and end +print(stats['strategy'][strategy]['drawdown_start']) +print(stats['strategy'][strategy]['drawdown_end']) + + +# Get strategy comparison (only relevant if multiple strategies were compared) +print(stats['strategy_comparison']) + +``` + + +```python +# Load backtested trades as dataframe +trades = load_backtest_data(backtest_dir) # Show value-counts per pair trades.groupby("pair")["sell_reason"].value_counts() diff --git a/freqtrade/data/btanalysis.py b/freqtrade/data/btanalysis.py index 6931b1685..cf6e18e64 100644 --- a/freqtrade/data/btanalysis.py +++ b/freqtrade/data/btanalysis.py @@ -72,7 +72,7 @@ def load_backtest_stats(filename: Union[Path, str]) -> Dict[str, Any]: def load_backtest_data(filename: Union[Path, str], strategy: Optional[str] = None) -> pd.DataFrame: """ Load backtest data file. - :param filename: pathlib.Path object, or string pointing to the file. + :param filename: pathlib.Path object, or string pointing to a file or directory :param strategy: Strategy to load - mainly relevant for multi-strategy backtests Can also serve as protection to load the correct result. :return: a dataframe with the analysis results diff --git a/freqtrade/templates/strategy_analysis_example.ipynb b/freqtrade/templates/strategy_analysis_example.ipynb index dffa308ce..31a5b536a 100644 --- a/freqtrade/templates/strategy_analysis_example.ipynb +++ b/freqtrade/templates/strategy_analysis_example.ipynb @@ -136,10 +136,51 @@ "metadata": {}, "outputs": [], "source": [ - "from freqtrade.data.btanalysis import load_backtest_data\n", + "from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats\n", "\n", - "# Load backtest results\n", - "trades = load_backtest_data(config[\"user_data_dir\"] / \"backtest_results/backtest-result.json\")\n", + "# if backtest_dir points to a directory, it'll automatically load the last backtest file.\n", + "backtest_dir = config[\"user_data_dir\"] / \"backtest_results\"\n", + "# backtest_dir can also point to a specific file \n", + "# backtest_dir = config[\"user_data_dir\"] / \"backtest_results/backtest-result-2020-07-01_20-04-22.json\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# You can get the full backtest statistics by using the following command.\n", + "# This contains all information used to generate the backtest result.\n", + "stats = load_backtest_stats(backtest_dir)\n", + "\n", + "strategy = 'SampleStrategy'\n", + "# All statistics are available per strategy, so if `--strategy-list` was used during backtest, this will be reflected here as well.\n", + "# Example usages:\n", + "print(stats['strategy'][strategy]['results_per_pair'])\n", + "# Get pairlist used for this backtest\n", + "print(stats['strategy'][strategy]['pairlist'])\n", + "# Get market change (average change of all pairs from start to end of the backtest period)\n", + "print(stats['strategy'][strategy]['market_change'])\n", + "# Maximum drawdown ()\n", + "print(stats['strategy'][strategy]['max_drawdown'])\n", + "# Maximum drawdown start and end\n", + "print(stats['strategy'][strategy]['drawdown_start'])\n", + "print(stats['strategy'][strategy]['drawdown_end'])\n", + "\n", + "\n", + "# Get strategy comparison (only relevant if multiple strategies were compared)\n", + "print(stats['strategy_comparison'])\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Load backtested trades as dataframe\n", + "trades = load_backtest_data(backtest_dir)\n", "\n", "# Show value-counts per pair\n", "trades.groupby(\"pair\")[\"sell_reason\"].value_counts()"