一、交易策略解释
核心思想
TRIX策略的核心思想是利用三重指数平滑移动平均线来识别市场趋势的转变和动量的强度。 通过三重平滑处理,TRIX指标能够有效滤除市场噪音,专注于捕捉主要趋势的变化。具体而言,该策略通过监测TRIX指标与零线或信号线的交叉来生成交易信号:
TRIX上穿零线:表示市场动量从看跌转为看涨,产生做多信号
TRIX下穿零线:表示市场动量从看涨转为看跌,产生做空信号
TRIX上穿信号线:表示看涨动量增强,产生做多信号
TRIX下穿信号线:表示看跌动量增强,产生做空信号
多重平滑处理的优势在于减少了虚假信号,使指标对真正的趋势变化更为敏感,而对短期市场噪音不太敏感。
理论基础
TRIX指标由Jack Hutson在20世纪80年代开发,其理论基础是动量交易理论与移动平均线理论的结合。该指标的设计旨在解决传统移动平均线存在的两个主要问题:滞后性和噪音干扰。
在学术研究中,多重平滑移动平均线已被证明可以减少滞后性,同时保持较强的信号质量。TRIX通过计算平滑后数据的变化率,进一步强化了对趋势转变的敏感性。从数学角度看,三重指数平滑实际上类似于一种低通滤波器,能够过滤高频噪音而保留低频趋势信号。
市场证据表明,在强趋势市场中,TRIX指标的零线交叉能有效识别趋势的起始,而信号线交叉则能捕捉趋势中的调整机会。尤其在期货市场这种波动较大的市场中,TRIX的多重平滑特性能够帮助交易者避免被短期噪音误导。
策略适用场景
中长期趋势市场:由于三重平滑的特性,TRIX在捕捉中长期趋势方面表现更佳,适合日线及以上周期的交易。
波动性较大的期货品种:对于金属、能源、农产品等波动性较大的期货品种,TRIX的平滑特性能够帮助交易者过滤噪音,专注于主要趋势。
突破策略的辅助确认:可以与价格突破等其他技术分析方法结合使用,作为趋势确认工具。
趋势转变点的识别:特别适合识别趋势转变的早期阶段,有助于交易者及早进入新趋势。
策略原理
TRIX指标的计算过程如下:
- 第一重EMA:计算收盘价的N周期指数移动平均线
EMA1 = EMA(Close, N)- 第二重EMA:计算第一重EMA的N周期指数移动平均线
EMA2 = EMA(EMA1, N)- 第三重EMA:计算第二重EMA的N周期指数移动平均线
EMA3 = EMA(EMA2, N)- TRIX:计算三重EMA的百分比变化率
TRIX = (EMA3 - EMA3前一周期) / EMA3前一周期 * 100- 信号线:计算TRIX的M周期简单移动平均线
Signal = SMA(TRIX, M)交易逻辑
开仓条件:
多头开仓:当TRIX上穿零线或TRIX上穿信号线时,开多仓
空头开仓:当TRIX下穿零线或TRIX下穿信号线时,开空仓
平仓信号:
多头平仓:当TRIX下穿信号线时,平多仓
空头平仓:当TRIX上穿信号线时,平空仓
止损设置:
多头止损:入场点下方N个ATR(平均真实波幅)
空头止损:入场点上方N个ATR
回测
回测初始设置
- 测试周期: 2022 年 11 月 1 日 - 2023 年 4 月 30 日
- 交易品种: CFFEX.IC2306
- 初始资金: 1000万元
回测结果

上述回测累计收益走势图

代码
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = "Chaos" from datetime import date import pandas as pd from tqsdk import TqApi, TqAuth, TqBacktest, TargetPosTask, BacktestFinished from tqsdk.ta import ATR # ===== 全局参数设置 ===== SYMBOL = "CFFEX.IC2306" # 黄金期货合约 POSITION_SIZE = 30 # 基础持仓手数 START_DATE = date(2022, 11, 1) # 回测开始日期 END_DATE = date(2023, 4, 30) # 回测结束日期 # TRIX指标参数 TRIX_PERIOD = 12 # TRIX计算周期 SIGNAL_PERIOD = 9 # 信号线计算周期 MA_PERIOD = 60 # 长期移动平均线周期,用于趋势过滤 # 信号阈值参数 SIGNAL_THRESHOLD = 0.05 # TRIX与信号线差值的阈值,避免微小交叉 # 风控参数 ATR_PERIOD = 14 # ATR计算周期 STOP_LOSS_MULTIPLIER = 2.0 # 止损ATR倍数 TAKE_PROFIT_MULTIPLIER = 3.0 # 止盈ATR倍数 MAX_HOLDING_DAYS = 15 # 最大持仓天数 # ===== 全局变量 ===== current_direction = 0 # 当前持仓方向:1=多头,-1=空头,0=空仓 entry_price = 0 # 开仓价格 stop_loss_price = 0 # 止损价格 entry_date = None # 开仓日期 trade_count = 0 # 交易次数 win_count = 0 # 盈利次数 # ===== TRIX指标计算函数 ===== def calculate_trix(close_prices, period): """计算TRIX指标和信号线""" # 第一重EMA ema1 = close_prices.ewm(span=period, adjust=False).mean() # 第二重EMA ema2 = ema1.ewm(span=period, adjust=False).mean() # 第三重EMA ema3 = ema2.ewm(span=period, adjust=False).mean() # 计算TRIX trix = 100 * (ema3 / ema3.shift(1) - 1) # 计算信号线 signal = trix.rolling(SIGNAL_PERIOD).mean() return trix, signal # ===== 策略开始 ===== print("开始运行TRIX指标期货策略...") print(f"品种: {SYMBOL}, 回测周期: {START_DATE} - {END_DATE}") print(f"TRIX参数: 周期={TRIX_PERIOD}, 信号线周期={SIGNAL_PERIOD}") # 创建API实例 api = TqApi(backtest=TqBacktest(start_dt=START_DATE, end_dt=END_DATE), auth=TqAuth("快期账号", "快期密码")) # 订阅合约的K线数据 klines = api.get_kline_serial(SYMBOL, 60 * 60 * 24) # 日线数据 # 创建目标持仓任务 target_pos = TargetPosTask(api, SYMBOL) try: while True: # 等待更新 api.wait_update() # 如果K线有更新 if api.is_changing(klines.iloc[-1], "datetime"): # 确保有足够的数据计算指标 if len(klines) < max(TRIX_PERIOD, SIGNAL_PERIOD, MA_PERIOD, ATR_PERIOD) + 10: continue # 计算TRIX指标和信号线 klines['trix'], klines['signal'] = calculate_trix(klines.close, TRIX_PERIOD) # 计算长期移动平均线,用于趋势过滤 klines['ma'] = klines.close.rolling(window=MA_PERIOD).mean() # 计算ATR用于设置止损 atr_data = ATR(klines, ATR_PERIOD) # 获取最新数据 current_price = float(klines.close.iloc[-1]) current_datetime = pd.to_datetime(klines.datetime.iloc[-1], unit='ns') current_trix = float(klines.trix.iloc[-1]) previous_trix = float(klines.trix.iloc[-2]) current_signal = float(klines.signal.iloc[-1]) previous_signal = float(klines.signal.iloc[-2]) current_ma = float(klines.ma.iloc[-1]) current_atr = float(atr_data.atr.iloc[-1]) # 计算TRIX与信号线的差值 trix_diff = current_trix - current_signal previous_trix_diff = previous_trix - previous_signal # 输出调试信息 print(f"日期: {current_datetime.strftime('%Y-%m-%d')}, 价格: {current_price:.2f}") print(f"TRIX: {current_trix:.4f}, 信号线: {current_signal:.4f}, 差值: {trix_diff:.4f}") # ===== 交易逻辑 ===== # 空仓状态 - 寻找开仓机会 if current_direction == 0: # 多头开仓条件:TRIX上穿信号线 if previous_trix < previous_signal and current_trix > current_signal: current_direction = 1 target_pos.set_target_volume(POSITION_SIZE) entry_price = current_price stop_loss_price = entry_price - STOP_LOSS_MULTIPLIER * current_atr take_profit_price = entry_price + TAKE_PROFIT_MULTIPLIER * current_atr print(f"多头开仓: 价格={entry_price}, 止损={stop_loss_price:.2f}, 止盈={take_profit_price:.2f}") # 空头开仓条件:TRIX下穿信号线 elif previous_trix > previous_signal and current_trix < current_signal: current_direction = -1 target_pos.set_target_volume(-POSITION_SIZE) entry_price = current_price stop_loss_price = entry_price + STOP_LOSS_MULTIPLIER * current_atr take_profit_price = entry_price - TAKE_PROFIT_MULTIPLIER * current_atr print(f"空头开仓: 价格={entry_price}, 止损={stop_loss_price:.2f}, 止盈={take_profit_price:.2f}") # 多头持仓 - 检查平仓条件 elif current_direction == 1: # 止损条件 if current_price <= stop_loss_price: profit_pct = (current_price - entry_price) / entry_price * 100 target_pos.set_target_volume(0) current_direction = 0 print(f"多头止损平仓: 价格={current_price}, 盈亏={profit_pct:.2f}%") # 止盈条件 elif current_price >= take_profit_price: profit_pct = (current_price - entry_price) / entry_price * 100 target_pos.set_target_volume(0) current_direction = 0 print(f"多头止盈平仓: 价格={current_price}, 盈亏={profit_pct:.2f}%") # 信号平仓:TRIX下穿信号线 elif previous_trix > previous_signal and current_trix < current_signal: profit_pct = (current_price - entry_price) / entry_price * 100 target_pos.set_target_volume(0) current_direction = 0 print(f"多头信号平仓: 价格={current_price}, 盈亏={profit_pct:.2f}%") # 空头持仓 - 检查平仓条件 elif current_direction == -1: # 止损条件 if current_price >= stop_loss_price: profit_pct = (entry_price - current_price) / entry_price * 100 target_pos.set_target_volume(0) current_direction = 0 print(f"空头止损平仓: 价格={current_price}, 盈亏={profit_pct:.2f}%") # 止盈条件 elif current_price <= take_profit_price: profit_pct = (entry_price - current_price) / entry_price * 100 target_pos.set_target_volume(0) current_direction = 0 print(f"空头止盈平仓: 价格={current_price}, 盈亏={profit_pct:.2f}%") # 信号平仓:TRIX上穿信号线 elif previous_trix < previous_signal and current_trix > current_signal: profit_pct = (entry_price - current_price) / entry_price * 100 target_pos.set_target_volume(0) current_direction = 0 print(f"空头信号平仓: 价格={current_price}, 盈亏={profit_pct:.2f}%") except BacktestFinished as e: print("回测结束") api.close()