diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
| commit | 07e18043145f3d91c8ea7ec3ad41b3c905585277 (patch) | |
| tree | 2bdeae46973f4bdfce263679a4008ac768011f28 /lib/fisher_transform.c | |
initial commit: ts time series analysis library v0.1.0
- 354 financial and mathematical time-series indicators
- Uniform C11 API with consistent function signatures
- 349 passing tests with -Werror
- Static and shared library builds
- pkg-config integration
- Python bindings
- Fuzzing and sanitizer support
- Security hardening flags
Diffstat (limited to 'lib/fisher_transform.c')
| -rw-r--r-- | lib/fisher_transform.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/fisher_transform.c b/lib/fisher_transform.c new file mode 100644 index 0000000..2b416ba --- /dev/null +++ b/lib/fisher_transform.c @@ -0,0 +1,78 @@ +// Fisher Transform +// +// fisher_transform - FT +// +// 0.5 * log(price_position / (1 - price_position)), normalized to range +// +// Inputs: 4 (OHLC) +// Outputs: 2 (Transform, Trigger) +// Constants: 1 (period) + +#include <math.h> +#include "ts.h" + +typedef struct { + uint_t k; +} ts_fisher_transform_params_t; + +ts_parameter_t const ts_fisher_transform_p = { .n_inputs = 4, .n_outputs = 2, .n_constants = 1, .params_size = sizeof(ts_fisher_transform_params_t) }; + +uint_t ts_fisher_transform_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( c ? ((ts_fisher_transform_params_t *)c)->k - 1 : 0 ); +} + +int ts_fisher_transform( + fp_t * const *y, uint_t y_n, + fp_t const * const *x, uint_t x_n, + uint_t n, ts_constant_t const *c +) +{ + TS_PROLOGUE( ts_fisher_transform_p ); + if ( !c ) return -1; + uint_t k = ((ts_fisher_transform_params_t *)c)->k; + if (k == 0 || n < k) return -1; + + for (uint_t i = k - 1; i < n; ++i) + { + /* Find high and low over window */ + fp_t hh = x[1][i - k + 1]; + fp_t ll = x[2][i - k + 1]; + for (uint_t j = i - k + 2; j <= i; ++j) + { + if (x[1][j] > hh) hh = x[1][j]; + if (x[2][j] < ll) ll = x[2][j]; + } + + fp_t range = hh - ll; + fp_t val = range != 0 ? (x[3][i] - ll) / range : 0.5; + + /* Clamp to [0.01, 0.99] to avoid log(0) */ + if (val < 0.01) val = 0.01; + if (val > 0.99) val = 0.99; + + fp_t fisher = 0.5 * log(val / (1.0 - val)); + + /* Smooth with previous value (skip first output) */ + uint_t out_i = i; + if (i > k - 1) + { + fisher = fisher + 0.5 * y[0][out_i - 1]; + } + y[0][out_i] = fisher; + + /* Trigger line = previous fisher */ + if (out_i > 0) + { + y[1][out_i] = y[0][out_i - 1]; + } + else + { + y[1][out_i] = 0; + } + } + return 0; +} + + |