summaryrefslogtreecommitdiffstats
path: root/lib/stoch_rsi.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/stoch_rsi.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/stoch_rsi.c')
-rw-r--r--lib/stoch_rsi.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/stoch_rsi.c b/lib/stoch_rsi.c
new file mode 100644
index 0000000..81992a6
--- /dev/null
+++ b/lib/stoch_rsi.c
@@ -0,0 +1,96 @@
+// Stoch Rsi
+//
+// stoch_rsi
+//
+// Stochastic RSI
+//
+//
+//
+// Inputs: 1
+// Outputs: 2
+// Constants: 2
+
+
+#include <math.h>
+#include <stdlib.h>
+#include "ts.h"
+
+typedef struct {
+ uint_t rsi_period;
+ uint_t stoch_period;
+} ts_stoch_rsi_params_t;
+
+ts_parameter_t const ts_stoch_rsi_p = { .n_inputs = 1, .n_outputs = 2, .n_constants = 2, .params_size = sizeof(ts_stoch_rsi_params_t) };
+
+uint_t ts_stoch_rsi_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( c ? ((ts_stoch_rsi_params_t *)c)->rsi_period + ((ts_stoch_rsi_params_t *)c)->stoch_period - 1 : 0 );
+}
+
+int ts_stoch_rsi(
+ 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_stoch_rsi_p );
+ if ( !c ) return -1;
+ uint_t rsi_period = ((ts_stoch_rsi_params_t *)c)->rsi_period;
+ uint_t stoch_period = ((ts_stoch_rsi_params_t *)c)->stoch_period;
+ if (rsi_period == 0 || stoch_period == 0) return -1;
+
+ fp_t *rsi_values = NULL;
+ if( n > 0 )
+ {
+ rsi_values = calloc( n, sizeof( fp_t ) );
+ if( !rsi_values ) return -1;
+ }
+
+ for( uint_t i = rsi_period; i < n; ++i )
+ {
+ fp_t sum_gain = 0;
+ fp_t sum_loss = 0;
+
+ for( uint_t j = 0; j < rsi_period; ++j )
+ {
+ fp_t change = x[0][i - rsi_period + 1 + j] - x[0][i - rsi_period + j];
+ if( change > 0 )
+ sum_gain += change;
+ else if( change < 0 )
+ sum_loss += (-change);
+ }
+
+ if( sum_loss == 0 )
+ rsi_values[i] = 100.0;
+ else
+ {
+ fp_t rs = sum_gain / sum_loss;
+ rsi_values[i] = 100.0 - ( 100.0 / ( 1.0 + rs ) );
+ }
+ }
+
+ for (uint_t j = 0; j < n && j < rsi_period + stoch_period - 1; ++j) { y[0][j] = 0; y[1][j] = 0; }
+ for( uint_t i = rsi_period + stoch_period - 1; i < n; ++i )
+ {
+ fp_t rsi_min = rsi_values[i - stoch_period + 1];
+ fp_t rsi_max = rsi_values[i - stoch_period + 1];
+
+ for( uint_t j = 1; j < stoch_period; ++j )
+ {
+ rsi_min = fmin( rsi_min, rsi_values[i - stoch_period + 1 + j] );
+ rsi_max = fmax( rsi_max, rsi_values[i - stoch_period + 1 + j] );
+ }
+
+ fp_t denom = rsi_max - rsi_min;
+ fp_t stoch_k = denom != 0 ? ( rsi_values[i] - rsi_min ) / denom : 0;
+ fp_t stoch_d = stoch_k;
+
+ y[0][i] = stoch_k;
+ y[1][i] = stoch_d;
+ }
+
+ free( rsi_values );
+ return 0;
+}
+