// 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; }