summaryrefslogtreecommitdiffstats
path: root/lib/rainbow_ma.c
blob: 3311ec9cc186566242f9c3171e4f8798dab05d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}