Add notebooks with evaluations, visualizations are commented out due to large size

This commit is contained in:
Filip Stefaniuk 2024-10-16 21:00:44 +02:00
parent 7989813534
commit aa4bc26c53
8 changed files with 810553 additions and 327420 deletions

View File

@ -0,0 +1,817 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import pickle\n",
"import plotly.graph_objs as go\n",
"import latextable\n",
"from texttable import Texttable\n",
"import plotly.graph_objs as go\n",
"from strategy.strategy import (\n",
" BuyAndHoldStrategy,\n",
" MACDStrategy,\n",
" RSIStrategy,\n",
" ModelQuantilePredictionsStrategy,\n",
" ModelGmadlPredictionsStrategy,\n",
" ConcatenatedStrategies\n",
")\n",
"from strategy.util import (\n",
" get_data_windows,\n",
" get_sweep_window_predictions,\n",
" get_predictions_dataframe\n",
")\n",
"from strategy.evaluation import (\n",
" parameter_sweep,\n",
" evaluate_strategy\n",
")\n",
"from strategy.plotting import (\n",
" plot_sweep_results\n",
")\n",
"\n",
"PADDING=5000\n",
"VALID_PART=0.2\n",
"INTERVAL='15min'\n",
"METRIC='mod_ir'\n",
"TOP_N=10"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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-15m:latest, 248.65MB. 12 files... \n",
"\u001b[34m\u001b[1mwandb\u001b[0m: 12 of 12 files downloaded. \n",
"Done. 0:0:0.5\n"
]
}
],
"source": [
"data_windows = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-15m:latest',\n",
" min_window=0, \n",
" max_window=5\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def sweeps_on_all_windows(data_windows, strategy_class, params, **kwargs):\n",
" result = []\n",
" for in_sample, _ in data_windows:\n",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" result.append(parameter_sweep(in_sample[data_part-PADDING:], strategy_class, params, padding=PADDING, interval=INTERVAL, **kwargs))\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"buyandhold_best_strategies = [BuyAndHoldStrategy() for _ in data_windows] "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 3840/3840 [00:06<00:00, 614.41it/s]\n",
"100%|██████████| 3840/3840 [00:08<00:00, 479.05it/s]\n",
"100%|██████████| 3840/3840 [00:06<00:00, 554.94it/s]\n",
"100%|██████████| 3840/3840 [00:06<00:00, 575.21it/s]\n",
"100%|██████████| 3840/3840 [00:06<00:00, 571.03it/s]\n",
"100%|██████████| 3840/3840 [00:06<00:00, 576.86it/s]\n"
]
}
],
"source": [
"MACD_PARAMS = {\n",
" 'fast_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'slow_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'signal_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'short_sell': [True, False]\n",
"}\n",
"MACD_PARAMS_FILTER = lambda p: (\n",
" p['slow_window_size'] > p['fast_window_size']\n",
")\n",
"macd_sweep_results = sweeps_on_all_windows(\n",
" data_windows,\n",
" MACDStrategy,\n",
" MACD_PARAMS,\n",
" params_filter=MACD_PARAMS_FILTER,\n",
" sort_by=METRIC)\n",
"macd_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in macd_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in macd_sweep_results[0]]), parameters=MACD_PARAMS.keys())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 11088/11088 [00:19<00:00, 580.54it/s]\n",
"100%|██████████| 11088/11088 [00:17<00:00, 627.38it/s]\n",
"100%|██████████| 11088/11088 [00:17<00:00, 624.72it/s]\n",
"100%|██████████| 11088/11088 [00:18<00:00, 586.62it/s]\n",
"100%|██████████| 11088/11088 [00:17<00:00, 618.44it/s]\n",
"100%|██████████| 11088/11088 [00:18<00:00, 597.40it/s]\n"
]
}
],
"source": [
"RSI_FILTER = lambda p: (\n",
" ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
" or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
" and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
" and (p['enter_long'] is None or p['exit_short'] is None or (p['exit_short'] < p['enter_long'])))\n",
"\n",
"RSI_PARAMS = {\n",
" 'window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'enter_long': [None, 70, 75, 80, 85, 90, 95],\n",
" 'exit_long': [None, 5, 10, 15, 20, 25, 30],\n",
" 'enter_short': [None, 5, 10, 15, 20, 25, 30],\n",
" 'exit_short': [None, 70, 75, 80, 85, 90, 95],\n",
"}\n",
"rsi_sweep_results = sweeps_on_all_windows(data_windows, RSIStrategy, RSI_PARAMS, params_filter=RSI_FILTER, sort_by=METRIC)\n",
"rsi_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in rsi_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in rsi_sweep_results[0]]), parameters=RSI_PARAMS.keys())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\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",
"\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"
]
}
],
"source": [
"# Model with quantile loss\n",
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/btu0mf6u'\n",
"train_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'train')\n",
"valid_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'valid')\n",
"test_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'test')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1125/1125 [00:21<00:00, 51.66it/s]\n",
"100%|██████████| 1125/1125 [00:20<00:00, 53.98it/s]\n",
"100%|██████████| 1125/1125 [00:22<00:00, 49.63it/s]\n",
"100%|██████████| 1125/1125 [00:24<00:00, 45.57it/s]\n",
"100%|██████████| 1125/1125 [00:23<00:00, 47.76it/s]\n",
"100%|██████████| 1125/1125 [00:24<00:00, 46.33it/s]\n"
]
}
],
"source": [
"MODEL_QUANTILE_LOSS_FILTER = lambda p: (\n",
" ((p['quantile_enter_long'] is not None and (p['quantile_enter_short'] is not None or p['quantile_exit_long'] is not None))\n",
" or (p['quantile_enter_short'] is not None and (p['quantile_exit_short'] is not None or p['quantile_enter_long'] is not None)))\n",
" and (p['quantile_enter_short'] is None or p['quantile_exit_long'] is None or (p['quantile_exit_long'] < p['quantile_enter_short']))\n",
" and (p['quantile_enter_long'] is None or p['quantile_exit_short'] is None or (p['quantile_exit_short'] < p['quantile_enter_long'])))\n",
"\n",
"quantile_model_sweep_results = []\n",
"for (in_sample, _), train_preds, valid_preds, test_preds in zip(data_windows, train_pred_windows, valid_pred_windows, test_pred_windows):\n",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" params={\n",
" 'predictions': [get_predictions_dataframe(valid_preds, test_preds)],\n",
" 'quantiles': [[0.01, 0.02, 0.03, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.97, 0.98, 0.99]],\n",
" 'quantile_enter_long': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_exit_long': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_enter_short': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_exit_short': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'exchange_fee': [0.001, 0.002, 0.003],\n",
" 'future': [1]\n",
" }\n",
" \n",
" quantile_model_sweep_results.append(parameter_sweep(\n",
" in_sample[data_part-PADDING:],\n",
" ModelQuantilePredictionsStrategy,\n",
" params,\n",
" params_filter=MODEL_QUANTILE_LOSS_FILTER,\n",
" padding=PADDING,\n",
" interval=INTERVAL,\n",
" sort_by=METRIC))\n",
"\n",
"quantile_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in quantile_model_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in quantile_model_sweep_results[0]]), parameters=['quantile_enter_long', 'quantile_exit_long', 'quantile_enter_short', 'quantile_exit_short', 'future'])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\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",
"\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"
]
}
],
"source": [
"# Model with quantile loss\n",
"# SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/a6q8zv10'\n",
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/7n3w718v'\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": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1176/1176 [00:19<00:00, 60.00it/s]\n",
"100%|██████████| 1176/1176 [00:19<00:00, 61.63it/s]\n",
"100%|██████████| 1176/1176 [00:16<00:00, 69.59it/s]\n",
"100%|██████████| 1176/1176 [00:17<00:00, 65.37it/s]\n",
"100%|██████████| 1176/1176 [00:17<00:00, 66.33it/s]\n",
"100%|██████████| 1176/1176 [00:17<00:00, 67.86it/s]\n"
]
}
],
"source": [
"MODEL_GMADL_LOSS_FILTER = lambda p: (\n",
" ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
" or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
" and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
" 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",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" params={\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",
" 'exit_short': [None, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007],\n",
" }\n",
" \n",
" gmadl_model_sweep_results.append(parameter_sweep(\n",
" in_sample[data_part-PADDING:],\n",
" ModelGmadlPredictionsStrategy,\n",
" params,\n",
" params_filter=MODEL_GMADL_LOSS_FILTER,\n",
" padding=PADDING,\n",
" interval=INTERVAL,\n",
" sort_by=METRIC))\n",
" \n",
"\n",
"gmadl_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in gmadl_model_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"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",
" 'gmadl_model': gmadl_model_best_strategies\n",
"}\n",
"\n",
"with open('cache/15min-best-strategies.pkl', 'wb') as outp:\n",
" pickle.dump(best_strategies, outp, pickle.HIGHEST_PROTOCOL)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualizations & Tables"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/15min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies = pickle.load(inpt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Tables with parameters"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lcccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{Fast Window Size} & \\textbf{Slow Window Size} & \\textbf{Signal Window Size} & \\textbf{Short sell} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tWindow 1 & 8 & 377 & 1597 & True \\\\\n",
"\t\t\tWindow 2 & 144 & 987 & 2584 & True \\\\\n",
"\t\t\tWindow 3 & 377 & 2584 & 2584 & True \\\\\n",
"\t\t\tWindow 4 & 377 & 2584 & 610 & True \\\\\n",
"\t\t\tWindow 5 & 233 & 610 & 233 & False \\\\\n",
"\t\t\tWindow 6 & 144 & 377 & 2584 & False \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"# Best hparams for MACD strategy\n",
"table_macd_params = Texttable()\n",
"table_macd_params.set_deco(Texttable.HEADER)\n",
"table_macd_params.set_cols_align([\"l\", \"c\", \"c\", \"c\", \"c\"])\n",
"table_macd_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{Fast Window Size}\",\n",
" \"\\\\textbf{Slow Window Size}\",\n",
" \"\\\\textbf{Signal Window Size}\",\n",
" \"\\\\textbf{Short sell}\"\n",
"])\n",
"\n",
"for i, macd_strategy in enumerate(best_strategies['macd_strategies']):\n",
" macd_strategy_info = macd_strategy[0].info()\n",
" table_macd_params.add_row([\n",
" f\"Window {i+1}\",\n",
" macd_strategy_info['fast_window_size'],\n",
" macd_strategy_info['slow_window_size'],\n",
" macd_strategy_info['signal_window_size'],\n",
" macd_strategy_info['short_sell']\n",
" ])\n",
"print(latextable.draw_latex(table_macd_params))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lccccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{window}} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit long}} & \\textbf{\\textit{enter short}} & \\textbf{\\textit{exit short}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-15min & 5 & 80 & - & 15 & - \\\\\n",
"\t\t\tW2-15min & 34 & 75 & - & 30 & - \\\\\n",
"\t\t\tW3-15min & 5 & 95 & - & 10 & - \\\\\n",
"\t\t\tW4-15min & 34 & 75 & - & 30 & - \\\\\n",
"\t\t\tW5-15min & 13 & 70 & - & 5 & - \\\\\n",
"\t\t\tW6-15min & 21 & 85 & 15 & - & - \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_rsi_params = Texttable()\n",
"table_rsi_params.set_deco(Texttable.HEADER)\n",
"table_rsi_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\"])\n",
"table_rsi_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{window}}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit long}}\",\n",
" \"\\\\textbf{\\\\textit{enter short}}\",\n",
" \"\\\\textbf{\\\\textit{exit short}}\"\n",
"])\n",
"\n",
"for i, rsi_strategy in enumerate(best_strategies['rsi_strategies']):\n",
" rsi_strategy_info = rsi_strategy[0].info()\n",
" table_rsi_params.add_row([\n",
" f\"W{i+1}-15min\",\n",
" rsi_strategy_info['window_size'] or '-',\n",
" rsi_strategy_info['enter_long'] or '-',\n",
" rsi_strategy_info['exit_long'] or '-',\n",
" rsi_strategy_info['enter_short'] or '-',\n",
" rsi_strategy_info['exit_short'] or '-'\n",
" ])\n",
"print(latextable.draw_latex(table_rsi_params))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lccccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit Long}} & \\textbf{\\textit{enter Short}} & \\textbf{\\textit{exit Short}} & \\textbf{\\textit{threshold}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-15min & 0.980 & - & 0.990 & - & 0.003 \\\\\n",
"\t\t\tW2-15min & 0.990 & 0.950 & - & 0.900 & 0.003 \\\\\n",
"\t\t\tW3-15min & - & - & 0.990 & 0.990 & 0.003 \\\\\n",
"\t\t\tW4-15min & 0.990 & 0.970 & - & 0.980 & 0.003 \\\\\n",
"\t\t\tW5-15min & 0.950 & 0.990 & - & - & 0.001 \\\\\n",
"\t\t\tW6-15min & 0.990 & 0.950 & - & 0.950 & 0.002 \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_quantile_params = Texttable()\n",
"table_quantile_params.set_deco(Texttable.HEADER)\n",
"table_quantile_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\"])\n",
"table_quantile_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit Long}}\",\n",
" \"\\\\textbf{\\\\textit{enter Short}}\",\n",
" \"\\\\textbf{\\\\textit{exit Short}}\",\n",
" \"\\\\textbf{\\\\textit{threshold}}\"\n",
"])\n",
"\n",
"for i, quantile_strategy in enumerate(best_strategies['quantile_model']):\n",
" quantile_strategy_info = quantile_strategy[0].info()\n",
" table_quantile_params.add_row([\n",
" f\"W{i+1}-{INTERVAL}\",\n",
" quantile_strategy_info['quantile_enter_long'] or '-',\n",
" quantile_strategy_info['quantile_exit_long'] or '-',\n",
" quantile_strategy_info['quantile_enter_short'] or '-',\n",
" quantile_strategy_info['quantile_exit_short'] or '-',\n",
" quantile_strategy_info['exchange_fee']\n",
" ])\n",
"print(latextable.draw_latex(table_quantile_params))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lcccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit Long}} & \\textbf{\\textit{enter Short}} & \\textbf{\\textit{exit Short}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-15min & 0.005 & - & -0.002 & - \\\\\n",
"\t\t\tW2-15min & 0.006 & - & -0.002 & - \\\\\n",
"\t\t\tW3-15min & - & - & -0.001 & 0.005 \\\\\n",
"\t\t\tW4-15min & 0.007 & - & -0.005 & - \\\\\n",
"\t\t\tW5-15min & 0.001 & - & -0.004 & - \\\\\n",
"\t\t\tW6-15min & 0.002 & -0.002 & - & - \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_gmadl_params = Texttable()\n",
"table_gmadl_params.set_deco(Texttable.HEADER)\n",
"table_gmadl_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\"])\n",
"table_gmadl_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit Long}}\",\n",
" \"\\\\textbf{\\\\textit{enter Short}}\",\n",
" \"\\\\textbf{\\\\textit{exit Short}}\",\n",
"])\n",
"\n",
"for i, gmadl_strategy in enumerate(best_strategies['gmadl_model']):\n",
" gmadl_strategy_info = gmadl_strategy[0].info()\n",
" table_gmadl_params.add_row([\n",
" f\"W{i+1}-{INTERVAL}\",\n",
" gmadl_strategy_info['enter_long'] or '-',\n",
" gmadl_strategy_info['exit_long'] or '-',\n",
" gmadl_strategy_info['enter_short'] or '-',\n",
" gmadl_strategy_info['exit_short'] or '-'\n",
" ])\n",
"print(latextable.draw_latex(table_gmadl_params))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Evaluation on the windows"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"def results_plot(idx, result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model, width=850, height=500, notitle=False):\n",
"\n",
" fig = go.Figure([\n",
" go.Scatter(y=result_buyandhold['portfolio_value'], x=result_buyandhold['time'], name=\"Buy and Hold\"),\n",
" go.Scatter(y=result_macd['portfolio_value'], x=result_macd['time'], name=\"MACD Strategy\"),\n",
" go.Scatter(y=result_rsi['portfolio_value'], x=result_rsi['time'], name=\"RSI Strategy\"),\n",
" go.Scatter(y=result_quantile_model['portfolio_value'], x=result_quantile_model['time'], name='Quantile Informer Strategy'),\n",
" go.Scatter(y=result_gmadl_model['portfolio_value'], x=result_gmadl_model['time'], name='GMADL Informer Strategy')\n",
" ])\n",
" fig.update_layout(\n",
" title={\n",
" 'text': f\"W{idx}-{INTERVAL}\",\n",
" 'y':0.97,\n",
" 'x':0.5,\n",
" 'xanchor': 'center',\n",
" 'yanchor': 'top'} if not notitle else None,\n",
" yaxis_title=\"Portfolio Value\",\n",
" xaxis_title=\"Date\",\n",
" font=dict(\n",
" # family=\"Courier New, monospace\",\n",
" size=14,\n",
" ),\n",
" autosize=False,\n",
" width=width,\n",
" height=height,\n",
" margin=dict(l=20, r=20, t=20 if notitle else 110, b=20),\n",
" plot_bgcolor='white',\n",
" legend=dict(\n",
" orientation=\"h\",\n",
" yanchor=\"bottom\",\n",
" y=1.02,\n",
" xanchor=\"left\",\n",
" x=0.02\n",
" )\n",
" )\n",
" fig.update_xaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.update_yaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.write_image(f\"images/eval-w{idx}-{INTERVAL}.png\")\n",
" fig.show()\n",
" \n",
"def results_table(result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model):\n",
" 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",
"\n",
" table_eval_windows.header([\n",
" \"\\\\textbf{Strategy}\",\n",
" \"\\\\textbf{VAL}\",\n",
" \"\\\\textbf{ARC}\",\n",
" \"\\\\textbf{ASD}\",\n",
" \"\\\\textbf{IR*}\",\n",
" \"\\\\textbf{MD}\",\n",
" \"\\\\textbf{IR**}\",\n",
" \"\\\\textbf{N}\",\n",
" \"\\\\textbf{LONG}\",\n",
" \"\\\\textbf{SHORT}\",\n",
" ])\n",
"\n",
" strategy_name_result = [\n",
" ('Buy and Hold', result_buyandhold),\n",
" ('MACD Strategy', result_macd),\n",
" ('RSI Strategy', result_rsi),\n",
" ('Quantile Informer', result_quantile_model),\n",
" ('GMADL Informer', result_gmadl_model)\n",
" ]\n",
" for strategy_name, result in strategy_name_result:\n",
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" result['ir'],\n",
" result['md'],\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# for i, (in_sample, out_of_sample) in enumerate(data_windows):\n",
"# padded_window = pd.concat([in_sample.iloc[-PADDING:], out_of_sample])\n",
"# result_buyandhold = evaluate_strategy(padded_window, best_strategies['buy_and_hold'][i], padding=PADDING, interval=INTERVAL)\n",
"# result_macd = evaluate_strategy(padded_window, [s[0] for s in best_strategies['macd_strategies']][i], padding=PADDING, interval=INTERVAL)\n",
"# result_rsi = evaluate_strategy(padded_window, [s[0] for s in best_strategies['rsi_strategies']][i], padding=PADDING, interval=INTERVAL)\n",
"# 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",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 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",
"# macd_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['macd_strategies']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# rsi_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['rsi_strategies']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# quantile_model_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['quantile_model']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# 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"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# for i, (in_sample, out_of_sample) in enumerate(data_windows):\n",
"# padded_window = pd.concat([in_sample.iloc[-PADDING:], out_of_sample])\n",
"# result_buyandhold = evaluate_strategy(padded_window, buyandhold_best_strategies[i], padding=PADDING, interval='15min')\n",
"# result_macd = evaluate_strategy(padded_window, macd_best_strategies[i], padding=PADDING, interval='15min')\n",
"# result_rsi = evaluate_strategy(padded_window, rsi_best_strategies[i], padding=PADDING, interval='15min')\n",
"# result_quantile_model = evaluate_strategy(padded_window, quantile_model_best_strategies[i], padding=PADDING, interval='15min')\n",
"# result_gmadl_model = evaluate_strategy(padded_window, gmadl_model_best_strategies[i], padding=PADDING, interval='15min')\n",
"\n",
"# go.Figure([\n",
"# go.Scatter(y=result_buyandhold['portfolio_value'], x=result_buyandhold['time'], name=result_buyandhold['strategy_name']),\n",
"# go.Scatter(y=result_macd['portfolio_value'], x=result_macd['time'], name=result_macd['strategy_name']),\n",
"# go.Scatter(y=result_rsi['portfolio_value'], x=result_rsi['time'], name=result_rsi['strategy_name']),\n",
"# go.Scatter(y=result_quantile_model['portfolio_value'], x=result_quantile_model['time'], name='Quantile Model'),\n",
"# go.Scatter(y=result_gmadl_model['portfolio_value'], x=result_gmadl_model['time'], name='GMADL model')\n",
"# ]).show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "wnemsc",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,826 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import pickle\n",
"import plotly.graph_objs as go\n",
"import latextable\n",
"from texttable import Texttable\n",
"from strategy.strategy import (\n",
" BuyAndHoldStrategy,\n",
" MACDStrategy,\n",
" RSIStrategy,\n",
" BaselineReturnsStrategy,\n",
" ModelQuantilePredictionsStrategy,\n",
" ModelGmadlPredictionsStrategy,\n",
" ConcatenatedStrategies\n",
")\n",
"from strategy.util import (\n",
" get_data_windows,\n",
" get_sweep_window_predictions,\n",
" get_predictions_dataframe\n",
")\n",
"from strategy.evaluation import (\n",
" parameter_sweep,\n",
" evaluate_strategy\n",
")\n",
"from strategy.plotting import (\n",
" plot_sweep_results\n",
")\n",
"\n",
"PADDING=5000\n",
"VALID_PART=0.4\n",
"INTERVAL='30min'\n",
"METRIC='mod_ir'\n",
"TOP_N=10"
]
},
{
"cell_type": "code",
"execution_count": 101,
"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"
]
}
],
"source": [
"data_windows = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-30m:latest',\n",
" min_window=0, \n",
" max_window=5\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"def sweeps_on_all_windows(data_windows, strategy_class, params, **kwargs):\n",
" result = []\n",
" for in_sample, _ in data_windows:\n",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" result.append(parameter_sweep(in_sample[data_part-PADDING:], strategy_class, params, padding=PADDING, interval=INTERVAL, **kwargs))\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"buyandhold_best_strategies = [BuyAndHoldStrategy() for _ in data_windows] "
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 3840/3840 [00:04<00:00, 769.13it/s]\n",
"100%|██████████| 3840/3840 [00:04<00:00, 779.61it/s]\n",
"100%|██████████| 3840/3840 [00:05<00:00, 754.69it/s]\n",
"100%|██████████| 3840/3840 [00:06<00:00, 579.24it/s]\n",
"100%|██████████| 3840/3840 [00:04<00:00, 777.65it/s]\n",
"100%|██████████| 3840/3840 [00:04<00:00, 782.54it/s]\n"
]
}
],
"source": [
"MACD_PARAMS = {\n",
" 'fast_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'slow_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'signal_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'short_sell': [True, False]\n",
" # 'short_sell': [False]\n",
"}\n",
"MACD_PARAMS_FILTER = lambda p: (\n",
" p['slow_window_size'] > p['fast_window_size']\n",
")\n",
"macd_sweep_results = sweeps_on_all_windows(\n",
" data_windows,\n",
" MACDStrategy,\n",
" MACD_PARAMS,\n",
" params_filter=MACD_PARAMS_FILTER)\n",
"macd_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in macd_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in macd_sweep_results[0]]), parameters=MACD_PARAMS.keys())"
]
},
{
"cell_type": "code",
"execution_count": 108,
"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"
]
}
],
"source": [
"RSI_FILTER = lambda p: (\n",
" ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
" or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
" and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
" and (p['enter_long'] is None or p['exit_short'] is None or (p['exit_short'] < p['enter_long'])))\n",
"\n",
"RSI_PARAMS = {\n",
" 'window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'enter_long': [None, 70, 75, 80, 85, 90, 95],\n",
" 'exit_long': [None, 5, 10, 15, 20, 25, 30],\n",
" 'enter_short': [None, 5, 10, 15, 20, 25, 30],\n",
" 'exit_short': [None, 70, 75, 80, 85, 90, 95],\n",
" # 'enter_short': [None],\n",
" # 'exit_short': [None],\n",
"}\n",
"rsi_sweep_results = sweeps_on_all_windows(data_windows, RSIStrategy, RSI_PARAMS, params_filter=RSI_FILTER)\n",
"rsi_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in rsi_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"# BASELINE_FILTER = lambda p: (\n",
"# ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
"# or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
"# and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
"# and (p['enter_long'] is None or p['exit_short'] is None or (p['exit_short'] < p['enter_long'])))\n",
"\n",
"# BASELINE_PARAMS = {\n",
"# 'enter_long': [None, 0.001, 0.002, 0.003, 0.005, 0.006, 0.007],\n",
"# 'exit_long': [None, -0.001, -0.002, -0.003, -0.005, -0.006, -0.007],\n",
"# 'enter_short': [None, -0.001, -0.002, -0.003, -0.005, -0.006, -0.007],\n",
"# 'exit_short': [None, 0.001, 0.002, 0.003, 0.005, 0.006, 0.007],\n",
"# }\n",
"# baseline_sweep_results = sweeps_on_all_windows(data_windows, BaselineReturnsStrategy, BASELINE_PARAMS, params_filter=BASELINE_FILTER)\n",
"# baseline_best_strategies = [results[0][1] for results in baseline_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in baseline_sweep_results[0]]), parameters=BASELINE_PARAMS.keys())"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\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",
"\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"
]
}
],
"source": [
"# Model with quantile loss\n",
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/vimckxe2'\n",
"train_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'train')\n",
"valid_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'valid')\n",
"test_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'test')"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1125/1125 [00:28<00:00, 40.16it/s]\n",
"100%|██████████| 1125/1125 [00:27<00:00, 41.37it/s]\n",
"100%|██████████| 1125/1125 [00:27<00:00, 41.47it/s]\n",
"100%|██████████| 1125/1125 [00:27<00:00, 40.47it/s]\n",
"100%|██████████| 1125/1125 [00:25<00:00, 44.84it/s]\n",
"100%|██████████| 1125/1125 [00:25<00:00, 44.11it/s]\n"
]
}
],
"source": [
"MODEL_QUANTILE_LOSS_FILTER = lambda p: (\n",
" ((p['quantile_enter_long'] is not None and (p['quantile_enter_short'] is not None or p['quantile_exit_long'] is not None))\n",
" or (p['quantile_enter_short'] is not None and (p['quantile_exit_short'] is not None or p['quantile_enter_long'] is not None)))\n",
" and (p['quantile_enter_short'] is None or p['quantile_exit_long'] is None or (p['quantile_exit_long'] < p['quantile_enter_short']))\n",
" and (p['quantile_enter_long'] is None or p['quantile_exit_short'] is None or (p['quantile_exit_short'] < p['quantile_enter_long'])))\n",
"\n",
"quantile_model_sweep_results = []\n",
"for (in_sample, _), train_preds, valid_preds, test_preds in zip(data_windows, train_pred_windows, valid_pred_windows, test_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",
" 'quantiles': [[0.01, 0.02, 0.03, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.97, 0.98, 0.99]],\n",
" 'quantile_enter_long': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_exit_long': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_enter_short': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_exit_short': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'exchange_fee': [0.001, 0.002, 0.003],\n",
" # 'exchange_fee': [0.001, 0.0015, 0.002, 0.0025, 0.003, 0.005],\n",
" # 'quantile_enter_short': [None],\n",
" # 'quantile_exit_short': [None],\n",
" 'future': [1]\n",
" }\n",
" \n",
" quantile_model_sweep_results.append(parameter_sweep(\n",
" in_sample[data_part-PADDING:],\n",
" ModelQuantilePredictionsStrategy,\n",
" params,\n",
" params_filter=MODEL_QUANTILE_LOSS_FILTER,\n",
" padding=PADDING,\n",
" interval=INTERVAL,\n",
" sort_by=METRIC))\n",
"\n",
"quantile_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in quantile_model_sweep_results]\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in quantile_model_sweep_results[0]]), parameters=['exchange_fee', 'quantile_enter_long', 'quantile_exit_long', 'quantile_enter_short', 'quantile_exit_short', 'future'], round=5)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\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",
"\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"
]
}
],
"source": [
"# Model with gmadl loss\n",
"# SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/tmqx4epx'\n",
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/7p7tdxbn'\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": 51,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1176/1176 [00:22<00:00, 51.45it/s]\n",
"100%|██████████| 1176/1176 [00:24<00:00, 48.45it/s]\n",
"100%|██████████| 1176/1176 [00:21<00:00, 53.53it/s]\n",
"100%|██████████| 1176/1176 [00:24<00:00, 48.36it/s]\n",
"100%|██████████| 1176/1176 [00:22<00:00, 51.90it/s]\n",
"100%|██████████| 1176/1176 [00:21<00:00, 54.04it/s]\n"
]
}
],
"source": [
"MODEL_GMADL_LOSS_FILTER = lambda p: (\n",
" ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
" or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
" and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
" 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",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" params={\n",
" 'predictions': [get_predictions_dataframe(train_preds, 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",
" 'exit_short': [None, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007],\n",
" # 'enter_short': [None],\n",
" # 'exit_short': [None],\n",
" }\n",
" \n",
" gmadl_model_sweep_results.append(parameter_sweep(\n",
" in_sample[data_part-PADDING:],\n",
" ModelGmadlPredictionsStrategy,\n",
" params,\n",
" params_filter=MODEL_GMADL_LOSS_FILTER,\n",
" padding=PADDING,\n",
" interval=INTERVAL,\n",
" sort_by=METRIC))\n",
" \n",
"\n",
"gmadl_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in gmadl_model_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in gmadl_model_sweep_results[0]]), parameters=['enter_long', 'exit_long', 'enter_short', 'exit_short'], round=5)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"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",
" # 'gmadl_model': gmadl_model_best_strategies\n",
"}\n",
"\n",
"with open('cache/30min-best-strategies-04.pkl', 'wb') as outp:\n",
" pickle.dump(best_strategies, outp, pickle.HIGHEST_PROTOCOL)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualizations & Tables"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/30min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies = pickle.load(inpt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Tables with parameters"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lcccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{Fast Window Size} & \\textbf{Slow Window Size} & \\textbf{Signal Window Size} & \\textbf{Short sell} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tWindow 1 & 1597 & 2584 & 2584 & True \\\\\n",
"\t\t\tWindow 2 & 233 & 2584 & 2584 & True \\\\\n",
"\t\t\tWindow 3 & 13 & 2584 & 2584 & True \\\\\n",
"\t\t\tWindow 4 & 233 & 987 & 233 & True \\\\\n",
"\t\t\tWindow 5 & 144 & 233 & 233 & False \\\\\n",
"\t\t\tWindow 6 & 1597 & 2584 & 1597 & False \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"# Best hparams for MACD strategy\n",
"table_macd_params = Texttable()\n",
"table_macd_params.set_deco(Texttable.HEADER)\n",
"table_macd_params.set_cols_align([\"l\", \"c\", \"c\", \"c\", \"c\"])\n",
"table_macd_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{Fast Window Size}\",\n",
" \"\\\\textbf{Slow Window Size}\",\n",
" \"\\\\textbf{Signal Window Size}\",\n",
" \"\\\\textbf{Short sell}\"\n",
"])\n",
"\n",
"for i, macd_strategy in enumerate(best_strategies['macd_strategies']):\n",
" macd_strategy_info = macd_strategy[0].info()\n",
" table_macd_params.add_row([\n",
" f\"Window {i+1}\",\n",
" macd_strategy_info['fast_window_size'],\n",
" macd_strategy_info['slow_window_size'],\n",
" macd_strategy_info['signal_window_size'],\n",
" macd_strategy_info['short_sell']\n",
" ])\n",
"print(latextable.draw_latex(table_macd_params))"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lccccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{window}} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit long}} & \\textbf{\\textit{enter short}} & \\textbf{\\textit{exit short}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-30min & 5 & 95 & - & 5 & - \\\\\n",
"\t\t\tW2-30min & 21 & 70 & - & 30 & - \\\\\n",
"\t\t\tW3-30min & 5 & 95 & - & 15 & 85 \\\\\n",
"\t\t\tW4-30min & 8 & 95 & 5 & - & 90 \\\\\n",
"\t\t\tW5-30min & 13 & 70 & - & 5 & - \\\\\n",
"\t\t\tW6-30min & 34 & 80 & 25 & - & - \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_rsi_params = Texttable()\n",
"table_rsi_params.set_deco(Texttable.HEADER)\n",
"table_rsi_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\"])\n",
"table_rsi_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{window}}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit long}}\",\n",
" \"\\\\textbf{\\\\textit{enter short}}\",\n",
" \"\\\\textbf{\\\\textit{exit short}}\"\n",
"])\n",
"\n",
"for i, rsi_strategy in enumerate(best_strategies['rsi_strategies']):\n",
" rsi_strategy_info = rsi_strategy[0].info()\n",
" table_rsi_params.add_row([\n",
" f\"W{i+1}-30min\",\n",
" rsi_strategy_info['window_size'] or '-',\n",
" rsi_strategy_info['enter_long'] or '-',\n",
" rsi_strategy_info['exit_long'] or '-',\n",
" rsi_strategy_info['enter_short'] or '-',\n",
" rsi_strategy_info['exit_short'] or '-'\n",
" ])\n",
"print(latextable.draw_latex(table_rsi_params))"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lccccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit Long}} & \\textbf{\\textit{enter Short}} & \\textbf{\\textit{exit Short}} & \\textbf{\\textit{threshold}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-30min & 0.980 & - & 0.950 & 0.970 & 0.003 \\\\\n",
"\t\t\tW2-30min & - & 0.900 & 0.980 & 0.990 & 0.001 \\\\\n",
"\t\t\tW3-30min & - & 0.980 & 0.990 & 0.900 & 0.003 \\\\\n",
"\t\t\tW4-30min & 0.950 & 0.990 & - & 0.900 & 0.001 \\\\\n",
"\t\t\tW5-30min & 0.900 & 0.990 & - & - & 0.001 \\\\\n",
"\t\t\tW6-30min & 0.970 & 0.980 & - & 0.950 & 0.003 \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_quantile_params = Texttable()\n",
"table_quantile_params.set_deco(Texttable.HEADER)\n",
"table_quantile_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\"])\n",
"table_quantile_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit Long}}\",\n",
" \"\\\\textbf{\\\\textit{enter Short}}\",\n",
" \"\\\\textbf{\\\\textit{exit Short}}\",\n",
" \"\\\\textbf{\\\\textit{threshold}}\"\n",
"])\n",
"\n",
"for i, quantile_strategy in enumerate(best_strategies['quantile_model']):\n",
" quantile_strategy_info = quantile_strategy[0].info()\n",
" table_quantile_params.add_row([\n",
" f\"W{i+1}-{INTERVAL}\",\n",
" quantile_strategy_info['quantile_enter_long'] or '-',\n",
" quantile_strategy_info['quantile_exit_long'] or '-',\n",
" quantile_strategy_info['quantile_enter_short'] or '-',\n",
" quantile_strategy_info['quantile_exit_short'] or '-',\n",
" quantile_strategy_info['exchange_fee']\n",
" ])\n",
"print(latextable.draw_latex(table_quantile_params))"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lcccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit Long}} & \\textbf{\\textit{enter Short}} & \\textbf{\\textit{exit Short}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-30min & 0.007 & - & -0.001 & - \\\\\n",
"\t\t\tW2-30min & 0.003 & - & -0.007 & - \\\\\n",
"\t\t\tW3-30min & - & - & -0.001 & 0.005 \\\\\n",
"\t\t\tW4-30min & 0.002 & - & -0.006 & - \\\\\n",
"\t\t\tW5-30min & 0.005 & -0.001 & - & - \\\\\n",
"\t\t\tW6-30min & 0.003 & - & -0.004 & - \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_gmadl_params = Texttable()\n",
"table_gmadl_params.set_deco(Texttable.HEADER)\n",
"table_gmadl_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\"])\n",
"table_gmadl_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit Long}}\",\n",
" \"\\\\textbf{\\\\textit{enter Short}}\",\n",
" \"\\\\textbf{\\\\textit{exit Short}}\",\n",
"])\n",
"\n",
"for i, gmadl_strategy in enumerate(best_strategies['gmadl_model']):\n",
" gmadl_strategy_info = gmadl_strategy[0].info()\n",
" table_gmadl_params.add_row([\n",
" f\"W{i+1}-{INTERVAL}\",\n",
" gmadl_strategy_info['enter_long'] or '-',\n",
" gmadl_strategy_info['exit_long'] or '-',\n",
" gmadl_strategy_info['enter_short'] or '-',\n",
" gmadl_strategy_info['exit_short'] or '-'\n",
" ])\n",
"print(latextable.draw_latex(table_gmadl_params))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Evaluation on the windows"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"def results_plot(idx, result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model, width=850, height=500, notitle=False):\n",
"\n",
" fig = go.Figure([\n",
" go.Scatter(y=result_buyandhold['portfolio_value'], x=result_buyandhold['time'], name=\"Buy and Hold\"),\n",
" go.Scatter(y=result_macd['portfolio_value'], x=result_macd['time'], name=\"MACD Strategy\"),\n",
" go.Scatter(y=result_rsi['portfolio_value'], x=result_rsi['time'], name=\"RSI Strategy\"),\n",
" go.Scatter(y=result_quantile_model['portfolio_value'], x=result_quantile_model['time'], name='Quantile Informer Strategy'),\n",
" go.Scatter(y=result_gmadl_model['portfolio_value'], x=result_gmadl_model['time'], name='GMADL Informer Strategy')\n",
" ])\n",
" fig.update_layout(\n",
" title={\n",
" 'text': f\"W{idx}-{INTERVAL}\",\n",
" 'y':0.97,\n",
" 'x':0.5,\n",
" 'xanchor': 'center',\n",
" 'yanchor': 'top'} if not notitle else None,\n",
" yaxis_title=\"Portfolio Value\",\n",
" xaxis_title=\"Date\",\n",
" font=dict(\n",
" # family=\"Courier New, monospace\",\n",
" size=14,\n",
" ),\n",
" autosize=False,\n",
" width=width,\n",
" height=height,\n",
" margin=dict(l=20, r=20, t=20 if notitle else 110, b=20),\n",
" plot_bgcolor='white',\n",
" legend=dict(\n",
" orientation=\"h\",\n",
" yanchor=\"bottom\",\n",
" y=1.02,\n",
" xanchor=\"left\",\n",
" x=0.02\n",
" )\n",
" )\n",
" fig.update_xaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.update_yaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.write_image(f\"images/eval-w{idx}-{INTERVAL}.png\")\n",
" fig.show()\n",
" \n",
"def results_table(result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model):\n",
" 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",
"\n",
" table_eval_windows.header([\n",
" \"\\\\textbf{Strategy}\",\n",
" \"\\\\textbf{VAL}\",\n",
" \"\\\\textbf{ARC}\",\n",
" \"\\\\textbf{ASD}\",\n",
" \"\\\\textbf{IR*}\",\n",
" \"\\\\textbf{MD}\",\n",
" \"\\\\textbf{IR**}\",\n",
" \"\\\\textbf{N}\",\n",
" \"\\\\textbf{LONG}\",\n",
" \"\\\\textbf{SHORT}\",\n",
" ])\n",
"\n",
" strategy_name_result = [\n",
" ('Buy and Hold', result_buyandhold),\n",
" ('MACD Strategy', result_macd),\n",
" ('RSI Strategy', result_rsi),\n",
" ('Quantile Informer', result_quantile_model),\n",
" ('GMADL Informer', result_gmadl_model)\n",
" ]\n",
" for strategy_name, result in strategy_name_result:\n",
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" result['ir'],\n",
" result['md'],\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# for i, (in_sample, out_of_sample) in enumerate(data_windows):\n",
"# padded_window = pd.concat([in_sample.iloc[-PADDING:], out_of_sample])\n",
"# result_buyandhold = evaluate_strategy(padded_window, best_strategies['buy_and_hold'][i], padding=PADDING, interval=INTERVAL)\n",
"# result_macd = evaluate_strategy(padded_window, [s[0] for s in best_strategies['macd_strategies']][i], padding=PADDING, interval=INTERVAL)\n",
"# result_rsi = evaluate_strategy(padded_window, [s[0] for s in best_strategies['rsi_strategies']][i], padding=PADDING, interval=INTERVAL)\n",
"# 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",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Plot & Table for the whole testing period"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 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",
"# macd_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['macd_strategies']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# rsi_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['rsi_strategies']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# quantile_model_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['quantile_model']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# 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"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "wnemsc",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,789 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import pickle\n",
"import plotly.graph_objs as go\n",
"import latextable\n",
"from texttable import Texttable\n",
"from strategy.strategy import (\n",
" BuyAndHoldStrategy,\n",
" MACDStrategy,\n",
" RSIStrategy,\n",
" ModelQuantilePredictionsStrategy,\n",
" ModelGmadlPredictionsStrategy,\n",
" ConcatenatedStrategies\n",
")\n",
"from strategy.util import (\n",
" get_data_windows,\n",
" get_sweep_window_predictions,\n",
" get_predictions_dataframe\n",
")\n",
"from strategy.evaluation import (\n",
" parameter_sweep,\n",
" evaluate_strategy\n",
")\n",
"from strategy.plotting import (\n",
" plot_sweep_results\n",
")\n",
"\n",
"PADDING=5000\n",
"VALID_PART=0.4\n",
"INTERVAL='5min'\n",
"METRIC='mod_ir'\n",
"TOP_N=10"
]
},
{
"cell_type": "code",
"execution_count": 94,
"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"
]
}
],
"source": [
"data_windows = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-5m:latest',\n",
" min_window=0, \n",
" max_window=5\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"def sweeps_on_all_windows(data_windows, strategy_class, params, **kwargs):\n",
" result = []\n",
" for in_sample, _ in data_windows:\n",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" result.append(parameter_sweep(in_sample[data_part-PADDING:], strategy_class, params, padding=PADDING, interval=INTERVAL, **kwargs))\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"buyandhold_best_strategies = [BuyAndHoldStrategy() for _ in data_windows] "
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 3840/3840 [00:25<00:00, 148.93it/s]\n",
"100%|██████████| 3840/3840 [00:25<00:00, 149.05it/s]\n",
"100%|██████████| 3840/3840 [00:27<00:00, 138.86it/s]\n",
"100%|██████████| 3840/3840 [00:28<00:00, 133.50it/s]\n",
"100%|██████████| 3840/3840 [00:25<00:00, 152.79it/s]\n",
"100%|██████████| 3840/3840 [00:22<00:00, 168.94it/s]\n"
]
}
],
"source": [
"MACD_PARAMS = {\n",
" 'fast_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'slow_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'signal_window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'short_sell': [True, False]\n",
"}\n",
"MACD_PARAMS_FILTER = lambda p: (\n",
" p['slow_window_size'] > p['fast_window_size']\n",
")\n",
"macd_sweep_results = sweeps_on_all_windows(\n",
" data_windows,\n",
" MACDStrategy,\n",
" MACD_PARAMS,\n",
" params_filter=MACD_PARAMS_FILTER,\n",
" sort_by=METRIC)\n",
"macd_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in macd_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in macd_sweep_results[0]]), parameters=MACD_PARAMS.keys(), objective=METRIC)\n",
"# macd_sweep_results[0][:3]\n",
"# macd_best_strategies"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 11088/11088 [01:14<00:00, 149.71it/s]\n",
"100%|██████████| 11088/11088 [01:18<00:00, 140.49it/s]\n",
"100%|██████████| 11088/11088 [01:14<00:00, 148.45it/s]\n",
"100%|██████████| 11088/11088 [01:15<00:00, 147.54it/s]\n",
"100%|██████████| 11088/11088 [01:09<00:00, 160.15it/s]\n",
"100%|██████████| 11088/11088 [01:10<00:00, 158.01it/s]\n"
]
}
],
"source": [
"RSI_FILTER = lambda p: (\n",
" ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
" or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
" and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
" and (p['enter_long'] is None or p['exit_short'] is None or (p['exit_short'] < p['enter_long'])))\n",
"\n",
"RSI_PARAMS = {\n",
" 'window_size': [2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584],\n",
" 'enter_long': [None, 70, 75, 80, 85, 90, 95],\n",
" 'exit_long': [None, 5, 10, 15, 20, 25, 30],\n",
" 'enter_short': [None, 5, 10, 15, 20, 25, 30],\n",
" 'exit_short': [None, 70, 75, 80, 85, 90, 95],\n",
"}\n",
"rsi_sweep_results = sweeps_on_all_windows(data_windows, RSIStrategy, RSI_PARAMS, params_filter=RSI_FILTER, sort_by=METRIC)\n",
"rsi_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in rsi_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in rsi_sweep_results[0]]), parameters=RSI_PARAMS.keys(), objective=METRIC)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\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",
"\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"
]
}
],
"source": [
"# Model with quantile loss\n",
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/8m3hwwmx'\n",
"train_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'train')\n",
"valid_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'valid')\n",
"test_pred_windows = get_sweep_window_predictions(SWEEP_ID, 'test')"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1125/1125 [02:49<00:00, 6.65it/s]\n",
"100%|██████████| 1125/1125 [02:51<00:00, 6.55it/s]\n",
"100%|██████████| 1125/1125 [02:56<00:00, 6.36it/s]\n",
"100%|██████████| 1125/1125 [02:50<00:00, 6.61it/s]\n",
"100%|██████████| 1125/1125 [02:47<00:00, 6.72it/s]\n",
"100%|██████████| 1125/1125 [02:48<00:00, 6.66it/s]\n"
]
}
],
"source": [
"MODEL_QUANTILE_LOSS_FILTER = lambda p: (\n",
" ((p['quantile_enter_long'] is not None and (p['quantile_enter_short'] is not None or p['quantile_exit_long'] is not None))\n",
" or (p['quantile_enter_short'] is not None and (p['quantile_exit_short'] is not None or p['quantile_enter_long'] is not None)))\n",
" and (p['quantile_enter_short'] is None or p['quantile_exit_long'] is None or (p['quantile_exit_long'] < p['quantile_enter_short']))\n",
" and (p['quantile_enter_long'] is None or p['quantile_exit_short'] is None or (p['quantile_exit_short'] < p['quantile_enter_long'])))\n",
"\n",
"quantile_model_sweep_results = []\n",
"for (in_sample, _), train_preds, valid_preds, test_preds in zip(data_windows, train_pred_windows, valid_pred_windows, test_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",
" 'quantiles': [[0.01, 0.02, 0.03, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.97, 0.98, 0.99]],\n",
" 'quantile_enter_long': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_exit_long': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_enter_short': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'quantile_exit_short': [None, 0.9, 0.95, 0.97, 0.98, 0.99],\n",
" 'exchange_fee': [0.001, 0.002, 0.003],\n",
" 'future': [1]\n",
" }\n",
" \n",
" quantile_model_sweep_results.append(parameter_sweep(\n",
" in_sample[data_part-PADDING:],\n",
" ModelQuantilePredictionsStrategy,\n",
" params,\n",
" params_filter=MODEL_QUANTILE_LOSS_FILTER,\n",
" padding=PADDING,\n",
" interval=INTERVAL,\n",
" sort_by=METRIC))\n",
"\n",
"quantile_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in quantile_model_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"# plot_sweep_results(pd.DataFrame([result for result, _ in quantile_model_sweep_results[0]]), parameters=['quantile_enter_long', 'quantile_exit_long', 'quantile_enter_short', 'quantile_exit_short', 'future'], objective=METRIC)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\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",
"\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"
]
}
],
"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",
"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,
"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"
]
}
],
"source": [
"MODEL_GMADL_LOSS_FILTER = lambda p: (\n",
" ((p['enter_long'] is not None and (p['enter_short'] is not None or p['exit_long'] is not None))\n",
" or (p['enter_short'] is not None and (p['exit_short'] is not None or p['enter_long'] is not None)))\n",
" and (p['enter_short'] is None or p['exit_long'] is None or (p['exit_long'] > p['enter_short']))\n",
" 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",
" data_part = int((1 - VALID_PART) * len(in_sample))\n",
" params={\n",
" 'predictions': [get_predictions_dataframe(train_preds, 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",
" 'exit_short': [None, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007],\n",
" }\n",
" \n",
" gmadl_model_sweep_results.append(parameter_sweep(\n",
" in_sample[data_part-PADDING:],\n",
" ModelGmadlPredictionsStrategy,\n",
" params,\n",
" params_filter=MODEL_GMADL_LOSS_FILTER,\n",
" padding=PADDING,\n",
" interval=INTERVAL,\n",
" sort_by=METRIC))\n",
" \n",
"\n",
"gmadl_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in gmadl_model_sweep_results]"
]
},
{
"cell_type": "code",
"execution_count": 111,
"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",
" 'gmadl_model': gmadl_model_best_strategies\n",
"}\n",
"\n",
"with open('cache/5min-best-strategies-04.pkl', 'wb') as outp:\n",
" pickle.dump(best_strategies, outp, pickle.HIGHEST_PROTOCOL)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualizations & Tables"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/5min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies = pickle.load(inpt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Tables with parameters"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lcccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{Fast Window Size} & \\textbf{Slow Window Size} & \\textbf{Signal Window Size} & \\textbf{Short sell} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tWindow 1 & 8 & 2584 & 987 & True \\\\\n",
"\t\t\tWindow 2 & 377 & 610 & 610 & True \\\\\n",
"\t\t\tWindow 3 & 987 & 2584 & 987 & True \\\\\n",
"\t\t\tWindow 4 & 610 & 2584 & 987 & True \\\\\n",
"\t\t\tWindow 5 & 987 & 1597 & 987 & False \\\\\n",
"\t\t\tWindow 6 & 1597 & 2584 & 377 & False \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"# Best hparams for MACD strategy\n",
"table_macd_params = Texttable()\n",
"table_macd_params.set_deco(Texttable.HEADER)\n",
"table_macd_params.set_cols_align([\"l\", \"c\", \"c\", \"c\", \"c\"])\n",
"table_macd_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{Fast Window Size}\",\n",
" \"\\\\textbf{Slow Window Size}\",\n",
" \"\\\\textbf{Signal Window Size}\",\n",
" \"\\\\textbf{Short sell}\"\n",
"])\n",
"\n",
"for i, macd_strategy in enumerate(best_strategies['macd_strategies']):\n",
" macd_strategy_info = macd_strategy[0].info()\n",
" table_macd_params.add_row([\n",
" f\"Window {i+1}\",\n",
" macd_strategy_info['fast_window_size'],\n",
" macd_strategy_info['slow_window_size'],\n",
" macd_strategy_info['signal_window_size'],\n",
" macd_strategy_info['short_sell']\n",
" ])\n",
"print(latextable.draw_latex(table_macd_params))"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lccccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{window}} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit long}} & \\textbf{\\textit{enter short}} & \\textbf{\\textit{exit short}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-5min & 21 & 80 & - & 25 & - \\\\\n",
"\t\t\tW2-5min & 34 & 75 & - & 25 & - \\\\\n",
"\t\t\tW3-5min & 5 & 95 & - & 15 & - \\\\\n",
"\t\t\tW4-5min & 8 & 90 & 5 & - & 85 \\\\\n",
"\t\t\tW5-5min & 13 & 90 & 20 & - & 85 \\\\\n",
"\t\t\tW6-5min & 21 & 85 & 15 & - & - \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_rsi_params = Texttable()\n",
"table_rsi_params.set_deco(Texttable.HEADER)\n",
"table_rsi_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\"])\n",
"table_rsi_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{window}}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit long}}\",\n",
" \"\\\\textbf{\\\\textit{enter short}}\",\n",
" \"\\\\textbf{\\\\textit{exit short}}\"\n",
"])\n",
"\n",
"for i, rsi_strategy in enumerate(best_strategies['rsi_strategies']):\n",
" rsi_strategy_info = rsi_strategy[0].info()\n",
" table_rsi_params.add_row([\n",
" f\"W{i+1}-5min\",\n",
" rsi_strategy_info['window_size'] or '-',\n",
" rsi_strategy_info['enter_long'] or '-',\n",
" rsi_strategy_info['exit_long'] or '-',\n",
" rsi_strategy_info['enter_short'] or '-',\n",
" rsi_strategy_info['exit_short'] or '-'\n",
" ])\n",
"print(latextable.draw_latex(table_rsi_params))"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lccccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit Long}} & \\textbf{\\textit{enter Short}} & \\textbf{\\textit{exit Short}} & \\textbf{\\textit{threshold}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-5min & - & 0.970 & 0.980 & 0.970 & 0.003 \\\\\n",
"\t\t\tW2-5min & 0.970 & - & 0.950 & - & 0.003 \\\\\n",
"\t\t\tW3-5min & 0.990 & - & 0.950 & - & 0.002 \\\\\n",
"\t\t\tW4-5min & 0.970 & 0.990 & - & 0.900 & 0.003 \\\\\n",
"\t\t\tW5-5min & 0.900 & 0.900 & - & - & 0.002 \\\\\n",
"\t\t\tW6-5min & 0.950 & 0.950 & - & - & 0.003 \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_quantile_params = Texttable()\n",
"table_quantile_params.set_deco(Texttable.HEADER)\n",
"table_quantile_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\", \"c\"])\n",
"table_quantile_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit Long}}\",\n",
" \"\\\\textbf{\\\\textit{enter Short}}\",\n",
" \"\\\\textbf{\\\\textit{exit Short}}\",\n",
" \"\\\\textbf{\\\\textit{threshold}}\"\n",
"])\n",
"\n",
"for i, quantile_strategy in enumerate(best_strategies['quantile_model']):\n",
" quantile_strategy_info = quantile_strategy[0].info()\n",
" table_quantile_params.add_row([\n",
" f\"W{i+1}-{INTERVAL}\",\n",
" quantile_strategy_info['quantile_enter_long'] or '-',\n",
" quantile_strategy_info['quantile_exit_long'] or '-',\n",
" quantile_strategy_info['quantile_enter_short'] or '-',\n",
" quantile_strategy_info['quantile_exit_short'] or '-',\n",
" quantile_strategy_info['exchange_fee']\n",
" ])\n",
"print(latextable.draw_latex(table_quantile_params))"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{table}\n",
"\t\\begin{center}\n",
"\t\t\\begin{tabular}{lcccc}\n",
"\t\t\t\\textbf{Window} & \\textbf{\\textit{enter long}} & \\textbf{\\textit{exit Long}} & \\textbf{\\textit{enter Short}} & \\textbf{\\textit{exit Short}} \\\\\n",
"\t\t\t\\hline\n",
"\t\t\tW1-5min & 0.004 & - & -0.005 & - \\\\\n",
"\t\t\tW2-5min & 0.002 & - & -0.001 & - \\\\\n",
"\t\t\tW3-5min & - & - & -0.006 & 0.003 \\\\\n",
"\t\t\tW4-5min & 0.002 & - & -0.005 & - \\\\\n",
"\t\t\tW5-5min & 0.002 & - & -0.003 & - \\\\\n",
"\t\t\tW6-5min & 0.001 & - & -0.007 & - \\\\\n",
"\t\t\\end{tabular}\n",
"\t\\end{center}\n",
"\\end{table}\n"
]
}
],
"source": [
"table_gmadl_params = Texttable()\n",
"table_gmadl_params.set_deco(Texttable.HEADER)\n",
"table_gmadl_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\"])\n",
"table_gmadl_params.header([\n",
" \"\\\\textbf{Window}\",\n",
" \"\\\\textbf{\\\\textit{enter long}}\",\n",
" \"\\\\textbf{\\\\textit{exit Long}}\",\n",
" \"\\\\textbf{\\\\textit{enter Short}}\",\n",
" \"\\\\textbf{\\\\textit{exit Short}}\",\n",
"])\n",
"\n",
"for i, gmadl_strategy in enumerate(best_strategies['gmadl_model']):\n",
" gmadl_strategy_info = gmadl_strategy[0].info()\n",
" table_gmadl_params.add_row([\n",
" f\"W{i+1}-{INTERVAL}\",\n",
" gmadl_strategy_info['enter_long'] or '-',\n",
" gmadl_strategy_info['exit_long'] or '-',\n",
" gmadl_strategy_info['enter_short'] or '-',\n",
" gmadl_strategy_info['exit_short'] or '-'\n",
" ])\n",
"print(latextable.draw_latex(table_gmadl_params))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Evaluation on the windows"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"def results_plot(idx, result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model, width=850, height=500, notitle=False):\n",
"\n",
" fig = go.Figure([\n",
" go.Scatter(y=result_buyandhold['portfolio_value'], x=result_buyandhold['time'], name=\"Buy and Hold\"),\n",
" go.Scatter(y=result_macd['portfolio_value'], x=result_macd['time'], name=\"MACD Strategy\"),\n",
" go.Scatter(y=result_rsi['portfolio_value'], x=result_rsi['time'], name=\"RSI Strategy\"),\n",
" go.Scatter(y=result_quantile_model['portfolio_value'], x=result_quantile_model['time'], name='Quantile Informer Strategy'),\n",
" go.Scatter(y=result_gmadl_model['portfolio_value'], x=result_gmadl_model['time'], name='GMADL Informer Strategy')\n",
" ])\n",
" fig.update_layout(\n",
" title={\n",
" 'text': f\"W{idx}-{INTERVAL}\",\n",
" 'y':0.97,\n",
" 'x':0.5,\n",
" 'xanchor': 'center',\n",
" 'yanchor': 'top'} if not notitle else None,\n",
" yaxis_title=\"Portfolio Value\",\n",
" xaxis_title=\"Date\",\n",
" font=dict(\n",
" # family=\"Courier New, monospace\",\n",
" size=14,\n",
" ),\n",
" autosize=False,\n",
" width=width,\n",
" height=height,\n",
" margin=dict(l=20, r=20, t=20 if notitle else 110, b=20),\n",
" plot_bgcolor='white',\n",
" legend=dict(\n",
" orientation=\"h\",\n",
" yanchor=\"bottom\",\n",
" y=1.02,\n",
" xanchor=\"left\",\n",
" x=0.02\n",
" )\n",
" )\n",
" fig.update_xaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.update_yaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.write_image(f\"images/eval-w{idx}-{INTERVAL}.png\")\n",
" fig.show()\n",
" \n",
"def results_table(result_buyandhold, result_macd, result_rsi, result_quantile_model, result_gmadl_model):\n",
" 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",
"\n",
" table_eval_windows.header([\n",
" \"\\\\textbf{Strategy}\",\n",
" \"\\\\textbf{VAL}\",\n",
" \"\\\\textbf{ARC}\",\n",
" \"\\\\textbf{ASD}\",\n",
" \"\\\\textbf{IR*}\",\n",
" \"\\\\textbf{MD}\",\n",
" \"\\\\textbf{IR**}\",\n",
" \"\\\\textbf{N}\",\n",
" \"\\\\textbf{LONG}\",\n",
" \"\\\\textbf{SHORT}\",\n",
" ])\n",
"\n",
" strategy_name_result = [\n",
" ('Buy and Hold', result_buyandhold),\n",
" ('MACD Strategy', result_macd),\n",
" ('RSI Strategy', result_rsi),\n",
" ('Quantile Informer', result_quantile_model),\n",
" ('GMADL Informer', result_gmadl_model)\n",
" ]\n",
" for strategy_name, result in strategy_name_result:\n",
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" result['ir'],\n",
" result['md'],\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# for i, (in_sample, out_of_sample) in enumerate(data_windows):\n",
"# padded_window = pd.concat([in_sample.iloc[-PADDING:], out_of_sample])\n",
"# result_buyandhold = evaluate_strategy(padded_window, best_strategies['buy_and_hold'][i], padding=PADDING, interval=INTERVAL)\n",
"# result_macd = evaluate_strategy(padded_window, [s[0] for s in best_strategies['macd_strategies']][i], padding=PADDING, interval=INTERVAL)\n",
"# result_rsi = evaluate_strategy(padded_window, [s[0] for s in best_strategies['rsi_strategies']][i], padding=PADDING, interval=INTERVAL)\n",
"# 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",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 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)\n",
"# macd_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['macd_strategies']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# rsi_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['rsi_strategies']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# quantile_model_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['quantile_model']], padding=PADDING), padding=PADDING, interval=INTERVAL)\n",
"# 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",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "wnemsc",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

100610
notebooks/gmadl.ipynb Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,535 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import plotly.figure_factory as ff\n",
"import pandas as pd\n",
"import pickle\n",
"import plotly.graph_objs as go\n",
"import latextable\n",
"from texttable import Texttable\n",
"from strategy.strategy import (\n",
" BuyAndHoldStrategy,\n",
" MACDStrategy,\n",
" RSIStrategy,\n",
" ModelQuantilePredictionsStrategy,\n",
" ModelGmadlPredictionsStrategy,\n",
" ConcatenatedStrategies\n",
")\n",
"from strategy.util import (\n",
" get_data_windows,\n",
" get_sweep_window_predictions,\n",
" get_predictions_dataframe\n",
")\n",
"from strategy.evaluation import (\n",
" parameter_sweep,\n",
" evaluate_strategy\n",
")\n",
"from strategy.plotting import (\n",
" plot_sweep_results\n",
")\n",
"\n",
"PADDING=5000\n",
"VALID_PART=0.2"
]
},
{
"cell_type": "code",
"execution_count": 48,
"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.9\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",
"\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"
]
}
],
"source": [
"data_windows_5min = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-5m:latest',\n",
" min_window=0, \n",
" max_window=5\n",
")\n",
"\n",
"data_windows_15min = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-15m:latest',\n",
" min_window=0, \n",
" max_window=5\n",
")\n",
"\n",
"data_windows_30min = get_data_windows(\n",
" 'wne-masters-thesis-testing',\n",
" 'btc-usdt-30m:latest',\n",
" min_window=0, \n",
" max_window=5\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/5min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies_5min = pickle.load(inpt)\n",
"\n",
"with open('cache/15min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies_15min = pickle.load(inpt)\n",
"\n",
"with open('cache/30min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies_30min = pickle.load(inpt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Best 5 strategies evaluation"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def results_plot(buy_and_hold_concat, gmadl_5min_model_concat, rsi_5_min_concat, gmadl_15min_model_concat, rsi_30min_concat, macd_30min_concat, width=850, height=500, notitle=False):\n",
"\n",
" fig = go.Figure([\n",
" 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=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",
" fig.update_layout(\n",
" # title={\n",
" # 'text': f\"W{idx}-{INTERVAL}\",\n",
" # 'y':0.97,\n",
" # 'x':0.5,\n",
" # 'xanchor': 'center',\n",
" # 'yanchor': 'top'} if not notitle else None,\n",
" yaxis_title=\"Portfolio Value\",\n",
" xaxis_title=\"Date\",\n",
" font=dict(\n",
" # family=\"Courier New, monospace\",\n",
" size=14,\n",
" ),\n",
" autosize=False,\n",
" width=width,\n",
" height=height,\n",
" margin=dict(l=20, r=20, t=20, b=20),\n",
" plot_bgcolor='white',\n",
" legend=dict(\n",
" orientation=\"h\",\n",
" yanchor=\"bottom\",\n",
" y=1.02,\n",
" xanchor=\"left\",\n",
" x=0.02\n",
" )\n",
" )\n",
" fig.update_xaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.update_yaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" # fig.write_image(f\"images/eval-w{idx}-{INTERVAL}.png\")\n",
" fig.show()\n",
" \n",
"def results_table(buy_and_hold_concat, gmadl_5min_model_concat, rsi_5_min_concat, gmadl_15min_model_concat, rsi_30min_concat, macd_30min_concat):\n",
" 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",
"\n",
" table_eval_windows.header([\n",
" \"\\\\textbf{Strategy}\",\n",
" \"\\\\textbf{VAL}\",\n",
" \"\\\\textbf{ARC}\",\n",
" \"\\\\textbf{ASD}\",\n",
" \"\\\\textbf{IR*}\",\n",
" \"\\\\textbf{MD}\",\n",
" \"\\\\textbf{IR**}\",\n",
" \"\\\\textbf{N}\",\n",
" \"\\\\textbf{LONG}\",\n",
" \"\\\\textbf{SHORT}\",\n",
" ])\n",
"\n",
" strategy_name_result = [\n",
" ('Buy and Hold', buy_and_hold_concat),\n",
" ('GMADL Informer (5min)', gmadl_5min_model_concat),\n",
" ('RSI Strategy (5min)', rsi_5_min_concat),\n",
" ('GMADL Informer (15min)', gmadl_15min_model_concat),\n",
" ('RSI Strategy (30min)', rsi_30min_concat),\n",
" ('MACD Strategy (30min)', macd_30min_concat)\n",
" ]\n",
" for strategy_name, result in strategy_name_result:\n",
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" result['ir'],\n",
" result['md'],\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))"
]
},
{
"cell_type": "code",
"execution_count": 1,
"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_concat = evaluate_strategy(test_data_5min, BuyAndHoldStrategy(), padding=PADDING, interval='5min')\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')\n",
"\n",
"# results_table(buy_and_hold_concat, gmadl_5min_model_concat, rsi_5_min_concat, gmadl_15min_model_concat, rsi_30min_concat, macd_30min_concat)\n",
"# 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": [
"## TOP N Strategies Sensistivity Analisys"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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",
"\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",
"\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",
"# results_for_strats(data_windows_30min, best_strategies_30min, interval='30min', top_n=10) \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Validation size for: GMADL Informer (5min)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/5min-best-strategies-01.pkl', 'rb') as inpt:\n",
" best_strategies_5min_01 = pickle.load(inpt)\n",
"\n",
"with open('cache/5min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies_5min_02 = pickle.load(inpt)\n",
"\n",
"with open('cache/5min-best-strategies-03.pkl', 'rb') as inpt:\n",
" best_strategies_5min_03 = pickle.load(inpt)\n",
"\n",
"with open('cache/5min-best-strategies-04.pkl', 'rb') as inpt:\n",
" best_strategies_5min_04 = pickle.load(inpt)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'strategy_name': 'Unknown model', 'future': 1, 'target': 'close_price', 'enter_long': 0.004, 'exit_long': None, 'enter_short': -0.005, 'exit_short': None}\n",
"{'strategy_name': 'Unknown model', 'future': 1, 'target': 'close_price', 'enter_long': 0.007, 'exit_long': -0.003, 'enter_short': None, 'exit_short': None}\n"
]
}
],
"source": [
"# print(best_strategies_5min_02['gmadl_model'][0][0].info())\n",
"# print(best_strategies_5min_03['gmadl_model'][0][0].info())"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"def results_plot2(result_buyandhold, results, names, width=850, height=500):\n",
"\n",
" fig = go.Figure([\n",
" go.Scatter(y=result_buyandhold['portfolio_value'], x=result_buyandhold['time'], name=\"Buy and Hold\")] +[\n",
" go.Scatter(y=result['portfolio_value'], x=result['time'], name=name) for name, result in zip(names, results)\n",
" ])\n",
" # go.Scatter(y=result_macd['portfolio_value'], x=result_macd['time'], name=\"MACD Strategy\"),\n",
" # go.Scatter(y=result_rsi['portfolio_value'], x=result_rsi['time'], name=\"RSI Strategy\"),\n",
" # go.Scatter(y=result_quantile_model['portfolio_value'], x=result_quantile_model['time'], name='Quantile Informer Strategy'),\n",
" # go.Scatter(y=result_gmadl_model['portfolio_value'], x=result_gmadl_model['time'], name='GMADL Informer Strategy')\n",
" # ])\n",
" fig.update_layout(\n",
" yaxis_title=\"Portfolio Value\",\n",
" xaxis_title=\"Date\",\n",
" font=dict(\n",
" # family=\"Courier New, monospace\",\n",
" size=14,\n",
" ),\n",
" autosize=False,\n",
" width=width,\n",
" height=height,\n",
" margin=dict(l=20, r=20, t=20, b=20),\n",
" plot_bgcolor='white',\n",
" legend=dict(\n",
" orientation=\"h\",\n",
" yanchor=\"bottom\",\n",
" y=1.02,\n",
" xanchor=\"left\",\n",
" x=0.02\n",
" )\n",
" )\n",
" fig.update_xaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.update_yaxes(\n",
" mirror=True,\n",
" ticks='outside',\n",
" showline=True,\n",
" linecolor='black',\n",
" gridcolor='lightgrey'\n",
" )\n",
" fig.show()\n",
" \n",
"def results_table2(result_buyandhold, results, names):\n",
" 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",
"\n",
" table_eval_windows.header([\n",
" \"\\\\textbf{Strategy}\",\n",
" \"\\\\textbf{VAL}\",\n",
" \"\\\\textbf{ARC}\",\n",
" \"\\\\textbf{ASD}\",\n",
" \"\\\\textbf{IR*}\",\n",
" \"\\\\textbf{MD}\",\n",
" \"\\\\textbf{IR**}\",\n",
" \"\\\\textbf{N}\",\n",
" \"\\\\textbf{LONG}\",\n",
" \"\\\\textbf{SHORT}\",\n",
" ])\n",
"\n",
" strategy_name_result = [\n",
" ('Buy and Hold', result_buyandhold)] + list(zip(names, results))\n",
" for strategy_name, result in strategy_name_result:\n",
" table_eval_windows.add_row([\n",
" strategy_name,\n",
" result['value'],\n",
" result['arc'],\n",
" result['asd'],\n",
" result['ir'],\n",
" result['md'],\n",
" result['mod_ir'],\n",
" result['n_trades'],\n",
" f\"{result['long_pos']*100:.2f}\\%\",\n",
" f\"{result['short_pos']*100:.2f}\\%\",\n",
" ])\n",
" print(latextable.draw_latex(table_eval_windows))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"def results_for_strats(data_windows, best_strategies, names, interval='5min', strat_name='gmadl_model'):\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, interval=interval)\n",
" \n",
" evaluation_results = []\n",
" for best_strat in best_strategies:\n",
" evaluation_results.append(\n",
" evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strat[strat_name]], padding=PADDING), padding=PADDING, interval=interval)\n",
" )\n",
" results_plot(buy_and_hold_concat, evaluation_results, names, width=1200)\n",
" results_table2(buy_and_hold_concat, evaluation_results, names)\n",
"\n",
"\n",
"# results_for_strats(data_windows_5min, [\n",
"# best_strategies_5min_01,\n",
"# best_strategies_5min_02,\n",
"# best_strategies_5min_03,\n",
"# best_strategies_5min_04\n",
"# ], [\n",
"# \"3 months\",\n",
"# \"6 months\",\n",
"# \"9 months\",\n",
"# \"12 months\",\n",
"# ])\n"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"with open('cache/30min-best-strategies-01.pkl', 'rb') as inpt:\n",
" best_strategies_30min_01 = pickle.load(inpt)\n",
"\n",
"with open('cache/30min-best-strategies.pkl', 'rb') as inpt:\n",
" best_strategies_30min_02 = pickle.load(inpt)\n",
"\n",
"with open('cache/30min-best-strategies-03.pkl', 'rb') as inpt:\n",
" best_strategies_30min_03 = pickle.load(inpt)\n",
"\n",
"with open('cache/30min-best-strategies-04.pkl', 'rb') as inpt:\n",
" best_strategies_30min_04 = pickle.load(inpt)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# results_for_strats(data_windows_30min, [\n",
"# best_strategies_30min_01,\n",
"# best_strategies_30min_02,\n",
"# best_strategies_30min_03,\n",
"# best_strategies_30min_04\n",
"# ], [\n",
"# \"3 months\",\n",
"# \"6 months\",\n",
"# \"9 months\",\n",
"# \"12 months\",\n",
"# ], strat_name='rsi_strategies', interval='30min')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# test_data = pd.concat([data_window[0] for data_window in data_windows_5min[:1]])\n",
"# buy_and_hold_concat = evaluate_strategy(test_data, BuyAndHoldStrategy(), padding=PADDING)\n",
"\n",
"# res = evaluate_strategy(test_data, ConcatenatedStrategies(1, [s[0] for s in best_strategies_5min_02['gmadl_model'][:1]], padding=PADDING), padding=PADDING, interval='5min')\n",
"# results_plot(buy_and_hold_concat, [res], width=1200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "wnemsc",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}