Python wrapper for TA-LIB providing 175+ technical analysis indicators for financial market data
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Hilbert Transform-based indicators for cycle analysis and trend vs cycle mode determination. These advanced indicators use mathematical transformations to identify market cycles, dominant periods, and phase relationships in price data.
Determines the dominant cycle period in the price data, helping identify the primary cyclical component.
def HT_DCPERIOD(real):
"""
Hilbert Transform - Dominant Cycle Period
Identifies the dominant cycle period in price data using Hilbert Transform analysis.
Parameters:
- real: array-like, input price data (typically close prices)
Returns:
numpy.ndarray: Dominant cycle period values (in periods)
"""Calculates the phase of the dominant cycle, indicating where the current price is within the cycle.
def HT_DCPHASE(real):
"""
Hilbert Transform - Dominant Cycle Phase
Determines the phase angle of the dominant cycle (0 to 360 degrees).
Parameters:
- real: array-like, input price data
Returns:
numpy.ndarray: Dominant cycle phase values (degrees)
"""Provides the in-phase and quadrature components of the dominant cycle, representing the cycle as a rotating vector.
def HT_PHASOR(real):
"""
Hilbert Transform - Phasor Components
Returns the in-phase and quadrature components of the dominant cycle.
Parameters:
- real: array-like, input price data
Returns:
tuple: (inphase, quadrature) - Phasor component values
"""Generates sine and lead sine waves based on the dominant cycle, useful for cycle timing and trend prediction.
def HT_SINE(real):
"""
Hilbert Transform - SineWave
Generates sine and lead sine waves from the dominant cycle analysis.
Parameters:
- real: array-like, input price data
Returns:
tuple: (sine, leadsine) - Sine wave and lead sine wave values
"""Determines whether the market is in a trending mode or cycling mode, helping choose appropriate indicators and strategies.
def HT_TRENDMODE(real):
"""
Hilbert Transform - Trend vs Cycle Mode
Identifies whether the market is trending or cycling.
Parameters:
- real: array-like, input price data
Returns:
tuple: (integer, trendmode) - Integer component and trend mode indicator arrays
- integer: array of integer values (first component)
- trendmode: array with 1 = trending, 0 = cycling (second component)
"""import talib
import numpy as np
# Sample price data (needs sufficient length for Hilbert Transform)
np.random.seed(42)
price_trend = np.linspace(100, 120, 100)
price_cycle = 5 * np.sin(np.linspace(0, 8*np.pi, 100))
price_data = price_trend + price_cycle + np.random.normal(0, 0.5, 100)
# Calculate Hilbert Transform indicators
dc_period = talib.HT_DCPERIOD(price_data)
dc_phase = talib.HT_DCPHASE(price_data)
inphase, quadrature = talib.HT_PHASOR(price_data)
sine, leadsine = talib.HT_SINE(price_data)
trendmode, _ = talib.HT_TRENDMODE(price_data)
# Analyze results
print(f"Dominant Cycle Period: {dc_period[-1]:.2f} periods")
print(f"Current Phase: {dc_phase[-1]:.1f} degrees")
print(f"Market Mode: {'Trending' if trendmode[-1] else 'Cycling'}")
# Trading applications:
# - Use sine/leadsine crossovers for cycle timing
# - Apply trend-following indicators when trendmode = 1
# - Apply oscillators when trendmode = 0
# - Use dominant cycle period to optimize indicator parameters
# Sine wave trading signals
if sine[-1] > leadsine[-1] and sine[-2] <= leadsine[-2]:
print("Potential bullish cycle signal")
elif sine[-1] < leadsine[-1] and sine[-2] >= leadsine[-2]:
print("Potential bearish cycle signal")The Hilbert Transform is a mathematical operation that creates a 90-degree phase shift in a signal, allowing for the analysis of instantaneous amplitude, phase, and frequency. In financial markets, this helps identify: