Files
NewQuant/real_trading/Spectral/FG.py
2025-12-16 00:36:36 +08:00

48 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import timedelta
from tqsdk import TqApi, TqAuth, TqAccount, TqKq
from futures_trading_strategies.FG.Spectral.SpectralTrendStrategy3 import SpectralTrendStrategy
# 导入 TqsdkEngine而不是原来的 BacktestEngine
from src.indicators.indicators import PriceRangeToVolatilityRatio, ZScoreATR, Hurst
from src.tqsdk_real_engine import TqsdkEngine
# 导入你的策略类
# --- 配置参数 ---
# Tqsdk 的本地数据文件路径,注意 Tqsdk 要求文件名为 KQ_m@交易所_品种名_周期.csv
# 主力合约的 symbol
symbol = "KQ.m@SHFE.ag"
strategy_parameters = {
'main_symbol': 'ag', # <-- 替换为你的交易品种代码,例如 'GC=F' (黄金期货), 'ZC=F' (玉米期货)
'trade_volume': 1,
# 'order_direction': ['SELL', 'BUY'],
'indicators': Hurst(115, 0, 0.5),
# 'model_indicator': ADX(7, 0, 30),
'bars_per_day': 23,
'spectral_window_days': 9, # STFT窗口大小(天)
'low_freq_days': 9, # 低频下限(天)
'high_freq_days': 4, # 高频上限(天)
'trend_strength_threshold': 0.6, # 相变临界值
'exit_threshold': 0.3, # 退出阈值
'enable_log': True,
'reverse': False,
}
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=SpectralTrendStrategy,
strategy_params=strategy_parameters,
api=api,
symbol=symbol,
duration_seconds=60 * 15,
roll_over_mode=True, # 启用换月模式检测
history_length=1000,
close_bar_delta=timedelta(minutes=58)
)
engine.run() # 这是一个同步方法,内部会运行 asyncio 循环