56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
|
|
from datetime import timedelta
|
|||
|
|
from src.analysis.result_analyzer import ResultAnalyzer
|
|||
|
|
|
|||
|
|
# 导入 TqsdkEngine,而不是原来的 BacktestEngine
|
|||
|
|
from src.indicators.indicators import RSI, HistoricalRange, BollingerBandwidth
|
|||
|
|
from src.tqsdk_real_engine import TqsdkEngine
|
|||
|
|
|
|||
|
|
# 导入你的策略类
|
|||
|
|
from src.strategies.OpenTwoFactorStrategy import SimpleLimitBuyStrategyLong, SimpleLimitBuyStrategyShort, SimpleLimitBuyStrategy
|
|||
|
|
|
|||
|
|
from tqsdk import TqApi, TqBacktest, TqAuth, TqKq, TqAccount
|
|||
|
|
|
|||
|
|
# --- 配置参数 ---
|
|||
|
|
# Tqsdk 的本地数据文件路径,注意 Tqsdk 要求文件名为 KQ_m@交易所_品种名_周期.csv
|
|||
|
|
|
|||
|
|
# 主力合约的 symbol
|
|||
|
|
symbol = 'KQ.m@CZCE.MA'
|
|||
|
|
strategy_parameters = {
|
|||
|
|
'main_symbol': 'MA', # 根据您的数据文件中的品种名称调整
|
|||
|
|
'trade_volume': 2,
|
|||
|
|
'lag': 7,
|
|||
|
|
# 'range_factor': 1.8, # 示例值,需要通过网格搜索优化
|
|||
|
|
# 'profit_factor': 2.8, # 示例值
|
|||
|
|
# 'range_factor': 1.6, # 示例值,需要通过网格搜索优化
|
|||
|
|
# 'profit_factor': 2.1, # 示例值
|
|||
|
|
'range_factor_l': 1.8, # 示例值,需要通过网格搜索优化
|
|||
|
|
'profit_factor_l': 2.8, # 示例值
|
|||
|
|
'range_factor_s': 1.6, # 示例值,需要通过网格搜索优化
|
|||
|
|
'profit_factor_s': 2.1, # 示例值
|
|||
|
|
'max_position': 10,
|
|||
|
|
'enable_log': True,
|
|||
|
|
'stop_loss_points': 20,
|
|||
|
|
'use_indicator': True,
|
|||
|
|
# 'indicator': HistoricalRange(11, 25, 20),
|
|||
|
|
# 'indicator': BollingerBandwidth(window=20, nbdev=2.0, down_bound=1.9, up_bound=3.25),
|
|||
|
|
'indicator_l': HistoricalRange(11, 25, 20),
|
|||
|
|
'indicator_s': BollingerBandwidth(window=20, nbdev=2.0, down_bound=1.9, up_bound=3.25),
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# api = TqApi(TqKq(), auth=TqAuth("emanresu", "dfgvfgdfgg"))
|
|||
|
|
api = TqApi(TqAccount('H宏源期货', '903308830', 'lzr520102'), auth=TqAuth("emanresu", "dfgvfgdfgg"))
|
|||
|
|
|
|||
|
|
# --- 1. 初始化回测引擎并运行 ---
|
|||
|
|
print("\n初始化 Tqsdk 回测引擎...")
|
|||
|
|
engine = TqsdkEngine(
|
|||
|
|
strategy_class=SimpleLimitBuyStrategy,
|
|||
|
|
strategy_params=strategy_parameters,
|
|||
|
|
api=api,
|
|||
|
|
symbol=symbol,
|
|||
|
|
duration_seconds=60 * 60,
|
|||
|
|
roll_over_mode=True, # 启用换月模式检测
|
|||
|
|
history_length=50,
|
|||
|
|
close_bar_delta=timedelta(minutes=58)
|
|||
|
|
)
|
|||
|
|
engine.run() # 这是一个同步方法,内部会运行 asyncio 循环
|