feat: expand TA-Lib wrapper with math transform functions
- Add 13 new math transform functions: Cos, Sin, Tan, ACos, ASin, ATan, CosH, SinH, TanH, Ceil, Exp, Floor, Sqrt, Ln, Log10 - Implement proper TA-Lib bindings for all math transform operations - Add comprehensive documentation comments to all new functions - Standardize error handling across all wrapper functions using slog.Debug - Refactor function definitions for consistency and maintainability The wrapper now exposes a comprehensive set of mathematical operations from TA-Lib, including trigonometric, hyperbolic, exponential, logarithmic, and rounding functions. All functions follow the established pattern of vector element-wise transformations.
This commit is contained in:
@@ -4,23 +4,25 @@ Go wrapper for selected [TA-Lib](https://ta-lib.org/) functions using [`purego`]
|
||||
|
||||
This project currently exposes a small subset of TA-Lib functions:
|
||||
|
||||
**Cycle Indicators (Hilbert Transform)**
|
||||
- `HT_DCPERIOD`
|
||||
- `HT_DCPHASE`
|
||||
- `HT_PHASOR`
|
||||
- `HT_SINE`
|
||||
- `HT_TRENDMODE`
|
||||
- `Add`
|
||||
- `Div`
|
||||
- `Max`
|
||||
- `MaxIndex`
|
||||
- `Min`
|
||||
- `MinIndex`
|
||||
- `MinMax`
|
||||
- `MinMaxIndex`
|
||||
- `Mult`
|
||||
- `Sub`
|
||||
|
||||
**Math Operators**
|
||||
- `Add`, `Sub`, `Mult`, `Div`
|
||||
- `Max`, `MaxIndex`, `Min`, `MinIndex`
|
||||
- `MinMax`, `MinMaxIndex`
|
||||
- `Sum`
|
||||
|
||||
**Math Transforms**
|
||||
- Trigonometric: `Cos`, `Sin`, `Tan`, `ACos`, `ASin`, `ATan`
|
||||
- Hyperbolic: `CosH`, `SinH`, `TanH`
|
||||
- Exponential/Logarithmic: `Exp`, `Ln`, `Log10`
|
||||
- Rounding: `Ceil`, `Floor`, `Sqrt`
|
||||
|
||||
## Requirements
|
||||
|
||||
- Go `1.26`
|
||||
|
||||
+135
@@ -163,4 +163,139 @@ var (
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
cos func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
acos func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
cosh func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
sin func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
asin func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
sinh func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
tan func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
atan func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
tanh func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
ceil func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
exp func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
floor func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
sqrt func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
ln func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
|
||||
log10 func(
|
||||
startIdx int32,
|
||||
endIdx int32,
|
||||
inReal []float64,
|
||||
outBegIdx *int32,
|
||||
outNBElement *int32,
|
||||
outReal []float64,
|
||||
) int32
|
||||
)
|
||||
|
||||
@@ -32,6 +32,7 @@ func Load() (uintptr, error) {
|
||||
purego.RegisterLibFunc(&ht_phasor, ptr, "TA_HT_PHASOR")
|
||||
purego.RegisterLibFunc(&ht_sine, ptr, "TA_HT_SINE")
|
||||
purego.RegisterLibFunc(&ht_trendmode, ptr, "TA_HT_TRENDMODE")
|
||||
|
||||
purego.RegisterLibFunc(&add, ptr, "TA_ADD")
|
||||
purego.RegisterLibFunc(&div, ptr, "TA_DIV")
|
||||
purego.RegisterLibFunc(&max, ptr, "TA_MAX")
|
||||
@@ -44,6 +45,24 @@ func Load() (uintptr, error) {
|
||||
purego.RegisterLibFunc(&sub, ptr, "TA_SUB")
|
||||
purego.RegisterLibFunc(&sum, ptr, "TA_SUM")
|
||||
|
||||
purego.RegisterLibFunc(&cos, ptr, "TA_COS")
|
||||
purego.RegisterLibFunc(&acos, ptr, "TA_ACOS")
|
||||
purego.RegisterLibFunc(&cosh, ptr, "TA_COSH")
|
||||
purego.RegisterLibFunc(&sin, ptr, "TA_SIN")
|
||||
purego.RegisterLibFunc(&asin, ptr, "TA_ASIN")
|
||||
purego.RegisterLibFunc(&sinh, ptr, "TA_SINH")
|
||||
purego.RegisterLibFunc(&tan, ptr, "TA_TAN")
|
||||
purego.RegisterLibFunc(&atan, ptr, "TA_ATAN")
|
||||
purego.RegisterLibFunc(&tanh, ptr, "TA_TANH")
|
||||
|
||||
purego.RegisterLibFunc(&ceil, ptr, "TA_CEIL")
|
||||
purego.RegisterLibFunc(&exp, ptr, "TA_EXP")
|
||||
purego.RegisterLibFunc(&floor, ptr, "TA_FLOOR")
|
||||
purego.RegisterLibFunc(&sqrt, ptr, "TA_SQRT")
|
||||
|
||||
purego.RegisterLibFunc(&ln, ptr, "TA_LN")
|
||||
purego.RegisterLibFunc(&log10, ptr, "TA_LOG10")
|
||||
|
||||
return ptr, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,395 @@
|
||||
package talib
|
||||
|
||||
import "log/slog"
|
||||
|
||||
// ACos - Vector trigonometric arc cosine: applies acos() to each input value.
|
||||
// A Math Transform passthrough with zero lookback.
|
||||
//
|
||||
// outReal[i] = acos(inReal[i])
|
||||
func ACos(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := acos(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("ACos", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Cos - Element-wise trigonometric cosine of the input series.
|
||||
// Applies the C library cos() to each sample.
|
||||
func Cos(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := cos(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Cos", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// CosH - Vector hyperbolic cosine: applies cosh element-wise to each input value.
|
||||
// A Math Transform primitive with no lookback.
|
||||
//
|
||||
// outReal[i] = cosh(inReal[i]) = (e^{inReal[i]} + e^{-inReal[i]}) / 2
|
||||
func CosH(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := cosh(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("CosH", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Sin - Vector trigonometric sine: applies sin() element-wise to each input value.
|
||||
// Part of the Math Transform group.
|
||||
func Sin(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := sin(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Sin", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// ASin - Element-wise arcsine (inverse sine) of each input value.
|
||||
// A vector math transform, not a market indicator.
|
||||
func ASin(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := asin(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("ASin", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// SinH - Element-wise hyperbolic sine of the input series.
|
||||
// A vector math transform applying sinh() to each value.
|
||||
//
|
||||
// outReal[i] = sinh(inReal[i])
|
||||
func SinH(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := sinh(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("SinH", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Tan - Vector trigonometric tangent: applies tan() element-wise to each input value.
|
||||
func Tan(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := tan(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Tan", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// ATan - Vector trigonometric arc tangent: applies atan element-wise to each input.
|
||||
// Pure math transform with no lookback.
|
||||
func ATan(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := atan(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("ATan", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// TanH - Vector hyperbolic tangent: applies tanh element-wise to the input series.
|
||||
//
|
||||
// outReal[i] = tanh(inReal[i])
|
||||
func TanH(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := tanh(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("TanH", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Ceil - Vector ceiling: element-wise ceiling of each input value (smallest integer >= input).
|
||||
func Ceil(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := ceil(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Ceil", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Exp - Vector arithmetic exponential: applies the base-e exponential to each input value. Element-wise math transform.
|
||||
//
|
||||
// outReal[i] = exp(inReal[i]) = e^
|
||||
func Exp(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := exp(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Exp", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Floor - Vector floor: rounds each input value down to the nearest integer. Element-wise math transform.
|
||||
func Floor(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := floor(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Floor", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Sqrt - Vector square root: applies the square-root function element-wise to each input value.
|
||||
func Sqrt(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := sqrt(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Sqrt", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Ln - Vector natural logarithm: applies the natural log (base e) elementwise to the input series.
|
||||
func Ln(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := ln(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Ln", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
|
||||
// Log10 - Vector base-10 logarithm. Applies log10 element-wise over each input value.
|
||||
func Log10(inReal []float64) []float64 {
|
||||
var (
|
||||
startIdx int32
|
||||
endIdx = int32(len(inReal) - 1)
|
||||
outBegIdx int32
|
||||
outNBElement int32
|
||||
outReal = make([]float64, len(inReal))
|
||||
)
|
||||
|
||||
if retCode := log10(
|
||||
startIdx,
|
||||
endIdx,
|
||||
inReal,
|
||||
&outBegIdx,
|
||||
&outNBElement,
|
||||
outReal,
|
||||
); SUCCESS != taResult(retCode) {
|
||||
slog.Debug("Log10", "result", retCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
return outReal
|
||||
}
|
||||
Reference in New Issue
Block a user