Files
talib/cycle_indicators.go
T
beejay e0715fb3e3 feat: add math operator wrappers and align TA-Lib binding types
Add Go wrappers for additional TA-Lib math operators:
Div, Max, MaxIndex, Min, MinIndex, MinMax, MinMaxIndex, Mult, Sub, and Sum.

Refactor the TA-Lib function signatures to use int32 consistently,
including taResult and Hilbert Transform bindings, so the Go layer
matches the native library more closely.

Fix HT_TRENDMODE to return []int32 and update related comments and
naming for better API consistency.
2026-07-22 17:18:22 +09:00

148 lines
4.2 KiB
Go

package talib
import (
"log/slog"
)
// HT_DCPERIOD - Hilbert Transform estimate of the dominant cycle period (in bars) of the price series.
// Outputs the smoothed instantaneous cycle period.
// Output is the estimated dominant cycle length in bars (clamped to 6-50).
func HT_DCPERIOD(inReal []float64) []float64 {
var (
startIdx int32
endIdx = int32(len(inReal) - 1)
outBegIdx int32
outNBElement int32
outReal = make([]float64, len(inReal))
)
if retCode := ht_dcperiod(startIdx, endIdx, inReal, &outBegIdx, &outNBElement, outReal); SUCCESS != taResult(retCode) {
slog.Debug("HT_DCPERIOD", "result", retCode)
return nil
}
return outReal
}
// HT_DCPHASE - Hilbert Transform Dominant Cycle Phase: the instantaneous phase (in degrees) of the dominant market cycle,
// derived from a homodyne discriminator on a Hilbert-transformed, smoothed price.
// One real output per bar. Output is degrees, wrapped so it never exceeds 315 (can go negative).
func HT_DCPHASE(inReal []float64) []float64 {
var (
startIdx int32
endIdx = int32(len(inReal) - 1)
outBegIdx int32
outNBElement int32
outReal = make([]float64, len(inReal))
)
if retCode := ht_dcphase(
startIdx,
endIdx,
inReal,
&outBegIdx,
&outNBElement,
outReal,
); SUCCESS != taResult(retCode) {
slog.Debug("HT_DCPHASE", "result", retCode)
return nil
}
return outReal
}
// HT_PHASOR - Hilbert Transform indicator that decomposes the price series into its in-phase (I) and quadrature (Q) phasor components.
// Shares the same detrend/Hilbert machinery as the other HT_* cycle functions.
//
// Smooth price with a 4-bar WMA (weights 1,2,3,4 /10).
// Apply the Hilbert Transform (a=0.0962, b=0.5769, scaled per bar by adjustedPrevPeriod = 0.075*period + 0.54) to get detrender = HT(smoothed) and Q1 = HT(detrender).
// Output: outInPhase = detrender delayed 3 price bars; outQuadrature = Q1.
// @param inReal Input price series
// @return outInPhase In-phase component (detrender delayed 3 bars)
// @return outQuadrature Quadrature component (Q1 of the Hilbert Transform)
func HT_PHASOR(inReal []float64) ([]float64, []float64) {
var (
startIdx int32
endIdx = int32(len(inReal) - 1)
outBegIdx int32
outNBElement int32
outInPhase = make([]float64, len(inReal))
outQuadrature = make([]float64, len(inReal))
)
if retCode := ht_phasor(
startIdx,
endIdx,
inReal,
&outBegIdx,
&outNBElement,
outInPhase,
outQuadrature,
); SUCCESS != taResult(retCode) {
slog.Debug("HT_PHASOR", "result", retCode)
return nil, nil
}
return outInPhase, outQuadrature
}
// HT_SINE - Hilbert Transform SineWave: derives the dominant-cycle phase from price and emits its sine plus a 45-degree-lead sine.
// The two curves cross near cycle turning points.
// outSine and outLeadSine crossing marks cycle turning points.
// @param inReal Input price series
// @return outSine Sine of the dominant-cycle phase
// @return outLeadSine Sine of the phase advanced 45 degrees (lead)
func HT_SINE(inReal []float64) ([]float64, []float64) {
var (
startIdx int32
endIdx = int32(len(inReal) - 1)
outBegIdx int32
outNBElement int32
outSine = make([]float64, len(inReal))
outLeadSine = make([]float64, len(inReal))
)
if retCode := ht_sine(
startIdx,
endIdx,
inReal,
&outBegIdx,
&outNBElement,
outSine,
outLeadSine,
); SUCCESS != taResult(retCode) {
slog.Debug("HT_SINE", "result", retCode)
return nil, nil
}
return outSine, outLeadSine
}
// HT_TRENDMODE - Hilbert Transform classifier that labels each bar as trending (1) or cycling (0).
// Reuses the MAMA dominant-cycle/phase DSP plus a SineWave/trendline test to decide the market mode.
// @param inReal Input price series
// @return outInteger 1 = trending market; 0 = cycle/mean-reverting market;
func HT_TRENDMODE(inReal []float64) []int32 {
var (
startIdx int32
endIdx = int32(len(inReal) - 1)
outBegIdx int32
outNBElement int32
outInteger = make([]int32, len(inReal))
)
if retCode := ht_trendmode(
startIdx,
endIdx,
inReal,
&outBegIdx,
&outNBElement,
outInteger,
); SUCCESS != taResult(retCode) {
slog.Debug("HT_TRENDMODE", "result", retCode)
return nil
}
return outInteger
}