// Coppock Curve // // coppock_curve - WMS // // WMA14(ROC14 + ROC11), weighted moving average of combined ROCs // // Inputs: 1 // Outputs: 1 (Coppock) // Constants: 0 #include #include #include "ts.h" ts_parameter_t const ts_coppock_curve_p = { .n_inputs = 1, .n_outputs = 1, .n_constants = 0, .params_size = 0 }; uint_t ts_coppock_curve_offset( ts_constant_t const *c ) { ( void )c; return ( 14 ); } int ts_coppock_curve( 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_coppock_curve_p ); (void)c; uint_t roc_long = 14; uint_t roc_short = 11; uint_t wma_period = 10; if (n < roc_long) return -1; fp_t *combined = calloc( n, sizeof( fp_t ) ); if ( !combined ) return -1; for ( uint_t i = roc_long; i < n; ++i ) { if ( x[0][i - roc_long] == 0 || x[0][i - roc_short] == 0 ) { combined[i - roc_long] = 0; continue; } fp_t roc14 = ( x[0][i] - x[0][i - roc_long] ) / x[0][i - roc_long] * 100.0; fp_t roc11 = ( x[0][i] - x[0][i - roc_short] ) / x[0][i - roc_short] * 100.0; combined[i - roc_long] = roc14 + roc11; } for ( uint_t j = 0; j < n && j < roc_long; ++j ) y[0][j] = 0; for ( uint_t i = roc_long; i < n; ++i ) { uint_t ci = i - roc_long; fp_t wsum = 0; fp_t wtotal = 0; uint_t start = ci + 1 >= wma_period ? ci + 1 - wma_period : 0; for ( uint_t j = start; j <= ci; ++j ) { uint_t weight = ci - j + 1; wsum += combined[j] * (fp_t)weight; wtotal += (fp_t)weight; } y[0][ci + roc_long] = wtotal != 0 ? wsum / wtotal : 0; } free( combined ); return 0; }