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/choppiness_index.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/choppiness_index.c')
| -rw-r--r-- | lib/choppiness_index.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/choppiness_index.c b/lib/choppiness_index.c new file mode 100644 index 0000000..55ecb46 --- /dev/null +++ b/lib/choppiness_index.c @@ -0,0 +1,68 @@ +// Choppiness Index +// +// choppiness_index - CHOP +// +// 100 * log(SUM(H-L) / Range) / log(k), measures trend vs chop +// +// Inputs: 3 (HLC) +// Outputs: 1 (CHOP) +// Constants: 0 + +#include <math.h> +#include "ts.h" + +typedef struct { + uint_t k; +} ts_choppiness_index_params_t; + +ts_parameter_t const ts_choppiness_index_p = { .n_inputs = 3, .n_outputs = 1, .n_constants = 1, .params_size = sizeof(ts_choppiness_index_params_t) }; + +uint_t ts_choppiness_index_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( c ? 2 * ((ts_choppiness_index_params_t *)c)->k : 0 ); +} + +int ts_choppiness_index( + 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_choppiness_index_p ); + if ( !c ) return -1; + uint_t k = ((ts_choppiness_index_params_t *)c)->k; + uint_t range_period = 2 * k; + if (k == 0 || n < range_period) return -1; + + for ( uint_t i = range_period; i < n; ++i ) + { + fp_t sum_range = 0; + for ( uint_t j = i - k + 1; j <= i; ++j ) + { + sum_range += fabs( ts_h( x, j ) - ts_l( x, j ) ); + } + + fp_t hh = ts_h( x, i - range_period + 1 ); + fp_t ll = ts_l( x, i - range_period + 1 ); + for ( uint_t j = i - range_period + 1; j <= i; ++j ) + { + if ( ts_h( x, j ) > hh ) hh = ts_h( x, j ); + if ( ts_l( x, j ) < ll ) ll = ts_l( x, j ); + } + + fp_t range = hh - ll; + if ( range == 0 || sum_range == 0 || k <= 1 ) + { + y[0][i] = 0; + } + else + { + y[0][i] = 100.0 * log( sum_range / range ) / log( ( fp_t )k ); + } + } + return 0; +} + + + |