Skip to content

Commit

Permalink
Update 9. Rebalancing Positions.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
edtechre committed Jun 25, 2024
1 parent 94a2a85 commit 1ea9884
Showing 1 changed file with 62 additions and 63 deletions.
125 changes: 62 additions & 63 deletions docs/source/notebooks/9. Rebalancing Positions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{
"data": {
"text/plain": [
"<diskcache.core.Cache at 0x7fd854443a60>"
"<diskcache.core.Cache at 0x7f7a205902e0>"
]
},
"execution_count": 1,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()})"
]
Expand Down Expand Up @@ -145,15 +145,15 @@
"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"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Finished backtest: 0:00:02\n"
"Finished backtest: 0:00:03\n"
]
}
],
Expand Down Expand Up @@ -214,51 +214,51 @@
" <tr>\n",
" <th>1</th>\n",
" <td>buy</td>\n",
" <td>NFLX</td>\n",
" <td>AAPL</td>\n",
" <td>2018-01-03</td>\n",
" <td>99</td>\n",
" <td>464</td>\n",
" <td>NaN</td>\n",
" <td>203.86</td>\n",
" <td>43.31</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>buy</td>\n",
" <td>AAPL</td>\n",
" <td>AMZN</td>\n",
" <td>2018-01-03</td>\n",
" <td>464</td>\n",
" <td>336</td>\n",
" <td>NaN</td>\n",
" <td>43.31</td>\n",
" <td>59.84</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>buy</td>\n",
" <td>TSLA</td>\n",
" <td>NFLX</td>\n",
" <td>2018-01-03</td>\n",
" <td>935</td>\n",
" <td>99</td>\n",
" <td>NaN</td>\n",
" <td>21.36</td>\n",
" <td>203.86</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>buy</td>\n",
" <td>AMZN</td>\n",
" <td>NVDA</td>\n",
" <td>2018-01-03</td>\n",
" <td>336</td>\n",
" <td>4013</td>\n",
" <td>NaN</td>\n",
" <td>59.84</td>\n",
" <td>5.22</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>buy</td>\n",
" <td>NVDA</td>\n",
" <td>TSLA</td>\n",
" <td>2018-01-03</td>\n",
" <td>376</td>\n",
" <td>873</td>\n",
" <td>NaN</td>\n",
" <td>52.18</td>\n",
" <td>21.36</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
Expand All @@ -272,7 +272,7 @@
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>292</th>\n",
" <th>293</th>\n",
" <td>sell</td>\n",
" <td>NFLX</td>\n",
" <td>2022-12-02</td>\n",
Expand All @@ -282,66 +282,66 @@
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>293</th>\n",
" <th>294</th>\n",
" <td>sell</td>\n",
" <td>NVDA</td>\n",
" <td>2022-12-02</td>\n",
" <td>97</td>\n",
" <td>974</td>\n",
" <td>NaN</td>\n",
" <td>166.89</td>\n",
" <td>16.69</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>294</th>\n",
" <th>295</th>\n",
" <td>buy</td>\n",
" <td>AAPL</td>\n",
" <td>2022-12-02</td>\n",
" <td>27</td>\n",
" <td>28</td>\n",
" <td>NaN</td>\n",
" <td>146.82</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>295</th>\n",
" <th>296</th>\n",
" <td>buy</td>\n",
" <td>TSLA</td>\n",
" <td>AMZN</td>\n",
" <td>2022-12-02</td>\n",
" <td>70</td>\n",
" <td>42</td>\n",
" <td>NaN</td>\n",
" <td>193.68</td>\n",
" <td>94.57</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>296</th>\n",
" <th>297</th>\n",
" <td>buy</td>\n",
" <td>AMZN</td>\n",
" <td>TSLA</td>\n",
" <td>2022-12-02</td>\n",
" <td>41</td>\n",
" <td>70</td>\n",
" <td>NaN</td>\n",
" <td>94.57</td>\n",
" <td>193.68</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>296 rows × 7 columns</p>\n",
"<p>297 rows × 7 columns</p>\n",
"</div>"
],
"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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -512,41 +511,41 @@
" <tr>\n",
" <th>2</th>\n",
" <td>buy</td>\n",
" <td>TSLA</td>\n",
" <td>AMZN</td>\n",
" <td>2019-01-04</td>\n",
" <td>1168</td>\n",
" <td>347</td>\n",
" <td>NaN</td>\n",
" <td>20.69</td>\n",
" <td>77.81</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>buy</td>\n",
" <td>AMZN</td>\n",
" <td>TSLA</td>\n",
" <td>2019-01-04</td>\n",
" <td>307</td>\n",
" <td>1020</td>\n",
" <td>NaN</td>\n",
" <td>77.81</td>\n",
" <td>20.69</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>sell</td>\n",
" <td>AAPL</td>\n",
" <td>2019-02-04</td>\n",
" <td>105</td>\n",
" <td>103</td>\n",
" <td>NaN</td>\n",
" <td>42.37</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>buy</td>\n",
" <td>TSLA</td>\n",
" <td>AMZN</td>\n",
" <td>2019-02-04</td>\n",
" <td>51</td>\n",
" <td>1</td>\n",
" <td>NaN</td>\n",
" <td>20.57</td>\n",
" <td>81.58</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
Expand All @@ -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,
Expand Down

0 comments on commit 1ea9884

Please sign in to comment.