summaryrefslogtreecommitdiffstats
path: root/lib/ichimoku.c
blob: 76f85e6ec23b1ccb305d6a9dc2fba2f1798676ef (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Ichimoku
//
// ichimoku
//
// Ichimoku Cloud
//
// Tenkan, Kijun, Senkou Span A/B, Chikou
//
// Inputs:    4
// Outputs:   5
// Constants: 3 (tenkan, kijun, senkou)


#include <math.h>
#include "ts.h"


typedef struct {
    uint_t tenkan;
    uint_t kijun;
    uint_t senkou;
} ts_ichimoku_params_t;

ts_parameter_t const ts_ichimoku_p = { .n_inputs = 4, .n_outputs = 5, .n_constants = 3, .params_size = sizeof(ts_ichimoku_params_t) };

uint_t ts_ichimoku_offset( ts_constant_t const *c )
{
	( void )c;
	return ( 52 );
}

int ts_ichimoku(
	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_ichimoku_p );
	uint_t tenkan = 9;
	uint_t kijun = 26;
	uint_t senkou = 52;

	if ( c )
	{
		ts_ichimoku_params_t *p = (ts_ichimoku_params_t *)c;
		if ( p->tenkan > 0 ) tenkan = p->tenkan;
		if ( p->kijun > 0 ) kijun = p->kijun;
		if ( p->senkou > 0 ) senkou = p->senkou;
	}

	if ( tenkan == 0 || kijun == 0 || senkou == 0 ) return -1;

    for( uint_t i = 0; i < n; ++i )
    {
        y[0][i] = 0;
        y[1][i] = 0;
        y[2][i] = 0;
        y[3][i] = 0;
        y[4][i] = 0;
    }

    for( uint_t i = tenkan - 1; i < n; ++i )
    {
        fp_t hh = ts_h( x, i - tenkan + 1 );
        fp_t ll = ts_l( x, i - tenkan + 1 );
        for( uint_t j = i - tenkan + 1; j <= i; ++j )
        {
            if( ts_h( x, j ) > hh ) hh = ts_h( x, j );
            if( ts_l( x, j ) < ll ) ll = ts_l( x, j );
        }
        y[0][i] = ( hh + ll ) / 2.0;
    }

    for( uint_t i = kijun - 1; i < n; ++i )
    {
        fp_t hh = ts_h( x, i - kijun + 1 );
        fp_t ll = ts_l( x, i - kijun + 1 );
        for( uint_t j = i - kijun + 1; j <= i; ++j )
        {
            if( ts_h( x, j ) > hh ) hh = ts_h( x, j );
            if( ts_l( x, j ) < ll ) ll = ts_l( x, j );
        }
        y[1][i] = ( hh + ll ) / 2.0;
    }

    for( uint_t i = kijun - 1; i < n; ++i )
    {
        uint_t idx = i + kijun;
        if( idx < n )
            y[2][idx] = ( y[0][i] + y[1][i] ) / 2.0;
    }

    for( uint_t i = senkou - 1; i < n; ++i )
    {
        fp_t hh = ts_h( x, i - senkou + 1 );
        fp_t ll = ts_l( x, i - senkou + 1 );
        for( uint_t j = i - senkou + 1; j <= i; ++j )
        {
            if( ts_h( x, j ) > hh ) hh = ts_h( x, j );
            if( ts_l( x, j ) < ll ) ll = ts_l( x, j );
        }
        uint_t idx = i + kijun;
        if( idx < n )
            y[3][idx] = ( hh + ll ) / 2.0;
    }

    for( uint_t i = kijun; i < n; ++i )
    {
        y[4][i] = ts_c( x, i - kijun );
    }
	return 0;
}