summaryrefslogtreecommitdiffstats
path: root/lib/coppock_curve.c
diff options
context:
space:
mode:
authorEthan Werner <mail@ethanwerner.com>2026-07-01 09:42:39 +0400
committerEthan Werner <mail@ethanwerner.com>2026-07-01 09:42:39 +0400
commit07e18043145f3d91c8ea7ec3ad41b3c905585277 (patch)
tree2bdeae46973f4bdfce263679a4008ac768011f28 /lib/coppock_curve.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/coppock_curve.c')
-rw-r--r--lib/coppock_curve.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/coppock_curve.c b/lib/coppock_curve.c
new file mode 100644
index 0000000..dfa1ad2
--- /dev/null
+++ b/lib/coppock_curve.c
@@ -0,0 +1,75 @@
+// Coppock Curve
+//
+// coppock_curve - WMS
+//
+// WMA14(ROC14 + ROC11), weighted moving average of combined ROCs
+//
+// Inputs: 1
+// Outputs: 1 (Coppock)
+// Constants: 0
+
+#include <math.h>
+#include <stdlib.h>
+#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 == 0 || 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;
+}
+
+
+