summaryrefslogtreecommitdiffstats
path: root/lib/cci.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/cci.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/cci.c')
-rw-r--r--lib/cci.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/cci.c b/lib/cci.c
new file mode 100644
index 0000000..956f619
--- /dev/null
+++ b/lib/cci.c
@@ -0,0 +1,89 @@
+// Commodity Channel Index
+//
+// cci - CCI
+//
+// (TP - SMA(TP)) / (0.015 * MAD(TP)), where TP = (H+L+C)/3
+//
+// Inputs: 4 (OHLC)
+// Outputs: 1 (CCI)
+// Constants: 0
+
+#include <math.h>
+#include "ts.h"
+
+typedef struct {
+ uint_t k;
+} ts_cci_params_t;
+
+ts_parameter_t const ts_cci_p = { .n_inputs = 4, .n_outputs = 1, .n_constants = 1, .params_size = sizeof(ts_cci_params_t) };
+
+uint_t ts_cci_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( c ? ((ts_cci_params_t *)c)->k - 1 : 0 );
+}
+
+int ts_cci(
+ 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_cci_p );
+ if ( !c ) return -1;
+ TS_CHECK_INPUT(x, x_n, n);
+ uint_t k = ((ts_cci_params_t *)c)->k;
+ if (k == 0 || n < k) return -1;
+
+ // Compute initial typical prices
+ fp_t tp_sum = 0;
+ for( uint_t j = 0; j < k; ++j )
+ {
+ tp_sum += ( ts_h( x, j ) + ts_l( x, j ) + ts_c( x, j ) ) / 3.0;
+ }
+ fp_t tp_sma = tp_sum / (fp_t)k;
+
+ // Compute initial MAD
+ fp_t mad_sum = 0;
+ for( uint_t j = 0; j < k; ++j )
+ {
+ fp_t tp = ( ts_h( x, j ) + ts_l( x, j ) + ts_c( x, j ) ) / 3.0;
+ mad_sum += fabs( tp - tp_sma );
+ }
+ fp_t mad = mad_sum / (fp_t)k;
+
+ {
+ fp_t tp = ( ts_h( x, k - 1 ) + ts_l( x, k - 1 ) + ts_c( x, k - 1 ) ) / 3.0;
+ if( mad == 0 )
+ y[0][k - 1] = 0;
+ else
+ y[0][k - 1] = ( tp - tp_sma ) / ( 0.015 * mad );
+ }
+
+ // Sliding window: incremental tp_sum, but MAD needs recompute when tp_sma changes
+ // MAD depends on tp_sma which changes each step, so we can't fully incremental MAD
+ // However, we can still use incremental tp_sum
+ for( uint_t i = k; i < n; ++i )
+ {
+ fp_t tp_new = ( ts_h( x, i ) + ts_l( x, i ) + ts_c( x, i ) ) / 3.0;
+ fp_t tp_old = ( ts_h( x, i - k ) + ts_l( x, i - k ) + ts_c( x, i - k ) ) / 3.0;
+ tp_sum = tp_sum + tp_new - tp_old;
+ tp_sma = tp_sum / (fp_t)k;
+
+ mad_sum = 0;
+ for( uint_t j = 0; j < k; ++j )
+ {
+ fp_t tp = ( ts_h( x, i - k + 1 + j ) + ts_l( x, i - k + 1 + j ) + ts_c( x, i - k + 1 + j ) ) / 3.0;
+ mad_sum += fabs( tp - tp_sma );
+ }
+ mad = mad_sum / (fp_t)k;
+
+ if( mad == 0 )
+ y[0][i] = 0;
+ else
+ y[0][i] = ( tp_new - tp_sma ) / ( 0.015 * mad );
+ }
+
+ return 0;
+}
+