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/starc_bands.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/starc_bands.c')
| -rw-r--r-- | lib/starc_bands.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/starc_bands.c b/lib/starc_bands.c new file mode 100644 index 0000000..0c545f8 --- /dev/null +++ b/lib/starc_bands.c @@ -0,0 +1,70 @@ +// Starc Bands +// +// starc_bands +// +// STARC Bands +// +// +// +// Inputs: 4 +// Outputs: 3 +// Constants: 2 + + +#include <math.h> +#include "ts.h" + + +typedef struct { + uint_t k; + fp_t mult; +} ts_starc_bands_params_t; + +ts_parameter_t const ts_starc_bands_p = { .n_inputs = 4, .n_outputs = 3, .n_constants = 2, .params_size = sizeof(ts_starc_bands_params_t) }; + + +uint_t ts_starc_bands_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( c ? ((ts_starc_bands_params_t *)c)->k : 0 ); +} + +int ts_starc_bands( + 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_starc_bands_p ); + if ( !c ) return -1; + uint_t k = ((ts_starc_bands_params_t *)c)->k; + if (k == 0) return -1; + if (n < k + 1) return -1; + + fp_t mult = ((ts_starc_bands_params_t *)c)->mult; + + for( uint_t i = k; i < n; ++i ) + { + fp_t tr_sum = 0; + for( uint_t j = 0; j < k; ++j ) + { + uint_t idx = i - k + 1 + j; + fp_t tr = fmax( ts_h( x, idx ) - ts_l( x, idx ), fmax( fabs( ts_h( x, idx ) - ts_c( x, idx - 1 ) ), fabs( ts_l( x, idx ) - ts_c( x, idx - 1 ) ) ) ); + tr_sum += tr; + } + fp_t atr = tr_sum / (fp_t)k; + + fp_t sum = 0; + for( uint_t j = 0; j < k; ++j ) + { + sum += ts_c( x, i - k + 1 + j ); + } + fp_t mean = sum / k; + + y[0][i] = mean + mult * atr; + y[1][i] = mean; + y[2][i] = mean - mult * atr; + } + return 0; +} + |