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/rainbow_ma.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/rainbow_ma.c')
| -rw-r--r-- | lib/rainbow_ma.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/rainbow_ma.c b/lib/rainbow_ma.c new file mode 100644 index 0000000..3311ec9 --- /dev/null +++ b/lib/rainbow_ma.c @@ -0,0 +1,61 @@ +// Rainbow Ma +// +// rainbow_ma +// +// Rainbow Moving Average +// +// +// +// Inputs: 1 +// Outputs: 1 +// Constants: 0 + + +#include "ts.h" + + +ts_parameter_t const ts_rainbow_ma_p = { .n_inputs = 1, .n_outputs = 1, .n_constants = 0, .params_size = 0 }; + +uint_t ts_rainbow_ma_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( 19 ); +} + +int ts_rainbow_ma( + 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_rainbow_ma_p ); + (void)c; + uint_t periods[7] = {3, 5, 7, 9, 11, 13, 15}; + + for( uint_t j = 0; j < n && j < 19; ++j ) y[0][j] = 0; + for( uint_t i = 19; i < n; ++i ) + { + fp_t sma_values[7]; + + for( uint_t p = 0; p < 7; ++p ) + { + fp_t sum = 0; + for( uint_t j = 0; j < periods[p]; ++j ) + sum += x[0][i - j]; + sma_values[p] = sum / periods[p]; + } + + for( uint_t a = 0; a < 6; ++a ) + for( uint_t b = a + 1; b < 7; ++b ) + if( sma_values[a] > sma_values[b] ) + { + fp_t t = sma_values[a]; + sma_values[a] = sma_values[b]; + sma_values[b] = t; + } + + y[0][i] = sma_values[2]; + } + return 0; +} + |