summaryrefslogtreecommitdiffstats
path: root/lib/dx.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/dx.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/dx.c')
-rw-r--r--lib/dx.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/dx.c b/lib/dx.c
new file mode 100644
index 0000000..b4552fa
--- /dev/null
+++ b/lib/dx.c
@@ -0,0 +1,79 @@
+// Directional Movement Index
+//
+// dx - DX
+//
+// 100 * |DI+ - DI-| / (DI+ + DI-), trend strength without smoothing
+//
+// Inputs: 4 (OHLC)
+// Outputs: 1 (DX)
+// Constants: 1 (period)
+
+#include <math.h>
+#include <stdlib.h>
+#include "ts.h"
+
+typedef struct {
+ uint_t k;
+} ts_dx_params_t;
+
+ts_parameter_t const ts_dx_p = { .n_inputs = 4, .n_outputs = 1, .n_constants = 1, .params_size = sizeof(ts_dx_params_t) };
+
+uint_t ts_dx_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( c ? ((ts_dx_params_t *)c)->k : 0 );
+}
+
+int ts_dx(
+ 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_dx_p );
+ if ( !c ) return -1;
+ uint_t k = ((ts_dx_params_t *)c)->k;
+ if (k == 0 || n < k) return -1;
+
+ fp_t *plus_dm_arr = calloc( n, sizeof( fp_t ) );
+ fp_t *minus_dm_arr = calloc( n, sizeof( fp_t ) );
+ fp_t *tr_arr = calloc( n, sizeof( fp_t ) );
+ if( !plus_dm_arr || !minus_dm_arr || !tr_arr ) { free( plus_dm_arr ); free( minus_dm_arr ); free( tr_arr ); return -1; }
+
+ y[0][0] = 0;
+
+ for( uint_t i = 1; i < n; ++i )
+ {
+ fp_t hdmd = ts_h( x, i ) - ts_h( x, i - 1 );
+ fp_t ldml = ts_l( x, i - 1 ) - ts_l( x, i );
+
+ fp_t plus_dm = hdmd > ldml && hdmd > 0 ? hdmd : 0;
+ fp_t minus_dm = ldml > hdmd && ldml > 0 ? ldml : 0;
+
+ fp_t tr = fmax( ts_h( x, i ) - ts_l( x, i ),
+ fmax( fabs( ts_h( x, i ) - ts_c( x, i - 1 ) ),
+ fabs( ts_l( x, i ) - ts_c( x, i - 1 ) ) ) );
+
+ plus_dm_arr[i] = i == 1 ? plus_dm
+ : plus_dm_arr[i - 1] + ( 1.0 / k ) * ( plus_dm - plus_dm_arr[i - 1] );
+ minus_dm_arr[i] = i == 1 ? minus_dm
+ : minus_dm_arr[i - 1] + ( 1.0 / k ) * ( minus_dm - minus_dm_arr[i - 1] );
+ tr_arr[i] = i == 1 ? tr
+ : tr_arr[i - 1] + ( 1.0 / k ) * ( tr - tr_arr[i - 1] );
+ }
+
+ for( uint_t i = k; i < n; ++i )
+ {
+ fp_t di_plus = tr_arr[i] != 0 ? 100.0 * plus_dm_arr[i] / tr_arr[i] : 0;
+ fp_t di_minus = tr_arr[i] != 0 ? 100.0 * minus_dm_arr[i] / tr_arr[i] : 0;
+ fp_t sum = di_plus + di_minus;
+ y[0][i] = sum != 0 ? 100.0 * fabs( di_plus - di_minus ) / sum : 0;
+ }
+
+ free( plus_dm_arr );
+ free( minus_dm_arr );
+ free( tr_arr );
+ return 0;
+}
+
+