From 3d92926a902017e7cf1f5738e99ff62da596ce12 Mon Sep 17 00:00:00 2001 From: Ethan Werner Date: Sun, 9 Aug 2026 15:02:21 +0400 Subject: refactor --- lib/macd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'lib/macd.c') diff --git a/lib/macd.c b/lib/macd.c index 362f251..63db964 100644 --- a/lib/macd.c +++ b/lib/macd.c @@ -25,7 +25,7 @@ ts_parameter_t const ts_macd_p = { .n_inputs = 1, .n_outputs = 3, .n_constants = uint_t ts_macd_offset( ts_constant_t const *c ) { ( void )c; - return ( c ? (uint_t)(((ts_macd_params_t *)c)->slow_k + ((ts_macd_params_t *)c)->signal_k - 2) : 0 ); + return ( c ? (uint_t)(((ts_macd_params_t const *)c)->slow_k + ((ts_macd_params_t const *)c)->signal_k - 2) : 0 ); } int ts_macd( @@ -37,13 +37,13 @@ int ts_macd( TS_PROLOGUE( ts_macd_p ); if ( !c ) return -1; TS_CHECK_INPUT(x, x_n, n); - fp_t fast_k = ((ts_macd_params_t *)c)->fast_k; - fp_t slow_k = ((ts_macd_params_t *)c)->slow_k; - fp_t signal_k = ((ts_macd_params_t *)c)->signal_k; + fp_t fast_k = ((ts_macd_params_t const *)c)->fast_k; + fp_t slow_k = ((ts_macd_params_t const *)c)->slow_k; + fp_t signal_k = ((ts_macd_params_t const *)c)->signal_k; if (fast_k < 1 || slow_k < 1 || signal_k < 1) return -1; if (isnan(fast_k) || isnan(slow_k) || isnan(signal_k)) return -1; - if (n == 0) return -1; + if (isinf(fast_k) || isinf(slow_k) || isinf(signal_k)) return -1; fp_t fast_a = 2.0 / (fast_k + 1.0); fp_t slow_a = 2.0 / (slow_k + 1.0); @@ -52,9 +52,17 @@ int ts_macd( fp_t fast_ema = x[0][0]; fp_t slow_ema = x[0][0]; + if (slow_k > (fp_t)UINT64_MAX || signal_k > (fp_t)UINT64_MAX) return -1; uint_t total_offset = (uint_t)(slow_k + signal_k - 2); if (n <= total_offset) return -1; + // Initialize warm-up period to NAN + for (uint_t i = 0; i < total_offset; i++) { + y[0][i] = NAN; + y[1][i] = NAN; + y[2][i] = NAN; + } + // Compute EMAs up to total_offset for( uint_t i = 1; i <= total_offset; ++i ) { -- cgit v1.3.1