summaryrefslogtreecommitdiffstats
path: root/lib/alligator.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/alligator.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/alligator.c')
-rw-r--r--lib/alligator.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/alligator.c b/lib/alligator.c
new file mode 100644
index 0000000..81450b0
--- /dev/null
+++ b/lib/alligator.c
@@ -0,0 +1,70 @@
+// Alligator Indicator
+//
+// William Elder's Alligator uses three SMAs shifted forward in time:
+// Jaw: SMA(13) shifted 8 bars
+// Teeth: SMA(8) shifted 5 bars
+// Lips: SMA(5) shifted 3 bars
+//
+// X_n - 1
+// Y_n - 3
+// C_n - 0
+
+#include "ts.h"
+
+
+ts_parameter_t const ts_alligator_p = { .n_inputs = 1, .n_outputs = 3, .n_constants = 0, .params_size = 0 };
+
+uint_t ts_alligator_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( 12 );
+}
+
+int ts_alligator(
+ 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_alligator_p );
+ (void)c;
+ if( n == 0 ) return -1;
+ /* Jaw: SMA(13) shifted 8 positions */
+ /* Teeth: SMA(8) shifted 5 positions */
+ /* Lips: SMA(5) shifted 3 positions */
+
+ /* Jaw: SMA13 of x[i-8..i+4], displayed at i */
+ for (uint_t i = 8; i - 8 + 13 <= n; ++i)
+ {
+ fp_t sum = 0;
+ for (uint_t j = 0; j < 13; ++j)
+ {
+ sum += x[0][i - 8 + j];
+ }
+ y[0][i] = sum / 13;
+ }
+
+ /* Teeth: SMA8 of x[i-5..i+2], displayed at i */
+ for (uint_t i = 5; i - 5 + 8 <= n; ++i)
+ {
+ fp_t sum = 0;
+ for (uint_t j = 0; j < 8; ++j)
+ {
+ sum += x[0][i - 5 + j];
+ }
+ y[1][i] = sum / 8;
+ }
+
+ /* Lips: SMA5 of x[i-3..i+1], displayed at i */
+ for (uint_t i = 3; i - 3 + 5 <= n; ++i)
+ {
+ fp_t sum = 0;
+ for (uint_t j = 0; j < 5; ++j)
+ {
+ sum += x[0][i - 3 + j];
+ }
+ y[2][i] = sum / 5;
+ }
+ return 0;
+}
+