修复未来函数bug
This commit is contained in:
191
futures_trading_strategies/MA/main2.ipynb
Normal file
191
futures_trading_strategies/MA/main2.ipynb
Normal file
@@ -0,0 +1,191 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "initial_id",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-07-12T14:39:30.843472Z",
|
||||
"start_time": "2025-07-12T14:39:30.823755Z"
|
||||
},
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from datetime import datetime\n",
|
||||
"%load_ext autoreload\n",
|
||||
"%autoreload 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a559dfcf",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-07-12T14:39:35.610573Z",
|
||||
"start_time": "2025-07-12T14:39:30.855663Z"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"初始化数据管理器...\n",
|
||||
"数据加载成功: /mnt/d/PyProject/NewQuant/data/data/KQ_m@CZCE_MA/KQ_m@CZCE_MA_min60.csv\n",
|
||||
"数据范围从 2021-12-31 14:00:00 到 2025-07-10 09:00:00\n",
|
||||
"总计 5904 条记录。\n",
|
||||
"\n",
|
||||
"初始化回测引擎...\n",
|
||||
"模拟器初始化:初始资金=100000.00, 滑点率=0.0, 佣金率=0.0001\n",
|
||||
"\n",
|
||||
"--- 回测引擎初始化完成 ---\n",
|
||||
" 策略: SimpleLimitBuyStrategyShort\n",
|
||||
" 初始资金: 100000.00\n",
|
||||
" 换月模式: 启用\n",
|
||||
"\n",
|
||||
"开始运行回测...\n",
|
||||
"\n",
|
||||
"--- 回测开始 ---\n",
|
||||
"SimpleLimitBuyStrategyShort 策略初始化回调被调用。\n",
|
||||
"开始将 DataFrame 转换为 Bar 对象流...\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"\n",
|
||||
"from src.analysis.result_analyzer import ResultAnalyzer\n",
|
||||
"# 导入所有必要的模块\n",
|
||||
"from src.data_manager import DataManager\n",
|
||||
"from src.backtest_engine import BacktestEngine\n",
|
||||
"from src.indicators.indicator_list import INDICATOR_LIST\n",
|
||||
"from src.indicators.indicators import RSI, HistoricalRange, StochasticOscillator\n",
|
||||
"from src.strategies.OpenTwoFactorStrategy import SimpleLimitBuyStrategyLong, SimpleLimitBuyStrategyShort, SimpleLimitBuyStrategy\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# --- 配置参数 ---\n",
|
||||
"# 获取当前脚本所在目录,假设数据文件在项目根目录下的 data 文件夹内\n",
|
||||
"# data_file_path = '/mnt/d/PyProject/NewQuant/data/data/SHFE_rb2510/SHFE_rb2510_min60.csv'\n",
|
||||
"data_file_path = \"/mnt/d/PyProject/NewQuant/data/data/KQ_m@CZCE_MA/KQ_m@CZCE_MA_min60.csv\"\n",
|
||||
"\n",
|
||||
"initial_capital = 100000.0\n",
|
||||
"slippage_rate = 0.000 # 假设每笔交易0.1%的滑点\n",
|
||||
"commission_rate = 0.0001 # 假设每笔交易0.02%的佣金\n",
|
||||
"\n",
|
||||
"global_config = {\n",
|
||||
" 'symbol': 'KQ_m@CZCE_MA',\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"strategy_parameters = {\n",
|
||||
" # 'symbol': \"SHFE_rb2501\", # 根据您的数据文件中的品种名称调整\n",
|
||||
" 'trade_volume': 1,\n",
|
||||
" # 'range_factor': 1.8, # 示例值,需要通过网格搜索优化\n",
|
||||
" # 'profit_factor': 2.3, # 示例值\n",
|
||||
" 'range_factor': 1.6, # 示例值,需要通过网格搜索优化\n",
|
||||
" 'profit_factor': 2.1, # 示例值\n",
|
||||
" # 'range_factor_l': 1.8, # 示例值,需要通过网格搜索优化\n",
|
||||
" # 'profit_factor_l': 2.8, # 示例值\n",
|
||||
" # 'range_factor_s': 1.6, # 示例值,需要通过网格搜索优化\n",
|
||||
" # 'profit_factor_s': 2.1, # 示例值\n",
|
||||
" 'max_position': 10,\n",
|
||||
" 'enable_log': False,\n",
|
||||
" 'stop_loss_points': 15,\n",
|
||||
" 'use_indicator': True,\n",
|
||||
" # 'indicator': HistoricalRange(shift_window=20, down_bound=10, up_bound=24),\n",
|
||||
" 'indicator': RSI(5, 15, 60),\n",
|
||||
" # 'indicator_l': HistoricalRange(21, 10, 25),\n",
|
||||
" # 'indicator_s': RSI(5, 20, 60),\n",
|
||||
"}\n",
|
||||
"start_time = datetime(2022, 1, 1)\n",
|
||||
"end_time = datetime(2024, 6, 1)\n",
|
||||
"\n",
|
||||
"# start_time = datetime(2024, 6, 1)\n",
|
||||
"# end_time = datetime(2025, 6, 1)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# --- 1. 初始化数据管理器 ---\n",
|
||||
"print(\"初始化数据管理器...\")\n",
|
||||
"data_manager = DataManager(file_path=data_file_path, symbol=global_config['symbol'], start_time=start_time, end_time=end_time)\n",
|
||||
"# 确保 DataManager 能够重置以进行多次回测\n",
|
||||
"# data_manager.reset() # 首次运行不需要重置\n",
|
||||
"\n",
|
||||
"# --- 2. 初始化回测引擎并运行 ---\n",
|
||||
"print(\"\\n初始化回测引擎...\")\n",
|
||||
"engine = BacktestEngine(\n",
|
||||
" data_manager=data_manager,\n",
|
||||
" strategy_class=SimpleLimitBuyStrategyShort,\n",
|
||||
" # current_segment_symbol=strategy_parameters['symbol'],\n",
|
||||
" strategy_params=strategy_parameters,\n",
|
||||
" initial_capital=initial_capital,\n",
|
||||
" slippage_rate=slippage_rate,\n",
|
||||
" commission_rate=commission_rate,\n",
|
||||
" roll_over_mode=True,\n",
|
||||
" start_time=start_time,\n",
|
||||
" end_time=end_time,\n",
|
||||
" indicators=INDICATOR_LIST\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"\\n开始运行回测...\")\n",
|
||||
"engine.run_backtest()\n",
|
||||
"print(\"\\n回测运行完毕。\")\n",
|
||||
"\n",
|
||||
"# --- 3. 获取回测结果 ---\n",
|
||||
"results = engine.get_backtest_results()\n",
|
||||
"portfolio_snapshots = results[\"portfolio_snapshots\"]\n",
|
||||
"trade_history = results[\"trade_history\"]\n",
|
||||
"initial_capital_result = results[\"initial_capital\"]\n",
|
||||
"bars = results[\"all_bars\"]\n",
|
||||
"\n",
|
||||
"# --- 4. 结果分析与可视化 ---\n",
|
||||
"if portfolio_snapshots:\n",
|
||||
" analyzer = ResultAnalyzer(portfolio_snapshots, trade_history, bars, initial_capital_result, INDICATOR_LIST)\n",
|
||||
"\n",
|
||||
" analyzer.generate_report()\n",
|
||||
" analyzer.plot_performance()\n",
|
||||
" metrics = analyzer.calculate_all_metrics()\n",
|
||||
" print(metrics)\n",
|
||||
"\n",
|
||||
" analyzer.analyze_indicators()\n",
|
||||
"else:\n",
|
||||
" print(\"\\n没有生成投资组合快照,无法进行结果分析。\")\n",
|
||||
"\n",
|
||||
"# --- 4. 结果分析与可视化 (待实现) ---\n",
|
||||
"# if portfolio_snapshots:\n",
|
||||
"# analyzer = ResultAnalyzer(portfolio_snapshots, trade_history, initial_capital_result)\n",
|
||||
"# metrics = analyzer.calculate_all_metrics()\n",
|
||||
"# print(\"\\n--- 绩效指标 ---\")\n",
|
||||
"# for key, value in metrics.items():\n",
|
||||
"# print(f\" {key}: {value:.4f}\")\n",
|
||||
"#\n",
|
||||
"# print(\"\\n--- 绘制绩效图表 ---\")\n",
|
||||
"# analyzer.plot_performance()\n",
|
||||
"# else:\n",
|
||||
"# print(\"\\n没有生成投资组合快照,无法进行结果分析。\")\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
8686
futures_trading_strategies/MA/tqsdk_main2.ipynb
Normal file
8686
futures_trading_strategies/MA/tqsdk_main2.ipynb
Normal file
File diff suppressed because one or more lines are too long
651
futures_trading_strategies/jm/main2_lag1.ipynb
Normal file
651
futures_trading_strategies/jm/main2_lag1.ipynb
Normal file
File diff suppressed because one or more lines are too long
607
futures_trading_strategies/rb/main2.ipynb
Normal file
607
futures_trading_strategies/rb/main2.ipynb
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user