diff --git a/docs/notebooks/6. Training a Model.ipynb b/docs/notebooks/6. Training a Model.ipynb index 7c7b2bd..63d1ddd 100644 --- a/docs/notebooks/6. Training a Model.ipynb +++ b/docs/notebooks/6. Training a Model.ipynb @@ -25,7 +25,7 @@ "import pandas as pd\n", "import pybroker\n", "from numba import njit\n", - "from pybroker import Strategy, YFinance" + "from pybroker import Strategy, StrategyConfig, YFinance" ] }, { @@ -194,7 +194,8 @@ "metadata": {}, "outputs": [], "source": [ - "strategy = Strategy(YFinance(), '3/1/2017', '3/1/2022')\n", + "config = StrategyConfig(bootstrap_sample_size=100)\n", + "strategy = Strategy(YFinance(), '3/1/2017', '3/1/2022', config)\n", "strategy.add_execution(None, ['NVDA', 'AMD'], models=model_slr)" ] }, @@ -388,7 +389,7 @@ "output_type": "stream", "text": [ "\n", - "Calculating bootstrap metrics: sample_size=1000, samples=10000...\n", + "Calculating bootstrap metrics: sample_size=100, samples=10000...\n", "Calculated bootstrap metrics: 0:00:02 \n", "\n", "Finished backtest: 0:00:03\n" @@ -446,7 +447,7 @@ " \n", " 0\n", " trade_count\n", - " 129.000000\n", + " 43.000000\n", " \n", " \n", " 1\n", @@ -461,160 +462,165 @@ " \n", " 3\n", " total_pnl\n", - " 33879.000000\n", + " 11293.000000\n", " \n", " \n", " 4\n", - " total_return_pct\n", - " 33.879000\n", + " unrealized_pnl\n", + " -2814.000000\n", " \n", " \n", " 5\n", - " total_profit\n", - " 59796.000000\n", + " total_return_pct\n", + " 11.293000\n", " \n", " \n", " 6\n", - " total_loss\n", - " -25917.000000\n", + " total_profit\n", + " 19932.000000\n", " \n", " \n", " 7\n", + " total_loss\n", + " -8639.000000\n", + " \n", + " \n", + " 8\n", " total_fees\n", " 0.000000\n", " \n", " \n", - " 8\n", + " 9\n", " max_drawdown\n", - " -18151.000000\n", + " -14177.000000\n", " \n", " \n", - " 9\n", + " 10\n", " max_drawdown_pct\n", - " -15.898222\n", + " -12.417448\n", " \n", " \n", - " 10\n", + " 11\n", " win_rate\n", " 76.744186\n", " \n", " \n", - " 11\n", + " 12\n", " loss_rate\n", " 23.255814\n", " \n", " \n", - " 12\n", + " 13\n", " winning_trades\n", - " 99.000000\n", + " 33.000000\n", " \n", " \n", - " 13\n", + " 14\n", " losing_trades\n", - " 30.000000\n", + " 10.000000\n", " \n", " \n", - " 14\n", + " 15\n", " avg_pnl\n", " 262.627907\n", " \n", " \n", - " 15\n", + " 16\n", " avg_return_pct\n", " 4.588837\n", " \n", " \n", - " 16\n", + " 17\n", " avg_trade_bars\n", " 26.534884\n", " \n", " \n", - " 17\n", + " 18\n", " avg_profit\n", " 604.000000\n", " \n", " \n", - " 18\n", + " 19\n", " avg_profit_pct\n", " 8.635455\n", " \n", " \n", - " 19\n", + " 20\n", " avg_winning_trade_bars\n", " 20.515152\n", " \n", " \n", - " 20\n", + " 21\n", " avg_loss\n", " -863.900000\n", " \n", " \n", - " 21\n", + " 22\n", " avg_loss_pct\n", " -8.765000\n", " \n", " \n", - " 22\n", + " 23\n", " avg_losing_trade_bars\n", " 46.400000\n", " \n", " \n", - " 23\n", + " 24\n", " largest_win\n", " 2715.000000\n", " \n", " \n", - " 24\n", + " 25\n", " largest_win_bars\n", " 2.000000\n", " \n", " \n", - " 25\n", + " 26\n", " largest_loss\n", " -5054.000000\n", " \n", " \n", - " 26\n", + " 27\n", " largest_loss_bars\n", " 43.000000\n", " \n", " \n", - " 27\n", + " 28\n", " max_wins\n", " 13.000000\n", " \n", " \n", - " 28\n", + " 29\n", " max_losses\n", " 2.000000\n", " \n", " \n", - " 29\n", + " 30\n", " sharpe\n", - " 0.005986\n", + " 0.020121\n", " \n", " \n", - " 30\n", + " 31\n", " profit_factor\n", - " 1.025191\n", + " 1.079581\n", " \n", " \n", - " 31\n", + " 32\n", " ulcer_index\n", - " 1.409153\n", + " 1.195682\n", " \n", " \n", - " 32\n", + " 33\n", " upi\n", - " 0.002833\n", + " 0.007973\n", " \n", " \n", - " 33\n", + " 34\n", " equity_r2\n", - " 0.082263\n", + " 0.740368\n", " \n", " \n", - " 34\n", + " 35\n", " std_error\n", " 3945.320465\n", " \n", @@ -624,41 +630,42 @@ ], "text/plain": [ " name value\n", - "0 trade_count 129.000000\n", + "0 trade_count 43.000000\n", "1 initial_market_value 100000.000000\n", "2 end_market_value 108479.000000\n", - "3 total_pnl 33879.000000\n", - "4 total_return_pct 33.879000\n", - "5 total_profit 59796.000000\n", - "6 total_loss -25917.000000\n", - "7 total_fees 0.000000\n", - "8 max_drawdown -18151.000000\n", - "9 max_drawdown_pct -15.898222\n", - "10 win_rate 76.744186\n", - "11 loss_rate 23.255814\n", - "12 winning_trades 99.000000\n", - "13 losing_trades 30.000000\n", - "14 avg_pnl 262.627907\n", - "15 avg_return_pct 4.588837\n", - "16 avg_trade_bars 26.534884\n", - "17 avg_profit 604.000000\n", - "18 avg_profit_pct 8.635455\n", - "19 avg_winning_trade_bars 20.515152\n", - "20 avg_loss -863.900000\n", - "21 avg_loss_pct -8.765000\n", - "22 avg_losing_trade_bars 46.400000\n", - "23 largest_win 2715.000000\n", - "24 largest_win_bars 2.000000\n", - "25 largest_loss -5054.000000\n", - "26 largest_loss_bars 43.000000\n", - "27 max_wins 13.000000\n", - "28 max_losses 2.000000\n", - "29 sharpe 0.005986\n", - "30 profit_factor 1.025191\n", - "31 ulcer_index 1.409153\n", - "32 upi 0.002833\n", - "33 equity_r2 0.082263\n", - "34 std_error 3945.320465" + "3 total_pnl 11293.000000\n", + "4 unrealized_pnl -2814.000000\n", + "5 total_return_pct 11.293000\n", + "6 total_profit 19932.000000\n", + "7 total_loss -8639.000000\n", + "8 total_fees 0.000000\n", + "9 max_drawdown -14177.000000\n", + "10 max_drawdown_pct -12.417448\n", + "11 win_rate 76.744186\n", + "12 loss_rate 23.255814\n", + "13 winning_trades 33.000000\n", + "14 losing_trades 10.000000\n", + "15 avg_pnl 262.627907\n", + "16 avg_return_pct 4.588837\n", + "17 avg_trade_bars 26.534884\n", + "18 avg_profit 604.000000\n", + "19 avg_profit_pct 8.635455\n", + "20 avg_winning_trade_bars 20.515152\n", + "21 avg_loss -863.900000\n", + "22 avg_loss_pct -8.765000\n", + "23 avg_losing_trade_bars 46.400000\n", + "24 largest_win 2715.000000\n", + "25 largest_win_bars 2.000000\n", + "26 largest_loss -5054.000000\n", + "27 largest_loss_bars 43.000000\n", + "28 max_wins 13.000000\n", + "29 max_losses 2.000000\n", + "30 sharpe 0.020121\n", + "31 profit_factor 1.079581\n", + "32 ulcer_index 1.195682\n", + "33 upi 0.007973\n", + "34 equity_r2 0.740368\n", + "35 std_error 3945.320465" ] }, "execution_count": 11, @@ -712,34 +719,34 @@ " \n", " Log Profit Factor\n", " 97.5%\n", - " -0.295939\n", - " 0.230177\n", + " -1.192473\n", + " 0.114899\n", " \n", " \n", " 95%\n", - " -0.244621\n", - " 0.190217\n", + " -1.192473\n", + " -0.001504\n", " \n", " \n", " 90%\n", - " -0.190077\n", - " 0.146544\n", + " -1.104707\n", + " -0.133840\n", " \n", " \n", " Sharpe Ratio\n", " 97.5%\n", - " -0.065667\n", - " 0.059654\n", + " -0.303638\n", + " -0.010800\n", " \n", " \n", " 95%\n", - " -0.055891\n", - " 0.049016\n", + " -0.303638\n", + " -0.042949\n", " \n", " \n", " 90%\n", - " -0.044904\n", - " 0.037764\n", + " -0.303638\n", + " -0.078839\n", " \n", " \n", "\n", @@ -748,12 +755,12 @@ "text/plain": [ " lower upper\n", "name conf \n", - "Log Profit Factor 97.5% -0.295939 0.230177\n", - " 95% -0.244621 0.190217\n", - " 90% -0.190077 0.146544\n", - "Sharpe Ratio 97.5% -0.065667 0.059654\n", - " 95% -0.055891 0.049016\n", - " 90% -0.044904 0.037764" + "Log Profit Factor 97.5% -1.192473 0.114899\n", + " 95% -1.192473 -0.001504\n", + " 90% -1.104707 -0.133840\n", + "Sharpe Ratio 97.5% -0.303638 -0.010800\n", + " 95% -0.303638 -0.042949\n", + " 90% -0.303638 -0.078839" ] }, "execution_count": 12, @@ -804,23 +811,23 @@ " \n", " \n", " 99.9%\n", - " -55216.50\n", - " -40.780992\n", + " -14837.00\n", + " -13.108652\n", " \n", " \n", " 99%\n", - " -42949.50\n", - " -33.320717\n", + " -11073.50\n", + " -9.822489\n", " \n", " \n", " 95%\n", - " -33154.75\n", - " -26.928774\n", + " -8393.25\n", + " -7.575274\n", " \n", " \n", " 90%\n", - " -28447.50\n", - " -23.517172\n", + " -7263.25\n", + " -6.592774\n", " \n", " \n", "\n", @@ -829,10 +836,10 @@ "text/plain": [ " amount percent\n", "conf \n", - "99.9% -55216.50 -40.780992\n", - "99% -42949.50 -33.320717\n", - "95% -33154.75 -26.928774\n", - "90% -28447.50 -23.517172" + "99.9% -14837.00 -13.108652\n", + "99% -11073.50 -9.822489\n", + "95% -8393.25 -7.575274\n", + "90% -7263.25 -6.592774" ] }, "execution_count": 13,