From 1ea988425e232dc44f7000d38646292ff8bbe7a3 Mon Sep 17 00:00:00 2001 From: edtechre Date: Mon, 24 Jun 2024 19:48:23 -0700 Subject: [PATCH] Update 9. Rebalancing Positions.ipynb --- .../notebooks/9. Rebalancing Positions.ipynb | 125 +++++++++--------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/docs/source/notebooks/9. Rebalancing Positions.ipynb b/docs/source/notebooks/9. Rebalancing Positions.ipynb index 55f5d27..4167967 100644 --- a/docs/source/notebooks/9. Rebalancing Positions.ipynb +++ b/docs/source/notebooks/9. Rebalancing Positions.ipynb @@ -19,7 +19,7 @@ { "data": { "text/plain": [ - "" + "" ] }, "execution_count": 1, @@ -54,7 +54,8 @@ "metadata": {}, "outputs": [], "source": [ - "def start_of_month(dt: datetime) -> bool:\n", + "def start_of_month(ctxs: dict[str, ExecContext]) -> bool:\n", + " dt = tuple(ctxs.values())[0].dt\n", " if dt.month != pyb.param('current_month'):\n", " pyb.param('current_month', dt.month)\n", " return True\n", @@ -110,8 +111,7 @@ "outputs": [], "source": [ "def rebalance(ctxs: dict[str, ExecContext]):\n", - " dt = tuple(ctxs.values())[0].dt\n", - " if start_of_month(dt):\n", + " if start_of_month(ctxs):\n", " target = 1 / len(ctxs)\n", " set_target_shares(ctxs, {symbol: target for symbol in ctxs.keys()})" ] @@ -145,7 +145,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "100% (1259 of 1259) |####################| Elapsed Time: 0:00:00 Time: 0:00:00\n" + "100% (1259 of 1259) |####################| Elapsed Time: 0:00:01 Time: 0:00:01\n" ] }, { @@ -153,7 +153,7 @@ "output_type": "stream", "text": [ "\n", - "Finished backtest: 0:00:02\n" + "Finished backtest: 0:00:03\n" ] } ], @@ -214,51 +214,51 @@ " \n", " 1\n", " buy\n", - " NFLX\n", + " AAPL\n", " 2018-01-03\n", - " 99\n", + " 464\n", " NaN\n", - " 203.86\n", + " 43.31\n", " 0.0\n", " \n", " \n", " 2\n", " buy\n", - " AAPL\n", + " AMZN\n", " 2018-01-03\n", - " 464\n", + " 336\n", " NaN\n", - " 43.31\n", + " 59.84\n", " 0.0\n", " \n", " \n", " 3\n", " buy\n", - " TSLA\n", + " NFLX\n", " 2018-01-03\n", - " 935\n", + " 99\n", " NaN\n", - " 21.36\n", + " 203.86\n", " 0.0\n", " \n", " \n", " 4\n", " buy\n", - " AMZN\n", + " NVDA\n", " 2018-01-03\n", - " 336\n", + " 4013\n", " NaN\n", - " 59.84\n", + " 5.22\n", " 0.0\n", " \n", " \n", " 5\n", " buy\n", - " NVDA\n", + " TSLA\n", " 2018-01-03\n", - " 376\n", + " 873\n", " NaN\n", - " 52.18\n", + " 21.36\n", " 0.0\n", " \n", " \n", @@ -272,7 +272,7 @@ " ...\n", " \n", " \n", - " 292\n", + " 293\n", " sell\n", " NFLX\n", " 2022-12-02\n", @@ -282,66 +282,66 @@ " 0.0\n", " \n", " \n", - " 293\n", + " 294\n", " sell\n", " NVDA\n", " 2022-12-02\n", - " 97\n", + " 974\n", " NaN\n", - " 166.89\n", + " 16.69\n", " 0.0\n", " \n", " \n", - " 294\n", + " 295\n", " buy\n", " AAPL\n", " 2022-12-02\n", - " 27\n", + " 28\n", " NaN\n", " 146.82\n", " 0.0\n", " \n", " \n", - " 295\n", + " 296\n", " buy\n", - " TSLA\n", + " AMZN\n", " 2022-12-02\n", - " 70\n", + " 42\n", " NaN\n", - " 193.68\n", + " 94.57\n", " 0.0\n", " \n", " \n", - " 296\n", + " 297\n", " buy\n", - " AMZN\n", + " TSLA\n", " 2022-12-02\n", - " 41\n", + " 70\n", " NaN\n", - " 94.57\n", + " 193.68\n", " 0.0\n", " \n", " \n", "\n", - "

296 rows × 7 columns

\n", + "

297 rows × 7 columns

\n", "" ], "text/plain": [ " type symbol date shares limit_price fill_price fees\n", "id \n", - "1 buy NFLX 2018-01-03 99 NaN 203.86 0.0\n", - "2 buy AAPL 2018-01-03 464 NaN 43.31 0.0\n", - "3 buy TSLA 2018-01-03 935 NaN 21.36 0.0\n", - "4 buy AMZN 2018-01-03 336 NaN 59.84 0.0\n", - "5 buy NVDA 2018-01-03 376 NaN 52.18 0.0\n", + "1 buy AAPL 2018-01-03 464 NaN 43.31 0.0\n", + "2 buy AMZN 2018-01-03 336 NaN 59.84 0.0\n", + "3 buy NFLX 2018-01-03 99 NaN 203.86 0.0\n", + "4 buy NVDA 2018-01-03 4013 NaN 5.22 0.0\n", + "5 buy TSLA 2018-01-03 873 NaN 21.36 0.0\n", ".. ... ... ... ... ... ... ...\n", - "292 sell NFLX 2022-12-02 15 NaN 315.99 0.0\n", - "293 sell NVDA 2022-12-02 97 NaN 166.89 0.0\n", - "294 buy AAPL 2022-12-02 27 NaN 146.82 0.0\n", - "295 buy TSLA 2022-12-02 70 NaN 193.68 0.0\n", - "296 buy AMZN 2022-12-02 41 NaN 94.57 0.0\n", + "293 sell NFLX 2022-12-02 15 NaN 315.99 0.0\n", + "294 sell NVDA 2022-12-02 974 NaN 16.69 0.0\n", + "295 buy AAPL 2022-12-02 28 NaN 146.82 0.0\n", + "296 buy AMZN 2022-12-02 42 NaN 94.57 0.0\n", + "297 buy TSLA 2022-12-02 70 NaN 193.68 0.0\n", "\n", - "[296 rows x 7 columns]" + "[297 rows x 7 columns]" ] }, "execution_count": 6, @@ -386,8 +386,7 @@ "\n", "def optimization(ctxs: dict[str, ExecContext]):\n", " lookback = pyb.param('lookback')\n", - " first_ctx = tuple(ctxs.values())[0]\n", - " if start_of_month(first_ctx.dt):\n", + " if start_of_month(ctxs):\n", " Y = calculate_returns(ctxs, lookback)\n", " port = rp.Portfolio(returns=Y)\n", " port.assets_stats(method_mu='hist', method_cov='hist', d=0.94)\n", @@ -512,21 +511,21 @@ " \n", " 2\n", " buy\n", - " TSLA\n", + " AMZN\n", " 2019-01-04\n", - " 1168\n", + " 347\n", " NaN\n", - " 20.69\n", + " 77.81\n", " 0.0\n", " \n", " \n", " 3\n", " buy\n", - " AMZN\n", + " TSLA\n", " 2019-01-04\n", - " 307\n", + " 1020\n", " NaN\n", - " 77.81\n", + " 20.69\n", " 0.0\n", " \n", " \n", @@ -534,7 +533,7 @@ " sell\n", " AAPL\n", " 2019-02-04\n", - " 105\n", + " 103\n", " NaN\n", " 42.37\n", " 0.0\n", @@ -542,11 +541,11 @@ " \n", " 5\n", " buy\n", - " TSLA\n", + " AMZN\n", " 2019-02-04\n", - " 51\n", + " 1\n", " NaN\n", - " 20.57\n", + " 81.58\n", " 0.0\n", " \n", " \n", @@ -557,10 +556,10 @@ " type symbol date shares limit_price fill_price fees\n", "id \n", "1 buy AAPL 2019-01-04 1420 NaN 36.54 0.0\n", - "2 buy TSLA 2019-01-04 1168 NaN 20.69 0.0\n", - "3 buy AMZN 2019-01-04 307 NaN 77.81 0.0\n", - "4 sell AAPL 2019-02-04 105 NaN 42.37 0.0\n", - "5 buy TSLA 2019-02-04 51 NaN 20.57 0.0" + "2 buy AMZN 2019-01-04 347 NaN 77.81 0.0\n", + "3 buy TSLA 2019-01-04 1020 NaN 20.69 0.0\n", + "4 sell AAPL 2019-02-04 103 NaN 42.37 0.0\n", + "5 buy AMZN 2019-02-04 1 NaN 81.58 0.0" ] }, "execution_count": 9,