update the notebooks

This commit is contained in:
Filip Stefaniuk 2024-11-02 14:53:01 +01:00
parent eef02cbbe2
commit fb731be598
5 changed files with 660 additions and 22284 deletions

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 107,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@ -34,7 +34,7 @@
")\n",
"\n",
"PADDING=5000\n",
"VALID_PART=0.4\n",
"VALID_PART=0.2\n",
"INTERVAL='30min'\n",
"METRIC='mod_ir'\n",
"TOP_N=10"
@ -42,16 +42,16 @@
},
{
"cell_type": "code",
"execution_count": 101,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-30m:latest, 124.19MB. 12 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 12 of 12 files downloaded. \n",
"Done. 0:0:0.6\n"
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-30m-long:latest, 74.51MB. 6 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 6 of 6 files downloaded. \n",
"Done. 0:0:0.4\n"
]
}
],
@ -66,7 +66,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@ -80,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
@ -135,19 +135,16 @@
},
{
"cell_type": "code",
"execution_count": 108,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 11088/11088 [00:18<00:00, 611.54it/s]\n",
"100%|██████████| 11088/11088 [00:17<00:00, 644.30it/s]\n",
"100%|██████████| 11088/11088 [00:16<00:00, 653.15it/s]\n",
"100%|██████████| 11088/11088 [00:17<00:00, 640.80it/s]\n",
"100%|██████████| 11088/11088 [00:14<00:00, 762.97it/s]\n",
"100%|██████████| 11088/11088 [00:15<00:00, 737.06it/s]\n"
"100%|██████████| 11088/11088 [00:13<00:00, 814.39it/s]\n",
"100%|██████████| 11088/11088 [00:13<00:00, 802.08it/s]\n",
"100%|██████████| 11088/11088 [00:14<00:00, 771.08it/s]\n"
]
}
],
@ -404,20 +401,20 @@
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"# Persist best strategies, so that they don't have to be recomputed every time\n",
"best_strategies = {\n",
" # 'buy_and_hold': buyandhold_best_strategies,\n",
" # 'macd_strategies': macd_best_strategies,\n",
" 'buy_and_hold': buyandhold_best_strategies,\n",
" 'macd_strategies': macd_best_strategies,\n",
" 'rsi_strategies': rsi_best_strategies,\n",
" # 'quantile_model': quantile_model_best_strategies,\n",
" # 'gmadl_model': gmadl_model_best_strategies\n",
" 'quantile_model': quantile_model_best_strategies,\n",
" 'gmadl_model': gmadl_model_best_strategies\n",
"}\n",
"\n",
"with open('cache/30min-best-strategies-04.pkl', 'wb') as outp:\n",
"with open('cache/30min-best-strategies-long.pkl', 'wb') as outp:\n",
" pickle.dump(best_strategies, outp, pickle.HIGHEST_PROTOCOL)"
]
},
@ -430,7 +427,7 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@ -659,7 +656,7 @@
},
{
"cell_type": "code",
"execution_count": 98,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@ -745,14 +742,14 @@
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" f\"{result['arc']*100:.1f}\\%\",\n",
" f\"{result['asd']*100:.1f}\\%\",\n",
" result['ir'],\n",
" result['md'],\n",
" f\"{result['md']*100:.1f}\\%\",\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" f\"{result['long_pos']*100:.1f}\\%\",\n",
" f\"{result['short_pos']*100:.1f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))\n",
"\n"
@ -760,7 +757,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 109,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@ -33,7 +33,7 @@
")\n",
"\n",
"PADDING=5000\n",
"VALID_PART=0.4\n",
"VALID_PART=0.2\n",
"INTERVAL='5min'\n",
"METRIC='mod_ir'\n",
"TOP_N=10"
@ -41,16 +41,16 @@
},
{
"cell_type": "code",
"execution_count": 94,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-5m:latest, 745.12MB. 12 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 12 of 12 files downloaded. \n",
"Done. 0:0:0.8\n"
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-5m-short:latest, 1341.36MB. 24 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 24 of 24 files downloaded. \n",
"Done. 0:0:1.1\n"
]
}
],
@ -79,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": 96,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@ -279,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 104,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@ -303,6 +303,12 @@
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 2 of 2 files downloaded. \n"
]
}
@ -310,26 +316,33 @@
"source": [
"# Model with gmadl loss\n",
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/0pro3i5i'\n",
"train_gmadl_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'train')\n",
"# SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/v3epl3qk'\n",
"# train_gmadl_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'train')\n",
"valid_gmadl_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'valid')\n",
"test_gmadl_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'test')"
]
},
{
"cell_type": "code",
"execution_count": 110,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1176/1176 [02:30<00:00, 7.82it/s]\n",
"100%|██████████| 1176/1176 [02:31<00:00, 7.78it/s]\n",
"100%|██████████| 1176/1176 [02:31<00:00, 7.77it/s]\n",
"100%|██████████| 1176/1176 [02:32<00:00, 7.72it/s]\n",
"100%|██████████| 1176/1176 [02:30<00:00, 7.84it/s]\n",
"100%|██████████| 1176/1176 [02:30<00:00, 7.80it/s]\n"
"100%|██████████| 1176/1176 [00:41<00:00, 28.28it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.90it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.84it/s]\n",
"100%|██████████| 1176/1176 [02:09<00:00, 9.10it/s]\n",
"100%|██████████| 1176/1176 [01:07<00:00, 17.48it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.56it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.60it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.51it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.39it/s]\n",
"100%|██████████| 1176/1176 [00:42<00:00, 27.40it/s]\n",
"100%|██████████| 1176/1176 [00:43<00:00, 27.34it/s]\n",
"100%|██████████| 1176/1176 [00:43<00:00, 27.33it/s]\n"
]
}
],
@ -341,10 +354,10 @@
" and (p['enter_long'] is None or p['exit_short'] is None or (p['exit_short'] < p['enter_long'])))\n",
"\n",
"gmadl_model_sweep_results = []\n",
"for (in_sample, _), train_preds, valid_preds, test_preds in zip(data_windows, train_gmadl_pred_windows, valid_gmadl_pred_windows, test_gmadl_pred_windows):\n",
"for (in_sample, _), valid_preds, test_preds in zip(data_windows, valid_gmadl_pred_windows, test_gmadl_pred_windows):\n",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" params={\n",
" 'predictions': [get_predictions_dataframe(train_preds, valid_preds, test_preds)],\n",
" 'predictions': [get_predictions_dataframe(valid_preds, test_preds)],\n",
" 'enter_long': [None, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007],\n",
" 'exit_long': [None, -0.001, -0.002, -0.003, -0.004, -0.005, -0.006, -0.007],\n",
" 'enter_short': [None, -0.001, -0.002, -0.003, -0.004, -0.005, -0.006, -0.007],\n",
@ -366,20 +379,20 @@
},
{
"cell_type": "code",
"execution_count": 111,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Persist best strategies, so that they don't have to be recomputed every time\n",
"best_strategies = {\n",
" 'buy_and_hold': buyandhold_best_strategies,\n",
" 'macd_strategies': macd_best_strategies,\n",
" 'rsi_strategies': rsi_best_strategies,\n",
" 'quantile_model': quantile_model_best_strategies,\n",
" # 'macd_strategies': macd_best_strategies,\n",
" # 'rsi_strategies': rsi_best_strategies,\n",
" # 'quantile_model': quantile_model_best_strategies,\n",
" 'gmadl_model': gmadl_model_best_strategies\n",
"}\n",
"\n",
"with open('cache/5min-best-strategies-04.pkl', 'wb') as outp:\n",
"with open('cache/5min-best-strategies-long.pkl', 'wb') as outp:\n",
" pickle.dump(best_strategies, outp, pickle.HIGHEST_PROTOCOL)"
]
},
@ -621,7 +634,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@ -734,8 +747,8 @@
"# result_quantile_model = evaluate_strategy(padded_window, [s[0] for s in best_strategies['quantile_model']][i], padding=PADDING, interval=INTERVAL)\n",
"# result_gmadl_model = evaluate_strategy(padded_window, [s[0] for s in best_strategies['gmadl_model']][i], padding=PADDING, interval=INTERVAL)\n",
"\n",
"# results_table(result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model)\n",
"# results_plot(i+1, result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model)\n",
" # results_table(result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model)\n",
" # results_plot(i+1, result_buyandhold, None, None, None, result_gmadl_model)\n",
" \n"
]
},
@ -753,7 +766,7 @@
"# gmadl_model_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['gmadl_model']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"\n",
"# results_table(buy_and_hold_concat, macd_concat, rsi_concat, quantile_model_concat, gmadl_model_concat)\n",
"# results_plot(0, buy_and_hold_concat, macd_concat, rsi_concat, quantile_model_concat, gmadl_model_concat, width=1200, notitle=True)\n",
"# results_plot(0, buy_and_hold_concat, None, None, None, gmadl_model_concat, width=1200, notitle=True)\n",
"\n"
]
},

File diff suppressed because it is too large Load Diff

View File

@ -99687,7 +99687,7 @@
"yref": "paper"
}
],
"height": 400,
"height": 450,
"margin": {
"b": 20,
"l": 20,
@ -100510,13 +100510,16 @@
}
}
},
"width": 800,
"width": 1000,
"xaxis": {
"anchor": "y",
"domain": [
0,
0.45
]
],
"title": {
"text": "y_predicted"
}
},
"xaxis2": {
"anchor": "y2",
@ -100525,7 +100528,7 @@
1
],
"title": {
"text": "Label x-axis 1"
"text": "y_predicted"
}
},
"yaxis": {
@ -100533,14 +100536,20 @@
"domain": [
0,
1
]
],
"title": {
"text": "y_actual"
}
},
"yaxis2": {
"anchor": "x2",
"domain": [
0,
1
]
],
"title": {
"text": "y_actual"
}
}
}
}

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@ -39,22 +39,23 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Failed to detect the name of this notebook, you can set it manually with the WANDB_NOTEBOOK_NAME environment variable to enable code saving.\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-5m:latest, 745.12MB. 12 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 12 of 12 files downloaded. \n",
"Done. 0:0:0.9\n",
"Done. 0:0:1.1\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-15m:latest, 248.65MB. 12 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 12 of 12 files downloaded. \n",
"Done. 0:0:0.7\n",
"Done. 0:0:0.6\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-30m:latest, 124.19MB. 12 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 12 of 12 files downloaded. \n",
"Done. 0:0:0.6\n"
"Done. 0:0:0.4\n"
]
}
],
@ -83,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@ -106,7 +107,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@ -116,7 +117,7 @@
" go.Scatter(y=buy_and_hold_concat['portfolio_value'], x=buy_and_hold_concat['time'], name=\"Buy and Hold\"),\n",
" go.Scatter(y=gmadl_5min_model_concat['portfolio_value'], x=gmadl_5min_model_concat['time'], name=\"GMADL Informer Strategy (5min)\"),\n",
" go.Scatter(y=rsi_5_min_concat['portfolio_value'], x=rsi_5_min_concat['time'], name=\"RSI Strategy (5min)\"),\n",
" go.Scatter(y=gmadl_15min_model_concat['portfolio_value'], x=gmadl_15min_model_concat['time'], name='GNADL Informer Strategy (15min)'),\n",
" go.Scatter(y=gmadl_15min_model_concat['portfolio_value'], x=gmadl_15min_model_concat['time'], name='GMADL Informer Strategy (15min)'),\n",
" go.Scatter(y=rsi_30min_concat['portfolio_value'], x=rsi_30min_concat['time'], name='RSI Strategy (30min)'),\n",
" go.Scatter(y=macd_30min_concat['portfolio_value'], x=macd_30min_concat['time'], name='MACD Strategy (30min)')\n",
" ])\n",
@ -208,7 +209,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@ -227,6 +228,79 @@
"# results_plot(buy_and_hold_concat, gmadl_5min_model_concat, gmadl_15min_model_concat, rsi_5_min_concat, rsi_30min_concat, macd_30min_concat, width=1200, notitle=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Statistical significance"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"test_data_5min = pd.concat([data_windows_5min[0][0][-PADDING:]] + [data_window[1] for data_window in data_windows_5min])\n",
"test_data_15min = pd.concat([data_windows_15min[0][0][-PADDING:]] + [data_window[1] for data_window in data_windows_15min])\n",
"test_data_30min = pd.concat([data_windows_30min[0][0][-PADDING:]] + [data_window[1] for data_window in data_windows_30min])\n",
"\n",
"buy_and_hold_5min = evaluate_strategy(test_data_5min, BuyAndHoldStrategy(), padding=PADDING, interval='5min')\n",
"buy_and_hold_15min = evaluate_strategy(test_data_15min, BuyAndHoldStrategy(), padding=PADDING, interval='15min')\n",
"buy_and_hold_30min = evaluate_strategy(test_data_30min, BuyAndHoldStrategy(), padding=PADDING, interval='30min')\n",
"gmadl_5min_model_concat = evaluate_strategy(test_data_5min, ConcatenatedStrategies(len(data_windows_5min[0][1]), [s[0] for s in best_strategies_5min['gmadl_model']], padding=PADDING), padding=PADDING, interval='5min')\n",
"gmadl_15min_model_concat = evaluate_strategy(test_data_15min, ConcatenatedStrategies(len(data_windows_15min[0][1]), [s[0] for s in best_strategies_15min['gmadl_model']], padding=PADDING), padding=PADDING, interval='15min')\n",
"rsi_5_min_concat = evaluate_strategy(test_data_5min, ConcatenatedStrategies(len(data_windows_5min[0][1]), [s[0] for s in best_strategies_5min['rsi_strategies']], padding=PADDING), padding=PADDING, interval='5min')\n",
"rsi_30min_concat = evaluate_strategy(test_data_30min, ConcatenatedStrategies(len(data_windows_30min[0][1]), [s[0] for s in best_strategies_30min['rsi_strategies']], padding=PADDING), padding=PADDING, interval='30min')\n",
"macd_30min_concat = evaluate_strategy(test_data_30min, ConcatenatedStrategies(len(data_windows_30min[0][1]), [s[0] for s in best_strategies_30min['macd_strategies']], padding=PADDING), padding=PADDING, interval='30min')"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GMADL (5min) & 311040 & 2.820834 & 375.84 & 0.000000*** \\\\\n",
"GMADL (15 min) & 103680 & 0.558743 & 408.13 & 0.000000*** \\\\\n",
"RSI (5 min) & 311040 & 0.648741 & 662.77 & 0.000000*** \\\\\n",
"RSI (30 min) & 51840 & 1.065079 & 258.49 & 0.000000*** \\\\\n",
"MACD (30 min) & 51840 & 0.326504 & 174.34 & 0.000000*** \\\\\n"
]
}
],
"source": [
"import numpy as np\n",
"import scipy.stats\n",
"\n",
"def ttest(benchmark, strategy):\n",
" sigma = np.std(strategy['portfolio_value'] - benchmark['portfolio_value'])\n",
" N = len(strategy['strategy_returns'])\n",
" tt = (strategy['ir'] - benchmark['ir']) / (sigma / np.sqrt(N))\n",
"\n",
" pval = scipy.stats.t.sf(np.abs(tt), N-1)*2\n",
"\n",
" return tt, pval, sigma, N\n",
"\n",
"gmadl_5min_ttest = ttest(buy_and_hold_5min, gmadl_5min_model_concat)\n",
"gmadl_15min_ttest = ttest(buy_and_hold_15min, gmadl_15min_model_concat)\n",
"rsi_5min_ttest = ttest(buy_and_hold_5min, rsi_5_min_concat)\n",
"rsi_30min_ttest = ttest(buy_and_hold_30min, rsi_30min_concat)\n",
"macd_30min_ttest = ttest(buy_and_hold_30min, macd_30min_concat)\n",
"\n",
"for name, result in [\n",
" (\"GMADL (5min)\", gmadl_5min_ttest),\n",
" (\"GMADL (15 min)\", gmadl_15min_ttest),\n",
" (\"RSI (5 min)\", rsi_5min_ttest),\n",
" (\"RSI (30 min)\", rsi_30min_ttest),\n",
" (\"MACD (30 min)\", macd_30min_ttest)\n",
"]:\n",
" print(f\"{name} & {result[3]} & {result[2]:.6f} & {result[0]:.2f} & {result[1]:.6f}*** \\\\\\\\\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -236,48 +310,48 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"def results_for_strats(data_windows, best_strategies, interval='5min', top_n=10):\n",
" test_data = pd.concat([data_windows[0][0][-PADDING:]] + [data_window[1] for data_window in data_windows])\n",
"# def results_for_strats(data_windows, best_strategies, interval='5min', top_n=10):\n",
"# test_data = pd.concat([data_windows[0][0][-PADDING:]] + [data_window[1] for data_window in data_windows])\n",
"\n",
" buy_and_hold_concat = evaluate_strategy(test_data, BuyAndHoldStrategy(), padding=PADDING)\n",
" \n",
" macd_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['macd_strategies']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
" rsi_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['rsi_strategies']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
" quantile_model_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['quantile_model']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
" gmadl_model_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['gmadl_model']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
"# buy_and_hold_concat = evaluate_strategy(test_data, BuyAndHoldStrategy(), padding=PADDING, interval=interval)\n",
"# macd_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['macd_strategies']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
"# rsi_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['rsi_strategies']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
"# quantile_model_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['quantile_model']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
"# gmadl_model_concat = [evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[x] for s in best_strategies['gmadl_model']], padding=PADDING), padding=PADDING, interval=interval) for x in range(top_n)]\n",
"\n",
" z = list(reversed([\n",
" list(reversed([round(buy_and_hold_concat['mod_ir'], 3)]*top_n)),\n",
" list(reversed([round(x['mod_ir'], 3) for x in macd_concat])),\n",
" list(reversed([round(x['mod_ir'], 3) for x in rsi_concat])),\n",
" list(reversed([round(x['mod_ir'], 3) for x in quantile_model_concat])),\n",
" list(reversed([round(x['mod_ir'], 3) for x in gmadl_model_concat])),\n",
" ]))\n",
" x = list(reversed(range(1, top_n+1)))\n",
" y = list(reversed([\n",
" \"Buy and Hold\",\n",
" \"MACD Strategy\",\n",
" \"RSI Strategy\",\n",
" \"Quantile Informer\",\n",
" \"Gmadl Informer\"\n",
" ]))\n",
" # 'Portland'\n",
" fig = ff.create_annotated_heatmap(z, x=x, y=y, colorscale='thermal', zmid=buy_and_hold_concat['mod_ir'])\n",
" fig.update_layout(\n",
" margin=dict(l=20, r=20, b=20, t=20),\n",
" width=1100,\n",
" height=450,\n",
" font=dict(\n",
" # family=\"Courier New, monospace\",\n",
" size=18, # Set the font size here\n",
" # color=\"RebeccaPurple\"\n",
" )\n",
" )\n",
" fig.show()\n",
"# z = list(reversed([\n",
"# list(reversed([round(buy_and_hold_concat['mod_ir'], 3)]*top_n)),\n",
"# list(reversed([round(x['mod_ir'], 3) for x in macd_concat])),\n",
"# list(reversed([round(x['mod_ir'], 3) for x in rsi_concat])),\n",
"# list(reversed([round(x['mod_ir'], 3) for x in quantile_model_concat])),\n",
"# list(reversed([round(x['mod_ir'], 3) for x in gmadl_model_concat])),\n",
"# ]))\n",
"# x = list(reversed(range(1, top_n+1)))\n",
"# y = list(reversed([\n",
"# \"Buy and Hold\",\n",
"# \"MACD Strategy\",\n",
"# \"RSI Strategy\",\n",
"# \"Quantile Informer\",\n",
"# \"Gmadl Informer\"\n",
"# ]))\n",
"# # 'Portland'\n",
"# fig = ff.create_annotated_heatmap(z, x=x, y=y, colorscale='thermal', zmid=buy_and_hold_concat['mod_ir'])\n",
"# fig.update_layout(\n",
"# margin=dict(l=20, r=20, b=20, t=20),\n",
"# width=1100,\n",
"# height=450,\n",
"# font=dict(\n",
"# # family=\"Courier New, monospace\",\n",
"# size=18, # Set the font size here\n",
"# # color=\"RebeccaPurple\"\n",
"# )\n",
"# )\n",
"# fig.show()\n",
"\n",
"# results_for_strats(data_windows_5min, best_strategies_5min, interval='5min', top_n=10) \n",
"# results_for_strats(data_windows_15min, best_strategies_15min, interval='15min', top_n=10) \n",
@ -331,7 +405,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
@ -386,7 +460,7 @@
" table_eval_windows = Texttable()\n",
" table_eval_windows.set_deco(Texttable.HEADER)\n",
" table_eval_windows.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\", \"c\", \"c\", \"c\", \"c\"])\n",
" table_eval_windows.set_precision(3)\n",
" table_eval_windows.set_precision(2)\n",
"\n",
" table_eval_windows.header([\n",
" \"\\\\textbf{Strategy}\",\n",
@ -407,21 +481,21 @@
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" f\"{result['arc']*100:.1f}\\%\",\n",
" f\"{result['asd']*100:.1f}\\%\",\n",
" result['ir'],\n",
" result['md'],\n",
" f\"{result['md']*100:.1f}\\%\",\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" f\"{result['long_pos']*100:.1f}\\%\",\n",
" f\"{result['short_pos']*100:.1f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
@ -503,6 +577,125 @@
"# results_plot(buy_and_hold_concat, [res], width=1200)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Different number of test windows"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/5min-best-strategies-short.pkl', 'rb') as inpt:\n",
" best_strategies_5min_short = pickle.load(inpt)\n",
"\n",
"with open('cache/5min-best-strategies-long.pkl', 'rb') as inpt:\n",
" best_strategies_5min_long = pickle.load(inpt)\n",
"\n",
"with open('cache/30min-best-strategies-short.pkl', 'rb') as inpt:\n",
" best_strategies_30min_short = pickle.load(inpt)\n",
"\n",
"with open('cache/30min-best-strategies-long.pkl', 'rb') as inpt:\n",
" best_strategies_30min_long = pickle.load(inpt)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-5m-short:latest, 1341.36MB. 24 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 24 of 24 files downloaded. \n",
"Done. 0:0:0.7\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-5m-long:latest, 446.99MB. 6 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 6 of 6 files downloaded. \n",
"Done. 0:0:1.1\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-30m-short:latest, 223.56MB. 24 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 24 of 24 files downloaded. \n",
"Done. 0:0:0.7\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-30m-long:latest, 74.51MB. 6 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 6 of 6 files downloaded. \n",
"Done. 0:0:0.4\n"
]
}
],
"source": [
"data_windows_5min_short = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-5m-short:latest',\n",
" min_window=0, \n",
" max_window=11\n",
")\n",
"\n",
"data_windows_5min_long = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-5m-long:latest',\n",
" min_window=0, \n",
" max_window=2\n",
")\n",
"\n",
"data_windows_30min_short = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-30m-short:latest',\n",
" min_window=0, \n",
" max_window=11\n",
")\n",
"\n",
"data_windows_30min_long = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-30m-long:latest',\n",
" min_window=0, \n",
" max_window=2\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"# def results_for_strats_different_windows(buy_and_hold_data_windows, *args, interval='5min'):\n",
"# evaluation_results = []\n",
"# names = []\n",
"# buy_and_hold_data = pd.concat([buy_and_hold_data_windows[0][0][-PADDING:]] + [data_window[1] for data_window in buy_and_hold_data_windows])\n",
"# buy_and_hold_concat = evaluate_strategy(buy_and_hold_data, BuyAndHoldStrategy(), padding=PADDING, interval=interval)\n",
"# for data_windows, strategy, name in args:\n",
"# test_data = pd.concat([data_windows[0][0][-PADDING:]] + [data_window[1] for data_window in data_windows])\n",
"# # buy_and_hold_concat = evaluate_strategy(test_data, BuyAndHoldStrategy(), padding=PADDING, interval=interval)\n",
"# evaluation_results.append(\n",
"# evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in strategy], padding=PADDING), padding=PADDING, interval=interval\n",
"# ))\n",
"# names.append(name)\n",
" \n",
"# results_plot2(buy_and_hold_concat, evaluation_results, names, width=1200)\n",
"# results_table2(buy_and_hold_concat, evaluation_results, names)\n",
"\n",
"# results_for_strats_different_windows(\n",
"# data_windows_30min,\n",
"# (data_windows_30min_short, best_strategies_30min_short['rsi_strategies'], '12 windows'),\n",
"# (data_windows_30min, best_strategies_30min['rsi_strategies'], '6 windows'),\n",
"# (data_windows_30min_long, best_strategies_30min_long['rsi_strategies'], '3 windows'),\n",
"# interval='30min'\n",
"# )\n",
"\n",
"# results_for_strats_different_windows(\n",
"# data_windows_5min,\n",
"# (data_windows_5min_short, best_strategies_5min_short['gmadl_model'], '12 windows'),\n",
"# (data_windows_5min, best_strategies_5min['gmadl_model'], '6 windows'),\n",
"# (data_windows_5min_long, best_strategies_5min_long['gmadl_model'], '3 windows'),\n",
"# interval='5min'\n",
"# )"
]
},
{
"cell_type": "code",
"execution_count": null,