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/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 'lib/awesome_oscillator.c')
| -rw-r--r-- | lib/awesome_oscillator.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/awesome_oscillator.c b/lib/awesome_oscillator.c new file mode 100644 index 0000000..be1932e --- /dev/null +++ b/lib/awesome_oscillator.c @@ -0,0 +1,66 @@ +// Awesome Oscillator +// +// awesome_oscillator - AO +// +// EMA5(MP) - EMA34(MP), where MP = (H + L) / 2 +// +// Inputs: 4 (OHLC) +// Outputs: 1 (Oscillator) +// Constants: 0 + +#include "ts.h" + + +ts_parameter_t const ts_awesome_oscillator_p = { .n_inputs = 4, .n_outputs = 1, .n_constants = 0, .params_size = 0 }; + +uint_t ts_awesome_oscillator_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( 33 ); +} + +int ts_awesome_oscillator( + 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_awesome_oscillator_p ); + (void)c; + if (n < 34) return -1; + + uint_t fast_period = 5; + uint_t slow_period = 34; + + fp_t fast_a = 2.0 / ( ( fp_t )fast_period + 1.0 ); + fp_t slow_a = 2.0 / ( ( fp_t )slow_period + 1.0 ); + + fp_t sum_fast = 0; + fp_t sum_slow = 0; + + for ( uint_t j = 0; j < slow_period; ++j ) + { + fp_t mp = ( ts_h( x, j ) + ts_l( x, j ) ) / 2.0; + sum_slow += mp; + if ( j < fast_period ) + sum_fast += mp; + } + + fp_t ema_fast = sum_fast / ( fp_t )fast_period; + fp_t ema_slow = sum_slow / ( fp_t )slow_period; + + for ( uint_t i = 0; i < slow_period - 1; ++i ) + y[0][i] = 0; + + y[0][slow_period - 1] = ema_fast - ema_slow; + + for( uint_t i = slow_period; i < n; ++i ) + { + fp_t mp = ( ts_h( x, i ) + ts_l( x, i ) ) / 2.0; + ema_fast = ema_fast + fast_a * ( mp - ema_fast ); + ema_slow = ema_slow + slow_a * ( mp - ema_slow ); + y[0][i] = ema_fast - ema_slow; + } + return 0; +} + |