#include #include #include #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 ) ); if ( !result ) { free( o ); free( h ); free( l ); free( c ); return false; } 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; }