457 lines
120 KiB
Plaintext
457 lines
120 KiB
Plaintext
|
|
{
|
|||
|
|
"cells": [
|
|||
|
|
{
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"id": "782ec73f",
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-11-07T07:55:54.364036Z",
|
|||
|
|
"start_time": "2025-11-07T07:55:54.346679Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"source": [
|
|||
|
|
"import sys\n",
|
|||
|
|
"\n",
|
|||
|
|
"if '/mnt/d/PyProject/NewQuant/' not in sys.path:\n",
|
|||
|
|
" sys.path.append('/mnt/d/PyProject/NewQuant/')\n",
|
|||
|
|
"\n",
|
|||
|
|
"%load_ext autoreload\n",
|
|||
|
|
"%autoreload 2\n",
|
|||
|
|
"\n"
|
|||
|
|
],
|
|||
|
|
"outputs": [
|
|||
|
|
{
|
|||
|
|
"name": "stdout",
|
|||
|
|
"output_type": "stream",
|
|||
|
|
"text": [
|
|||
|
|
"The autoreload extension is already loaded. To reload it, use:\n",
|
|||
|
|
" %reload_ext autoreload\n"
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"execution_count": 7
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"id": "298b8b26",
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-11-07T07:55:54.385222Z",
|
|||
|
|
"start_time": "2025-11-07T07:55:54.370043Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"source": [
|
|||
|
|
"import pandas as pd\n",
|
|||
|
|
"from datetime import datetime\n",
|
|||
|
|
"import itertools\n",
|
|||
|
|
"from typing import Dict, Any, List, Tuple, Optional\n",
|
|||
|
|
"import multiprocessing # 导入 multiprocessing 模块\n",
|
|||
|
|
"import math # 保留 math 导入,因为您的策略内部可能需要用到数学函数\n",
|
|||
|
|
"\n",
|
|||
|
|
"# 导入所有必要的模块\n",
|
|||
|
|
"# 请确保这些导入路径与您的项目结构相符\n",
|
|||
|
|
"from src.analysis.grid_search_analyzer import GridSearchAnalyzer\n",
|
|||
|
|
"from src.analysis.result_analyzer import ResultAnalyzer\n",
|
|||
|
|
"from src.common_utils import generate_parameter_range\n",
|
|||
|
|
"from src.data_manager import DataManager\n",
|
|||
|
|
"from src.backtest_engine import BacktestEngine\n",
|
|||
|
|
"# 导入策略类\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"import builtins\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"origin_print = print\n",
|
|||
|
|
"\n"
|
|||
|
|
],
|
|||
|
|
"outputs": [],
|
|||
|
|
"execution_count": 8
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"id": "76f9a2e9",
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-11-07T07:55:54.401394Z",
|
|||
|
|
"start_time": "2025-11-07T07:55:54.388328Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"source": [
|
|||
|
|
"\n",
|
|||
|
|
"# --- 单个回测任务函数 ---\n",
|
|||
|
|
"# 这个函数将在每个独立的进程中运行,因此它必须是自包含的\n"
|
|||
|
|
],
|
|||
|
|
"outputs": [],
|
|||
|
|
"execution_count": 9
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"id": "c0984689",
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-11-07T07:55:54.417453Z",
|
|||
|
|
"start_time": "2025-11-07T07:55:54.404008Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"source": [
|
|||
|
|
"\n",
|
|||
|
|
"def slient_print(*args):\n",
|
|||
|
|
" pass\n"
|
|||
|
|
],
|
|||
|
|
"outputs": [],
|
|||
|
|
"execution_count": 10
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-11-07T07:59:14.620530Z",
|
|||
|
|
"start_time": "2025-11-07T07:55:54.421507Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"source": [
|
|||
|
|
"from src.strategies.KalmanStrategy.utils import run_single_backtest\n",
|
|||
|
|
"from src.strategies.KalmanStrategy.KalmanStrategy2 import DualModeKalmanStrategy\n",
|
|||
|
|
"# --- 主执行块 ---\n",
|
|||
|
|
"# 这是多进程代码的入口点,必须在 'if __name__ == \"__main__\":' 保护块中\n",
|
|||
|
|
"# 确保 autoreload 启用 (在Jupyter Notebook中使用,纯Python脚本运行时可移除)\n",
|
|||
|
|
"# %load_ext autoreload\n",
|
|||
|
|
"# %autoreload 2\n",
|
|||
|
|
"\n",
|
|||
|
|
"from src.strategies.ValueMigrationStrategy.ValueMigrationStrategy import ValueMigrationStrategy\n",
|
|||
|
|
"\n",
|
|||
|
|
"# --- 全局配置 ---\n",
|
|||
|
|
"data_file_path = \"D:/PyProject/NewQuant/data/data/KQ_m@CZCE_MA/KQ_m@CZCE_MA_min15.csv\"\n",
|
|||
|
|
"\n",
|
|||
|
|
"\n",
|
|||
|
|
"initial_capital = 100000.0\n",
|
|||
|
|
"slippage_rate = 0.0000\n",
|
|||
|
|
"commission_rate = 0.0001\n",
|
|||
|
|
"global_config = {\n",
|
|||
|
|
" 'symbol': 'MA',\n",
|
|||
|
|
"}\n",
|
|||
|
|
"# 确保每个合约的tick_size在这里定义或获取\n",
|
|||
|
|
"RB_TICK_SIZE = 1.0 # 螺纹钢的最小变动单位\n",
|
|||
|
|
"\n",
|
|||
|
|
"# --- 定义参数网格 ---\n",
|
|||
|
|
"param1_name = \"kalman_measurement_noise\"\n",
|
|||
|
|
"param1_values = generate_parameter_range(start=0.01, end=5.01, step=0.5)\n",
|
|||
|
|
"param2_name = \"entry_threshold_atr\"\n",
|
|||
|
|
"param2_values = generate_parameter_range(start=0.1, end=3.1, step=0.3)\n",
|
|||
|
|
"optimization_metric = 'total_return'\n",
|
|||
|
|
"\n",
|
|||
|
|
"# 生成所有参数组合\n",
|
|||
|
|
"param_combinations = list(itertools.product(param1_values, param2_values))\n",
|
|||
|
|
"total_combinations = len(param_combinations)\n",
|
|||
|
|
"print(f\"总计 {total_combinations} 种参数组合需要回测。\")\n",
|
|||
|
|
"\n",
|
|||
|
|
"all_results: List[Dict[str, Any]] = []\n",
|
|||
|
|
"grid_results: List[Dict[str, Any]] = []\n",
|
|||
|
|
"\n",
|
|||
|
|
"# 准备传递给每个子进程的公共配置字典\n",
|
|||
|
|
"common_config_for_processes = {\n",
|
|||
|
|
" 'main_symbol': global_config['symbol'],\n",
|
|||
|
|
" 'symbol': global_config['symbol'],\n",
|
|||
|
|
" 'data_path': data_file_path,\n",
|
|||
|
|
" 'initial_capital': initial_capital,\n",
|
|||
|
|
" 'slippage_rate': slippage_rate,\n",
|
|||
|
|
" 'commission_rate': commission_rate,\n",
|
|||
|
|
" 'start_time': datetime(2022, 1, 1), # 回测起始时间\n",
|
|||
|
|
" 'end_time': datetime(2024, 1, 1), # 回测结束时间\n",
|
|||
|
|
" 'roll_over_mode': True, # 保持换月模式\n",
|
|||
|
|
" 'param1_name': param1_name,\n",
|
|||
|
|
" 'param2_name': param2_name,\n",
|
|||
|
|
" 'optimization_metric': optimization_metric,\n",
|
|||
|
|
" 'strategy': DualModeKalmanStrategy,\n",
|
|||
|
|
" 'order_direction': ['BUY', 'SELL'],\n",
|
|||
|
|
" 'stop_loss_tick': 5,\n",
|
|||
|
|
" 'strategy_mode': 'REVERSION'\n",
|
|||
|
|
"}\n",
|
|||
|
|
"\n",
|
|||
|
|
"# 确定要使用的进程数量 (通常是CPU核心数)\n",
|
|||
|
|
"num_processes = int(multiprocessing.cpu_count() * 0.6)\n",
|
|||
|
|
"if num_processes < 1:\n",
|
|||
|
|
" num_processes = 1\n",
|
|||
|
|
"\n",
|
|||
|
|
"print(f\"--- 启动多进程网格搜索,使用 {num_processes} 个进程 ---\")\n",
|
|||
|
|
"\n",
|
|||
|
|
"# builtins.print = slient_print\n",
|
|||
|
|
"\n",
|
|||
|
|
"# 创建一个进程池\n",
|
|||
|
|
"with multiprocessing.Pool(processes=num_processes) as pool:\n",
|
|||
|
|
" # 准备 run_single_backtest 函数的参数列表\n",
|
|||
|
|
" # starmap 需要一个可迭代对象,其中每个元素是传递给目标函数的参数元组\n",
|
|||
|
|
" args_for_starmap = [\n",
|
|||
|
|
" (combo, common_config_for_processes) for combo in param_combinations\n",
|
|||
|
|
" ]\n",
|
|||
|
|
"\n",
|
|||
|
|
" # 使用 starmap() 来并行执行 run_single_backtest 函数\n",
|
|||
|
|
" # starmap 是阻塞的,会等待所有任务完成并返回结果列表\n",
|
|||
|
|
" for i, result_entry in enumerate(pool.starmap(run_single_backtest, args_for_starmap)):\n",
|
|||
|
|
" if result_entry: # 确保结果不为空\n",
|
|||
|
|
" all_results.append(result_entry)\n",
|
|||
|
|
" # 仅将成功的(无错误的)结果添加到用于网格分析的列表中\n",
|
|||
|
|
" if 'error' not in result_entry:\n",
|
|||
|
|
" grid_results.append(\n",
|
|||
|
|
" {\n",
|
|||
|
|
" param1_name: result_entry.get(param1_name),\n",
|
|||
|
|
" param2_name: result_entry.get(param2_name),\n",
|
|||
|
|
" optimization_metric: result_entry.get(optimization_metric, 0.0),\n",
|
|||
|
|
" }\n",
|
|||
|
|
" )\n",
|
|||
|
|
" else:\n",
|
|||
|
|
" # 对于失败的组合,将其优化指标设置为一个特殊值,便于识别\n",
|
|||
|
|
" grid_results.append(\n",
|
|||
|
|
" {\n",
|
|||
|
|
" param1_name: result_entry.get(param1_name),\n",
|
|||
|
|
" param2_name: result_entry.get(param2_name),\n",
|
|||
|
|
" optimization_metric: float('-inf'), # 用负无穷表示失败\n",
|
|||
|
|
" 'error_mesMAge': result_entry['error']\n",
|
|||
|
|
" }\n",
|
|||
|
|
" )\n",
|
|||
|
|
"\n",
|
|||
|
|
"builtins.print = origin_print\n",
|
|||
|
|
"print(\"\\n--- 网格搜索回测完毕 ---\")"
|
|||
|
|
],
|
|||
|
|
"id": "a5f8ff3d084c2349",
|
|||
|
|
"outputs": [
|
|||
|
|
{
|
|||
|
|
"name": "stdout",
|
|||
|
|
"output_type": "stream",
|
|||
|
|
"text": [
|
|||
|
|
"总计 121 种参数组合需要回测。\n",
|
|||
|
|
"--- 启动多进程网格搜索,使用 12 个进程 ---\n",
|
|||
|
|
"\n",
|
|||
|
|
"--- 网格搜索回测完毕 ---\n"
|
|||
|
|
]
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"execution_count": 11
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"metadata": {
|
|||
|
|
"ExecuteTime": {
|
|||
|
|
"end_time": "2025-11-07T07:59:14.871144Z",
|
|||
|
|
"start_time": "2025-11-07T07:59:14.632051Z"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"cell_type": "code",
|
|||
|
|
"source": [
|
|||
|
|
"\n",
|
|||
|
|
"# --- 5. 后处理和最佳结果选择 ---\n",
|
|||
|
|
"if all_results:\n",
|
|||
|
|
" results_df = pd.DataFrame(all_results)\n",
|
|||
|
|
" # print(\"\\n--- 所有回测结果汇总 ---\")\n",
|
|||
|
|
" # # 确保打印时浮点数格式化\n",
|
|||
|
|
" # pd.set_option('display.float_format', lambda x: '%.4f' % x)\n",
|
|||
|
|
" # print(results_df.to_string())\n",
|
|||
|
|
"\n",
|
|||
|
|
" # 找到最佳组合 (排除有错误的)\n",
|
|||
|
|
" # 过滤掉包含 'error' 键的行,或者 'error' 键的值不为空的行\n",
|
|||
|
|
" # 同时确保优化指标是数值,并且不为无穷大\n",
|
|||
|
|
" print(results_df.info())\n",
|
|||
|
|
" successful_results_df = results_df[(pd.to_numeric(results_df[optimization_metric], errors='coerce').notna()) &\n",
|
|||
|
|
" (pd.to_numeric(results_df[optimization_metric], errors='coerce') != float(\n",
|
|||
|
|
" '-inf'))\n",
|
|||
|
|
" ].copy() # 使用 .copy() 避免 SettingWithCopyWarning\n",
|
|||
|
|
"\n",
|
|||
|
|
" if not successful_results_df.empty and optimization_metric in successful_results_df.columns:\n",
|
|||
|
|
" # 确保优化指标列是数值类型\n",
|
|||
|
|
" successful_results_df[optimization_metric] = pd.to_numeric(successful_results_df[optimization_metric],\n",
|
|||
|
|
" errors='coerce')\n",
|
|||
|
|
"\n",
|
|||
|
|
" if not successful_results_df.empty and optimization_metric in successful_results_df.columns:\n",
|
|||
|
|
" # 过滤掉NaN值,如果所有夏普比率都是NaN,则可能没有有效结果\n",
|
|||
|
|
" normal_results = successful_results_df[\n",
|
|||
|
|
" (results_df['total_trades'] > 100)\n",
|
|||
|
|
" # &\n",
|
|||
|
|
" # (results_df['profit_factor'] < 3.)\n",
|
|||
|
|
" ]\n",
|
|||
|
|
" if len(normal_results) > 0:\n",
|
|||
|
|
" best_result = normal_results.loc[(normal_results[optimization_metric].idxmax())]\n",
|
|||
|
|
" print(f\"\\n--- 最优参数组合 (按{optimization_metric}) ---\")\n",
|
|||
|
|
" print(best_result)\n",
|
|||
|
|
" else:\n",
|
|||
|
|
" print('ERROR!!!!!!!!!!!!!!!!!!!!')\n",
|
|||
|
|
"\n",
|
|||
|
|
" # 找到最大值的索引\n",
|
|||
|
|
" # best_result = successful_results_df.loc[successful_results_df[optimization_metric].idxmax()]\n",
|
|||
|
|
" # print(f\"\\n--- 最优参数组合 (按 {optimization_metric}) ---\")\n",
|
|||
|
|
" # print(best_result)\n",
|
|||
|
|
"\n",
|
|||
|
|
" # 导出到CSV\n",
|
|||
|
|
" output_filename = f\"grid_search_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv\"\n",
|
|||
|
|
" # results_df.to_csv(output_filename, index=False, encoding='utf-8')\n",
|
|||
|
|
" # print(f\"\\n所有结果已导出到: {output_filename}\")\n",
|
|||
|
|
"\n",
|
|||
|
|
" # 打印枢轴表\n",
|
|||
|
|
" grid_df = pd.DataFrame(grid_results)\n",
|
|||
|
|
" # 确保优化指标列是数值类型,非数值的(如 -inf)在pandas中可能被正确处理\n",
|
|||
|
|
" grid_df[optimization_metric] = pd.to_numeric(grid_df[optimization_metric], errors='coerce')\n",
|
|||
|
|
"\n",
|
|||
|
|
" pivot_table = grid_df.pivot_table(\n",
|
|||
|
|
" index=param1_name, columns=param2_name, values=optimization_metric\n",
|
|||
|
|
" )\n",
|
|||
|
|
" # print(f\"\\n{optimization_metric} 网格结果 (Pivoted):\")\n",
|
|||
|
|
" # print(pivot_table.to_string())\n",
|
|||
|
|
" else:\n",
|
|||
|
|
" print(f\"\\n没有成功的组合结果可供分析,或优化指标 '{optimization_metric}' 不在结果中,或所有组合均失败。\")\n",
|
|||
|
|
"else:\n",
|
|||
|
|
" print(\"没有可用的回测结果。\")\n",
|
|||
|
|
"print(\"\\n--- 动态网格搜索完成 ---\")\n",
|
|||
|
|
"\n",
|
|||
|
|
"# --- 6. 可视化 (依赖 GridSearchAnalyzer) ---\n",
|
|||
|
|
"if grid_results:\n",
|
|||
|
|
" grid_analyzer = GridSearchAnalyzer(grid_results, optimization_metric)\n",
|
|||
|
|
" grid_analyzer.find_best_parameters() # 这会找到并打印最佳参数\n",
|
|||
|
|
" grid_analyzer.plot_heatmap() # 这会绘制热力图\n",
|
|||
|
|
"else:\n",
|
|||
|
|
" print(\"\\n没有生成任何网格搜索结果,无法进行分析。\")"
|
|||
|
|
],
|
|||
|
|
"id": "b6838275ff519227",
|
|||
|
|
"outputs": [
|
|||
|
|
{
|
|||
|
|
"name": "stdout",
|
|||
|
|
"output_type": "stream",
|
|||
|
|
"text": [
|
|||
|
|
"<class 'pandas.core.frame.DataFrame'>\n",
|
|||
|
|
"RangeIndex: 121 entries, 0 to 120\n",
|
|||
|
|
"Data columns (total 40 columns):\n",
|
|||
|
|
" # Column Non-Null Count Dtype \n",
|
|||
|
|
"--- ------ -------------- ----- \n",
|
|||
|
|
" 0 main_symbol 121 non-null object \n",
|
|||
|
|
" 1 trade_volume 121 non-null int64 \n",
|
|||
|
|
" 2 kalman_measurement_noise 121 non-null float64\n",
|
|||
|
|
" 3 entry_threshold_atr 121 non-null float64\n",
|
|||
|
|
" 4 order_direction 121 non-null object \n",
|
|||
|
|
" 5 enable_log 121 non-null bool \n",
|
|||
|
|
" 6 strategy_mode 121 non-null object \n",
|
|||
|
|
" 7 初始资金 121 non-null float64\n",
|
|||
|
|
" 8 最终资金 121 non-null float64\n",
|
|||
|
|
" 9 总收益率 121 non-null float64\n",
|
|||
|
|
" 10 年化收益率 121 non-null float64\n",
|
|||
|
|
" 11 最大回撤 121 non-null float64\n",
|
|||
|
|
" 12 夏普比率 121 non-null float64\n",
|
|||
|
|
" 13 卡玛比率 121 non-null float64\n",
|
|||
|
|
" 14 总交易次数 121 non-null int64 \n",
|
|||
|
|
" 15 交易成本 121 non-null float64\n",
|
|||
|
|
" 16 总实现盈亏 121 non-null float64\n",
|
|||
|
|
" 17 胜率 121 non-null float64\n",
|
|||
|
|
" 18 盈亏比 121 non-null float64\n",
|
|||
|
|
" 19 盈利交易次数 121 non-null int64 \n",
|
|||
|
|
" 20 亏损交易次数 121 non-null int64 \n",
|
|||
|
|
" 21 平均每次盈利 121 non-null float64\n",
|
|||
|
|
" 22 平均每次亏损 121 non-null float64\n",
|
|||
|
|
" 23 initial_capital 121 non-null float64\n",
|
|||
|
|
" 24 final_capital 121 non-null float64\n",
|
|||
|
|
" 25 total_return 121 non-null float64\n",
|
|||
|
|
" 26 annualized_return 121 non-null float64\n",
|
|||
|
|
" 27 max_drawdown 121 non-null float64\n",
|
|||
|
|
" 28 sharpe_ratio 121 non-null float64\n",
|
|||
|
|
" 29 calmar_ratio 121 non-null float64\n",
|
|||
|
|
" 30 sortino_ratio 121 non-null float64\n",
|
|||
|
|
" 31 total_trades 121 non-null int64 \n",
|
|||
|
|
" 32 transaction_costs 121 non-null float64\n",
|
|||
|
|
" 33 total_realized_pnl 121 non-null float64\n",
|
|||
|
|
" 34 win_rate 121 non-null float64\n",
|
|||
|
|
" 35 profit_loss_ratio 121 non-null float64\n",
|
|||
|
|
" 36 winning_trades_count 121 non-null int64 \n",
|
|||
|
|
" 37 losing_trades_count 121 non-null int64 \n",
|
|||
|
|
" 38 avg_profit_per_trade 121 non-null float64\n",
|
|||
|
|
" 39 avg_loss_per_trade 121 non-null float64\n",
|
|||
|
|
"dtypes: bool(1), float64(29), int64(7), object(3)\n",
|
|||
|
|
"memory usage: 37.1+ KB\n",
|
|||
|
|
"None\n",
|
|||
|
|
"\n",
|
|||
|
|
"--- 最优参数组合 (按total_return) ---\n",
|
|||
|
|
"main_symbol MA\n",
|
|||
|
|
"trade_volume 1\n",
|
|||
|
|
"kalman_measurement_noise 0.51\n",
|
|||
|
|
"entry_threshold_atr 0.7\n",
|
|||
|
|
"order_direction [BUY, SELL]\n",
|
|||
|
|
"enable_log False\n",
|
|||
|
|
"strategy_mode REVERSION\n",
|
|||
|
|
"初始资金 100000.0\n",
|
|||
|
|
"最终资金 101025.8533\n",
|
|||
|
|
"总收益率 0.010259\n",
|
|||
|
|
"年化收益率 0.003559\n",
|
|||
|
|
"最大回撤 0.00583\n",
|
|||
|
|
"夏普比率 0.169574\n",
|
|||
|
|
"卡玛比率 0.610437\n",
|
|||
|
|
"总交易次数 542\n",
|
|||
|
|
"交易成本 140.3901\n",
|
|||
|
|
"总实现盈亏 1165.0\n",
|
|||
|
|
"胜率 0.299242\n",
|
|||
|
|
"盈亏比 3.075544\n",
|
|||
|
|
"盈利交易次数 79\n",
|
|||
|
|
"亏损交易次数 185\n",
|
|||
|
|
"平均每次盈利 61.810127\n",
|
|||
|
|
"平均每次亏损 -20.097297\n",
|
|||
|
|
"initial_capital 100000.0\n",
|
|||
|
|
"final_capital 101025.8533\n",
|
|||
|
|
"total_return 0.010259\n",
|
|||
|
|
"annualized_return 0.003559\n",
|
|||
|
|
"max_drawdown 0.00583\n",
|
|||
|
|
"sharpe_ratio 0.169574\n",
|
|||
|
|
"calmar_ratio 0.610437\n",
|
|||
|
|
"sortino_ratio 0.211033\n",
|
|||
|
|
"total_trades 542\n",
|
|||
|
|
"transaction_costs 140.3901\n",
|
|||
|
|
"total_realized_pnl 1165.0\n",
|
|||
|
|
"win_rate 0.299242\n",
|
|||
|
|
"profit_loss_ratio 3.075544\n",
|
|||
|
|
"winning_trades_count 79\n",
|
|||
|
|
"losing_trades_count 185\n",
|
|||
|
|
"avg_profit_per_trade 61.810127\n",
|
|||
|
|
"avg_loss_per_trade -20.097297\n",
|
|||
|
|
"Name: 13, dtype: object\n",
|
|||
|
|
"\n",
|
|||
|
|
"--- 动态网格搜索完成 ---\n",
|
|||
|
|
"\n",
|
|||
|
|
"--- 最佳参数组合 ---\n",
|
|||
|
|
" kalman_measurement_noise: 0.51\n",
|
|||
|
|
" entry_threshold_atr: 0.7\n",
|
|||
|
|
" total_return: 0.0103\n",
|
|||
|
|
"[0.01, 5.01, 0.1, 3.1]\n"
|
|||
|
|
]
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"data": {
|
|||
|
|
"text/plain": [
|
|||
|
|
"<Figure size 1000x800 with 2 Axes>"
|
|||
|
|
],
|
|||
|
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7YAAAMWCAYAAADF5hp2AAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjcsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvTLEjVAAAAAlwSFlzAAAPYQAAD2EBqD+naQABAABJREFUeJzs3Qd4FFUXBuAvvZKQhJbQe++99947goIFexfLDypVRRQVC6KoFCkKKh1EepHea0gInRRCgPSe7P+cu2zYJJtqYDOb7/UZye7M7szeuVPO3Gal0+l0ICIiIiIiItIoa3NvABEREREREdF/wcCWiIiIiIiINI2BLREREREREWkaA1siIiIiIiLSNAa2REREREREpGkMbImIiIiIiEjTGNgSERERERGRpjGwJSIiIiIiIk1jYEtERERERESaZpGB7dSpU2FlZYW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bXKsyvUtN1PG3ynHolyfq1evrmvYsKEuNTU1bb6s7+WXX87zOSsv11DJy3I9kt8ly8m+l3wsx/2qVavyvO+ITLHowFaCBGODBw9WN0TGN/9y4/Txxx+nW04uiHJxMX5fDvKMJDCRA/LatWtp78mJwVSgaDj45eIWHh6e9v7EiRPV+3KSSUpKSntfbublZCcnsey24fnnn1cXL+PlDMGqnDgNEhIS1EVagl3jE01uAltx5MiRtJtPY/Jd8p316tXTxcXFpb0vFydZfvLkyTmmjanfJt8r3yk3iQUR2FpbW+vOnTuX7n0JBiT4CQsLS/e+BCFy8jVsU1a/PT+
|
|||
|
|
},
|
|||
|
|
"metadata": {},
|
|||
|
|
"output_type": "display_data",
|
|||
|
|
"jetTransient": {
|
|||
|
|
"display_id": null
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"execution_count": 12
|
|||
|
|
}
|
|||
|
|
],
|
|||
|
|
"metadata": {
|
|||
|
|
"kernelspec": {
|
|||
|
|
"display_name": "quant",
|
|||
|
|
"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.12.11"
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"nbformat": 4,
|
|||
|
|
"nbformat_minor": 5
|
|||
|
|
}
|