周期指标

周期指标使用希尔伯特变换来识别价格数据中的主导周期。这些指标基于 John Ehlers 的工作,对于识别周期性模式和趋势与周期模式非常有用。


HT_DCPERIOD - 主导周期周期

主导周期周期使用希尔伯特变换识别价格数据中主导周期的长度(以K线数表示)。

公式

希尔伯特变换应用于价格数据以提取主导周期周期。算法:

  1. 应用希尔伯特变换创建同相和正交分量

  2. 从相位关系计算周期

  3. 平滑周期估计值

特性

属性

输入

收盘价

输出

单个值(周期,以K线数表示)

回看期

63

内存

O(1)

API 用法

from techkit import HT_DCPERIOD

dcperiod = HT_DCPERIOD()
result = dcperiod.update(close_price)
if result.valid:
    print(f"Dominant Cycle Period: {result.value:.1f} bars")
const tk = require('techkit');

const dcperiod = tk.htDcperiod();
const result = dcperiod.update(close);
#include <techkit/techkit.hpp>

techkit::HT_DCPERIOD dcperiod;
auto result = dcperiod.update(close);
tk_indicator dcperiod = tk_ht_dcperiod_new();
tk_result r = tk_update(dcperiod, close);
tk_free(dcperiod);

交易用法

  • 周期长度:典型值范围从 10-50 根K线

  • 自适应指标:使用周期来设置自适应移动平均周期

  • 入场时机:在周期极值处入场交易

  • 趋势与周期:结合 HT_TRENDMODE 来确定市场状态


HT_DCPHASE - 主导周期相位

主导周期相位返回主导周期的当前相位(以度为单位,0-360)。

公式

相位从同相和正交分量计算:

\[\text{phase} = \arctan2(\text{Quadrature}, \text{InPhase}) \times \frac{180}{\pi}\]

相位归一化到 0-360 度。

特性

属性

输入

收盘价

输出

单个值(相位,以度为单位,0-360)

回看期

63

内存

O(1)

API 用法

from techkit import HT_DCPHASE

dcphase = HT_DCPHASE()
result = dcphase.update(close_price)
if result.valid:
    phase = result.value
    if phase < 90:
        print("Early cycle")
    elif phase < 180:
        print("Mid cycle up")
    elif phase < 270:
        print("Late cycle")
    else:
        print("Mid cycle down")

交易用法

  • 周期位置

    • 0-90°:周期早期(潜在买入)

    • 90-180°:周期中期上升

    • 180-270°:周期后期(潜在卖出)

    • 270-360°:周期中期下降

  • 入场/出场时机:使用相位极值进行入场

  • 周期确认:结合 HT_SINE 进行周期确认


HT_PHASOR - 相量分量

HT_PHASOR 返回希尔伯特变换的同相和正交分量,它们以复数形式表示周期。

公式

希尔伯特变换提取:

  • 同相 (I1):周期的实部分量

  • 正交 (Q1):周期的虚部分量

这些分量相位相差 90 度。

特性

属性

输入

收盘价

输出

两个值(同相,正交)

回看期

63

内存

O(1)

API 用法

from techkit import HT_PHASOR

phasor = HT_PHASOR()
result = phasor.update(close_price)
if result.valid:
    print(f"InPhase: {result.inphase:.4f}")
    print(f"Quadrature: {result.quadrature:.4f}")
    
    # Calculate amplitude
    amplitude = (result.inphase**2 + result.quadrature**2)**0.5
    print(f"Amplitude: {amplitude:.4f}")

交易用法

  • 周期振幅:从 I1² + Q1² 计算

  • 周期相位:从 atan2(Q1, I1) 计算

  • 高级分析:用于自定义周期指标

  • 滤波:用于从价格数据中过滤噪声


HT_SINE - 正弦波

HT_SINE 返回主导周期相位的正弦和领先正弦。领先正弦比正弦提前 45 度。

公式

\[\text{Sine} = \sin(\text{phase})\]
\[\text{LeadSine} = \sin(\text{phase} + 45°)\]

其中相位从 HT_PHASOR 分量计算。

特性

属性

输入

收盘价

输出

两个值(正弦,领先正弦)

回看期

63

内存

O(1)

API 用法

from techkit import HT_SINE

sine = HT_SINE()
result = sine.update(close_price)
if result.valid:
    print(f"Sine: {result.sine:.4f}")
    print(f"LeadSine: {result.leadsine:.4f}")
    
    # Buy signal: Sine crosses above LeadSine
    if result.sine > result.leadsine:
        print("Bullish signal")
    # Sell signal: Sine crosses below LeadSine
    elif result.sine < result.leadsine:
        print("Bearish signal")

交易用法

  • 买入信号:正弦上穿领先正弦

  • 卖出信号:正弦下穿领先正弦

  • 周期极值

    • 正弦接近 +1:周期峰值(潜在卖出)

    • 正弦接近 -1:周期谷值(潜在买入)

  • 趋势跟踪:在趋势市场中使用(由 HT_TRENDMODE 确认)

常见策略

# Combined cycle strategy
sine = HT_SINE()
trendmode = HT_TRENDMODE()

for price in prices:
    sine_result = sine.update(price)
    trend_result = trendmode.update(price)
    
    if sine_result.valid and trend_result.valid:
        # Only trade in cycle mode (not trending)
        if trend_result.value == 0:  # Cycle mode
            if sine_result.sine > sine_result.leadsine:
                # Buy signal
                pass
            elif sine_result.sine < sine_result.leadsine:
                # Sell signal
                pass

HT_TRENDLINE - 瞬时趋势线

HT_TRENDLINE 返回基于主导周期的瞬时趋势线。它比简单移动平均更平滑,并能适应周期。

公式

趋势线从同相分量计算,经过滤波以去除周期分量,仅保留趋势。

特性

属性

输入

收盘价

输出

单个值(趋势线价格)

回看期

63

内存

O(1)

API 用法

from techkit import HT_TRENDLINE

trendline = HT_TRENDLINE()
result = trendline.update(close_price)
if result.valid:
    print(f"Trendline: {result.value:.2f}")
    
    # Price above trendline = uptrend
    if close_price > result.value:
        print("Uptrend")
    else:
        print("Downtrend")

交易用法

  • 趋势识别:价格高于趋势线 = 上升趋势

  • 支撑/阻力:趋势线充当动态支撑/阻力

  • 入场信号:当价格上穿趋势线时买入

  • 比较:比 EMA 更平滑,能适应周期


HT_TRENDMODE - 趋势与周期模式

HT_TRENDMODE 确定市场是处于趋势模式(1)还是周期模式(0)。

公式

该指标比较趋势分量与周期分量的强度。如果趋势占主导,返回 1;如果周期占主导,返回 0。

特性

属性

输入

收盘价

输出

单个值(1 = 趋势,0 = 周期)

回看期

63

内存

O(1)

API 用法

from techkit import HT_TRENDMODE

trendmode = HT_TRENDMODE()
result = trendmode.update(close_price)
if result.valid:
    if result.value == 1:
        print("Market is trending - use trend-following indicators")
    else:
        print("Market is cycling - use cycle indicators")

交易用法

  • 指标选择

    • 趋势(1):使用趋势跟踪指标(MA、ADX)

    • 周期(0):使用周期指标(HT_SINE、振荡器)

  • 策略适应:根据模式切换策略

  • 过滤:在趋势模式下过滤周期信号

组合策略示例

from techkit import HT_TRENDMODE, HT_SINE, HT_TRENDLINE, ADX

trendmode = HT_TRENDMODE()
sine = HT_SINE()
trendline = HT_TRENDLINE()
adx = ADX(period=14)

for bar in bars:
    mode = trendmode.update(bar.close)
    sine_result = sine.update(bar.close)
    trend_result = trendline.update(bar.close)
    adx_result = adx.update_ohlcv(bar)
    
    if all results valid:
        if mode.value == 1:  # Trending
            # Use trend-following logic
            if bar.close > trend_result.value and adx_result.adx > 25:
                # Strong uptrend
                pass
        else:  # Cycling
            # Use cycle logic
            if sine_result.sine > sine_result.leadsine:
                # Cycle buy signal
                pass

希尔伯特变换概述

希尔伯特变换是一种数学技术,它:

  1. 将趋势与周期分量分离

  2. 识别主导周期周期

  3. 计算周期相位和振幅

  4. 确定市场模式(趋势与周期)

优势

  • 自适应:自动适应变化的周期长度

  • 无滞后:瞬时计算(无前瞻偏差)

  • 模式检测:识别何时使用趋势与周期策略

局限性

  • 需要预热:在有效输出之前需要 63 根K线

  • 噪声敏感性:可能受到市场噪声的影响

  • 复杂:需要理解周期分析


相关指标

  • MAMA:MESA 自适应移动平均(使用希尔伯特变换)

  • FAMA:跟随自适应移动平均(MAMA 的配套指标)

  • ADX:在趋势模式中使用以衡量趋势强度

  • RSI:在周期模式中使用以衡量超买/超卖


延伸阅读

  • Ehlers, J. F. (2001). Rocket Science for Traders: Digital Signal Processing Applications

  • Ehlers, J. F. (2004). Cybernetic Analysis for Stocks and Futures