1122 lines
50 KiB
Plaintext
1122 lines
50 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"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.2\n",
|
|
"INTERVAL='30min'\n",
|
|
"METRIC='mod_ir'\n",
|
|
"TOP_N=10"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Failed to detect the name of this notebook, you can set it manually with the WANDB_NOTEBOOK_NAME environment variable to enable code saving.\n",
|
|
"\u001b[34m\u001b[1mwandb\u001b[0m: Downloading large artifact btc-usdt-30m:latest, 124.19MB. 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-30m:latest',\n",
|
|
" min_window=0, \n",
|
|
" max_window=5\n",
|
|
")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"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": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"buyandhold_best_strategies = [BuyAndHoldStrategy() for _ in data_windows] "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"100%|██████████| 3840/3840 [00:05<00:00, 762.16it/s] \n",
|
|
"100%|██████████| 3840/3840 [00:04<00:00, 937.71it/s] \n",
|
|
"100%|██████████| 3840/3840 [00:03<00:00, 990.52it/s] \n",
|
|
"100%|██████████| 3840/3840 [00:04<00:00, 956.93it/s] \n",
|
|
"100%|██████████| 3840/3840 [00:04<00:00, 931.01it/s] \n",
|
|
"100%|██████████| 3840/3840 [00:04<00:00, 951.89it/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": 6,
|
|
"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:13<00:00, 838.13it/s]\n",
|
|
"100%|██████████| 11088/11088 [00:10<00:00, 1054.57it/s]\n",
|
|
"100%|██████████| 11088/11088 [00:10<00:00, 1032.31it/s]\n",
|
|
"100%|██████████| 11088/11088 [00:11<00:00, 967.27it/s] \n",
|
|
"100%|██████████| 11088/11088 [00:11<00:00, 995.52it/s] \n",
|
|
"100%|██████████| 11088/11088 [00:10<00:00, 1023.57it/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": 8,
|
|
"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": 9,
|
|
"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": 10,
|
|
"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 rmse loss\n",
|
|
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/j9d5r6tg'\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": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.99it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 58.14it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.39it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.46it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:22<00:00, 52.43it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.39it/s]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"MODEL_RMSE_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",
|
|
"rmse_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",
|
|
" '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",
|
|
" rmse_model_sweep_results.append(parameter_sweep(\n",
|
|
" in_sample[data_part-PADDING:],\n",
|
|
" ModelGmadlPredictionsStrategy,\n",
|
|
" params,\n",
|
|
" params_filter=MODEL_RMSE_LOSS_FILTER,\n",
|
|
" padding=PADDING,\n",
|
|
" interval=INTERVAL,\n",
|
|
" sort_by=METRIC))\n",
|
|
" \n",
|
|
"\n",
|
|
"rmse_model_best_strategies = [[strat for _, strat in results[:TOP_N]] for results in rmse_model_sweep_results]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# plot_sweep_results(pd.DataFrame([result for result, _ in rmse_model_sweep_results[0]]), parameters=['enter_long', 'exit_long', 'enter_short', 'exit_short'], round=5)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"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": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"100%|██████████| 1125/1125 [00:23<00:00, 47.74it/s]\n",
|
|
"100%|██████████| 1125/1125 [00:23<00:00, 48.10it/s]\n",
|
|
"100%|██████████| 1125/1125 [00:23<00:00, 47.79it/s]\n",
|
|
"100%|██████████| 1125/1125 [00:23<00:00, 47.27it/s]\n",
|
|
"100%|██████████| 1125/1125 [00:23<00:00, 47.82it/s]\n",
|
|
"100%|██████████| 1125/1125 [00:23<00:00, 47.74it/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": 14,
|
|
"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": 5,
|
|
"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' (old)\n",
|
|
"# SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/7x42xn5j'\n",
|
|
"SWEEP_ID = 'filipstefaniuk/wne-masters-thesis-testing/z776cpvj'\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": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 58.39it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.42it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.34it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.48it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 58.43it/s]\n",
|
|
"100%|██████████| 1176/1176 [00:20<00:00, 57.45it/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": 7,
|
|
"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",
|
|
" 'rmse_model': rmse_model_best_strategies,\n",
|
|
" 'quantile_model': quantile_model_best_strategies,\n",
|
|
" 'gmadl_model': gmadl_model_best_strategies\n",
|
|
"}\n",
|
|
"\n",
|
|
"with open('cache/30min-best-strategies-v6.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": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"with open('cache/30min-best-strategies-v6.pkl', 'rb') as inpt:\n",
|
|
" best_strategies = pickle.load(inpt)\n",
|
|
" buyandhold_best_strategies = best_strategies['buy_and_hold']\n",
|
|
" macd_best_strategies = best_strategies['macd_strategies']\n",
|
|
" rsi_best_strategies = best_strategies['rsi_strategies']\n",
|
|
" rmse_model_best_strategies = best_strategies['rmse_model']\n",
|
|
" quantile_model_best_strategies = best_strategies['quantile_model']\n",
|
|
" gmadl_model_best_strategies = best_strategies['gmadl_model']"
|
|
]
|
|
},
|
|
{
|
|
"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": 4,
|
|
"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.002 & - & -0.003 & - \\\\\n",
|
|
"\t\t\tW2-30min & - & - & -0.002 & 0.001 \\\\\n",
|
|
"\t\t\tW3-30min & - & - & -0.001 & 0.002 \\\\\n",
|
|
"\t\t\tW4-30min & 0.001 & - & -0.002 & - \\\\\n",
|
|
"\t\t\tW5-30min & 0.001 & - & -0.002 & - \\\\\n",
|
|
"\t\t\tW6-30min & 0.001 & - & -0.002 & - \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"table_rmse_params = Texttable()\n",
|
|
"table_rmse_params.set_deco(Texttable.HEADER)\n",
|
|
"table_rmse_params.set_cols_align([\"l\", \"c\",\"c\", \"c\", \"c\"])\n",
|
|
"table_rmse_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, rmse_strategy in enumerate(best_strategies['rmse_model']):\n",
|
|
" rmse_strategy_info = rmse_strategy[0].info()\n",
|
|
" table_rmse_params.add_row([\n",
|
|
" f\"W{i+1}-{INTERVAL}\",\n",
|
|
" rmse_strategy_info['enter_long'] or '-',\n",
|
|
" rmse_strategy_info['exit_long'] or '-',\n",
|
|
" rmse_strategy_info['enter_short'] or '-',\n",
|
|
" rmse_strategy_info['exit_short'] or '-'\n",
|
|
" ])\n",
|
|
"print(latextable.draw_latex(table_rmse_params))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"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.005 \\\\\n",
|
|
"\t\t\tW2-30min & - & -0.006 & -0.007 & 0.004 \\\\\n",
|
|
"\t\t\tW3-30min & - & - & -0.004 & 0.007 \\\\\n",
|
|
"\t\t\tW4-30min & 0.003 & -0.007 & - & - \\\\\n",
|
|
"\t\t\tW5-30min & 0.006 & - & -0.004 & - \\\\\n",
|
|
"\t\t\tW6-30min & 0.001 & - & -0.005 & - \\\\\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": 12,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def results_plot(idx, result_buyandhold, result_macd, result_rsi, result_rmse_model, result_quantile_model, result_gmadl_model, width=850, height=500, notitle=False, v_lines=None):\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_rmse_model['portfolio_value'], x=result_rmse_model['time'], name='RMSE Informer 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",
|
|
" \n",
|
|
" if v_lines:\n",
|
|
" for v_line in v_lines:\n",
|
|
" fig.add_shape(\n",
|
|
" go.layout.Shape(type=\"line\",\n",
|
|
" yref=\"paper\",\n",
|
|
" xref=\"x\",\n",
|
|
" x0=v_line,\n",
|
|
" x1=v_line,\n",
|
|
" y0=0,\n",
|
|
" y1=1,\n",
|
|
" line=dict(width=1.5, dash='dash', color='rgb(140,140,140)')))\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_rmse_model, 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",
|
|
" ('RMSE Informer', result_rmse_model),\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",
|
|
" f\"{result['arc']*100:.2f}\\%\",\n",
|
|
" f\"{result['asd']*100:.2f}\\%\",\n",
|
|
" result['ir'],\n",
|
|
" f\"{result['md']*100:.2f}\\%\",\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": 18,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 0.924 & -14.84\\% & 66.12\\% & -0.225 & 51.75\\% & -0.064 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 1.269 & 62.19\\% & 65.90\\% & 0.944 & 31.81\\% & 1.845 & 15 & 50.36\\% & 47.71\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 1.843 & 245.59\\% & 66.51\\% & 3.693 & 35.02\\% & 25.897 & 26 & 42.40\\% & 57.60\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 0.924 & -14.84\\% & 66.12\\% & -0.225 & 51.75\\% & -0.064 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 0.743 & -45.20\\% & 60.57\\% & -0.746 & 29.11\\% & -1.159 & 443 & 30.43\\% & 53.98\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 1.030 & 6.23\\% & 19.98\\% & 0.312 & 10.01\\% & 0.194 & 39 & 0.00\\% & 8.72\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n",
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 0.548 & -70.51\\% & 72.38\\% & -0.974 & 63.18\\% & -1.087 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 1.303 & 70.93\\% & 71.82\\% & 0.988 & 36.06\\% & 1.943 & 19 & 57.32\\% & 40.76\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 1.703 & 194.24\\% & 72.36\\% & 2.684 & 34.89\\% & 14.945 & 106 & 39.96\\% & 60.04\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 1.387 & 94.09\\% & 41.55\\% & 2.264 & 16.34\\% & 13.037 & 8 & 0.00\\% & 34.41\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 1.403 & 98.74\\% & 27.62\\% & 3.575 & 9.44\\% & 37.393 & 255 & 0.00\\% & 18.79\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 1.050 & 10.44\\% & 23.40\\% & 0.446 & 14.45\\% & 0.322 & 90 & 0.00\\% & 5.98\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n",
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 1.018 & 3.75\\% & 51.25\\% & 0.073 & 37.47\\% & 0.007 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 0.673 & -55.16\\% & 51.01\\% & -1.081 & 59.24\\% & -1.007 & 95 & 81.60\\% & 16.48\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 0.988 & -2.50\\% & 38.04\\% & -0.066 & 30.42\\% & -0.005 & 238 & 2.34\\% & 50.54\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 0.980 & -4.00\\% & 51.36\\% & -0.078 & 34.51\\% & -0.009 & 2 & 0.00\\% & 100.00\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 0.884 & -22.19\\% & 23.41\\% & -0.948 & 18.39\\% & -1.144 & 171 & 0.00\\% & 18.83\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 0.737 & -46.11\\% & 42.58\\% & -1.083 & 42.71\\% & -1.169 & 302 & 0.00\\% & 81.21\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n",
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 1.229 & 51.90\\% & 45.01\\% & 1.153 & 21.74\\% & 2.753 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 1.031 & 6.45\\% & 45.08\\% & 0.143 & 30.15\\% & 0.031 & 94 & 35.18\\% & 64.82\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 1.010 & 2.03\\% & 1.87\\% & 1.084 & 0.30\\% & 7.410 & 4 & 0.02\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 1.414 & 101.80\\% & 45.03\\% & 2.260 & 21.74\\% & 10.584 & 10 & 92.95\\% & 7.05\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 0.629 & -60.99\\% & 28.76\\% & -2.121 & 39.23\\% & -3.297 & 448 & 36.32\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 1.054 & 11.31\\% & 28.25\\% & 0.400 & 24.18\\% & 0.187 & 345 & 34.69\\% & 0.00\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n",
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 1.439 & 109.30\\% & 41.49\\% & 2.634 & 20.18\\% & 14.265 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 1.393 & 95.71\\% & 31.47\\% & 3.041 & 15.00\\% & 19.407 & 90 & 48.66\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 1.439 & 109.30\\% & 41.49\\% & 2.634 & 20.18\\% & 14.265 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 1.439 & 109.30\\% & 41.49\\% & 2.634 & 20.18\\% & 14.265 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 1.277 & 64.08\\% & 34.28\\% & 1.869 & 16.61\\% & 7.212 & 311 & 76.90\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 1.672 & 183.71\\% & 41.56\\% & 4.420 & 20.18\\% & 40.230 & 22 & 78.48\\% & 21.52\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n",
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 1.559 & 145.95\\% & 52.85\\% & 2.762 & 26.69\\% & 15.103 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 1.227 & 51.44\\% & 36.38\\% & 1.414 & 18.55\\% & 3.921 & 11 & 40.69\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 1 & 0.00\\% & 0.00\\% & 0 & 0.00\\% & 0 & 0 & 0.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 1.059 & 12.32\\% & 52.86\\% & 0.233 & 41.44\\% & 0.069 & 10 & 93.44\\% & 6.56\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 0.855 & -27.14\\% & 34.50\\% & -0.787 & 35.23\\% & -0.606 & 150 & 37.79\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 1.617 & 165.10\\% & 52.85\\% & 3.124 & 24.64\\% & 20.929 & 10 & 99.88\\% & 0.12\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n"
|
|
]
|
|
}
|
|
],
|
|
"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_rmse_model = evaluate_strategy(padded_window, [s[0] for s in best_strategies['rmse_model']][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_rmse_model, result_quantile_model, result_gmadl_model)\n",
|
|
" # results_plot(i+1, result_buyandhold, result_macd, result_rsi, result_rmse_model, 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": 19,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\\begin{table}\n",
|
|
"\t\\begin{center}\n",
|
|
"\t\t\\begin{tabular}{lccccccccc}\n",
|
|
"\t\t\t\\textbf{Strategy} & \\textbf{VAL} & \\textbf{ARC} & \\textbf{ASD} & \\textbf{IR*} & \\textbf{MD} & \\textbf{IR**} & \\textbf{N} & \\textbf{LONG} & \\textbf{SHORT} \\\\\n",
|
|
"\t\t\t\\hline\n",
|
|
"\t\t\tBuy and Hold & 1.440 & 13.12\\% & 55.95\\% & 0.235 & 77.20\\% & 0.040 & 2 & 100.00\\% & 0.00\\% \\\\\n",
|
|
"\t\t\tMACD Strategy & 1.952 & 25.37\\% & 52.36\\% & 0.485 & 59.24\\% & 0.207 & 327 & 52.30\\% & 28.30\\% \\\\\n",
|
|
"\t\t\tRSI Strategy & 4.542 & 66.77\\% & 46.25\\% & 1.444 & 39.91\\% & 2.415 & 377 & 30.79\\% & 28.03\\% \\\\\n",
|
|
"\t\t\tRMSE Informer & 2.727 & 40.37\\% & 50.47\\% & 0.800 & 51.75\\% & 0.624 & 34 & 64.40\\% & 24.67\\% \\\\\n",
|
|
"\t\t\tQuantile Informer & 0.629 & -14.51\\% & 36.91\\% & -0.393 & 55.09\\% & -0.104 & 1783 & 30.24\\% & 15.27\\% \\\\\n",
|
|
"\t\t\tGMADL Informer & 2.263 & 31.79\\% & 36.70\\% & 0.866 & 53.35\\% & 0.516 & 811 & 35.51\\% & 19.59\\% \\\\\n",
|
|
"\t\t\\end{tabular}\n",
|
|
"\t\\end{center}\n",
|
|
"\\end{table}\n"
|
|
]
|
|
}
|
|
],
|
|
"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",
|
|
"rmse_model_concat = evaluate_strategy(test_data, ConcatenatedStrategies(len(data_windows[0][1]), [s[0] for s in best_strategies['rmse_model']], 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",
|
|
"# v_lines=[data_window[1]['close_time'].iloc[-1] for data_window in data_windows][:-1]\n",
|
|
"\n",
|
|
"results_table(buy_and_hold_concat, macd_concat, rsi_concat, rmse_model_concat, quantile_model_concat, gmadl_model_concat)\n",
|
|
"# results_plot(0, buy_and_hold_concat, macd_concat, rsi_concat, rmse_model_concat, quantile_model_concat, gmadl_model_concat, width=1200, height=500, notitle=True)\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
|
|
}
|