From 976f9b259044e114ba220ac3b94066aa8ffa41ea Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 19 Aug 2024 18:23:36 +0200 Subject: [PATCH] chore: re-format ipynb notebook --- docs/strategy_analysis_example.md | 44 +++++++++++++------ .../templates/strategy_analysis_example.ipynb | 42 +++++++++++++----- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/docs/strategy_analysis_example.md b/docs/strategy_analysis_example.md index 22828b899..30faa27ad 100644 --- a/docs/strategy_analysis_example.md +++ b/docs/strategy_analysis_example.md @@ -13,6 +13,7 @@ Please follow the [documentation](https://www.freqtrade.io/en/stable/data-downlo import os from pathlib import Path + # Change directory # Modify this cell to insure that the output shows the correct path. # Define all paths relative to the project root shown in the cell output @@ -20,12 +21,14 @@ project_root = "somedir/freqtrade" i=0 try: os.chdir(project_root) - assert Path('LICENSE').is_file() -except: - while i<4 and (not Path('LICENSE').is_file()): - os.chdir(Path(Path.cwd(), '../')) - i+=1 - project_root = Path.cwd() + if not Path('LICENSE').is_file(): + i = 0 + while i < 4 and (not Path('LICENSE').is_file()): + os.chdir(Path(Path.cwd(), '../')) + i += 1 + project_root = Path.cwd() +except FileNotFoundError: + print("Please define the project root relative to the current directory") print(Path.cwd()) ``` @@ -35,6 +38,7 @@ print(Path.cwd()) ```python from freqtrade.configuration import Configuration + # Customize these according to your needs. # Initialize empty configuration object @@ -58,6 +62,7 @@ pair = "BTC/USDT" from freqtrade.data.history import load_pair_history from freqtrade.enums import CandleType + candles = load_pair_history(datadir=data_location, timeframe=config["timeframe"], pair=pair, @@ -76,8 +81,10 @@ candles.head() ```python # Load strategy using values set above -from freqtrade.resolvers import StrategyResolver from freqtrade.data.dataprovider import DataProvider +from freqtrade.resolvers import StrategyResolver + + strategy = StrategyResolver.load_strategy(config) strategy.dp = DataProvider(config, None, None) strategy.ft_bot_start() @@ -119,10 +126,13 @@ Analyze a trades dataframe (also used below for plotting) ```python from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats + # 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" +# 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" +# ) ``` @@ -132,7 +142,8 @@ backtest_dir = config["user_data_dir"] / "backtest_results" 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. +# 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 @@ -166,10 +177,12 @@ trades.groupby("pair")["exit_reason"].value_counts() ```python # Plotting equity line (starting with 0 on day 1 and adding daily profit for each backtested day) +import pandas as pd +import plotly.express as px + from freqtrade.configuration import Configuration from freqtrade.data.btanalysis import load_backtest_stats -import plotly.express as px -import pandas as pd + # strategy = 'SampleStrategy' # config = Configuration.from_files(["user_data/config.json"]) @@ -194,6 +207,7 @@ In case you did already some trading and want to analyze your performance ```python from freqtrade.data.btanalysis import load_trades_from_db + # Fetch trades from database trades = load_trades_from_db("sqlite:///tradesv3.sqlite") @@ -210,6 +224,7 @@ This can be useful to find the best `max_open_trades` parameter, when used with ```python from freqtrade.data.btanalysis import analyze_trade_parallelism + # Analyze the above parallel_trades = analyze_trade_parallelism(trades, '5m') @@ -222,7 +237,9 @@ Freqtrade offers interactive plotting capabilities based on plotly. ```python -from freqtrade.plot.plotting import generate_candlestick_graph +from freqtrade.plot.plotting import generate_candlestick_graph + + # Limit graph period to keep plotly quick and reactive # Filter trades to one pair @@ -257,6 +274,7 @@ graph.show(renderer="browser") ```python import plotly.figure_factory as ff + hist_data = [trades.profit_ratio] group_labels = ['profit_ratio'] # name of the dataset diff --git a/freqtrade/templates/strategy_analysis_example.ipynb b/freqtrade/templates/strategy_analysis_example.ipynb index 8d4459a3c..d7008eca9 100644 --- a/freqtrade/templates/strategy_analysis_example.ipynb +++ b/freqtrade/templates/strategy_analysis_example.ipynb @@ -29,6 +29,7 @@ "import os\n", "from pathlib import Path\n", "\n", + "\n", "# Change directory\n", "# Modify this cell to insure that the output shows the correct path.\n", "# Define all paths relative to the project root shown in the cell output\n", @@ -36,12 +37,14 @@ "i=0\n", "try:\n", " os.chdir(project_root)\n", - " assert Path('LICENSE').is_file()\n", - "except:\n", - " while i<4 and (not Path('LICENSE').is_file()):\n", - " os.chdir(Path(Path.cwd(), '../'))\n", - " i+=1\n", - " project_root = Path.cwd()\n", + " if not Path('LICENSE').is_file():\n", + " i = 0\n", + " while i < 4 and (not Path('LICENSE').is_file()):\n", + " os.chdir(Path(Path.cwd(), '../'))\n", + " i += 1\n", + " project_root = Path.cwd()\n", + "except FileNotFoundError:\n", + " print(\"Please define the project root relative to the current directory\")\n", "print(Path.cwd())" ] }, @@ -60,6 +63,7 @@ "source": [ "from freqtrade.configuration import Configuration\n", "\n", + "\n", "# Customize these according to your needs.\n", "\n", "# Initialize empty configuration object\n", @@ -87,6 +91,7 @@ "from freqtrade.data.history import load_pair_history\n", "from freqtrade.enums import CandleType\n", "\n", + "\n", "candles = load_pair_history(datadir=data_location,\n", " timeframe=config[\"timeframe\"],\n", " pair=pair,\n", @@ -114,8 +119,10 @@ "outputs": [], "source": [ "# Load strategy using values set above\n", - "from freqtrade.resolvers import StrategyResolver\n", "from freqtrade.data.dataprovider import DataProvider\n", + "from freqtrade.resolvers import StrategyResolver\n", + "\n", + "\n", "strategy = StrategyResolver.load_strategy(config)\n", "strategy.dp = DataProvider(config, None, None)\n", "strategy.ft_bot_start()\n", @@ -179,10 +186,13 @@ "source": [ "from freqtrade.data.btanalysis import load_backtest_data, load_backtest_stats\n", "\n", + "\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\"" + "# backtest_dir = (\n", + "# config[\"user_data_dir\"] / \"backtest_results/backtest-result-2020-07-01_20-04-22.json\"\n", + "# )" ] }, { @@ -196,7 +206,8 @@ "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", + "# All statistics are available per strategy, so if `--strategy-list` was used during backtest,\n", + "# 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", @@ -242,10 +253,12 @@ "source": [ "# Plotting equity line (starting with 0 on day 1 and adding daily profit for each backtested day)\n", "\n", + "import pandas as pd\n", + "import plotly.express as px\n", + "\n", "from freqtrade.configuration import Configuration\n", "from freqtrade.data.btanalysis import load_backtest_stats\n", - "import plotly.express as px\n", - "import pandas as pd\n", + "\n", "\n", "# strategy = 'SampleStrategy'\n", "# config = Configuration.from_files([\"user_data/config.json\"])\n", @@ -278,6 +291,7 @@ "source": [ "from freqtrade.data.btanalysis import load_trades_from_db\n", "\n", + "\n", "# Fetch trades from database\n", "trades = load_trades_from_db(\"sqlite:///tradesv3.sqlite\")\n", "\n", @@ -303,6 +317,7 @@ "source": [ "from freqtrade.data.btanalysis import analyze_trade_parallelism\n", "\n", + "\n", "# Analyze the above\n", "parallel_trades = analyze_trade_parallelism(trades, '5m')\n", "\n", @@ -324,7 +339,9 @@ "metadata": {}, "outputs": [], "source": [ - "from freqtrade.plot.plotting import generate_candlestick_graph\n", + "from freqtrade.plot.plotting import generate_candlestick_graph\n", + "\n", + "\n", "# Limit graph period to keep plotly quick and reactive\n", "\n", "# Filter trades to one pair\n", @@ -370,6 +387,7 @@ "source": [ "import plotly.figure_factory as ff\n", "\n", + "\n", "hist_data = [trades.profit_ratio]\n", "group_labels = ['profit_ratio'] # name of the dataset\n", "\n",