From 0d27ec7deace97c8dc46e307bcbac2a6a7f8f465 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Wed, 24 Jun 2026 00:25:59 -0700 Subject: [PATCH] More progression --- .../part1.ipynb | 662 ++++++++++++++++-- 1 file changed, 622 insertions(+), 40 deletions(-) diff --git a/tutorials/20260622164035_Let's Build a Quant Trading Strategy, MemLabs/part1.ipynb b/tutorials/20260622164035_Let's Build a Quant Trading Strategy, MemLabs/part1.ipynb index d19b11c..3409506 100644 --- a/tutorials/20260622164035_Let's Build a Quant Trading Strategy, MemLabs/part1.ipynb +++ b/tutorials/20260622164035_Let's Build a Quant Trading Strategy, MemLabs/part1.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 3, "id": "ee4d8884", "metadata": {}, "outputs": [ @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 4, "id": "390e1b3d", "metadata": {}, "outputs": [], @@ -58,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 5, "id": "3849b967", "metadata": {}, "outputs": [ @@ -68,7 +68,7 @@ "polars.config.Config" ] }, - "execution_count": 10, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -89,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "f60cc47e", "metadata": {}, "outputs": [ @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "a935f32c", "metadata": {}, "outputs": [ @@ -126,7 +126,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "Downloading BTCUSDT: 100%|██████████| 346/346 [00:11<00:00, 29.86it/s]\n" + "Downloading BTCUSDT: 100%|██████████| 346/346 [00:12<00:00, 27.60it/s]\n" ] } ], @@ -146,7 +146,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "Loading BTCUSDT: 100%|██████████| 346/346 [00:18<00:00, 18.69day/s]\n" + "Loading BTCUSDT: 100%|██████████| 346/346 [00:19<00:00, 18.01day/s]\n" ] }, { @@ -202,7 +202,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "Loading BTCUSDT: 100%|██████████| 346/346 [00:19<00:00, 17.69day/s]\n" + "Loading BTCUSDT: 100%|██████████| 346/346 [00:19<00:00, 17.43day/s]\n" ] }, { @@ -270,7 +270,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "id": "ee7b4018", "metadata": {}, "outputs": [ @@ -279,23 +279,23 @@ "text/html": [ "\n", "\n", - "
\n", + "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, - "execution_count": 13, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -370,7 +370,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "id": "fe1ac560", "metadata": {}, "outputs": [ @@ -379,23 +379,23 @@ "text/html": [ "\n", "\n", - "
\n", + "
\n", "" ], "text/plain": [ "alt.Chart(...)" ] }, - "execution_count": 14, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -462,7 +462,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "497659ef", "metadata": {}, "outputs": [ @@ -476,22 +476,22 @@ " white-space: pre-wrap;\n", "}\n", "\n", - "shape: (3, 5)
pricedeltareturnlog_returntest
f64f64f64f64f64
100.0nullnullnullnull
120.020.00.20.182322100.0
100.0-20.0-0.166667-0.182322120.0
" + "shape: (3, 4)
pricedeltareturnlog_return
f64f64f64f64
100.0nullnullnull
120.020.00.20.182322
100.0-20.0-0.166667-0.182322
" ], "text/plain": [ - "shape: (3, 5)\n", - "┌───────┬───────┬───────────┬────────────┬───────┐\n", - "│ price ┆ delta ┆ return ┆ log_return ┆ test │\n", - "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", - "│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", - "╞═══════╪═══════╪═══════════╪════════════╪═══════╡\n", - "│ 100.0 ┆ null ┆ null ┆ null ┆ null │\n", - "│ 120.0 ┆ 20.0 ┆ 0.2 ┆ 0.182322 ┆ 100.0 │\n", - "│ 100.0 ┆ -20.0 ┆ -0.166667 ┆ -0.182322 ┆ 120.0 │\n", - "└───────┴───────┴───────────┴────────────┴───────┘" + "shape: (3, 4)\n", + "┌───────┬───────┬───────────┬────────────┐\n", + "│ price ┆ delta ┆ return ┆ log_return │\n", + "│ --- ┆ --- ┆ --- ┆ --- │\n", + "│ f64 ┆ f64 ┆ f64 ┆ f64 │\n", + "╞═══════╪═══════╪═══════════╪════════════╡\n", + "│ 100.0 ┆ null ┆ null ┆ null │\n", + "│ 120.0 ┆ 20.0 ┆ 0.2 ┆ 0.182322 │\n", + "│ 100.0 ┆ -20.0 ┆ -0.166667 ┆ -0.182322 │\n", + "└───────┴───────┴───────────┴────────────┘" ] }, - "execution_count": 23, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -503,6 +503,588 @@ " (pl.col('price')/pl.col('price').shift()).log().alias('log_return'),\n", ")" ] + }, + { + "cell_type": "markdown", + "id": "1e74e180", + "metadata": {}, + "source": [ + "## Create target and lagged features" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "4e5bd81f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "shape: (8_304, 6)
datetimeopenhighlowcloseclose_log_return
datetime[μs]f64f64f64f64f64
2024-10-29 00:00:0069939.969998.469733.069939.5null
2024-10-29 01:00:0069939.570399.469810.870287.00.004956
2024-10-29 02:00:0070286.971607.070198.171173.90.012539
2024-10-29 03:00:0071173.871409.070870.270927.5-0.003468
2024-10-29 04:00:0070927.471123.470906.171062.60.001903
2025-10-09 19:00:00120600.0121060.1120600.0120897.60.002465
2025-10-09 20:00:00120897.6121147.2120787.9121045.40.001222
2025-10-09 21:00:00121045.3121457.2120998.3121407.80.002989
2025-10-09 22:00:00121407.9121712.4121342.0121619.50.001742
2025-10-09 23:00:00121619.6121671.1121271.3121579.3-0.000331
" + ], + "text/plain": [ + "shape: (8_304, 6)\n", + "┌─────────────────────┬──────────┬──────────┬──────────┬──────────┬──────────────────┐\n", + "│ datetime ┆ open ┆ high ┆ low ┆ close ┆ close_log_return │\n", + "│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", + "│ datetime[μs] ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", + "╞═════════════════════╪══════════╪══════════╪══════════╪══════════╪══════════════════╡\n", + "│ 2024-10-29 00:00:00 ┆ 69939.9 ┆ 69998.4 ┆ 69733.0 ┆ 69939.5 ┆ null │\n", + "│ 2024-10-29 01:00:00 ┆ 69939.5 ┆ 70399.4 ┆ 69810.8 ┆ 70287.0 ┆ 0.004956 │\n", + "│ 2024-10-29 02:00:00 ┆ 70286.9 ┆ 71607.0 ┆ 70198.1 ┆ 71173.9 ┆ 0.012539 │\n", + "│ 2024-10-29 03:00:00 ┆ 71173.8 ┆ 71409.0 ┆ 70870.2 ┆ 70927.5 ┆ -0.003468 │\n", + "│ 2024-10-29 04:00:00 ┆ 70927.4 ┆ 71123.4 ┆ 70906.1 ┆ 71062.6 ┆ 0.001903 │\n", + "│ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n", + "│ 2025-10-09 19:00:00 ┆ 120600.0 ┆ 121060.1 ┆ 120600.0 ┆ 120897.6 ┆ 0.002465 │\n", + "│ 2025-10-09 20:00:00 ┆ 120897.6 ┆ 121147.2 ┆ 120787.9 ┆ 121045.4 ┆ 0.001222 │\n", + "│ 2025-10-09 21:00:00 ┆ 121045.3 ┆ 121457.2 ┆ 120998.3 ┆ 121407.8 ┆ 0.002989 │\n", + "│ 2025-10-09 22:00:00 ┆ 121407.9 ┆ 121712.4 ┆ 121342.0 ┆ 121619.5 ┆ 0.001742 │\n", + "│ 2025-10-09 23:00:00 ┆ 121619.6 ┆ 121671.1 ┆ 121271.3 ┆ 121579.3 ┆ -0.000331 │\n", + "└─────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────────────┘" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "target = \"close_log_return\"\n", + "ts = ts.with_columns((pl.col('close')/pl.col('close').shift(forecast_horizon)).log().alias(target))\n", + "ts" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c6f5b2e3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "shape: (8_299, 10)
datetimeopenhighlowcloseclose_log_returnclose_log_return_lag_1close_log_return_lag_2close_log_return_lag_3close_log_return_lag_4
datetime[μs]f64f64f64f64f64f64f64f64f64
2024-10-29 05:00:0071062.571088.770880.371010.9-0.0007280.001903-0.0034680.0125390.004956
2024-10-29 06:00:0071010.871343.970983.171226.60.003033-0.0007280.001903-0.0034680.012539
2024-10-29 07:00:0071226.671250.070803.071004.2-0.0031270.003033-0.0007280.001903-0.003468
2024-10-29 08:00:0071004.171208.770960.071122.10.001659-0.0031270.003033-0.0007280.001903
2024-10-29 09:00:0071122.171332.871100.771280.00.0022180.001659-0.0031270.003033-0.000728
2025-10-09 19:00:00120600.0121060.1120600.0120897.60.002465-0.0026130.009669-0.011023-0.001285
2025-10-09 20:00:00120897.6121147.2120787.9121045.40.0012220.002465-0.0026130.009669-0.011023
2025-10-09 21:00:00121045.3121457.2120998.3121407.80.0029890.0012220.002465-0.0026130.009669
2025-10-09 22:00:00121407.9121712.4121342.0121619.50.0017420.0029890.0012220.002465-0.002613
2025-10-09 23:00:00121619.6121671.1121271.3121579.3-0.0003310.0017420.0029890.0012220.002465
" + ], + "text/plain": [ + "shape: (8_299, 10)\n", + "┌─────────────────────┬──────────┬──────────┬──────────┬──────────┬──────────────────┬────────────────────────┬────────────────────────┬────────────────────────┬────────────────────────┐\n", + "│ datetime ┆ open ┆ high ┆ low ┆ close ┆ close_log_return ┆ close_log_return_lag_1 ┆ close_log_return_lag_2 ┆ close_log_return_lag_3 ┆ close_log_return_lag_4 │\n", + "│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", + "│ datetime[μs] ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", + "╞═════════════════════╪══════════╪══════════╪══════════╪══════════╪══════════════════╪════════════════════════╪════════════════════════╪════════════════════════╪════════════════════════╡\n", + "│ 2024-10-29 05:00:00 ┆ 71062.5 ┆ 71088.7 ┆ 70880.3 ┆ 71010.9 ┆ -0.000728 ┆ 0.001903 ┆ -0.003468 ┆ 0.012539 ┆ 0.004956 │\n", + "│ 2024-10-29 06:00:00 ┆ 71010.8 ┆ 71343.9 ┆ 70983.1 ┆ 71226.6 ┆ 0.003033 ┆ -0.000728 ┆ 0.001903 ┆ -0.003468 ┆ 0.012539 │\n", + "│ 2024-10-29 07:00:00 ┆ 71226.6 ┆ 71250.0 ┆ 70803.0 ┆ 71004.2 ┆ -0.003127 ┆ 0.003033 ┆ -0.000728 ┆ 0.001903 ┆ -0.003468 │\n", + "│ 2024-10-29 08:00:00 ┆ 71004.1 ┆ 71208.7 ┆ 70960.0 ┆ 71122.1 ┆ 0.001659 ┆ -0.003127 ┆ 0.003033 ┆ -0.000728 ┆ 0.001903 │\n", + "│ 2024-10-29 09:00:00 ┆ 71122.1 ┆ 71332.8 ┆ 71100.7 ┆ 71280.0 ┆ 0.002218 ┆ 0.001659 ┆ -0.003127 ┆ 0.003033 ┆ -0.000728 │\n", + "│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n", + "│ 2025-10-09 19:00:00 ┆ 120600.0 ┆ 121060.1 ┆ 120600.0 ┆ 120897.6 ┆ 0.002465 ┆ -0.002613 ┆ 0.009669 ┆ -0.011023 ┆ -0.001285 │\n", + "│ 2025-10-09 20:00:00 ┆ 120897.6 ┆ 121147.2 ┆ 120787.9 ┆ 121045.4 ┆ 0.001222 ┆ 0.002465 ┆ -0.002613 ┆ 0.009669 ┆ -0.011023 │\n", + "│ 2025-10-09 21:00:00 ┆ 121045.3 ┆ 121457.2 ┆ 120998.3 ┆ 121407.8 ┆ 0.002989 ┆ 0.001222 ┆ 0.002465 ┆ -0.002613 ┆ 0.009669 │\n", + "│ 2025-10-09 22:00:00 ┆ 121407.9 ┆ 121712.4 ┆ 121342.0 ┆ 121619.5 ┆ 0.001742 ┆ 0.002989 ┆ 0.001222 ┆ 0.002465 ┆ -0.002613 │\n", + "│ 2025-10-09 23:00:00 ┆ 121619.6 ┆ 121671.1 ┆ 121271.3 ┆ 121579.3 ┆ -0.000331 ┆ 0.001742 ┆ 0.002989 ┆ 0.001222 ┆ 0.002465 │\n", + "└─────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────────────┴────────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┘" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ts = research.add_lags(ts, target, max_lags, forecast_horizon).drop_nulls()\n", + "ts" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "ff5f2dd0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Plotting price movements\n", + "research.plot_distribution(ts, target, no_bins=200)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "b32adf51", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "
\n", + "" + ], + "text/plain": [ + "alt.Chart(...)" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Plotting close price\n", + "research.plot_distribution(ts, \"close\", no_bins=200)" + ] + }, + { + "cell_type": "markdown", + "id": "486b9fa3", + "metadata": {}, + "source": [ + "## Build Model" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "5d2b9580", + "metadata": {}, + "outputs": [], + "source": [ + "class LinearModel(nn.Module):\n", + " def __init__(self, input_features):\n", + " super(LinearModel, self).__init__()\n", + " self.linear = nn.Linear(input_features, 1)\n", + "\n", + " def forward(self, x):\n", + " return self.linear(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "59fffc10", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "============================================================\n", + "Linear Model\n", + "============================================================\n", + "\n", + "Architecture:\n", + " LinearModel(\n", + " (linear): Linear(in_features=1, out_features=1, bias=True)\n", + ")\n", + "\n", + "Parameter Count:\n", + " Total parameters: 2\n", + " Trainable parameters: 2\n", + "============================================================\n", + "\n" + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "input_features = 1\n", + "\n", + "linear_model = LinearModel(input_features)\n", + "\n", + "research.print_model_info(linear_model, \"Linear Model\")\n", + "research.total_model_params(linear_model)" + ] + }, + { + "cell_type": "markdown", + "id": "70299f84", + "metadata": {}, + "source": [ + "Linear model, ie model is just $y = w \\cdot x + b$. Where $w$ is a weight, $b$ is our bias" + ] + }, + { + "cell_type": "markdown", + "id": "1c82fb71", + "metadata": {}, + "source": [ + "## Split our data by time\n", + "\n", + "We should use most recent data for testing, and the oldest data for training. We do this to avoid data snooping bias and data leakage." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "f8317ae6", + "metadata": {}, + "outputs": [], + "source": [ + "features = [\"close_log_return_lag_1\"]\n", + "target = \"close_log_return\"\n", + "test_size = 0.25" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "c50bddb5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8299" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(ts)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "cfc966d3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6224" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "split_index = int(len(ts) * (1 - test_size))\n", + "split_index" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "285aaaf0", + "metadata": {}, + "outputs": [], + "source": [ + "ts_training = ts[:split_index]\n", + "ts_testing = ts[split_index:]" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "4baefd68", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "shape: (6_224, 10)
datetimeopenhighlowcloseclose_log_returnclose_log_return_lag_1close_log_return_lag_2close_log_return_lag_3close_log_return_lag_4
datetime[μs]f64f64f64f64f64f64f64f64f64
2024-10-29 05:00:0071062.571088.770880.371010.9-0.0007280.001903-0.0034680.0125390.004956
2024-10-29 06:00:0071010.871343.970983.171226.60.003033-0.0007280.001903-0.0034680.012539
2024-10-29 07:00:0071226.671250.070803.071004.2-0.0031270.003033-0.0007280.001903-0.003468
2024-10-29 08:00:0071004.171208.770960.071122.10.001659-0.0031270.003033-0.0007280.001903
2024-10-29 09:00:0071122.171332.871100.771280.00.0022180.001659-0.0031270.003033-0.000728
2025-07-15 08:00:00116777.1116871.0116300.0116766.5-0.000091-0.002343-0.002341-0.0013330.002323
2025-07-15 09:00:00116766.6117229.9116684.3116764.9-0.000014-0.000091-0.002343-0.002341-0.001333
2025-07-15 10:00:00116765.0117222.2116728.1117055.00.002481-0.000014-0.000091-0.002343-0.002341
2025-07-15 11:00:00117055.0117180.0116331.7117110.40.0004730.002481-0.000014-0.000091-0.002343
2025-07-15 12:00:00117110.4117458.6116388.0117193.10.0007060.0004730.002481-0.000014-0.000091
" + ], + "text/plain": [ + "shape: (6_224, 10)\n", + "┌─────────────────────┬──────────┬──────────┬──────────┬──────────┬──────────────────┬────────────────────────┬────────────────────────┬────────────────────────┬────────────────────────┐\n", + "│ datetime ┆ open ┆ high ┆ low ┆ close ┆ close_log_return ┆ close_log_return_lag_1 ┆ close_log_return_lag_2 ┆ close_log_return_lag_3 ┆ close_log_return_lag_4 │\n", + "│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", + "│ datetime[μs] ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", + "╞═════════════════════╪══════════╪══════════╪══════════╪══════════╪══════════════════╪════════════════════════╪════════════════════════╪════════════════════════╪════════════════════════╡\n", + "│ 2024-10-29 05:00:00 ┆ 71062.5 ┆ 71088.7 ┆ 70880.3 ┆ 71010.9 ┆ -0.000728 ┆ 0.001903 ┆ -0.003468 ┆ 0.012539 ┆ 0.004956 │\n", + "│ 2024-10-29 06:00:00 ┆ 71010.8 ┆ 71343.9 ┆ 70983.1 ┆ 71226.6 ┆ 0.003033 ┆ -0.000728 ┆ 0.001903 ┆ -0.003468 ┆ 0.012539 │\n", + "│ 2024-10-29 07:00:00 ┆ 71226.6 ┆ 71250.0 ┆ 70803.0 ┆ 71004.2 ┆ -0.003127 ┆ 0.003033 ┆ -0.000728 ┆ 0.001903 ┆ -0.003468 │\n", + "│ 2024-10-29 08:00:00 ┆ 71004.1 ┆ 71208.7 ┆ 70960.0 ┆ 71122.1 ┆ 0.001659 ┆ -0.003127 ┆ 0.003033 ┆ -0.000728 ┆ 0.001903 │\n", + "│ 2024-10-29 09:00:00 ┆ 71122.1 ┆ 71332.8 ┆ 71100.7 ┆ 71280.0 ┆ 0.002218 ┆ 0.001659 ┆ -0.003127 ┆ 0.003033 ┆ -0.000728 │\n", + "│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n", + "│ 2025-07-15 08:00:00 ┆ 116777.1 ┆ 116871.0 ┆ 116300.0 ┆ 116766.5 ┆ -0.000091 ┆ -0.002343 ┆ -0.002341 ┆ -0.001333 ┆ 0.002323 │\n", + "│ 2025-07-15 09:00:00 ┆ 116766.6 ┆ 117229.9 ┆ 116684.3 ┆ 116764.9 ┆ -0.000014 ┆ -0.000091 ┆ -0.002343 ┆ -0.002341 ┆ -0.001333 │\n", + "│ 2025-07-15 10:00:00 ┆ 116765.0 ┆ 117222.2 ┆ 116728.1 ┆ 117055.0 ┆ 0.002481 ┆ -0.000014 ┆ -0.000091 ┆ -0.002343 ┆ -0.002341 │\n", + "│ 2025-07-15 11:00:00 ┆ 117055.0 ┆ 117180.0 ┆ 116331.7 ┆ 117110.4 ┆ 0.000473 ┆ 0.002481 ┆ -0.000014 ┆ -0.000091 ┆ -0.002343 │\n", + "│ 2025-07-15 12:00:00 ┆ 117110.4 ┆ 117458.6 ┆ 116388.0 ┆ 117193.1 ┆ 0.000706 ┆ 0.000473 ┆ 0.002481 ┆ -0.000014 ┆ -0.000091 │\n", + "└─────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────────────┴────────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┘" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ts_training" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "af26db01", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "shape: (2_075, 10)
datetimeopenhighlowcloseclose_log_returnclose_log_return_lag_1close_log_return_lag_2close_log_return_lag_3close_log_return_lag_4
datetime[μs]f64f64f64f64f64f64f64f64f64
2025-07-15 13:00:00117193.0118499.8117132.6118067.70.0074350.0007060.0004730.002481-0.000014
2025-07-15 14:00:00118067.8118439.2115678.1115979.6-0.0178440.0074350.0007060.0004730.002481
2025-07-15 15:00:00115979.6116752.9115750.1116393.00.003558-0.0178440.0074350.0007060.000473
2025-07-15 16:00:00116393.1117727.4116360.9117195.90.0068740.003558-0.0178440.0074350.000706
2025-07-15 17:00:00117195.9117577.1117125.0117415.60.0018730.0068740.003558-0.0178440.007435
2025-10-09 19:00:00120600.0121060.1120600.0120897.60.002465-0.0026130.009669-0.011023-0.001285
2025-10-09 20:00:00120897.6121147.2120787.9121045.40.0012220.002465-0.0026130.009669-0.011023
2025-10-09 21:00:00121045.3121457.2120998.3121407.80.0029890.0012220.002465-0.0026130.009669
2025-10-09 22:00:00121407.9121712.4121342.0121619.50.0017420.0029890.0012220.002465-0.002613
2025-10-09 23:00:00121619.6121671.1121271.3121579.3-0.0003310.0017420.0029890.0012220.002465
" + ], + "text/plain": [ + "shape: (2_075, 10)\n", + "┌─────────────────────┬──────────┬──────────┬──────────┬──────────┬──────────────────┬────────────────────────┬────────────────────────┬────────────────────────┬────────────────────────┐\n", + "│ datetime ┆ open ┆ high ┆ low ┆ close ┆ close_log_return ┆ close_log_return_lag_1 ┆ close_log_return_lag_2 ┆ close_log_return_lag_3 ┆ close_log_return_lag_4 │\n", + "│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", + "│ datetime[μs] ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │\n", + "╞═════════════════════╪══════════╪══════════╪══════════╪══════════╪══════════════════╪════════════════════════╪════════════════════════╪════════════════════════╪════════════════════════╡\n", + "│ 2025-07-15 13:00:00 ┆ 117193.0 ┆ 118499.8 ┆ 117132.6 ┆ 118067.7 ┆ 0.007435 ┆ 0.000706 ┆ 0.000473 ┆ 0.002481 ┆ -0.000014 │\n", + "│ 2025-07-15 14:00:00 ┆ 118067.8 ┆ 118439.2 ┆ 115678.1 ┆ 115979.6 ┆ -0.017844 ┆ 0.007435 ┆ 0.000706 ┆ 0.000473 ┆ 0.002481 │\n", + "│ 2025-07-15 15:00:00 ┆ 115979.6 ┆ 116752.9 ┆ 115750.1 ┆ 116393.0 ┆ 0.003558 ┆ -0.017844 ┆ 0.007435 ┆ 0.000706 ┆ 0.000473 │\n", + "│ 2025-07-15 16:00:00 ┆ 116393.1 ┆ 117727.4 ┆ 116360.9 ┆ 117195.9 ┆ 0.006874 ┆ 0.003558 ┆ -0.017844 ┆ 0.007435 ┆ 0.000706 │\n", + "│ 2025-07-15 17:00:00 ┆ 117195.9 ┆ 117577.1 ┆ 117125.0 ┆ 117415.6 ┆ 0.001873 ┆ 0.006874 ┆ 0.003558 ┆ -0.017844 ┆ 0.007435 │\n", + "│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n", + "│ 2025-10-09 19:00:00 ┆ 120600.0 ┆ 121060.1 ┆ 120600.0 ┆ 120897.6 ┆ 0.002465 ┆ -0.002613 ┆ 0.009669 ┆ -0.011023 ┆ -0.001285 │\n", + "│ 2025-10-09 20:00:00 ┆ 120897.6 ┆ 121147.2 ┆ 120787.9 ┆ 121045.4 ┆ 0.001222 ┆ 0.002465 ┆ -0.002613 ┆ 0.009669 ┆ -0.011023 │\n", + "│ 2025-10-09 21:00:00 ┆ 121045.3 ┆ 121457.2 ┆ 120998.3 ┆ 121407.8 ┆ 0.002989 ┆ 0.001222 ┆ 0.002465 ┆ -0.002613 ┆ 0.009669 │\n", + "│ 2025-10-09 22:00:00 ┆ 121407.9 ┆ 121712.4 ┆ 121342.0 ┆ 121619.5 ┆ 0.001742 ┆ 0.002989 ┆ 0.001222 ┆ 0.002465 ┆ -0.002613 │\n", + "│ 2025-10-09 23:00:00 ┆ 121619.6 ┆ 121671.1 ┆ 121271.3 ┆ 121579.3 ┆ -0.000331 ┆ 0.001742 ┆ 0.002989 ┆ 0.001222 ┆ 0.002465 │\n", + "└─────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────────────┴────────────────────────┴────────────────────────┴────────────────────────┴────────────────────────┘" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ts_testing" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "ef986897", + "metadata": {}, + "outputs": [], + "source": [ + "X_train = torch.tensor(ts_training[features].to_numpy(), dtype=torch.float32)\n", + "X_test = ts_testing[features].to_torch().float()\n", + "y_train = torch.tensor(ts_training[target].to_numpy(), dtype=torch.float32)\n", + "y_test = torch.tensor(ts_testing[target].to_numpy(), dtype=torch.float32)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "4e29831b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tensor([[ 1.9030e-03],\n", + " [-7.2779e-04],\n", + " [ 3.0330e-03],\n", + " ...,\n", + " [-1.3703e-05],\n", + " [ 2.4814e-03],\n", + " [ 4.7317e-04]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X_train" + ] } ], "metadata": {