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 /test/awesome_oscillator.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 'test/awesome_oscillator.c')
| -rw-r--r-- | test/awesome_oscillator.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/test/awesome_oscillator.c b/test/awesome_oscillator.c new file mode 100644 index 0000000..205bf78 --- /dev/null +++ b/test/awesome_oscillator.c @@ -0,0 +1,58 @@ +#include <stddef.h> +#include <stdlib.h> +#include <math.h> +#include "ts.h" + +bool ts_test_awesome_oscillator( void ) +{ + uint_t n = 40; + fp_t *o = calloc( n, sizeof( fp_t ) ); + fp_t *h = calloc( n, sizeof( fp_t ) ); + fp_t *l = calloc( n, sizeof( fp_t ) ); + fp_t *c = calloc( n, sizeof( fp_t ) ); + + if ( !o || !h || !l || !c ) + { + free( o ); free( h ); free( l ); free( c ); + return false; + } + + for ( uint_t i = 0; i < n; ++i ) + { + o[i] = (fp_t)(10 + i); + h[i] = (fp_t)(12 + i); + l[i] = (fp_t)(8 + i); + c[i] = (fp_t)(11 + i); + } + + fp_t *result = calloc( n, sizeof( fp_t ) ); + + ts_awesome_oscillator(&result, 1, (fp_t const * const []){ o, h, l, c }, 4, n, NULL); + + /* Compute expected EMA(5) and EMA(34) of median price */ + fp_t fast_a = 2.0 / 6.0; + fp_t slow_a = 2.0 / 35.0; + fp_t sum_fast = 0, sum_slow = 0; + for ( uint_t i = 0; i < 34; ++i ) + { + fp_t mp = (h[i] + l[i]) / 2; + sum_slow += mp; + if ( i < 5 ) sum_fast += mp; + } + fp_t ema_fast = sum_fast / 5.0; + fp_t ema_slow = sum_slow / 34.0; + for ( uint_t i = 34; i < n; ++i ) + { + fp_t mp = (h[i] + l[i]) / 2; + ema_fast = ema_fast + fast_a * ( mp - ema_fast ); + ema_slow = ema_slow + slow_a * ( mp - ema_slow ); + } + + fp_t expected = ema_fast - ema_slow; + + bool pass = fabs( result[n - 1] - expected ) < 1e-10; + + free( o ); free( h ); free( l ); free( c ); free( result ); + + return pass; +} |