summaryrefslogtreecommitdiffstats
path: root/lib/rainbow_ma.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbow_ma.c')
-rw-r--r--lib/rainbow_ma.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/rainbow_ma.c b/lib/rainbow_ma.c
new file mode 100644
index 0000000..3311ec9
--- /dev/null
+++ b/lib/rainbow_ma.c
@@ -0,0 +1,61 @@
+// Rainbow Ma
+//
+// rainbow_ma
+//
+// Rainbow Moving Average
+//
+//
+//
+// Inputs: 1
+// Outputs: 1
+// Constants: 0
+
+
+#include "ts.h"
+
+
+ts_parameter_t const ts_rainbow_ma_p = { .n_inputs = 1, .n_outputs = 1, .n_constants = 0, .params_size = 0 };
+
+uint_t ts_rainbow_ma_offset( ts_constant_t const *c )
+{
+ ( void )c;
+ return ( 19 );
+}
+
+int ts_rainbow_ma(
+ 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_rainbow_ma_p );
+ (void)c;
+ uint_t periods[7] = {3, 5, 7, 9, 11, 13, 15};
+
+ for( uint_t j = 0; j < n && j < 19; ++j ) y[0][j] = 0;
+ for( uint_t i = 19; i < n; ++i )
+ {
+ fp_t sma_values[7];
+
+ for( uint_t p = 0; p < 7; ++p )
+ {
+ fp_t sum = 0;
+ for( uint_t j = 0; j < periods[p]; ++j )
+ sum += x[0][i - j];
+ sma_values[p] = sum / periods[p];
+ }
+
+ for( uint_t a = 0; a < 6; ++a )
+ for( uint_t b = a + 1; b < 7; ++b )
+ if( sma_values[a] > sma_values[b] )
+ {
+ fp_t t = sma_values[a];
+ sma_values[a] = sma_values[b];
+ sma_values[b] = t;
+ }
+
+ y[0][i] = sma_values[2];
+ }
+ return 0;
+}
+